def get_image_id_by_name(self, image_identifier): ''' Return an id for the specified image name. :param image_identifier: image name :returns: the id of the requested :image_identifier: :raises: exception.ImageNotFound, exception.PhysicalResourceNameAmbiguity ''' try: filters = {'name': image_identifier} image_list = self.client().images.find(**filters) except sahara_base.APIException as ex: raise exception.Error( _("Error retrieving image list from sahara: " "%s") % six.text_type(ex)) num_matches = len(image_list) if num_matches == 0: LOG.info(_LI("Image %s was not found in sahara images"), image_identifier) raise exception.ImageNotFound(image_name=image_identifier) elif num_matches > 1: LOG.info(_LI("Multiple images %s were found in sahara with name"), image_identifier) raise exception.PhysicalResourceNameAmbiguity( name=image_identifier) else: return image_list[0].id
def get_image_id_by_name(nova_client, image_identifier): ''' Return an id for the specified image name or identifier. :param nova_client: the nova client to use :param image_identifier: image name or a UUID-like identifier :returns: the id of the requested :image_identifier: :raises: exception.ImageNotFound, exception.PhysicalResourceNameAmbiguity ''' try: image_list = nova_client.images.list() except clients.novaclient.exceptions.ClientException as ex: raise exception.Error( message=(_("Error retrieving image list from nova: %s") % ex)) image_names = dict( (o.id, o.name) for o in image_list if o.name == image_identifier) if len(image_names) == 0: logger.info(_("Image %s was not found in glance") % image_identifier) raise exception.ImageNotFound(image_name=image_identifier) elif len(image_names) > 1: logger.info( _("Multiple images %s were found in glance with name") % image_identifier) raise exception.PhysicalResourceNameAmbiguity(name=image_identifier) image_id = image_names.popitem()[0] return image_id
def get_image_id_by_name(self, image_identifier): ''' Return an id for the specified image name. :param image_identifier: image name :returns: the id of the requested :image_identifier: :raises: exception.EntityNotFound, exception.PhysicalResourceNameAmbiguity ''' try: filters = {'name': image_identifier} image_list = list(self.client().images.list(filters=filters)) except exc.ClientException as ex: raise exception.Error( _("Error retrieving image list from glance: %s") % ex) num_matches = len(image_list) if num_matches == 0: LOG.info(_LI("Image %s was not found in glance"), image_identifier) raise exception.EntityNotFound(entity='Image', name=image_identifier) elif num_matches > 1: LOG.info(_LI("Multiple images %s were found in glance with name"), image_identifier) raise exception.PhysicalResourceNameAmbiguity( name=image_identifier) else: return image_list[0].id
def get_secgroup_uuids(self, security_groups): '''Returns a list of security group UUIDs. Args: security_groups: List of security group names or UUIDs ''' seclist = [] all_groups = None for sg in security_groups: if uuidutils.is_uuid_like(sg): seclist.append(sg) else: if not all_groups: response = self.client().list_security_groups() all_groups = response['security_groups'] same_name_groups = [g for g in all_groups if g['name'] == sg] groups = [g['id'] for g in same_name_groups] if len(groups) == 0: raise exception.EntityNotFound(entity='Resource', name=sg) elif len(groups) == 1: seclist.append(groups[0]) else: # for admin roles, can get the other users' # securityGroups, so we should match the tenant_id with # the groups, and return the own one own_groups = [ g['id'] for g in same_name_groups if g['tenant_id'] == self.context.tenant_id ] if len(own_groups) == 1: seclist.append(own_groups[0]) else: raise exception.PhysicalResourceNameAmbiguity(name=sg) return seclist
def get_image_id_by_name(glance_client, image_identifier): ''' Return an id for the specified image name or identifier. :param glance_client: the glance client to use :param image_identifier: image name or a UUID-like identifier :returns: the id of the requested :image_identifier: :raises: exception.ImageNotFound, exception.PhysicalResourceNameAmbiguity ''' try: filters = {'name': image_identifier} image_list = list(glance_client.images.list(filters=filters)) except glance_exceptions.ClientException as ex: raise exception.Error( _("Error retrieving image list from glance: %s") % ex) num_matches = len(image_list) if num_matches == 0: logger.info(_("Image %s was not found in glance") % image_identifier) raise exception.ImageNotFound(image_name=image_identifier) elif num_matches > 1: logger.info( _("Multiple images %s were found in glance with name") % image_identifier) raise exception.PhysicalResourceNameAmbiguity(name=image_identifier) else: return image_list[0].id
def test_instance_create_duplicate_image_name_err(self): stack_name = 'test_instance_create_image_name_err_stack' (tmpl, stack) = self._setup_test_stack(stack_name) # create an instance with a non unique image name wsp = tmpl.t['Resources']['WebServer']['Properties'] wsp['ImageId'] = 'CentOS 5.2' resource_defns = tmpl.resource_definitions(stack) instance = instances.Instance('instance_create_image_err', resource_defns['WebServer'], stack) self._mock_get_image_id_fail('CentOS 5.2', exception.PhysicalResourceNameAmbiguity( name='CentOS 5.2')) self.m.ReplayAll() create = scheduler.TaskRunner(instance.create) error = self.assertRaises(exception.ResourceFailure, create) self.assertEqual( 'StackValidationFailed: Property error : WebServer: ' 'ImageId Multiple physical resources were ' 'found with name (CentOS 5.2).', str(error)) self.m.VerifyAll()
def get_secgroup_uuids(self, security_groups): '''Returns a list of security group UUIDs. Args: security_groups: List of security group names or UUIDs ''' seclist = [] all_groups = None for sg in security_groups: if uuidutils.is_uuid_like(sg): seclist.append(sg) else: if not all_groups: # filtering by project_id so that if the user # has access to multiple (like admin) # only groups from the token scope are returned response = self.client().list_security_groups( project_id=self.context.project_id) all_groups = response['security_groups'] same_name_groups = [g for g in all_groups if g['name'] == sg] groups = [g['id'] for g in same_name_groups] if len(groups) == 0: raise exception.EntityNotFound(entity='Resource', name=sg) elif len(groups) == 1: seclist.append(groups[0]) else: raise exception.PhysicalResourceNameAmbiguity(name=sg) return seclist
def get_net_id_by_label(self, label): try: net_id = self.client().networks.find(label=label).id except exceptions.NotFound as ex: LOG.debug('Nova network (%(net)s) not found: %(ex)s', {'net': label, 'ex': ex}) raise exception.EntityNotFound(entity='Nova network', name=label) except exceptions.NoUniqueMatch as exc: LOG.debug('Nova network (%(net)s) is not unique matched: %(exc)s', {'net': label, 'exc': exc}) raise exception.PhysicalResourceNameAmbiguity(name=label) return net_id
def test_duplicated_image(self): t = template_format.parse(test_template_image) template = parser.Template(t) stack = parser.Stack(self.ctx, 'test_stack', template, environment.Environment({'KeyName': 'test'})) self._mock_get_image_id_fail( 'image_name', exception.PhysicalResourceNameAmbiguity(name='image_name')) self.m.ReplayAll() resource = stack['Instance'] self.assertRaises(exception.StackValidationFailed, resource.validate) self.m.VerifyAll()
def test_instance_create_duplicate_image_name_err(self): stack_name = 'test_instance_create_image_name_err_stack' (t, stack) = self._setup_test_stack(stack_name) # create an instance with a non unique image name t['Resources']['WebServer']['Properties']['ImageId'] = 'CentOS 5.2' instance = instances.Instance('instance_create_image_err', t['Resources']['WebServer'], stack) self._mock_get_image_id_fail( 'CentOS 5.2', exception.PhysicalResourceNameAmbiguity(name='CentOS 5.2')) self.m.ReplayAll() self.assertRaises(ValueError, instance.handle_create) self.m.VerifyAll()
def get_secgroup_uuids(security_groups, client, tenant_id): ''' Returns a list of security group UUIDs. Args: security_groups: List of security group names or UUIDs client: reference to neutronclient tenant_id: the tenant id to match the security_groups ''' warnings.warn('neutron.NeutronResource.get_secgroup_uuids is ' 'deprecated. Use ' 'self.client_plugin("neutron").get_secgroup_uuids') seclist = [] all_groups = None for sg in security_groups: if uuidutils.is_uuid_like(sg): seclist.append(sg) else: if not all_groups: response = client.list_security_groups() all_groups = response['security_groups'] same_name_groups = [g for g in all_groups if g['name'] == sg] groups = [g['id'] for g in same_name_groups] if len(groups) == 0: raise exception.PhysicalResourceNotFound(resource_id=sg) elif len(groups) == 1: seclist.append(groups[0]) else: # for admin roles, can get the other users' # securityGroups, so we should match the tenant_id with # the groups, and return the own one own_groups = [ g['id'] for g in same_name_groups if g['tenant_id'] == tenant_id ] if len(own_groups) == 1: seclist.append(own_groups[0]) else: raise exception.PhysicalResourceNameAmbiguity(name=sg) return seclist
def find_resource_by_name(self, resource_name, value): """Return the ID for the specified entity name. :raises: exception.EntityNotFound, exception.PhysicalResourceNameAmbiguity """ try: filters = {'name': value} obj = getattr(self.client(), resource_name) obj_list = obj.find(**filters) except sahara_base.APIException as ex: raise exception.Error( _("Error retrieving %(entity)s list from sahara: " "%(err)s") % dict(entity=resource_name, err=six.text_type(ex))) num_matches = len(obj_list) if num_matches == 0: raise exception.EntityNotFound(entity=resource_name or 'entity', name=value) elif num_matches > 1: raise exception.PhysicalResourceNameAmbiguity(name=value) else: return obj_list[0].id
def get_secgroup_uuids(security_groups, client): ''' Returns a list of security group UUIDs. Args: security_groups: List of security group names or UUIDs client: reference to neutronclient ''' seclist = [] all_groups = None for sg in security_groups: if uuidutils.is_uuid_like(sg): seclist.append(sg) else: if not all_groups: response = client.list_security_groups() all_groups = response['security_groups'] groups = [g['id'] for g in all_groups if g['name'] == sg] if len(groups) == 0: raise exception.PhysicalResourceNotFound(resource_id=sg) if len(groups) > 1: raise exception.PhysicalResourceNameAmbiguity(name=sg) seclist.append(groups[0]) return seclist
def get_image_id_by_name(self, image_identifier): """Return the ID for the specified image name. :param image_identifier: image name :returns: the id of the requested :image_identifier: :raises: exception.EntityNotFound, exception.PhysicalResourceNameAmbiguity """ try: filters = {'name': image_identifier} image_list = list(self.client().images.list(filters=filters)) except exc.ClientException as ex: raise exception.Error( _("Error retrieving image list from glance: %s") % ex) num_matches = len(image_list) if num_matches == 0: raise exception.EntityNotFound(entity='Image', name=image_identifier) elif num_matches > 1: raise exception.PhysicalResourceNameAmbiguity( name=image_identifier) else: return image_list[0].id
def get_image_id_by_name(self, image_identifier): """Return the ID for the specified image name. :param image_identifier: image name :returns: the id of the requested :image_identifier: :raises: exception.EntityNotFound, exception.PhysicalResourceNameAmbiguity """ try: filters = {'name': image_identifier} image_list = self.client().images.find(**filters) except sahara_base.APIException as ex: raise exception.Error( _("Error retrieving image list from sahara: " "%s") % six.text_type(ex)) num_matches = len(image_list) if num_matches == 0: raise exception.EntityNotFound(entity='Image', name=image_identifier) elif num_matches > 1: raise exception.PhysicalResourceNameAmbiguity( name=image_identifier) else: return image_list[0].id