Example #1
0
class GoogleTests(LibcloudTestCase):
    GoogleBaseConnection._get_token_info_from_file = lambda x: None
    GoogleBaseConnection._write_token_info_to_file = lambda x: None
    GoogleInstalledAppAuthConnection.get_code = lambda x: '1234'

    def setUp(self):
        GoogleDNSMockHttp.test = self
        GoogleDNSDriver.connectionCls.conn_classes = (GoogleDNSMockHttp,
                                                      GoogleDNSMockHttp)
        GoogleBaseAuthConnection.conn_classes = (GoogleAuthMockHttp,
                                                 GoogleAuthMockHttp)
        GoogleDNSMockHttp.type = None
        kwargs = DNS_KEYWORD_PARAMS_GOOGLE.copy()
        kwargs['auth_type'] = 'IA'
        self.driver = GoogleDNSDriver(*DNS_PARAMS_GOOGLE, **kwargs)

    def test_default_scopes(self):
        self.assertEqual(self.driver.scopes, None)

    def test_list_zones(self):
        zones = self.driver.list_zones()
        self.assertEqual(len(zones), 2)

    def test_list_records(self):
        zone = self.driver.list_zones()[0]
        records = self.driver.list_records(zone=zone)
        self.assertEqual(len(records), 3)

    def test_get_zone(self):
        zone = self.driver.get_zone('example-com')
        self.assertEqual(zone.id, 'example-com')
        self.assertEqual(zone.domain, 'example.com.')

    def test_get_zone_does_not_exist(self):
        GoogleDNSMockHttp.type = 'ZONE_DOES_NOT_EXIST'

        try:
            self.driver.get_zone('example-com')
        except ZoneDoesNotExistError:
            e = sys.exc_info()[1]
            self.assertEqual(e.zone_id, 'example-com')
        else:
            self.fail('Exception not thrown')

    def test_get_record(self):
        GoogleDNSMockHttp.type = 'FILTER_ZONES'
        zone = self.driver.list_zones()[0]
        record = self.driver.get_record(zone.id, "A:foo.example.com.")
        self.assertEqual(record.id, 'A:foo.example.com.')
        self.assertEqual(record.name, 'foo.example.com.')
        self.assertEqual(record.type, 'A')
        self.assertEqual(record.zone.id, 'example-com')

    def test_get_record_zone_does_not_exist(self):
        GoogleDNSMockHttp.type = 'ZONE_DOES_NOT_EXIST'

        try:
            self.driver.get_record('example-com', 'a:a')
        except ZoneDoesNotExistError:
            e = sys.exc_info()[1]
            self.assertEqual(e.zone_id, 'example-com')
        else:
            self.fail('Exception not thrown')

    def test_get_record_record_does_not_exist(self):
        GoogleDNSMockHttp.type = 'RECORD_DOES_NOT_EXIST'
        try:
            self.driver.get_record('example-com', "A:foo")
        except RecordDoesNotExistError:
            e = sys.exc_info()[1]
            self.assertEqual(e.record_id, 'A:foo')
        else:
            self.fail('Exception not thrown')

    def test_create_zone(self):
        extra = {'description': 'new domain for example.org'}
        zone = self.driver.create_zone('example.org.', extra)
        self.assertEqual(zone.domain, 'example.org.')
        self.assertEqual(zone.extra['description'], extra['description'])
        self.assertEqual(len(zone.extra['nameServers']), 4)

    def test_delete_zone(self):
        zone = self.driver.get_zone('example-com')
        res = self.driver.delete_zone(zone)
        self.assertTrue(res)
Example #2
0
class GoogleTests(LibcloudTestCase):
    GoogleBaseConnection._get_token_info_from_file = lambda x: None
    GoogleBaseConnection._write_token_info_to_file = lambda x: None
    GoogleInstalledAppAuthConnection.get_code = lambda x: "1234"

    def setUp(self):
        GoogleDNSMockHttp.test = self
        GoogleDNSDriver.connectionCls.conn_classes = (GoogleDNSMockHttp, GoogleDNSMockHttp)
        GoogleBaseAuthConnection.conn_classes = (GoogleAuthMockHttp, GoogleAuthMockHttp)
        GoogleDNSMockHttp.type = None
        kwargs = DNS_KEYWORD_PARAMS_GOOGLE.copy()
        kwargs["auth_type"] = "IA"
        self.driver = GoogleDNSDriver(*DNS_PARAMS_GOOGLE, **kwargs)

    def test_default_scopes(self):
        self.assertEqual(self.driver.scopes, None)

    def test_list_zones(self):
        zones = self.driver.list_zones()
        self.assertEqual(len(zones), 2)

    def test_list_records(self):
        zone = self.driver.list_zones()[0]
        records = self.driver.list_records(zone=zone)
        self.assertEqual(len(records), 3)

    def test_get_zone(self):
        zone = self.driver.get_zone("example-com")
        self.assertEqual(zone.id, "example-com")
        self.assertEqual(zone.domain, "example.com.")

    def test_get_zone_does_not_exist(self):
        GoogleDNSMockHttp.type = "ZONE_DOES_NOT_EXIST"

        try:
            self.driver.get_zone("example-com")
        except ZoneDoesNotExistError:
            e = sys.exc_info()[1]
            self.assertEqual(e.zone_id, "example-com")
        else:
            self.fail("Exception not thrown")

    def test_get_record(self):
        GoogleDNSMockHttp.type = "FILTER_ZONES"
        zone = self.driver.list_zones()[0]
        record = self.driver.get_record(zone.id, "A:foo.example.com.")
        self.assertEqual(record.id, "A:foo.example.com.")
        self.assertEqual(record.name, "foo.example.com.")
        self.assertEqual(record.type, "A")
        self.assertEqual(record.zone.id, "example-com")

    def test_get_record_zone_does_not_exist(self):
        GoogleDNSMockHttp.type = "ZONE_DOES_NOT_EXIST"

        try:
            self.driver.get_record("example-com", "a:a")
        except ZoneDoesNotExistError:
            e = sys.exc_info()[1]
            self.assertEqual(e.zone_id, "example-com")
        else:
            self.fail("Exception not thrown")

    def test_get_record_record_does_not_exist(self):
        GoogleDNSMockHttp.type = "RECORD_DOES_NOT_EXIST"
        try:
            self.driver.get_record("example-com", "A:foo")
        except RecordDoesNotExistError:
            e = sys.exc_info()[1]
            self.assertEqual(e.record_id, "A:foo")
        else:
            self.fail("Exception not thrown")

    def test_create_zone(self):
        extra = {"description": "new domain for example.org"}
        zone = self.driver.create_zone("example.org.", extra)
        self.assertEqual(zone.domain, "example.org.")
        self.assertEqual(zone.extra["description"], extra["description"])
        self.assertEqual(len(zone.extra["nameServers"]), 4)

    def test_delete_zone(self):
        zone = self.driver.get_zone("example-com")
        res = self.driver.delete_zone(zone)
        self.assertTrue(res)
Example #3
0
class GoogleTests(GoogleTestCase):
    def setUp(self):
        GoogleDNSMockHttp.test = self
        GoogleDNSDriver.connectionCls.conn_class = GoogleDNSMockHttp
        GoogleBaseAuthConnection.conn_class = GoogleAuthMockHttp
        GoogleDNSMockHttp.type = None
        kwargs = DNS_KEYWORD_PARAMS_GOOGLE.copy()
        kwargs["auth_type"] = "IA"
        self.driver = GoogleDNSDriver(*DNS_PARAMS_GOOGLE, **kwargs)

    def test_default_scopes(self):
        self.assertIsNone(self.driver.scopes)

    def test_list_zones(self):
        zones = self.driver.list_zones()
        self.assertEqual(len(zones), 2)

    def test_list_records(self):
        zone = self.driver.list_zones()[0]
        records = self.driver.list_records(zone=zone)
        self.assertEqual(len(records), 3)

    def test_get_zone(self):
        zone = self.driver.get_zone("example-com")
        self.assertEqual(zone.id, "example-com")
        self.assertEqual(zone.domain, "example.com.")

    def test_get_zone_does_not_exist(self):
        GoogleDNSMockHttp.type = "ZONE_DOES_NOT_EXIST"

        try:
            self.driver.get_zone("example-com")
        except ZoneDoesNotExistError as e:
            self.assertEqual(e.zone_id, "example-com")
        else:
            self.fail("Exception not thrown")

    def test_get_record(self):
        GoogleDNSMockHttp.type = "FILTER_ZONES"
        zone = self.driver.list_zones()[0]
        record = self.driver.get_record(zone.id, "A:foo.example.com.")
        self.assertEqual(record.id, "A:foo.example.com.")
        self.assertEqual(record.name, "foo.example.com.")
        self.assertEqual(record.type, "A")
        self.assertEqual(record.zone.id, "example-com")

    def test_get_record_zone_does_not_exist(self):
        GoogleDNSMockHttp.type = "ZONE_DOES_NOT_EXIST"

        try:
            self.driver.get_record("example-com", "a:a")
        except ZoneDoesNotExistError as e:
            self.assertEqual(e.zone_id, "example-com")
        else:
            self.fail("Exception not thrown")

    def test_get_record_record_does_not_exist(self):
        GoogleDNSMockHttp.type = "RECORD_DOES_NOT_EXIST"
        try:
            self.driver.get_record("example-com", "A:foo")
        except RecordDoesNotExistError as e:
            self.assertEqual(e.record_id, "A:foo")
        else:
            self.fail("Exception not thrown")

    def test_create_zone(self):
        extra = {"description": "new domain for example.org"}
        zone = self.driver.create_zone("example.org.", extra)
        self.assertEqual(zone.domain, "example.org.")
        self.assertEqual(zone.extra["description"], extra["description"])
        self.assertEqual(len(zone.extra["nameServers"]), 4)

    def test_delete_zone(self):
        zone = self.driver.get_zone("example-com")
        res = self.driver.delete_zone(zone)
        self.assertTrue(res)

    def test_ex_bulk_record_changes(self):
        zone = self.driver.get_zone("example-com")
        records = self.driver.ex_bulk_record_changes(zone, {})

        self.assertEqual(records["additions"][0].name, "foo.example.com.")
        self.assertEqual(records["additions"][0].type, "A")

        self.assertEqual(records["deletions"][0].name, "bar.example.com.")
        self.assertEqual(records["deletions"][0].type, "A")
Example #4
0
class GoogleTests(GoogleTestCase):

    def setUp(self):
        GoogleDNSMockHttp.test = self
        GoogleDNSDriver.connectionCls.conn_class = GoogleDNSMockHttp
        GoogleBaseAuthConnection.conn_class = GoogleAuthMockHttp
        GoogleDNSMockHttp.type = None
        kwargs = DNS_KEYWORD_PARAMS_GOOGLE.copy()
        kwargs['auth_type'] = 'IA'
        self.driver = GoogleDNSDriver(*DNS_PARAMS_GOOGLE, **kwargs)

    def test_default_scopes(self):
        self.assertEqual(self.driver.scopes, None)

    def test_list_zones(self):
        zones = self.driver.list_zones()
        self.assertEqual(len(zones), 2)

    def test_list_records(self):
        zone = self.driver.list_zones()[0]
        records = self.driver.list_records(zone=zone)
        self.assertEqual(len(records), 3)

    def test_get_zone(self):
        zone = self.driver.get_zone('example-com')
        self.assertEqual(zone.id, 'example-com')
        self.assertEqual(zone.domain, 'example.com.')

    def test_get_zone_does_not_exist(self):
        GoogleDNSMockHttp.type = 'ZONE_DOES_NOT_EXIST'

        try:
            self.driver.get_zone('example-com')
        except ZoneDoesNotExistError:
            e = sys.exc_info()[1]
            self.assertEqual(e.zone_id, 'example-com')
        else:
            self.fail('Exception not thrown')

    def test_get_record(self):
        GoogleDNSMockHttp.type = 'FILTER_ZONES'
        zone = self.driver.list_zones()[0]
        record = self.driver.get_record(zone.id, "A:foo.example.com.")
        self.assertEqual(record.id, 'A:foo.example.com.')
        self.assertEqual(record.name, 'foo.example.com.')
        self.assertEqual(record.type, 'A')
        self.assertEqual(record.zone.id, 'example-com')

    def test_get_record_zone_does_not_exist(self):
        GoogleDNSMockHttp.type = 'ZONE_DOES_NOT_EXIST'

        try:
            self.driver.get_record('example-com', 'a:a')
        except ZoneDoesNotExistError:
            e = sys.exc_info()[1]
            self.assertEqual(e.zone_id, 'example-com')
        else:
            self.fail('Exception not thrown')

    def test_get_record_record_does_not_exist(self):
        GoogleDNSMockHttp.type = 'RECORD_DOES_NOT_EXIST'
        try:
            self.driver.get_record('example-com', "A:foo")
        except RecordDoesNotExistError:
            e = sys.exc_info()[1]
            self.assertEqual(e.record_id, 'A:foo')
        else:
            self.fail('Exception not thrown')

    def test_create_zone(self):
        extra = {'description': 'new domain for example.org'}
        zone = self.driver.create_zone('example.org.', extra)
        self.assertEqual(zone.domain, 'example.org.')
        self.assertEqual(zone.extra['description'], extra['description'])
        self.assertEqual(len(zone.extra['nameServers']), 4)

    def test_delete_zone(self):
        zone = self.driver.get_zone('example-com')
        res = self.driver.delete_zone(zone)
        self.assertTrue(res)

    def test_ex_bulk_record_changes(self):
        zone = self.driver.get_zone('example-com')
        records = self.driver.ex_bulk_record_changes(zone, {})

        self.assertEqual(records['additions'][0].name, 'foo.example.com.')
        self.assertEqual(records['additions'][0].type, 'A')

        self.assertEqual(records['deletions'][0].name, 'bar.example.com.')
        self.assertEqual(records['deletions'][0].type, 'A')
Example #5
0
class GoogleTests(GoogleTestCase):
    def setUp(self):
        GoogleDNSMockHttp.test = self
        GoogleDNSDriver.connectionCls.conn_class = GoogleDNSMockHttp
        GoogleBaseAuthConnection.conn_class = GoogleAuthMockHttp
        GoogleDNSMockHttp.type = None
        kwargs = DNS_KEYWORD_PARAMS_GOOGLE.copy()
        kwargs['auth_type'] = 'IA'
        self.driver = GoogleDNSDriver(*DNS_PARAMS_GOOGLE, **kwargs)

    def test_default_scopes(self):
        self.assertIsNone(self.driver.scopes)

    def test_list_zones(self):
        zones = self.driver.list_zones()
        self.assertEqual(len(zones), 2)

    def test_list_records(self):
        zone = self.driver.list_zones()[0]
        records = self.driver.list_records(zone=zone)
        self.assertEqual(len(records), 3)

    def test_get_zone(self):
        zone = self.driver.get_zone('example-com')
        self.assertEqual(zone.id, 'example-com')
        self.assertEqual(zone.domain, 'example.com.')

    def test_get_zone_does_not_exist(self):
        GoogleDNSMockHttp.type = 'ZONE_DOES_NOT_EXIST'

        try:
            self.driver.get_zone('example-com')
        except ZoneDoesNotExistError as e:
            self.assertEqual(e.zone_id, 'example-com')
        else:
            self.fail('Exception not thrown')

    def test_get_record(self):
        GoogleDNSMockHttp.type = 'FILTER_ZONES'
        zone = self.driver.list_zones()[0]
        record = self.driver.get_record(zone.id, "A:foo.example.com.")
        self.assertEqual(record.id, 'A:foo.example.com.')
        self.assertEqual(record.name, 'foo.example.com.')
        self.assertEqual(record.type, 'A')
        self.assertEqual(record.zone.id, 'example-com')

    def test_get_record_zone_does_not_exist(self):
        GoogleDNSMockHttp.type = 'ZONE_DOES_NOT_EXIST'

        try:
            self.driver.get_record('example-com', 'a:a')
        except ZoneDoesNotExistError as e:
            self.assertEqual(e.zone_id, 'example-com')
        else:
            self.fail('Exception not thrown')

    def test_get_record_record_does_not_exist(self):
        GoogleDNSMockHttp.type = 'RECORD_DOES_NOT_EXIST'
        try:
            self.driver.get_record('example-com', "A:foo")
        except RecordDoesNotExistError as e:
            self.assertEqual(e.record_id, 'A:foo')
        else:
            self.fail('Exception not thrown')

    def test_create_zone(self):
        extra = {'description': 'new domain for example.org'}
        zone = self.driver.create_zone('example.org.', extra)
        self.assertEqual(zone.domain, 'example.org.')
        self.assertEqual(zone.extra['description'], extra['description'])
        self.assertEqual(len(zone.extra['nameServers']), 4)

    def test_delete_zone(self):
        zone = self.driver.get_zone('example-com')
        res = self.driver.delete_zone(zone)
        self.assertTrue(res)

    def test_ex_bulk_record_changes(self):
        zone = self.driver.get_zone('example-com')
        records = self.driver.ex_bulk_record_changes(zone, {})

        self.assertEqual(records['additions'][0].name, 'foo.example.com.')
        self.assertEqual(records['additions'][0].type, 'A')

        self.assertEqual(records['deletions'][0].name, 'bar.example.com.')
        self.assertEqual(records['deletions'][0].type, 'A')
class HostGCP():
    def __init__(self, config, gcp_credentials_file):
        self.config = config
        creds = read_json(gcp_credentials_file)
        self._compute = GCENodeDriver(
            creds['client_email'],
            gcp_credentials_file,
            project=creds['project_id'],
            datacenter=self.config['gcp_compute_zone'],
            timeout=self.config['compute_timeout'])
        self._dns = GoogleDNSDriver(creds['client_email'],
                                    gcp_credentials_file,
                                    project=creds['project_id'])
        self._dns_zone = self._dns.get_zone(config['gcp_dns_zone'])
        self.log = logging.getLogger(__name__ + '.' + self._name)

    @property
    def _name(self):
        return self.config['resources_name']

    async def start(self):
        self.log.info('start() begins')
        try:
            await self._start()
        except:
            self.log.exception('start() failed')
            raise
        self.log.info('start() finished')

    async def _start(self):
        try:
            self.log.info('Creating external static IP address')
            addr = await run_async(self._compute.ex_get_address, self._name)
            self.log.info('Exists')
        except ResourceNotFoundError:
            addr = await run_async(self._compute.ex_create_address, self._name)
            self.log.info('Created')

        self.log.info('External static IP address: %s', addr.address)

        try:
            self.log.info('Creating DNS record: %s', self.config['hostname'])
            await run_async(self._dns.create_record, self.config['hostname'],
                            self._dns_zone, 'A', {
                                'ttl': self.config['hostname_ttl'],
                                'rrdatas': [addr.address]
                            })
            self.log.info('Created')
        except ResourceExistsError:
            self.log.info('Exists')

        try:
            self.log.info('Creating data disk')
            data_disk_name = self._name + '-data'
            data_disk = await run_async(self._compute.ex_get_volume,
                                        data_disk_name)
            self.log.info('Exists')
        except ResourceNotFoundError:
            data_disk = await run_async(
                self._compute.create_volume,
                self.config['data_disk_size'],
                data_disk_name,
                ex_disk_type=('pd-ssd' if self.config['data_disk_ssd'] else
                              'pd-standard'))
            self.log.info('Created')

        self.log.info('Data disk: %sGB %s', data_disk.size,
                      data_disk.extra['type'])

        try:
            self.log.info('Creating host')
            host = await run_async(self._compute.ex_get_node, self._name)
            self.log.info('Exists')
        except ResourceNotFoundError:
            host = await run_async(
                self._compute.create_node,
                self._name,
                location=self.config['gcp_compute_zone'],
                size=self.config['machine_type'],
                image=self.config['boot_image'],
                external_ip=addr,
                ex_network=self.config['gcp_compute_net'],
                ex_subnetwork=self.config['gcp_compute_subnet'],
            )
            self.log.info('Created')

        self.log.info('Host: %s %s', host.extra['image'], host.size)

        try:
            self.log.info('Attaching data disk')
            await run_async(self._compute.attach_volume,
                            host,
                            data_disk,
                            ex_auto_delete=False)
            self.log.info('Attached')
        except ResourceInUseError:
            self.log.info('Already attached')

        self.log.info('Setting host tags')
        await run_async(self._compute.ex_set_node_tags, host,
                        self.config['gcp_compute_tags'])

        self.log.info('Setting host metadata')
        await run_async(self._compute.ex_set_node_metadata, host,
                        self.config['host_metadata'])

        self.log.info('Starting host')
        await run_async(self._compute.ex_start_node, host)
        self.log.info('Started')

    async def stop(self, clean=HostClean.STOP) -> None:
        self.log.info('stop(clean=%s) begins', clean.name)
        try:
            await self._stop(clean)
        except:
            self.log.exception('stop() failed')
            raise
        self.log.info('stop() finished')

    async def _stop(self, clean) -> None:
        try:
            self.log.info('Stopping host')
            host = await run_async(self._compute.ex_get_node, self._name,
                                   self.config['gcp_compute_zone'])
            await run_async(self._compute.ex_stop_node, host)
            self.log.info('Stopped')

            if clean <= HostClean.STOP:
                return

            self.log.info('Removing host')
            await run_async(self._compute.destroy_node,
                            host,
                            destroy_boot_disk=True)
            self.log.info('Removed')

        except ResourceNotFoundError:
            self.log.info('Not present')

        if clean <= HostClean.HOST:
            return

        try:
            self.log.info('Removing data disk')
            data_disk_name = self._name + '-data'
            data_disk = await run_async(self._compute.ex_get_volume,
                                        data_disk_name)
            await run_async(self._compute.destroy_volume, data_disk)
            self.log.info('Removed')
        except ResourceNotFoundError:
            self.log.info('Not present')

        if clean <= HostClean.DATA:
            return

        try:
            self.log.info('Removing DNS record')
            record = await run_async(self._dns.get_record, self._dns_zone.id,
                                     'A:' + self.config['hostname'])
            await run_async(self._dns.delete_record, record)
            self.log.info('Removed')
        except RecordDoesNotExistError:
            self.log.info('Not present')

        try:
            self.log.info('Removing external static IP address')
            await run_async(self._compute.ex_destroy_address, self._name)
            self.log.info('Removed')
        except ResourceNotFoundError:
            self.log.info('Not present')