def test_returns_when_record_already_exists(self): action = SetupMxRecords('my-user', 'my-key', provider='CLOUDFLARE') with patch.object(action, '_driver', new_callable=PropertyMock) as mock_driver: mock_driver.iterate_zones.return_value = [ Zone(id='1', domain='foo.com', type='master', ttl=1, driver=mock_driver), Zone(id='2', domain='my-zone', type='master', ttl=1, driver=mock_driver), ] mock_driver.create_record.side_effect = throw( LibcloudError('81057: The record already exists.', mock_driver)) action('my-domain.my-zone') self.assertEqual(mock_driver.iterate_zones.call_count, 1) self.assertEqual(mock_driver.create_record.call_count, 1)
def setUp(self): GandiLiveDNSDriver.connectionCls.conn_class = GandiLiveMockHttp GandiLiveMockHttp.type = None self.driver = GandiLiveDNSDriver(*DNS_GANDI_LIVE) self.test_zone = Zone(id='example.com', type='master', ttl=None, domain='example.com', extra={'zone_uuid': 'a53re'}, driver=self) self.test_bad_zone = Zone(id='badexample.com', type='master', ttl=None, domain='badexample.com', extra={'zone_uuid': 'a53rf'}, driver=self) self.test_record = Record(id='A:bob', type=RecordType.A, name='bob', zone=self.test_zone, data='127.0.0.1', driver=self, extra={}) self.test_bad_record = Record(id='A:jane', type=RecordType.A, name='jane', zone=self.test_bad_zone, data='127.0.0.1', driver=self, extra={})
def setUp(self): NFSNDNSDriver.connectionCls.conn_classes = (None, NFSNMockHttp) NFSNMockHttp.type = None self.driver = NFSNDNSDriver('testid', 'testsecret') self.test_zone = Zone(id='example.com', domain='example.com', driver=self.driver, type='master', ttl=None, extra={}) self.test_record = Record(id=None, name='', data='192.0.2.1', type=RecordType.A, zone=self.test_zone, driver=self.driver, extra={})
def test_makes_request_when_key_is_set(self): action = SetupMxRecords('my-user', 'my-key', provider='CLOUDFLARE') with patch.object(action, '_driver', new_callable=PropertyMock) as mock_driver: mock_driver.iterate_zones.return_value = [ Zone(id='1', domain='foo.com', type='master', ttl=1, driver=mock_driver), Zone(id='2', domain='my-zone', type='master', ttl=1, driver=mock_driver), ] action('my-domain.my-zone') self.assertEqual(mock_driver.iterate_zones.call_count, 1) self.assertEqual(mock_driver.create_record.call_count, 1)
def _to_zone(self, item): """ Build an Zone object from the item dictionary. """ zone = Zone(id=item['domain'], domain=item['domain'], type='master', ttl=item['ttl'], driver=self) return zone
def iterate_zones(self): for domain in self.connection.request("").object.split("\n"): yield Zone(id=domain, domain=domain + ".", type="master", ttl=0, driver=self)
def __res_to_zone(self, zone): return Zone(id=zone['id'], domain=zone['name'], type=DEFAULT_ZONE_TYPE, ttl=DEFAULT_ZONE_TTL, driver=self, extra={'created': zone['created'], 'servers': zone['servers'], 'account_id': zone['account_id'], 'cluster_id': zone['cluster_id']})
def _to_zone(self, zone): return Zone(id=str(zone['id']), domain=zone['name'], type='master', ttl=0, driver=self, extra={})
def create_zone(self, domain, type='master', ttl=None, extra=None): """ Returns a `Zone` object. :param domain: Zone domain name, (e.g. example.com). :type domain: ``str`` :param type: Zone type (master / slave). :type type: ``str`` :param ttl: TTL for new records. (optional) :type ttl: ``int`` :param extra: (optional) Extra attributes (driver specific). (e.g. {'serverip':'127.0.0.1'}) """ extra = extra or {} if extra and extra.get('serverip'): serverip = extra['serverip'] params = {'api_key': self.key} data = urlencode({'domain': domain, 'serverip': serverip}) action = '/v1/dns/create_domain' zones = self.list_zones() if self.ex_zone_exists(domain, zones): raise ZoneAlreadyExistsError(value='', driver=self, zone_id=domain) self.connection.request(params=params, action=action, data=data, method='POST') zone = Zone(id=domain, domain=domain, type=type, ttl=ttl, driver=self, extra=extra) return zone
def _to_zone(self, item): """ Build an Zone object from the item dictionary. """ extra = { "soa_email": item["soa_email"], "status": item["status"], "description": item["description"], "tags": item["tags"], "retry_sec": item["retry_sec"], "master_ips": item["master_ips"], "axfr_ips": item["axfr_ips"], "expire_sec": item["expire_sec"], "refresh_sec": item["refresh_sec"], "created": self._to_datetime(item["created"]), "updated": self._to_datetime(item["updated"]), } zone = Zone( id=item["id"], domain=item["domain"], type=item["type"], ttl=item["ttl_sec"], driver=self, extra=extra, ) return zone
def create_zone(self, domain, type="master", ttl=None, extra=None): """ Create a new zone. API docs: http://www.linode.com/api/dns/domain.create """ params = { "api_action": "domain.create", "Type": type, "Domain": domain } if ttl: params["TTL_sec"] = ttl merged = merge_valid_keys(params=params, valid_keys=VALID_ZONE_EXTRA_PARAMS, extra=extra) data = self.connection.request(API_ROOT, params=params).objects[0] zone = Zone( id=data["DomainID"], domain=domain, type=type, ttl=ttl, extra=merged, driver=self, ) return zone
def test_delete_zone(self): zone = Zone(id='177184419', domain='aperture-platform.com', type='master', ttl=None, driver=self.driver) self.driver.delete_zone(zone)
def _to_zone(self, line): data = line.split('\x1f') name = data[0] if data[1] == "P": type = "master" domain_data = self._get_domain_data(name) resp_lines = re.split('\r?\n', domain_data.body) soa_block = resp_lines[:6] zone_data = resp_lines[6:] extra = {'HOSTMASTER': soa_block[0], 'REFRESH': soa_block[1], 'RETRY': soa_block[2], 'EXPIRE': soa_block[3], 'SECURE': soa_block[5]} ttl = soa_block[4] for line in range(MAX_RECORD_ENTRIES): line_data = zone_data[line].split('\x1f') extra['S%s' % (line + 1)] = line_data[0] _type = line_data[1] extra['T%s' % (line + 1)] = _type if _type != 'NONE' else '' try: extra['D%s' % (line + 1)] = line_data[2] except IndexError: extra['D%s' % (line + 1)] = '' elif data[1] == 'S': type = 'slave' extra = {} ttl = 0 return Zone(id=name, domain=name, type=type, ttl=ttl, driver=self, extra=extra)
def get_record(self, zone_id, record_id): """ Return a Record instance. :param zone_id: ID of the required zone :type zone_id: ``str`` :param record_id: ID of the required record :type record_id: ``str`` :rtype: :class:`Record` """ records = self.list_records( Zone(id=zone_id, domain=zone_id, type=None, ttl=None, driver=self, extra=None)) foundrecords = list(filter(lambda x: x.id == record_id, records)) if len(foundrecords) > 0: return (foundrecords[0]) else: return (None)
def create_zone(self, domain, type='master', ttl=None, extra=None): """ >>> driver = DummyDNSDriver('key', 'secret') >>> zone = driver.create_zone(domain='apache.org', type='master', ... ttl=100) >>> zone <Zone: domain=apache.org, ttl=100, provider=Dummy DNS Provider ...> >>> zone = driver.create_zone(domain='apache.org', type='master', ... ttl=100) ... #doctest: +IGNORE_EXCEPTION_DETAIL Traceback (most recent call last): ZoneAlreadyExistsError: @inherits: L{DNSDriver.create_zone} """ id = 'id-%s' % (domain) if id in self._zones: raise ZoneAlreadyExistsError(zone_id=id, value=None, driver=self) zone = Zone(id=id, domain=domain, type=type, ttl=ttl, extra={}, driver=self) self._zones[id] = {'zone': zone, 'records': {}} return zone
def test_export_zone_to_bind_format_slave_should_throw(self): zone = Zone(id=1, domain="example.com", type="slave", ttl=900, driver=self.driver) self.assertRaises(ValueError, zone.export_to_bind_format)
def test_create_record_zone_does_not_exist(self): DurableDNSMockHttp.type = "ZONE_DOES_NOT_EXIST" z_extra = { "ns": "ns1.durabledns.com.", "mbox": "mail.myzone.com", "refresh": "13000", "retry": 7200, "expire": 1300, "minimum": 13, "xfer": "127.0.0.1", "update_acl": "127.0.0.1", } zone = Zone( id="deletedzone.com.", domain="deletedzone.com.", type="master", ttl=1300, driver=self.driver, extra=z_extra, ) try: self.driver.create_record(name="record1", zone=zone, type=RecordType.A, data="1.2.3.4") except ZoneDoesNotExistError: pass else: self.fail("Exception was not thrown")
def test_list_zones(self): extra = { 'ns': 'ns1.durabledns.com.', 'mbox': 'mail.myzone.com', 'serial': '1437473456', 'refresh': '13000', 'retry': 7200, 'expire': 1300, 'minimum': 13, 'xfer': '127.0.0.1', 'update_acl': '127.0.0.1' } zone = Zone(id='myzone.com.', domain='myzone.com.', type='master', ttl=1300, driver=self.driver, extra=extra) self.driver.get_zone = MagicMock(return_value=zone) zones = self.driver.list_zones() self.assertEqual(len(zones), 2) zone = zones[0] self.assertEqual(zone.id, 'myzone.com.') self.assertEqual(zone.domain, 'myzone.com.') self.assertEqual(zone.ttl, 1300) self.assertEqual(zone.extra['ns'], 'ns1.durabledns.com.') self.assertEqual(zone.extra['mbox'], 'mail.myzone.com') self.assertEqual(zone.extra['serial'], '1437473456') self.assertEqual(zone.extra['refresh'], '13000') self.assertEqual(zone.extra['retry'], 7200) self.assertEqual(zone.extra['expire'], 1300) self.assertEqual(zone.extra['minimum'], 13) self.assertEqual(zone.extra['xfer'], '127.0.0.1') self.assertEqual(zone.extra['update_acl'], '127.0.0.1') self.assertEqual(len(zone.extra.keys()), 9)
def test_list_records(self): z_extra = { 'ns': 'ns1.durabledns.com.', 'mbox': 'mail.myzone.com', 'refresh': '13000', 'retry': 7200, 'expire': 1300, 'minimum': 13, 'xfer': '127.0.0.1', 'update_acl': '127.0.0.1' } zone = Zone(id='myzone.com.', domain='myzone.com.', type='master', ttl=1300, driver=self.driver, extra=z_extra) extra = {'aux': 1, 'ttl': 3600} record = Record(id='353286987', type='A', zone=zone, name='record1', data='192.168.0.1', driver=self, extra=extra) self.driver.get_record = MagicMock(return_value=record) records = self.driver.list_records(zone=zone) self.assertEqual(len(records), 2) self.assertEqual(record.id, '353286987') self.assertEqual(record.name, 'record1') self.assertEqual(record.type, 'A') self.assertEqual(record.data, '192.168.0.1') self.assertEqual(record.zone, zone) self.assertEqual(record.extra['aux'], 1) self.assertEqual(record.extra['ttl'], 3600)
def test_delete_record(self): z_extra = { 'ns': 'ns1.durabledns.com.', 'mbox': 'mail.myzone.com', 'refresh': '13000', 'retry': 7200, 'expire': 1300, 'minimum': 13, 'xfer': '127.0.0.1', 'update_acl': '127.0.0.1' } zone = Zone(id='myzone.com.', domain='myzone.com.', type='master', ttl=1300, driver=self.driver, extra=z_extra) extra = {'aux': 1, 'ttl': 3600} record = Record(id='353286987', type='A', zone=zone, name='record1', data='192.168.0.1', driver=self, extra=extra) status = self.driver.delete_record(record=record) self.assertTrue(status)
def test_delete_record_record_does_not_exist(self): z_extra = { 'ns': 'ns1.durabledns.com.', 'mbox': 'mail.myzone.com', 'refresh': '13000', 'retry': 7200, 'expire': 1300, 'minimum': 13, 'xfer': '127.0.0.1', 'update_acl': '127.0.0.1' } zone = Zone(id='myzone.com.', domain='myzone.com.', type='master', ttl=1300, driver=self.driver, extra=z_extra) extra = {'aux': 1, 'ttl': 3600} record = Record(id='353286987', type='A', zone=zone, name='record1', data='192.168.0.1', driver=self, extra=extra) DurableDNSMockHttp.type = 'RECORD_DOES_NOT_EXIST' try: self.driver.delete_record(record=record) except RecordDoesNotExistError: pass else: self.fail('Exception was not thrown')
def test_create_record_zone_does_not_exist(self): DurableDNSMockHttp.type = 'ZONE_DOES_NOT_EXIST' z_extra = { 'ns': 'ns1.durabledns.com.', 'mbox': 'mail.myzone.com', 'refresh': '13000', 'retry': 7200, 'expire': 1300, 'minimum': 13, 'xfer': '127.0.0.1', 'update_acl': '127.0.0.1' } zone = Zone(id='deletedzone.com.', domain='deletedzone.com.', type='master', ttl=1300, driver=self.driver, extra=z_extra) try: self.driver.create_record(name='record1', zone=zone, type=RecordType.A, data='1.2.3.4') except ZoneDoesNotExistError: pass else: self.fail('Exception was not thrown')
def test_update_zone_zone_does_not_exist(self): DurableDNSMockHttp.type = 'ZONE_DOES_NOT_EXIST' z_extra = { 'ns': 'ns1.durabledns.com.', 'mbox': 'mail.myzone.com', 'refresh': '13000', 'retry': 7200, 'expire': 1300, 'minimum': 13, 'xfer': '127.0.0.1', 'serial': '1437473456', 'update_acl': '127.0.0.1' } zone = Zone(id='deletedzone.com.', domain='deletedzone.com.', type='master', ttl=1300, driver=self.driver, extra=z_extra) try: self.driver.update_zone(zone, zone.domain) except ZoneDoesNotExistError: pass else: self.fail('Exception was not thrown')
def _to_zone(self, elem): id = findtext(element=elem, xpath='id') domain = findtext(element=elem, xpath='domain') type = findtext(element=elem, xpath='ns-type') type = 'master' if type.find('pri') == 0 else 'slave' ttl = findtext(element=elem, xpath='default-ttl') hostmaster = findtext(element=elem, xpath='hostmaster') custom_ns = findtext(element=elem, xpath='custom-ns') custom_nameservers = findtext(element=elem, xpath='custom-nameservers') notes = findtext(element=elem, xpath='notes') nx_ttl = findtext(element=elem, xpath='nx-ttl') slave_nameservers = findtext(element=elem, xpath='slave-nameservers') tags = findtext(element=elem, xpath='tag-list') tags = tags.split(' ') if tags else [] extra = { 'hostmaster': hostmaster, 'custom-ns': custom_ns, 'custom-nameservers': custom_nameservers, 'notes': notes, 'nx-ttl': nx_ttl, 'slave-nameservers': slave_nameservers, 'tags': tags } zone = Zone(id=str(id), domain=domain, type=type, ttl=int(ttl), driver=self, extra=extra) return zone
def test_list_records_zone_does_not_exist(self): z_extra = { 'ns': 'ns1.durabledns.com.', 'mbox': 'mail.myzone.com', 'refresh': '13000', 'retry': 7200, 'expire': 1300, 'minimum': 13, 'xfer': '127.0.0.1', 'update_acl': '127.0.0.1' } zone = Zone(id='myzone.com.', domain='myzone.com.', type='master', ttl=1300, driver=self.driver, extra=z_extra) DurableDNSMockHttp.type = 'ZONE_DOES_NOT_EXIST' try: self.driver.list_records(zone=zone) except ZoneDoesNotExistError: e = sys.exc_info()[1] self.assertEqual(e.zone_id, zone.id) else: self.fail('Exception was not thrown')
def test_res_to_record(self): res = { 'id': 2, 'name': 'www', 'type': 'AAAA', 'content': '2001:db8:100', 'created': 1234, 'modified': 2345, 'disabled': False, 'ttl': 1800, 'prio': 10 } zone = Zone(id=1, domain='example.com', type=None, ttl=60, driver=self.driver) record = self.driver._AuroraDNSDriver__res_to_record(zone, res) self.assertEqual(res['name'], record.name) self.assertEqual(res['ttl'], record.extra['ttl']) self.assertEqual(res['prio'], record.extra['priority']) self.assertEqual(res['type'], record.type) self.assertEqual(res['content'], record.data) self.assertEqual(zone, record.zone) self.assertEqual(self.driver, record.driver)
def _to_zone(self, data): domain = data.get('domain') id = domain.get('id') name = domain.get('name') extra = { 'registrant_id': domain.get('registrant_id'), 'user_id': domain.get('user_id'), 'unicode_name': domain.get('unicode_name'), 'token': domain.get('token'), 'state': domain.get('state'), 'language': domain.get('language'), 'lockable': domain.get('lockable'), 'auto_renew': domain.get('auto_renew'), 'whois_protected': domain.get('whois_protected'), 'record_count': domain.get('record_count'), 'service_count': domain.get('service_count'), 'expires_on': domain.get('expires_on'), 'created_at': domain.get('created_at'), 'updated_at': domain.get('updated_at') } # All zones are primary by design type = 'master' return Zone(id=id, domain=name, type=type, ttl=DEFAULT_ZONE_TTL, driver=self, extra=extra)
def create_zone(self, domain, type='master', ttl=None, extra=None): """ Create a new zone. API docs: http://www.linode.com/api/dns/domain.create """ params = { 'api_action': 'domain.create', 'Type': type, 'Domain': domain } if ttl: params['TTL_sec'] = ttl merged = merge_valid_keys(params=params, valid_keys=VALID_ZONE_EXTRA_PARAMS, extra=extra) data = self.connection.request(API_ROOT, params=params).objects[0] zone = Zone(id=data['DomainID'], domain=domain, type=type, ttl=ttl, extra=merged, driver=self) return zone
def test_delete_record(self): z_extra = { "ns": "ns1.durabledns.com.", "mbox": "mail.myzone.com", "refresh": "13000", "retry": 7200, "expire": 1300, "minimum": 13, "xfer": "127.0.0.1", "update_acl": "127.0.0.1", } zone = Zone( id="myzone.com.", domain="myzone.com.", type="master", ttl=1300, driver=self.driver, extra=z_extra, ) extra = {"aux": 1, "ttl": 3600} record = Record( id="353286987", type="A", zone=zone, name="record1", data="192.168.0.1", driver=self, extra=extra, ) status = self.driver.delete_record(record=record) self.assertTrue(status)
def test_delete_zone_zone_does_not_exist(self): z_extra = { "ns": "ns1.durabledns.com.", "mbox": "mail.myzone.com", "refresh": "13000", "retry": 7200, "expire": 1300, "minimum": 13, "xfer": "127.0.0.1", "update_acl": "127.0.0.1", } zone = Zone( id="myzone.com.", domain="myzone.com.", type="master", ttl=1300, driver=self.driver, extra=z_extra, ) DurableDNSMockHttp.type = "ZONE_DOES_NOT_EXIST" try: self.driver.delete_zone(zone=zone) except ZoneDoesNotExistError: pass else: self.fail("Exception was not thrown")
def setUp(self): NFSNDNSDriver.connectionCls.conn_class = NFSNMockHttp NFSNMockHttp.type = None self.driver = NFSNDNSDriver('testid', 'testsecret') self.test_zone = Zone(id='example.com', domain='example.com', driver=self.driver, type='master', ttl=None, extra={}) self.test_record = Record(id=None, name='', data='192.0.2.1', type=RecordType.A, zone=self.test_zone, driver=self.driver, extra={})
class NFSNTestCase(LibcloudTestCase): def setUp(self): NFSNDNSDriver.connectionCls.conn_class = NFSNMockHttp NFSNMockHttp.type = None self.driver = NFSNDNSDriver('testid', 'testsecret') self.test_zone = Zone(id='example.com', domain='example.com', driver=self.driver, type='master', ttl=None, extra={}) self.test_record = Record(id=None, name='', data='192.0.2.1', type=RecordType.A, zone=self.test_zone, driver=self.driver, extra={}) def test_list_zones(self): with self.assertRaises(NotImplementedError): self.driver.list_zones() def test_create_zone(self): with self.assertRaises(NotImplementedError): self.driver.create_zone('example.com') def test_get_zone(self): zone = self.driver.get_zone('example.com') self.assertEquals(zone.id, None) self.assertEquals(zone.domain, 'example.com') def test_delete_zone(self): with self.assertRaises(NotImplementedError): self.driver.delete_zone(self.test_zone) def test_create_record(self): NFSNMockHttp.type = 'CREATED' record = self.test_zone.create_record(name='newrecord', type=RecordType.A, data='127.0.0.1', extra={'ttl': 900}) self.assertEquals(record.id, None) self.assertEquals(record.name, 'newrecord') self.assertEquals(record.data, '127.0.0.1') self.assertEquals(record.type, RecordType.A) self.assertEquals(record.ttl, 900) def test_get_record(self): with self.assertRaises(NotImplementedError): self.driver.get_record('example.com', '12345') def test_delete_record(self): self.assertTrue(self.test_record.delete()) def test_list_records(self): records = self.driver.list_records(self.test_zone) self.assertEqual(len(records), 2) def test_ex_get_records_by(self): NFSNMockHttp.type = 'ONE_RECORD' records = self.driver.ex_get_records_by(self.test_zone, type=RecordType.A) self.assertEqual(len(records), 1) record = records[0] self.assertEquals(record.name, '') self.assertEquals(record.data, '192.0.2.1') self.assertEquals(record.type, RecordType.A) self.assertEquals(record.ttl, 3600) def test_get_zone_not_found(self): NFSNMockHttp.type = 'NOT_FOUND' with self.assertRaises(ZoneDoesNotExistError): self.driver.get_zone('example.com') def test_delete_record_not_found(self): NFSNMockHttp.type = 'NOT_FOUND' with self.assertRaises(RecordDoesNotExistError): self.assertTrue(self.test_record.delete())