def region(self, value): if isinstance(value, list) and not isinstance(value, APIList): self._region = APIList(DynectSession.get_session, 'region', None, value) elif isinstance(value, APIList): self._region = value self._region.uri = self.uri
def keys(self, value): if isinstance(value, list) and not isinstance(value, APIList): self._keys = APIList(DynectSession.get_session, 'keys', None, value) elif isinstance(value, APIList): self._keys = value self._keys.uri = self.uri
def __init__(self, zone, *args, **kwargs): """Create a :class:`DNSSEC` object :param zone: the zone this service will be attached to :param keys: a list of :class:`DNSSECKey`'s for the service :param contact_nickname: Name of contact to receive notifications :param notify_events: A ``list`` of events that trigger notifications. Valid values are "create" (a new version of a key was created), "expire" (a key was automatically expired), or "warning" (early warnings (2 weeks, 1 week, 1 day) of events) """ super(DNSSEC, self).__init__() self.valid_notify_events = ('create', 'expire', 'warning') self._zone = zone self._contact_nickname = self._notify_events = None self._keys = APIList(DynectSession.get_session, 'keys') self._active = None self.uri = '/DNSSEC/{}/'.format(self._zone) if 'api' in kwargs: del kwargs['api'] self._build(kwargs) elif len(args) == 0 and len(kwargs) == 0: self._get() else: self._post(*args, **kwargs) self._keys.uri = self.uri
def _build(self, data, region=True): """Private method which builds the objects fields based on the data returned by an API call :param data: the data from the JSON respnose :param region: Boolean flag specifying whether to rebuild the region objects or not """ for key, val in data.items(): if key == 'region': if region: self._region = APIList(DynectSession.get_session, 'region') for region in val: region_code = region.pop('region_code', None) self._region.append(GSLBRegion(self._zone, self._fqdn, region_code, **region)) elif key == 'monitor': # We already have the monitor object, no need to rebuild it pass elif key == "task_id" and not val: self._task_id = None elif key == "task_id": self._task_id = Task(val) else: setattr(self, '_' + key, val) self._region.uri = self.uri
def _build(self, data): """Iterate over API data responses and update this object according to the data returned """ for key, val in data.items(): if key == 'keys': self._keys = APIList(DynectSession.get_session, 'keys') for key_data in val: key_data['key_type'] = key_data['type'] del key_data['type'] self._keys.append(DNSSECKey(**key_data)) elif key == 'active': self._active = Active(val) else: setattr(self, '_' + key, val) self.uri = '/DNSSEC/{}/'.format(self._zone) self._keys.uri = self.uri
def _build(self, data): """Build the neccesary substructures under this :class:`RTTM`""" for key, val in data.items(): if key == 'region': self._region = APIList(DynectSession.get_session, 'region') for region in val: code = region.pop('region_code', None) pool = region.pop('pool', None) status = region.pop('status', None) r = RTTMRegion(self._zone, self._fqdn, code, pool, **region) r._status = status self._region.append(r) elif key == 'monitor': if self._monitor is not None: self._monitor.zone = self._zone self._monitor.fqdn = self._fqdn else: proto = val.pop('protocol', None) inter = val.pop('interval', None) self._monitor = Monitor(proto, inter, **val) elif key == 'performance_monitor': if self._performance_monitor is not None: self._performance_monitor.zone = self._zone self._performance_monitor.fqdn = self._fqdn else: proto = val.pop('protocol', None) inter = val.pop('interval', None) self._performance_monitor = PerformanceMonitor(proto, inter, **val) elif key == 'notify_events': self._notify_events = [item.strip() for item in val.split(',')] elif key == 'active': self._active = Active(val) elif key == "task_id" and not val: self._task_id = None elif key == "task_id": self._task_id = Task(val) else: setattr(self, '_' + key, val) self._region.uri = self.uri
def __init__(self, zone, fqdn, *args, **kwargs): """Create a :class:`GSLB` object :param auto_recover: Indicates whether or not the service should automatically come out of failover when the IP addresses resume active status or if the service should remain in failover until manually reset. Must be 'Y' or 'N' :param ttl: Time To Live in seconds of records in the service. Must be less than 1/2 of the Health Probe's monitoring interval. Must be one of 30, 60, 150, 300, or 450 :param notify_events: A comma separated list of the events which trigger notifications. Must be one of 'ip', 'svc', or 'nosrv' :param syslog_server: The Hostname or IP address of a server to receive syslog notifications on monitoring events :param syslog_port: The port where the remote syslog server listens for notifications :param syslog_ident: The ident to use when sending syslog notifications :param syslog_facility: The syslog facility to use when sending syslog notifications. Must be one of 'kern', 'user', 'mail', 'daemon', 'auth', 'syslog', 'lpr', 'news', 'uucp', 'cron', 'authpriv', 'ftp', 'ntp', 'security', 'console', 'local0', 'local1', 'local2', 'local3', 'local4', 'local5', 'local6', or 'local7' :param syslog_delivery: The syslog delivery action type. 'all' will deliver notifications no matter what the endpoint state. 'change' (default) will deliver only on change in the detected endpoint state :param region: A list of :class:`GSLBRegion`'s :param monitor: The health :class:`Monitor` for this service :param contact_nickname: Name of contact to receive notifications :param syslog_probe_fmt: see below for format: :param syslog_status_fmt: see below for format: Use the following format for syslog_xxxx_fmt paramaters. %hos hostname %tim current timestamp or monitored interval %reg region code %sta status %ser record serial %rda rdata %sit monitoring site %rti response time %msg message from monitoring %adr address of monitored node %med median value %rts response times (RTTM) :param recovery_delay: number of up status polling intervals to consider service up """ super(GSLB, self).__init__() self.valid_auto_recover = ('Y', 'N') self.valid_ttls = (30, 60, 150, 300, 450) self.valid_notify_events = ('ip', 'svc', 'nosrv') self.valid_syslog_facility = ('kern', 'user', 'mail', 'daemon', 'auth', 'syslog', 'lpr', 'news', 'uucp', 'cron', 'authpriv', 'ftp', 'ntp', 'security', 'console', 'local0', 'local1', 'local2', 'local3', 'local4', 'local5', 'local6', 'local7') self._zone = zone self._fqdn = fqdn self.uri = '/GSLB/{}/{}/'.format(self._zone, self._fqdn) self._auto_recover = self._ttl = self._notify_events = None self._syslog_server = self._syslog_port = self._syslog_ident = None self._syslog_facility = self._monitor = self._contact_nickname = None self._syslog_probe_fmt = self._syslog_status_fmt = None self._active = self._status = self._syslog_delivery = None self._task_id = None self._recovery_delay = None self._region = APIList(DynectSession.get_session, 'region') if 'api' in kwargs: del kwargs['api'] self._build(kwargs) elif len(args) == 0 and len(kwargs) == 0: self._get() else: self._post(*args, **kwargs) self._region.uri = self.uri
class GSLB(object): """A Global Server Load Balancing (GSLB) service""" def __init__(self, zone, fqdn, *args, **kwargs): """Create a :class:`GSLB` object :param auto_recover: Indicates whether or not the service should automatically come out of failover when the IP addresses resume active status or if the service should remain in failover until manually reset. Must be 'Y' or 'N' :param ttl: Time To Live in seconds of records in the service. Must be less than 1/2 of the Health Probe's monitoring interval. Must be one of 30, 60, 150, 300, or 450 :param notify_events: A comma separated list of the events which trigger notifications. Must be one of 'ip', 'svc', or 'nosrv' :param syslog_server: The Hostname or IP address of a server to receive syslog notifications on monitoring events :param syslog_port: The port where the remote syslog server listens for notifications :param syslog_ident: The ident to use when sending syslog notifications :param syslog_facility: The syslog facility to use when sending syslog notifications. Must be one of 'kern', 'user', 'mail', 'daemon', 'auth', 'syslog', 'lpr', 'news', 'uucp', 'cron', 'authpriv', 'ftp', 'ntp', 'security', 'console', 'local0', 'local1', 'local2', 'local3', 'local4', 'local5', 'local6', or 'local7' :param syslog_delivery: The syslog delivery action type. 'all' will deliver notifications no matter what the endpoint state. 'change' (default) will deliver only on change in the detected endpoint state :param region: A list of :class:`GSLBRegion`'s :param monitor: The health :class:`Monitor` for this service :param contact_nickname: Name of contact to receive notifications :param syslog_probe_fmt: see below for format: :param syslog_status_fmt: see below for format: Use the following format for syslog_xxxx_fmt paramaters. %hos hostname %tim current timestamp or monitored interval %reg region code %sta status %ser record serial %rda rdata %sit monitoring site %rti response time %msg message from monitoring %adr address of monitored node %med median value %rts response times (RTTM) :param recovery_delay: number of up status polling intervals to consider service up """ super(GSLB, self).__init__() self.valid_auto_recover = ('Y', 'N') self.valid_ttls = (30, 60, 150, 300, 450) self.valid_notify_events = ('ip', 'svc', 'nosrv') self.valid_syslog_facility = ('kern', 'user', 'mail', 'daemon', 'auth', 'syslog', 'lpr', 'news', 'uucp', 'cron', 'authpriv', 'ftp', 'ntp', 'security', 'console', 'local0', 'local1', 'local2', 'local3', 'local4', 'local5', 'local6', 'local7') self._zone = zone self._fqdn = fqdn self.uri = '/GSLB/{}/{}/'.format(self._zone, self._fqdn) self._auto_recover = self._ttl = self._notify_events = None self._syslog_server = self._syslog_port = self._syslog_ident = None self._syslog_facility = self._monitor = self._contact_nickname = None self._syslog_probe_fmt = self._syslog_status_fmt = None self._active = self._status = self._syslog_delivery = None self._task_id = None self._recovery_delay = None self._region = APIList(DynectSession.get_session, 'region') if 'api' in kwargs: del kwargs['api'] self._build(kwargs) elif len(args) == 0 and len(kwargs) == 0: self._get() else: self._post(*args, **kwargs) self._region.uri = self.uri def _post(self, contact_nickname, region, auto_recover=None, ttl=None, notify_events=None, syslog_server=None, syslog_port=514, syslog_ident='dynect', syslog_facility='daemon', syslog_probe_fmt=None, syslog_status_fmt=None, monitor=None, syslog_delivery='change', recovery_delay=None): """Create a new :class:`GSLB` service object on the DynECT System""" self._auto_recover = auto_recover self._ttl = ttl self._notify_events = notify_events self._syslog_server = syslog_server self._syslog_port = syslog_port self._syslog_ident = syslog_ident self._syslog_facility = syslog_facility self._syslog_delivery = syslog_delivery self._syslog_probe_fmt = syslog_probe_fmt self._syslog_status_fmt = syslog_status_fmt self._recovery_delay = recovery_delay self._region += region self._monitor = monitor self._contact_nickname = contact_nickname api_args = {'contact_nickname': self._contact_nickname, 'region': [r._json for r in self._region]} if auto_recover: api_args['auto_recover'] = self._auto_recover if ttl: api_args['ttl'] = self._ttl if notify_events: api_args['notify_events'] = self._notify_events if syslog_server: api_args['syslog_server'] = self._syslog_server if syslog_port: api_args['syslog_port'] = self._syslog_port if syslog_ident: api_args['syslog_ident'] = self._syslog_ident if syslog_facility: api_args['syslog_facility'] = self._syslog_facility if syslog_delivery: api_args['syslog_delivery'] = self._syslog_delivery if syslog_probe_fmt: api_args['syslog_probe_fmt'] = self._syslog_probe_fmt if syslog_status_fmt: api_args['syslog_status_fmt'] = self._syslog_status_fmt if recovery_delay: api_args['recovery_delay'] = self._recovery_delay if monitor: api_args['monitor'] = self._monitor.to_json() self._monitor.zone = self._zone self._monitor.fqdn = self._fqdn response = DynectSession.get_session().execute(self.uri, 'POST', api_args) self._build(response['data']) def _get(self): """Get an existing :class:`GSLB` service object""" api_args = {} response = DynectSession.get_session().execute(self.uri, 'GET', api_args) self._build(response['data']) def _build(self, data, region=True): """Private method which builds the objects fields based on the data returned by an API call :param data: the data from the JSON respnose :param region: Boolean flag specifying whether to rebuild the region objects or not """ for key, val in data.items(): if key == 'region': if region: self._region = APIList(DynectSession.get_session, 'region') for region in val: region_code = region.pop('region_code', None) self._region.append(GSLBRegion(self._zone, self._fqdn, region_code, **region)) elif key == 'monitor': # We already have the monitor object, no need to rebuild it pass elif key == "task_id" and not val: self._task_id = None elif key == "task_id": self._task_id = Task(val) else: setattr(self, '_' + key, val) self._region.uri = self.uri @property def task(self): """:class:`Task` for most recent system action on this :class:`GSLB`. """ if self._task_id: self._task_id.refresh() return self._task_id def sync(self): """Sync this :class:`GSLB` object with the DynECT System""" self._get() def activate(self): """Activate this :class:`GSLB` service on the DynECT System""" api_args = {'activate': True} response = DynectSession.get_session().execute(self.uri, 'PUT', api_args) self._build(response['data'], region=False) def deactivate(self): """Deactivate this :class:`GSLB` service on the DynECT System""" api_args = {'deactivate': True} response = DynectSession.get_session().execute(self.uri, 'PUT', api_args) self._build(response['data'], region=False) def recover(self, address=None): """Recover the GSLB service on the designated zone node or a specific node IP within the service """ api_args = {} if address: api_args['recoverip'] = True api_args['address'] = address else: api_args['recover'] = True response = DynectSession.get_session().execute(self.uri, 'PUT', api_args) self._build(response['data'], region=False) @property def auto_recover(self): """Indicates whether or not the service should automatically come out of failover when the IP addresses resume active status or if the service should remain in failover until manually reset. Must be 'Y' or 'N' """ return self._auto_recover @auto_recover.setter def auto_recover(self, value): if value not in self.valid_auto_recover: raise DynectInvalidArgumentError('auto_recover', value, self.valid_auto_recover) self._auto_recover = value api_args = {'auto_recover': self._auto_recover} response = DynectSession.get_session().execute(self.uri, 'PUT', api_args) self._build(response['data'], region=False) @property def status(self): """The current state of the service. Will be one of 'unk', 'ok', 'trouble', or 'failover' """ api_args = {} respnose = DynectSession.get_session().execute(self.uri, 'GET', api_args) self._status = respnose['data']['status'] return self._status @status.setter def status(self, value): pass @property def active(self): """Indicates if the service is active. When setting directly, rather than using activate/deactivate valid arguments are 'Y' or True to activate, or 'N' or False to deactivate. Note: If your service is already active and you try to activate it, nothing will happen. And vice versa for deactivation. :returns: An :class:`Active` object representing the current state of this :class:`GSLB` Service """ return self._active @active.setter def active(self, value): deactivate = ('N', False) activate = ('Y', True) if value in deactivate and self.active: self.deactivate() elif value in activate and not self.active: self.activate() @property def ttl(self): """Time To Live in seconds of records in the service. Must be less than 1/2 of the Health Probe's monitoring interval. Must be one of 30, 60, 150, 300, or 450 """ return self._ttl @ttl.setter def ttl(self, value): if value not in self.valid_ttls: raise DynectInvalidArgumentError('ttl', value, self.valid_ttls) self._ttl = value api_args = {'ttl': self._ttl} response = DynectSession.get_session().execute(self.uri, 'PUT', api_args) self._build(response['data'], region=False) @property def notify_events(self): """A comma separated list of the events which trigger notifications. Must be one of 'ip', 'svc', or 'nosrv' """ return self._notify_events @notify_events.setter def notify_events(self, value): if value not in self.valid_notify_events: raise DynectInvalidArgumentError('notify_events', value, self.valid_notify_events) self._notify_events = value api_args = {'notify_events': self._notify_events} response = DynectSession.get_session().execute(self.uri, 'PUT', api_args) self._build(response['data'], region=False) @property def syslog_server(self): """The Hostname or IP address of a server to receive syslog notifications on monitoring events """ self._get() return self._syslog_server @syslog_server.setter def syslog_server(self, value): self._syslog_server = value api_args = {'syslog_server': self._syslog_server} response = DynectSession.get_session().execute(self.uri, 'PUT', api_args) self._build(response['data'], region=False) @property def syslog_port(self): """The port where the remote syslog server listens for notifications""" self._get() return self._syslog_port @syslog_port.setter def syslog_port(self, value): self._syslog_port = value api_args = {'syslog_port': self._syslog_port} response = DynectSession.get_session().execute(self.uri, 'PUT', api_args) self._build(response['data'], region=False) @property def syslog_ident(self): """The ident to use when sending syslog notifications""" self._get() return self._syslog_ident @syslog_ident.setter def syslog_ident(self, value): self._syslog_ident = value api_args = {'syslog_ident': self._syslog_ident} response = DynectSession.get_session().execute(self.uri, 'PUT', api_args) self._build(response['data'], region=False) @property def syslog_facility(self): """The syslog facility to use when sending syslog notifications. Must be one of 'kern', 'user', 'mail', 'daemon', 'auth', 'syslog', 'lpr', 'news', 'uucp', 'cron', 'authpriv', 'ftp', 'ntp', 'security', 'console', 'local0', 'local1', 'local2', 'local3', 'local4', 'local5', 'local6', or 'local7' """ self._get() return self._syslog_facility @syslog_facility.setter def syslog_facility(self, value): if value not in self.valid_syslog_facility: raise DynectInvalidArgumentError('syslog_facility', value, self.valid_syslog_facility) self._syslog_facility = value api_args = {'syslog_facility': self._syslog_facility} response = DynectSession.get_session().execute(self.uri, 'PUT', api_args) self._build(response['data'], region=False) @property def syslog_delivery(self): self._get() return self._syslog_delivery @syslog_delivery.setter def syslog_delivery(self, value): api_args = {'syslog_delivery': value} response = DynectSession.get_session().execute(self.uri, 'PUT', api_args) self._build(response['data'], region=False) @property def syslog_probe_format(self): self._get() return self._syslog_probe_fmt @syslog_probe_format.setter def syslog_probe_format(self, value): api_args = {'syslog_probe_fmt': value} response = DynectSession.get_session().execute(self.uri, 'PUT', api_args) self._build(response['data'], region=False) @property def syslog_status_format(self): self._get() return self._syslog_status_fmt @syslog_status_format.setter def syslog_status_format(self, value): api_args = {'syslog_status_fmt': value} response = DynectSession.get_session().execute(self.uri, 'PUT', api_args) self._build(response['data'], region=False) @property def recovery_delay(self): self._get() return self._recovery_delay @recovery_delay.setter def recovery_delay(self, value): api_args = {'recovery_delay': value} response = DynectSession.get_session().execute(self.uri, 'PUT', api_args) self._build(response['data'], region=False) @property def region(self): """A list of :class:`GSLBRegion`'s""" return self._region @region.setter def region(self, value): if isinstance(value, list) and not isinstance(value, APIList): self._region = APIList(DynectSession.get_session, 'region', None, value) elif isinstance(value, APIList): self._region = value self._region.uri = self.uri @property def monitor(self): """The health :class:`Monitor` for this service""" return self._monitor @monitor.setter def monitor(self, value): # We're only going accept new monitors of type Monitor if isinstance(value, Monitor): api_args = {'monitor': value.to_json()} response = DynectSession.get_session().execute(self.uri, 'PUT', api_args) self._build(response['data'], region=False) self._monitor = value @property def contact_nickname(self): """Name of contact to receive notifications from this :class:`GSLB` service """ return self._contact_nickname @contact_nickname.setter def contact_nickname(self, value): self._contact_nickname = value api_args = {'contact_nickname': self._contact_nickname} response = DynectSession.get_session().execute(self.uri, 'PUT', api_args) self._build(response['data'], region=False) def delete(self): """Delete this :class:`GSLB` service from the DynECT System""" api_args = {} DynectSession.get_session().execute(self.uri, 'DELETE', api_args) def __str__(self): """str override""" return force_unicode('<GSLB>: {}').format(self._fqdn) __repr__ = __unicode__ = __str__ def __bytes__(self): """bytes override""" return bytes(self.__str__())
class DNSSEC(object): """A DynECT System DNSSEC Service""" def __init__(self, zone, *args, **kwargs): """Create a :class:`DNSSEC` object :param zone: the zone this service will be attached to :param keys: a list of :class:`DNSSECKey`'s for the service :param contact_nickname: Name of contact to receive notifications :param notify_events: A ``list`` of events that trigger notifications. Valid values are "create" (a new version of a key was created), "expire" (a key was automatically expired), or "warning" (early warnings (2 weeks, 1 week, 1 day) of events) """ super(DNSSEC, self).__init__() self.valid_notify_events = ('create', 'expire', 'warning') self._zone = zone self._contact_nickname = self._notify_events = None self._keys = APIList(DynectSession.get_session, 'keys') self._active = None self.uri = '/DNSSEC/{}/'.format(self._zone) if 'api' in kwargs: del kwargs['api'] self._build(kwargs) elif len(args) == 0 and len(kwargs) == 0: self._get() else: self._post(*args, **kwargs) self._keys.uri = self.uri def _post(self, keys, contact_nickname, notify_events=None): """Create a new :class:`DNSSEC` Service on the Dynect System""" self._keys += keys self._contact_nickname = contact_nickname self._notify_events = notify_events api_args = {'keys': [key._json for key in self._keys], 'contact_nickname': self._contact_nickname} for key, val in self.__dict__.items(): if val is not None and not hasattr(val, '__call__') and \ key.startswith('_'): if key == '_user_name' or key == '_keys': pass else: api_args[key[1:]] = val # Need to cast to CSV for API if self._notify_events is not None: api_args['notify_events'] = ','.join(self._notify_events) response = DynectSession.get_session().execute(self.uri, 'POST', api_args) self._build(response['data']) def _get(self): """Update this object from an existing :class:`DNSSEC` service from the Dynect System. """ api_args = {} response = DynectSession.get_session().execute(self.uri, 'GET', api_args) self._build(response['data']) def _build(self, data): """Iterate over API data responses and update this object according to the data returned """ for key, val in data.items(): if key == 'keys': self._keys = APIList(DynectSession.get_session, 'keys') for key_data in val: key_data['key_type'] = key_data['type'] del key_data['type'] self._keys.append(DNSSECKey(**key_data)) elif key == 'active': self._active = Active(val) else: setattr(self, '_' + key, val) self.uri = '/DNSSEC/{}/'.format(self._zone) self._keys.uri = self.uri @property def zone(self): """The name of the zone where this service exists. This is a read-only property """ return self._zone @zone.setter def zone(self, value): pass @property def active(self): """The current status of this :class:`DNSSEC` service. When setting directly, rather than using activate/deactivate valid arguments are 'Y' or True to activate, or 'N' or False to deactivate. Note: If your service is already active and you try to activate it, nothing will happen. And vice versa for deactivation. :returns: An :class:`Active` object representing the current state of this :class:`DNSSEC` Service """ self._get() # Do a get to ensure an up-to-date status is returned return self._active @active.setter def active(self, value): deactivate = ('N', False) activate = ('Y', True) if value in deactivate and self.active: self.deactivate() elif value in activate and not self.active: self.activate() @property def contact_nickname(self): """Name of contact to receive notifications""" return self._contact_nickname @contact_nickname.setter def contact_nickname(self, value): self._contact_nickname = value api_args = {'contact_nickname': self._contact_nickname} response = DynectSession.get_session().execute(self.uri, 'PUT', api_args) self._build(response['data']) @property def notify_events(self): """A list of events that trigger notifications. Valid values are: create (a new version of a key was created), expire (a key was automatically expired), warning (early warnings (2 weeks, 1 week, 1 day) of events) """ return self._notify_events @notify_events.setter def notify_events(self, value): for val in value: if val not in self.valid_notify_events: raise DynectInvalidArgumentError('notify_events', val, self.valid_notify_events) value = ','.join(value) api_args = {'notify_events': value} response = DynectSession.get_session().execute(self.uri, 'PUT', api_args) self._build(response['data']) @property def keys(self): """A List of :class:`DNSSECKey`'s associated with this :class:`DNSSEC` service """ # Need this check for get_all_dnssec calls which do not return key info if self._keys is None or self._keys == []: self._get() return self._keys @keys.setter def keys(self, value): if isinstance(value, list) and not isinstance(value, APIList): self._keys = APIList(DynectSession.get_session, 'keys', None, value) elif isinstance(value, APIList): self._keys = value self._keys.uri = self.uri def activate(self): """Activate this :class:`DNSSEC` service""" api_args = {'activate': 'Y'} response = DynectSession.get_session().execute(self.uri, 'PUT', api_args) self._build(response['data']) def deactivate(self): """Deactivate this :class:`DNSSEC` service""" api_args = {'deactivate': 'Y'} response = DynectSession.get_session().execute(self.uri, 'PUT', api_args) self._build(response['data']) def timeline_report(self, start_ts=None, end_ts=None): """Generates a report of events this :class:`DNSSEC` service has performed and has scheduled to perform :param start_ts: datetime.datetime instance identifying point in time for the start of the timeline report :param end_ts: datetime.datetime instance identifying point in time for the end of the timeline report. Defaults to datetime.datetime.now() """ api_args = {'zone': self._zone} if start_ts is not None: api_args['start_ts'] = unix_date(start_ts) if end_ts is not None: api_args['end_ts'] = unix_date(end_ts) elif end_ts is None and start_ts is not None: api_args['end_ts'] = unix_date(datetime.now()) uri = '/DNSSECTimelineReport/' response = DynectSession.get_session().execute(uri, 'POST', api_args) return response['data'] def delete(self): """Delete this :class:`DNSSEC` Service from the DynECT System""" api_args = {} DynectSession.get_session().execute(self.uri, 'DELETE', api_args) def __str__(self): """str override""" return force_unicode('<DNSSEC>: {}').format(self._zone) __repr__ = __unicode__ = __str__ def __bytes__(self): """bytes override""" return bytes(self.__str__())
def __init__(self, zone, fqdn, *args, **kwargs): """Create a :class:`RTTM` object :param auto_recover: Indicates whether or not the service should automatically come out of failover when the IP addresses resume active status or if the service should remain in failover until manually reset. Must be one of 'Y' or 'N' :param ttl: Time To Live in seconds of records in the service. Must be less than 1/2 of the Health Probe's monitoring interval. Must be one of 30, 60, 150, 300, or 450. :param notify_events: A list of the events which trigger notifications. Must be one of 'ip', 'svc', or 'nosrv' :param syslog_server: The Hostname or IP address of a server to receive syslog notifications on monitoring events :param syslog_port: The port where the remote syslog server listens for notifications :param syslog_ident: The ident to use when sending syslog notifications :param syslog_facility: The syslog facility to use when sending syslog notifications. Must be one of kern, user, mail, daemon, auth, syslog, lpr, news, uucp, cron, authpriv, ftp, ntp, security, console, local0, local1, local2, local3, local4, local5, local6, or local7 :param syslog_delivery: The syslog delivery action type. 'all' will deliver notifications no matter what the endpoint state. 'change' (default) will deliver only on change in the detected endpoint state :param region: A list of :class:`RTTMRegion`'s :param monitor: The :class:`Monitor` for this service :param performance_monitor: The performance monitor for the service :param contact_nickname: Name of contact to receive notifications :param syslog_probe_fmt: see below for format: :param syslog_status_fmt: see below for format: :param syslog_rttm_fmt: see below for format: Use the following format for syslog_xxxx_fmt paramaters. %hos hostname %tim current timestamp or monitored interval %reg region code %sta status %ser record serial %rda rdata %sit monitoring site %rti response time %msg message from monitoring %adr address of monitored node %med median value %rts response times (RTTM) :param recovery_delay: number of up status polling intervals to consider service up """ super(RTTM, self).__init__() self.valid_ttls = (30, 60, 150, 300, 450) self.valid_notify_events = ('ip', 'svc', 'nosrv') self.valid_syslog_facilities = ('kern', 'user', 'mail', 'daemon', 'auth', 'syslog', 'lpr', 'news', 'uucp', 'cron', 'authpriv', 'ftp', 'ntp', 'security', 'console', 'local0', 'local1', 'local2', 'local3', 'local4', 'local5', 'local6', 'local7') self._zone = zone self._fqdn = fqdn self.uri = '/RTTM/{}/{}/'.format(self._zone, self._fqdn) self._auto_recover = self._ttl = self._notify_events = None self._syslog_server = self._syslog_port = self._syslog_ident = None self._syslog_facility = self._monitor = None self._performance_monitor = self._contact_nickname = None self._active = self._syslog_delivery = self._syslog_probe_fmt = None self._syslog_status_fmt = self._syslog_rttm_fmt = None self._recovery_delay = None self._region = APIList(DynectSession.get_session, 'region') self._task_id = None if 'api' in kwargs: del kwargs['api'] self._build(kwargs) elif len(args) == 0 and len(kwargs) == 0: self._get() else: self._post(*args, **kwargs) self._region.uri = self.uri
class RTTM(object): def __init__(self, zone, fqdn, *args, **kwargs): """Create a :class:`RTTM` object :param auto_recover: Indicates whether or not the service should automatically come out of failover when the IP addresses resume active status or if the service should remain in failover until manually reset. Must be one of 'Y' or 'N' :param ttl: Time To Live in seconds of records in the service. Must be less than 1/2 of the Health Probe's monitoring interval. Must be one of 30, 60, 150, 300, or 450. :param notify_events: A list of the events which trigger notifications. Must be one of 'ip', 'svc', or 'nosrv' :param syslog_server: The Hostname or IP address of a server to receive syslog notifications on monitoring events :param syslog_port: The port where the remote syslog server listens for notifications :param syslog_ident: The ident to use when sending syslog notifications :param syslog_facility: The syslog facility to use when sending syslog notifications. Must be one of kern, user, mail, daemon, auth, syslog, lpr, news, uucp, cron, authpriv, ftp, ntp, security, console, local0, local1, local2, local3, local4, local5, local6, or local7 :param syslog_delivery: The syslog delivery action type. 'all' will deliver notifications no matter what the endpoint state. 'change' (default) will deliver only on change in the detected endpoint state :param region: A list of :class:`RTTMRegion`'s :param monitor: The :class:`Monitor` for this service :param performance_monitor: The performance monitor for the service :param contact_nickname: Name of contact to receive notifications :param syslog_probe_fmt: see below for format: :param syslog_status_fmt: see below for format: :param syslog_rttm_fmt: see below for format: Use the following format for syslog_xxxx_fmt paramaters. %hos hostname %tim current timestamp or monitored interval %reg region code %sta status %ser record serial %rda rdata %sit monitoring site %rti response time %msg message from monitoring %adr address of monitored node %med median value %rts response times (RTTM) :param recovery_delay: number of up status polling intervals to consider service up """ super(RTTM, self).__init__() self.valid_ttls = (30, 60, 150, 300, 450) self.valid_notify_events = ('ip', 'svc', 'nosrv') self.valid_syslog_facilities = ('kern', 'user', 'mail', 'daemon', 'auth', 'syslog', 'lpr', 'news', 'uucp', 'cron', 'authpriv', 'ftp', 'ntp', 'security', 'console', 'local0', 'local1', 'local2', 'local3', 'local4', 'local5', 'local6', 'local7') self._zone = zone self._fqdn = fqdn self.uri = '/RTTM/{}/{}/'.format(self._zone, self._fqdn) self._auto_recover = self._ttl = self._notify_events = None self._syslog_server = self._syslog_port = self._syslog_ident = None self._syslog_facility = self._monitor = None self._performance_monitor = self._contact_nickname = None self._active = self._syslog_delivery = self._syslog_probe_fmt = None self._syslog_status_fmt = self._syslog_rttm_fmt = None self._recovery_delay = None self._region = APIList(DynectSession.get_session, 'region') self._task_id = None if 'api' in kwargs: del kwargs['api'] self._build(kwargs) elif len(args) == 0 and len(kwargs) == 0: self._get() else: self._post(*args, **kwargs) self._region.uri = self.uri def _post(self, contact_nickname, performance_monitor, region, ttl=None, auto_recover=None, notify_events=None, syslog_server=None, syslog_port=514, syslog_ident='dynect', syslog_facility='daemon', syslog_delivery='change', syslog_probe_fmt=None, syslog_status_fmt=None, syslog_rttm_fmt=None, recovery_delay=None, monitor=None): """Create a new RTTM Service on the DynECT System""" self._auto_recover = auto_recover self._ttl = ttl self._notify_events = notify_events self._syslog_server = syslog_server self._syslog_port = syslog_port self._syslog_ident = syslog_ident self._syslog_facility = syslog_facility self._syslog_delivery = syslog_delivery self._region += region self._monitor = monitor self._performance_monitor = performance_monitor self._contact_nickname = contact_nickname self._syslog_probe_fmt = syslog_probe_fmt self._syslog_status_fmt = syslog_status_fmt self._syslog_rttm_fmt = syslog_rttm_fmt self._recovery_delay = recovery_delay api_args = {} if auto_recover: if auto_recover not in ('Y', 'N'): raise DynectInvalidArgumentError('auto_recover', auto_recover, ('Y', 'N')) api_args['auto_recover'] = self._auto_recover if ttl: if ttl not in self.valid_ttls: raise DynectInvalidArgumentError('ttl', ttl, self.valid_ttls) api_args['ttl'] = self._ttl if notify_events: for event in notify_events: if event not in self.valid_notify_events: raise DynectInvalidArgumentError('notify_events', event, self.valid_notify_events) api_args['notify_events'] = self._notify_events if syslog_server: api_args['syslog_server'] = self._syslog_server if syslog_port: api_args['syslog_port'] = self._syslog_port if syslog_ident: api_args['syslog_ident'] = self._syslog_ident if syslog_facility: if syslog_facility not in self.valid_syslog_facilities: raise DynectInvalidArgumentError('syslog_facility', syslog_facility, self.valid_syslog_facilities) api_args['syslog_facility'] = self._syslog_facility if syslog_delivery: api_args['syslog_delivery'] = self._syslog_delivery if syslog_probe_fmt: api_args['syslog_probe_fmt'] = self._syslog_probe_fmt if syslog_status_fmt: api_args['syslog_status_fmt'] = self._syslog_status_fmt if syslog_rttm_fmt: api_args['syslog_rttm_fmt'] = self._syslog_rttm_fmt if recovery_delay: api_args['recovery_delay'] = self._recovery_delay if region: api_args['region'] = [reg._json for reg in self._region] if monitor: api_args['monitor'] = self._monitor.to_json() if performance_monitor: mon_args = self._performance_monitor.to_json() api_args['performance_monitor'] = mon_args if contact_nickname: api_args['contact_nickname'] = self._contact_nickname # API expects a CSV string, not a list if isinstance(self.notify_events, list): api_args['notify_events'] = ','.join(self.notify_events) response = DynectSession.get_session().execute(self.uri, 'POST', api_args) self._build(response['data']) def _get(self): """Build an object around an existing DynECT RTTM Service""" api_args = {} response = DynectSession.get_session().execute(self.uri, 'GET', api_args) self._build(response['data']) def _update(self, api_args): """Perform a PUT api call using this objects data""" response = DynectSession.get_session().execute(self.uri, 'PUT', api_args) self._build(response['data']) def _build(self, data): """Build the neccesary substructures under this :class:`RTTM`""" for key, val in data.items(): if key == 'region': self._region = APIList(DynectSession.get_session, 'region') for region in val: code = region.pop('region_code', None) pool = region.pop('pool', None) status = region.pop('status', None) r = RTTMRegion(self._zone, self._fqdn, code, pool, **region) r._status = status self._region.append(r) elif key == 'monitor': if self._monitor is not None: self._monitor.zone = self._zone self._monitor.fqdn = self._fqdn else: proto = val.pop('protocol', None) inter = val.pop('interval', None) self._monitor = Monitor(proto, inter, **val) elif key == 'performance_monitor': if self._performance_monitor is not None: self._performance_monitor.zone = self._zone self._performance_monitor.fqdn = self._fqdn else: proto = val.pop('protocol', None) inter = val.pop('interval', None) self._performance_monitor = PerformanceMonitor(proto, inter, **val) elif key == 'notify_events': self._notify_events = [item.strip() for item in val.split(',')] elif key == 'active': self._active = Active(val) elif key == "task_id" and not val: self._task_id = None elif key == "task_id": self._task_id = Task(val) else: setattr(self, '_' + key, val) self._region.uri = self.uri @property def task(self): """:class:`Task` for most recent system action on this :class:`ActiveFailover`.""" if self._task_id: self._task_id.refresh() return self._task_id def get_rrset_report(self, ts): """Generates a report of regional response sets for this RTTM service at a given point in time :param ts: UNIX timestamp identifying point in time for the log report :return: dictionary containing rrset report data """ api_args = {'zone': self._zone, 'fqdn': self._fqdn, 'ts': ts} response = DynectSession.get_session().execute('/RTTMRRSetReport/', 'POST', api_args) return response['data'] def get_log_report(self, start_ts, end_ts=None): """Generates a report with information about changes to an existing RTTM service :param start_ts: datetime.datetime instance identifying point in time for the start of the log report :param end_ts: datetime.datetime instance identifying point in time for the end of the log report. Defaults to datetime.datetime.now() :return: dictionary containing log report data """ end_ts = end_ts or datetime.now() api_args = {'zone': self._zone, 'fqdn': self._fqdn, 'start_ts': unix_date(start_ts), 'end_ts': unix_date(end_ts)} response = DynectSession.get_session().execute('/RTTMLogReport/', 'POST', api_args) return response['data'] def activate(self): """Activate this RTTM Service""" api_args = {'activate': True} response = DynectSession.get_session().execute(self.uri, 'PUT', api_args) self._build(response['data']) def deactivate(self): """Deactivate this RTTM Service""" api_args = {'deactivate': True} response = DynectSession.get_session().execute(self.uri, 'PUT', api_args) self._build(response['data']) def recover(self, recoverip=None, address=None): """Recovers the RTTM service or a specific node IP within the service """ api_args = {'recover': True} if recoverip: api_args['recoverip'] = recoverip api_args['address'] = address response = DynectSession.get_session().execute(self.uri, 'PUT', api_args) self._build(response['data']) @property def active(self): """Returns whether or not this :class:`RTTM` Service is currently active. When setting directly, rather than using activate/deactivate valid arguments are 'Y' or True to activate, or 'N' or False to deactivate. Note: If your service is already active and you try to activate it, nothing will happen. And vice versa for deactivation. :returns: An :class:`Active` object representing the current state of this :class:`ReverseDNS` Service """ return self._active @active.setter def active(self, value): deactivate = ('N', False) activate = ('Y', True) if value in deactivate and self.active: self.deactivate() elif value in activate and not self.active: self.activate() @property def auto_recover(self): """Indicates whether or not the service should automatically come out of failover when the IP addresses resume active status or if the service should remain in failover until manually reset. Must be one of 'Y' or 'N' """ return self._auto_recover @auto_recover.setter def auto_recover(self, value): if value not in ('Y', 'N'): raise DynectInvalidArgumentError('auto_recover', value, ('Y', 'N')) api_args = {'auto_recover': value} self._update(api_args) @property def ttl(self): """Time To Live in seconds of records in the service. Must be less than 1/2 of the Health Probe's monitoring interval. Must be one of 30, 60, 150, 300, or 450. """ return self._ttl @ttl.setter def ttl(self, value): if value not in self.valid_ttls: raise DynectInvalidArgumentError('ttl', value, self.valid_ttls) api_args = {'ttl': value} self._update(api_args) @property def notify_events(self): """A list of events which trigger notifications. Valid values are: 'ip', 'svc', and 'nosrv' """ return self._notify_events @notify_events.setter def notify_events(self, value): for val in value: if val not in self.valid_notify_events: raise DynectInvalidArgumentError('notify_events', val, self.valid_notify_events) value = ','.join(value) api_args = {'notify_events': value} self._update(api_args) @property def status(self): """Status""" self._get() return self._status @property def syslog_server(self): """The Hostname or IP address of a server to receive syslog notifications on monitoring events """ self._get() return self._syslog_server @syslog_server.setter def syslog_server(self, value): api_args = {'syslog_server': value} self._update(api_args) @property def syslog_port(self): """The port where the remote syslog server listens for notifications""" self._get() return self._syslog_port @syslog_port.setter def syslog_port(self, value): api_args = {'syslog_port': value} self._update(api_args) @property def syslog_ident(self): """The ident to use when sending syslog notifications""" self._get() return self._syslog_ident @syslog_ident.setter def syslog_ident(self, value): api_args = {'syslog_ident': value} self._update(api_args) @property def syslog_facility(self): """The syslog facility to use when sending syslog notifications. Must be one of kern, user, mail, daemon, auth, syslog, lpr, news, uucp, cron, authpriv, ftp, ntp, security, console, local0, local1, local2, local3, local4, local5, local6, or local7 """ self._get() return self._syslog_facility @syslog_facility.setter def syslog_facility(self, value): if value not in self.valid_syslog_facilities: raise DynectInvalidArgumentError('syslog_facility', value, self.valid_syslog_facilities) api_args = {'syslog_facility': value} self._update(api_args) @property def syslog_delivery(self): self._get() return self._syslog_delivery @syslog_delivery.setter def syslog_delivery(self, value): api_args = {'syslog_delivery': value} self._update(api_args) @property def syslog_probe_format(self): self._get() return self._syslog_probe_fmt @syslog_probe_format.setter def syslog_probe_format(self, value): api_args = {'syslog_probe_fmt': value} self._update(api_args) @property def syslog_status_format(self): self._get() return self._syslog_status_fmt @syslog_status_format.setter def syslog_status_format(self, value): api_args = {'syslog_status_fmt': value} self._update(api_args) @property def syslog_rttm_format(self): self._get() return self._syslog_rttm_fmt @syslog_rttm_format.setter def syslog_rttm_format(self, value): api_args = {'syslog_rttm_fmt': value} self._update(api_args) @property def recovery_delay(self): self._get() return self._recovery_delay @recovery_delay.setter def recovery_delay(self, value): api_args = {'recovery_delay': value} self._update(api_args) @property def region(self): """A list of :class:`RTTMRegion`'s""" return self._region @region.setter def region(self, value): if isinstance(value, list) and not isinstance(value, APIList): self._region = APIList(DynectSession.get_session, 'region', None, value) elif isinstance(value, APIList): self._region = value self._region.uri = self.uri @property def monitor(self): """The :class:`Monitor` for this service""" return self._monitor @monitor.setter def monitor(self, value): if isinstance(value, Monitor): self._monitor = value api_args = {'monitor': self._monitor.to_json()} self._update(api_args) @property def performance_monitor(self): """The Performance :class:`Monitor` for this service""" return self._performance_monitor @performance_monitor.setter def performance_monitor(self, value): if isinstance(value, Monitor): self._performance_monitor = value api_args = {'performance_monitor': self._performance_monitor.to_json()} self._update(api_args) @property def contact_nickname(self): """The name of contact to receive notifications from this service""" return self._contact_nickname @contact_nickname.setter def contact_nickname(self, value): api_args = {'contact_nickname': value} self._update(api_args) def delete(self): """Delete this RTTM Service""" api_args = {} DynectSession.get_session().execute(self.uri, 'DELETE', api_args) def __str__(self): """str override""" return force_unicode('<RTTM>: {}').format(self._fqdn) __repr__ = __unicode__ = __str__ def __bytes__(self): """bytes override""" return bytes(self.__str__())
class DNSSEC(object): """A DynECT System DNSSEC Service""" def __init__(self, zone, *args, **kwargs): """Create a :class:`DNSSEC` object :param zone: the zone this service will be attached to :param keys: a list of :class:`DNSSECKey`'s for the service :param contact_nickname: Name of contact to receive notifications :param notify_events: A ``list`` of events that trigger notifications. Valid values are "create" (a new version of a key was created), "expire" (a key was automatically expired), or "warning" (early warnings (2 weeks, 1 week, 1 day) of events) """ super(DNSSEC, self).__init__() self.valid_notify_events = ('create', 'expire', 'warning') self._zone = zone self._contact_nickname = self._notify_events = None self._keys = APIList(DynectSession.get_session, 'keys') self._active = None self.uri = '/DNSSEC/{}/'.format(self._zone) if 'api' in kwargs: del kwargs['api'] self._build(kwargs) elif len(args) == 0 and len(kwargs) == 0: self._get() else: self._post(*args, **kwargs) self._keys.uri = self.uri def _post(self, keys, contact_nickname, notify_events=None): """Create a new :class:`DNSSEC` Service on the Dynect System""" self._keys += keys self._contact_nickname = contact_nickname self._notify_events = notify_events api_args = { 'keys': [key._json for key in self._keys], 'contact_nickname': self._contact_nickname } for key, val in self.__dict__.items(): if val is not None and not hasattr(val, '__call__') and \ key.startswith('_'): if key == '_user_name' or key == '_keys': pass else: api_args[key[1:]] = val # Need to cast to CSV for API if self._notify_events is not None: api_args['notify_events'] = ','.join(self._notify_events) response = DynectSession.get_session().execute(self.uri, 'POST', api_args) self._build(response['data']) def _get(self): """Update this object from an existing :class:`DNSSEC` service from the Dynect System. """ api_args = {} response = DynectSession.get_session().execute(self.uri, 'GET', api_args) self._build(response['data']) def _build(self, data): """Iterate over API data responses and update this object according to the data returned """ for key, val in data.items(): if key == 'keys': self._keys = APIList(DynectSession.get_session, 'keys') for key_data in val: key_data['key_type'] = key_data['type'] del key_data['type'] self._keys.append(DNSSECKey(**key_data)) elif key == 'active': self._active = Active(val) else: setattr(self, '_' + key, val) self.uri = '/DNSSEC/{}/'.format(self._zone) self._keys.uri = self.uri @property def zone(self): """The name of the zone where this service exists. This is a read-only property """ return self._zone @zone.setter def zone(self, value): pass @property def active(self): """The current status of this :class:`DNSSEC` service. When setting directly, rather than using activate/deactivate valid arguments are 'Y' or True to activate, or 'N' or False to deactivate. Note: If your service is already active and you try to activate it, nothing will happen. And vice versa for deactivation. :returns: An :class:`Active` object representing the current state of this :class:`DNSSEC` Service """ self._get() # Do a get to ensure an up-to-date status is returned return self._active @active.setter def active(self, value): deactivate = ('N', False) activate = ('Y', True) if value in deactivate and self.active: self.deactivate() elif value in activate and not self.active: self.activate() @property def contact_nickname(self): """Name of contact to receive notifications""" return self._contact_nickname @contact_nickname.setter def contact_nickname(self, value): self._contact_nickname = value api_args = {'contact_nickname': self._contact_nickname} response = DynectSession.get_session().execute(self.uri, 'PUT', api_args) self._build(response['data']) @property def notify_events(self): """A list of events that trigger notifications. Valid values are: create (a new version of a key was created), expire (a key was automatically expired), warning (early warnings (2 weeks, 1 week, 1 day) of events) """ return self._notify_events @notify_events.setter def notify_events(self, value): for val in value: if val not in self.valid_notify_events: raise DynectInvalidArgumentError('notify_events', val, self.valid_notify_events) value = ','.join(value) api_args = {'notify_events': value} response = DynectSession.get_session().execute(self.uri, 'PUT', api_args) self._build(response['data']) @property def keys(self): """A List of :class:`DNSSECKey`'s associated with this :class:`DNSSEC` service """ # Need this check for get_all_dnssec calls which do not return key info if self._keys is None or self._keys == []: self._get() return self._keys @keys.setter def keys(self, value): if isinstance(value, list) and not isinstance(value, APIList): self._keys = APIList(DynectSession.get_session, 'keys', None, value) elif isinstance(value, APIList): self._keys = value self._keys.uri = self.uri def activate(self): """Activate this :class:`DNSSEC` service""" api_args = {'activate': 'Y'} response = DynectSession.get_session().execute(self.uri, 'PUT', api_args) self._build(response['data']) def deactivate(self): """Deactivate this :class:`DNSSEC` service""" api_args = {'deactivate': 'Y'} response = DynectSession.get_session().execute(self.uri, 'PUT', api_args) self._build(response['data']) def timeline_report(self, start_ts=None, end_ts=None): """Generates a report of events this :class:`DNSSEC` service has performed and has scheduled to perform :param start_ts: datetime.datetime instance identifying point in time for the start of the timeline report :param end_ts: datetime.datetime instance identifying point in time for the end of the timeline report. Defaults to datetime.datetime.now() """ api_args = {'zone': self._zone} if start_ts is not None: api_args['start_ts'] = unix_date(start_ts) if end_ts is not None: api_args['end_ts'] = unix_date(end_ts) elif end_ts is None and start_ts is not None: api_args['end_ts'] = unix_date(datetime.now()) uri = '/DNSSECTimelineReport/' response = DynectSession.get_session().execute(uri, 'POST', api_args) return response['data'] def delete(self): """Delete this :class:`DNSSEC` Service from the DynECT System""" api_args = {} DynectSession.get_session().execute(self.uri, 'DELETE', api_args) def __str__(self): """str override""" return force_unicode('<DNSSEC>: {}').format(self._zone) __repr__ = __unicode__ = __str__ def __bytes__(self): """bytes override""" return bytes(self.__str__())