def test_tag_instance(self):
        test_tag = 'agent-123'
        name = 'name'
        self.config['config']['network'] = 'default'
        instance = Instance(self.config['config'],
                            self.ctx.logger,
                            name=name,
                            image=self.config['instance']['image'],
                            tags=[test_tag],
                            machine_type=self.config['instance_type'])

        instance.create()
        instances = instance.list()
        item = utils.get_item_from_gcp_response('name', name, instances)
        tag = find_in_list(test_tag, item['tags'].get('items'))
        self.assertIsNotNone(tag)

        instance.delete()

        instance.tags = []
        instance.create()
        instances = instance.list()
        item = utils.get_item_from_gcp_response('name', name, instances)
        tags = item.get('tags', [])
        tag = find_in_list(test_tag, tags.get('items', []))
        self.assertIsNone(tag)

        instance.set_tags([test_tag])
        instances = instance.list()
        item = utils.get_item_from_gcp_response('name', name, instances)
        tag = find_in_list(test_tag, item['tags'].get('items'))
        self.assertIsNotNone(tag)

        instance.delete()
    def test_tag_instance(self):
        test_tag = 'agent-123'
        name = 'name'
        self.config['config']['network'] = 'default'
        instance = Instance(self.config['config'],
                            self.ctx.logger,
                            name=name,
                            image=self.config['instance']['image'],
                            tags=[test_tag],
                            machine_type=self.config['instance_type'])

        instance.create()
        instances = instance.list()
        item = utils.get_item_from_gcp_response('name', name, instances)
        tag = find_in_list(test_tag, item['tags'].get('items'))
        self.assertIsNotNone(tag)

        instance.delete()

        instance.tags = []
        instance.create()
        instances = instance.list()
        item = utils.get_item_from_gcp_response('name', name, instances)
        tags = item.get('tags', [])
        tag = find_in_list(test_tag, tags.get('items', []))
        self.assertIsNone(tag)

        instance.set_tags([test_tag])
        instances = instance.list()
        item = utils.get_item_from_gcp_response('name', name, instances)
        tag = find_in_list(test_tag, item['tags'].get('items'))
        self.assertIsNotNone(tag)

        instance.delete()
    def test_get_item_from_gcp_response(self):
        item = {'name': 'test'}
        sth_that_has_items = {'items': [item]}
        found_item = utils.get_item_from_gcp_response('name', 'test',
                                                      sth_that_has_items)
        self.assertEqual(found_item, item)

        found_item = utils.get_item_from_gcp_response('name', 'test2',
                                                      sth_that_has_items)
        self.assertIsNone(found_item)
    def test_get_item_from_gcp_response(self):
        item = {'name': 'test'}
        sth_that_has_items = {'items': [item]}
        found_item = utils.get_item_from_gcp_response(
            'name',
            'test',
            sth_that_has_items)
        self.assertEqual(found_item, item)

        found_item = utils.get_item_from_gcp_response(
            'name',
            'test2',
            sth_that_has_items)
        self.assertIsNone(found_item)
    def test_tag_firewall(self):
        network = Network(self.config['config'],
                          self.ctx.logger,
                          self.config['network'])
        network.create()

        firewall_rule = dict(self.config['firewall'])
        source_tag = 'source-tag'
        target_tag = 'target-tag'
        firewall_rule['sourceTags'] = [source_tag]
        firewall_rule['targetTags'] = [target_tag]
        firewall = FirewallRule(self.config['config'],
                                self.ctx.logger,
                                firewall_rule,
                                self.config['config']['network'])
        firewall.create()
        firewall_name = firewall.name
        firewalls = firewall.list()
        item = utils.get_item_from_gcp_response('name',
                                                firewall_name,
                                                firewalls)
        self.assertEqual(source_tag, find_in_list(source_tag,
                                                  item['sourceTags']))
        self.assertEqual(target_tag, find_in_list(target_tag,
                                                  item['targetTags']))

        firewall.firewall = self.config['firewall']
        firewall.firewall['name'] = firewall_name
        firewall.update()
        firewalls = firewall.list()
        item = utils.get_item_from_gcp_response('name',
                                                firewall_rule['name'],
                                                firewalls)
        self.assertIsNone(find_in_list(source_tag, item.get('sourceTags', [])))
        self.assertIsNone(find_in_list(target_tag, item.get('targetTags', [])))

        firewall.firewall = firewall_rule
        firewall.firewall['name'] = firewall_name
        firewall.update()
        firewalls = firewall.list()
        item = utils.get_item_from_gcp_response('name',
                                                firewall_rule['name'],
                                                firewalls)
        self.assertEqual(source_tag, find_in_list(source_tag,
                                                  item['sourceTags']))
        self.assertEqual(target_tag, find_in_list(target_tag,
                                                  item['targetTags']))
        firewall.delete()
        network.delete()
    def add_project_ssh_key(self):
        """
        Update project SSH private key. Add new key to project's
        common instance metadata.

        :return: REST response with operation responsible for the sshKeys
        addition to project metadata process and its status
        """
        common_instance_metadata = self.get_common_instance_metadata()
        if common_instance_metadata.get('items') is None:
            item = [{self.KEY_NAME: self.KEY_VALUE,
                    'value': utils.get_key_user_string(self.user,
                                                       self.public_key)}]
            common_instance_metadata['items'] = item
        else:
            item = utils.get_item_from_gcp_response(
                self.KEY_NAME,
                self.KEY_VALUE,
                common_instance_metadata)
            key = utils.get_key_user_string(self.user, self.public_key)
            if key not in item['value']:
                item['value'] = '{0}\n{1}'.format(item['value'], key)
        self.logger.info(
            'Add sshKey {0} to project {1} metadata'.format(
                self.public_key,
                self.project))
        return self.discovery.projects().setCommonInstanceMetadata(
            project=self.project,
            body=common_instance_metadata).execute()
示例#7
0
    def add_project_ssh_key(self):
        """
        Update project SSH private key. Add new key to project's
        common instance metadata.

        :return: REST response with operation responsible for the sshKeys
        addition to project metadata process and its status
        """
        common_instance_metadata = self.get_common_instance_metadata()
        if common_instance_metadata.get('items') is None:
            item = [{
                self.KEY_NAME:
                self.KEY_VALUE,
                'value':
                utils.get_key_user_string(self.user, self.public_key)
            }]
            common_instance_metadata['items'] = item
        else:
            item = utils.get_item_from_gcp_response(self.KEY_NAME,
                                                    self.KEY_VALUE,
                                                    common_instance_metadata)
            key = utils.get_key_user_string(self.user, self.public_key)
            if key not in item['value']:
                item['value'] = '{0}\n{1}'.format(item['value'], key)
        self.logger.info('Add sshKey {0} to project {1} metadata'.format(
            self.public_key, self.project))
        return self.discovery.projects().setCommonInstanceMetadata(
            project=self.project, body=common_instance_metadata).execute()
    def test_tag_firewall(self):
        network = Network(self.config['config'], self.ctx.logger,
                          self.config['network'])
        network.create()

        firewall_rule = dict(self.config['firewall'])
        source_tag = 'source-tag'
        target_tag = 'target-tag'
        firewall_rule['sourceTags'] = [source_tag]
        firewall_rule['targetTags'] = [target_tag]
        firewall = FirewallRule(self.config['config'], self.ctx.logger,
                                firewall_rule,
                                self.config['config']['network'])
        firewall.create()
        firewall_name = firewall.name
        firewalls = firewall.list()
        item = utils.get_item_from_gcp_response('name', firewall_name,
                                                firewalls)
        self.assertEqual(source_tag,
                         find_in_list(source_tag, item['sourceTags']))
        self.assertEqual(target_tag,
                         find_in_list(target_tag, item['targetTags']))

        firewall.firewall = self.config['firewall']
        firewall.firewall['name'] = firewall_name
        firewall.update()
        firewalls = firewall.list()
        item = utils.get_item_from_gcp_response('name', firewall_rule['name'],
                                                firewalls)
        self.assertIsNone(find_in_list(source_tag, item.get('sourceTags', [])))
        self.assertIsNone(find_in_list(target_tag, item.get('targetTags', [])))

        firewall.firewall = firewall_rule
        firewall.firewall['name'] = firewall_name
        firewall.update()
        firewalls = firewall.list()
        item = utils.get_item_from_gcp_response('name', firewall_rule['name'],
                                                firewalls)
        self.assertEqual(source_tag,
                         find_in_list(source_tag, item['sourceTags']))
        self.assertEqual(target_tag,
                         find_in_list(target_tag, item['targetTags']))
        firewall.delete()
        network.delete()
    def test_create_network(self):
        network = Network(self.config['config'], self.ctx.logger,
                          self.config['network'])
        networks = network.list()
        item = utils.get_item_from_gcp_response('name', network.name, networks)
        self.assertIsNone(item)
        self.ctx.logger.info(str(network.create))
        network.create()

        networks = network.list()
        item = utils.get_item_from_gcp_response('name', network.name, networks)
        self.assertIsNotNone(item)

        network.delete()

        networks = network.list()
        item = utils.get_item_from_gcp_response('name',
                                                self.config['network']['name'],
                                                networks)
        self.assertIsNone(item)
    def test_create_network(self):
        network = Network(self.config['config'],
                          self.ctx.logger,
                          self.config['network'])
        networks = network.list()
        item = utils.get_item_from_gcp_response(
            'name', network.name, networks)
        self.assertIsNone(item)
        self.ctx.logger.info(str(network.create))
        network.create()

        networks = network.list()
        item = utils.get_item_from_gcp_response(
            'name', network.name, networks)
        self.assertIsNotNone(item)

        network.delete()

        networks = network.list()
        item = utils.get_item_from_gcp_response(
            'name', self.config['network']['name'], networks)
        self.assertIsNone(item)
示例#11
0
def set_ip(instance, relationship=False):
    instances = instance.list()
    item = utils.get_item_from_gcp_response('name',
                                            instance.name,
                                            instances)
    try:
        if relationship:
            ctx.target.instance.runtime_properties['gcp_resource_id'] = \
                item['networkInterfaces'][0]['accessConfigs'][0]['natIP']
        else:
            ctx.instance.runtime_properties['ip'] = \
                item['networkInterfaces'][0]['networkIP']
        # only with one default network interface
    except (TypeError, KeyError):
        ctx.operation.retry('The instance has not yet created network interface', 10)
    def test_create_firewall_rule(self):
        network = Network(self.config['config'],
                          self.ctx.logger,
                          self.config['network'])
        networks = network.list()
        item = utils.get_item_from_gcp_response(
            'name', self.config['network']['name'], networks)
        self.assertIsNone(item)

        firewall = FirewallRule(self.config['config'],
                                self.ctx.logger,
                                self.config['firewall'],
                                self.config['config']['network'])
        firewall_rules = firewall.list()
        item = utils.get_item_from_gcp_response('name',
                                                firewall.name,
                                                firewall_rules)
        self.assertIsNone(item)

        network.create()

        networks = network.list()
        item = utils.get_item_from_gcp_response(
            'name', self.config['network']['name'], networks)
        self.assertIsNotNone(item)

        firewall.create()

        firewall_rules = firewall.list()
        item = utils.get_item_from_gcp_response(
            'name',
            firewall.name,
            firewall_rules)
        self.assertIsNotNone(item)

        firewall.delete()

        firewall_rules = firewall.list()
        item = utils.get_item_from_gcp_response(
            'name',
            firewall.name,
            firewall_rules)
        self.assertIsNone(item)

        network.delete()

        networks = network.list()
        item = utils.get_item_from_gcp_response(
            'name', self.config['network']['name'], networks)
        self.assertIsNone(item)
    def test_create_firewall_rule(self):
        network = Network(self.config['config'], self.ctx.logger,
                          self.config['network'])
        networks = network.list()
        item = utils.get_item_from_gcp_response('name',
                                                self.config['network']['name'],
                                                networks)
        self.assertIsNone(item)

        firewall = FirewallRule(self.config['config'], self.ctx.logger,
                                self.config['firewall'],
                                self.config['config']['network'])
        firewall_rules = firewall.list()
        item = utils.get_item_from_gcp_response('name', firewall.name,
                                                firewall_rules)
        self.assertIsNone(item)

        network.create()

        networks = network.list()
        item = utils.get_item_from_gcp_response('name',
                                                self.config['network']['name'],
                                                networks)
        self.assertIsNotNone(item)

        firewall.create()

        firewall_rules = firewall.list()
        item = utils.get_item_from_gcp_response('name', firewall.name,
                                                firewall_rules)
        self.assertIsNotNone(item)

        firewall.delete()

        firewall_rules = firewall.list()
        item = utils.get_item_from_gcp_response('name', firewall.name,
                                                firewall_rules)
        self.assertIsNone(item)

        network.delete()

        networks = network.list()
        item = utils.get_item_from_gcp_response('name',
                                                self.config['network']['name'],
                                                networks)
        self.assertIsNone(item)