def get_certificate_id(self, cert_id): certs = self.client().certificate.list()['certificates'] for cert in certs: if cert.id == cert_id: return cert.id raise exception.EntityNotFound(entity='Certificate', name=cert_id)
def get_notification(self, notification): try: return self.client().notifications.get( notification_id=notification)['id'] except monasca_exc.NotFound: raise heat_exc.EntityNotFound(entity='Monasca Notification', name=notification)
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_region_id(self, region): try: region_obj = self.client().client.regions.get(region) return region_obj.id except exceptions.NotFound: raise exception.EntityNotFound(entity='KeystoneRegion', name=region)
def load(cls, context, watch_name=None, watch=None): """Load the watchrule object. The object can be loaded either from the DB by name or from an existing DB object. """ if watch is None: try: watch = watch_rule_objects.WatchRule.get_by_name( context, watch_name) except Exception as ex: LOG.warning( _LW('WatchRule.load (%(watch_name)s) db error ' '%(ex)s'), { 'watch_name': watch_name, 'ex': ex }) if watch is None: raise exception.EntityNotFound(entity='Watch Rule', name=watch_name) else: return cls(context=context, watch_name=watch.name, rule=watch.rule, stack_id=watch.stack_id, state=watch.state, wid=watch.id, watch_data=watch.watch_data, last_evaluated=watch.last_evaluated)
def validate_with_client(self, client, value): neutron_plugin = client.client_plugin(CLIENT_NAME) if (self.extension and not neutron_plugin.has_extension(self.extension)): raise exception.EntityNotFound(entity='neutron extension', name=self.extension) neutron_plugin.find_resourceid_by_name_or_id( self.resource_name, value, cmd_resource=self.cmd_resource)
def get(context, entity_id, traversal_id, is_update): """Retrieves a sync point entry from DB.""" sync_point = sync_point_object.SyncPoint.get_by_key( context, entity_id, traversal_id, is_update) if sync_point is None: key = (entity_id, traversal_id, is_update) raise exception.EntityNotFound(entity='Sync Point', name=key) return sync_point
def validate_with_client(self, client, resource_id): # when user specify empty value in template, do not get the # responding resource from backend, otherwise an error will happen if resource_id == '': raise exception.EntityNotFound(entity=self.entity, name=resource_id) super(KeystoneBaseConstraint, self).validate_with_client(client, resource_id)
def get_container_by_ref(self, container_ref): try: # TODO(therve): replace with to_dict() return self.client().containers.get(container_ref) except Exception as ex: if self.is_not_found(ex): raise exception.EntityNotFound(entity="Container", name=container_ref) raise ex
def get_workflow_by_identifier(self, workflow_identifier): try: return self.client().workflows.get( workflow_identifier) except Exception as ex: if self.is_not_found(ex): raise exception.EntityNotFound( entity="Workflow", name=workflow_identifier) raise ex
def get_server(self, server): """Return fresh server object. Substitutes Nova's NotFound for Heat's EntityNotFound, to be returned to user as HTTP error. """ try: return self.client().servers.get(server) except exceptions.NotFound: raise exception.EntityNotFound(entity='Server', name=server)
def get_plugin_id(self, plugin_name): """Get the id for the specified plugin name. :param plugin_name: the name of the plugin to find :returns: the id of :plugin: :raises: exception.EntityNotFound """ try: self.client().plugins.get(plugin_name) except sahara_base.APIException: raise exception.EntityNotFound(entity='Plugin', name=plugin_name)
def get_role_id(self, role): try: role_obj = self.client().client.roles.get(role) return role_obj.id except exceptions.NotFound: role_list = self.client().client.roles.list(name=role) for role_obj in role_list: if role_obj.name == role: return role_obj.id raise exception.EntityNotFound(entity='KeystoneRole', name=role)
def get_keypair(self, key_name): """Get the public key specified by :key_name: :param key_name: the name of the key to look for :returns: the keypair (name, public_key) for :key_name: :raises: exception.EntityNotFound """ try: return self.client().keypairs.get(key_name) except exceptions.NotFound: raise exception.EntityNotFound(entity='Key', name=key_name)
def get_domain_id(self, domain_id_or_name): try: domain_obj = self.client().domains.get(domain_id_or_name) return domain_obj.id except exceptions.NotFound: for domain in self.client().domains.list(): if domain.name == domain_id_or_name: return domain.id raise heat_exception.EntityNotFound(entity='Designate Domain', name=domain_id_or_name)
def get_resource_info(self, resource_type, resource_name=None, registry_type=None, ignore=None): """Find possible matches to the resource type and name. Chain the results from the global and user registry to find a match. """ # use cases # 1) get the impl. # - filter_by(res_type=X), sort_by(res_name=W, is_user=True) # 2) in TemplateResource we need to get both the # TemplateClass and the ResourceClass # - filter_by(res_type=X, impl_type=TemplateResourceInfo), # sort_by(res_name=W, is_user=True) # - filter_by(res_type=X, impl_type=ClassResourceInfo), # sort_by(res_name=W, is_user=True) # 3) get_types() from the api # - filter_by(is_user=False) # 4) as_dict() to write to the db # - filter_by(is_user=True) if self.global_registry is not None: giter = self.global_registry.iterable_by(resource_type, resource_name) else: giter = [] matches = itertools.chain(self.iterable_by(resource_type, resource_name), giter) for info in sorted(matches): try: match = info.get_resource_info(resource_type, resource_name) except exception.EntityNotFound: continue if registry_type is None or isinstance(match, registry_type): if ignore is not None and match == ignore: continue # NOTE(prazumovsky): if resource_type defined in outer env # there is a risk to lose it due to h-eng restarting, so # store it to local env (exclude ClassResourceInfo because it # loads from resources; TemplateResourceInfo handles by # template_resource module). if (match and not match.user_resource and not isinstance(info, (TemplateResourceInfo, ClassResourceInfo))): self._register_info([resource_type], info) return match raise exception.EntityNotFound(entity='Resource Type', name=resource_type)
def get_secret_by_ref(self, secret_ref): try: secret = self.client().secrets.get(secret_ref) # Force lazy loading. TODO(therve): replace with to_dict() secret.name return secret except Exception as ex: if self.is_not_found(ex): raise exception.EntityNotFound(entity="Secret", name=secret_ref) raise ex
def get_group_id(self, group): if group is None: return None try: group_obj = self.client().client.groups.get(group) return group_obj.id except exceptions.NotFound: group_list = self.client().client.groups.list(name=group) for group_obj in group_list: if group_obj.name == group: return group_obj.id raise exception.EntityNotFound(entity='KeystoneGroup', name=group)
def get_domain_id(self, domain): if domain is None: return None try: domain_obj = self.client().client.domains.get(domain) return domain_obj.id except exceptions.NotFound: domain_list = self.client().client.domains.list(name=domain) for domain_obj in domain_list: if domain_obj.name == domain: return domain_obj.id raise exception.EntityNotFound(entity='KeystoneDomain', name=domain)
def get_project_id(self, project): if project is None: return None try: project_obj = self.client().client.projects.get(project) return project_obj.id except exceptions.NotFound: project_list = self.client().client.projects.list(name=project) for project_obj in project_list: if project_obj.name == project: return project_obj.id raise exception.EntityNotFound(entity='KeystoneProject', name=project)
def get_user_id(self, user): if user is None: return None try: user_obj = self.client().client.users.get(user) return user_obj.id except exceptions.NotFound: user_list = self.client().client.users.list(name=user) for user_obj in user_list: if user_obj.name == user: return user_obj.id raise exception.EntityNotFound(entity='KeystoneUser', name=user)
def get_host(self, host_name): """Get the host id specified by name. :param host_name: the name of host to find :returns: the list of match hosts :raises: exception.EntityNotFound """ host_list = self.client().hosts.list() for host in host_list: if host.host_name == host_name and host.service == self.COMPUTE: return host raise exception.EntityNotFound(entity='Host', name=host_name)
def get_volume_type(self, volume_type): vt_id = None volume_type_list = self.client().volume_types.list() for vt in volume_type_list: if vt.name == volume_type: vt_id = vt.id break if vt.id == volume_type: vt_id = vt.id break if vt_id is None: raise exception.EntityNotFound(entity='VolumeType', name=volume_type) return vt_id
def get_service_id(self, service): if service is None: return None try: service_obj = self.client().client.services.get(service) return service_obj.id except exceptions.NotFound: service_list = self.client().client.services.list(name=service) if len(service_list) == 1: return service_list[0].id elif len(service_list) > 1: raise exception.KeystoneServiceNameConflict(service=service) else: raise exception.EntityNotFound(entity='KeystoneService', name=service)
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 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
def _find_resource_by_id_or_name(id_or_name, resource_list, resource_type_name): """The method is trying to find id or name in item_list The method searches item with id_or_name in list and returns it. If there is more than one value or no values then it raises an exception :param id_or_name: resource id or name :param resource_list: list of resources :param resource_type_name: name of resource type that will be used for exceptions :raises EntityNotFound, NoUniqueMatch :return: resource or generate an exception otherwise """ search_result_by_id = [ res for res in resource_list if res.id == id_or_name ] if search_result_by_id: return search_result_by_id[0] else: # try to find resource by name search_result_by_name = [ res for res in resource_list if res.name == id_or_name ] match_count = len(search_result_by_name) if match_count > 1: message = ("Ambiguous {0} name '{1}'. Found more than one " "{0} for this name in Manila.").format( resource_type_name, id_or_name) raise exceptions.NoUniqueMatch(message) elif match_count == 1: return search_result_by_name[0] else: raise heat_exception.EntityNotFound(entity=resource_type_name, name=id_or_name)
def get_baymodel(self, value): try: self.client().baymodels.get(value) except mc_exc.NotFound: raise exception.EntityNotFound(entity='BayModel', name=value)
def get_volume_backup(self, backup): try: return self.client().backups.get(backup) except exceptions.NotFound: raise exception.EntityNotFound(entity='Volume backup', name=backup)
def get_live_resource_data(self): image_data = super(GlanceImage, self).get_live_resource_data() if image_data.get('status') in ('deleted', 'killed'): raise exception.EntityNotFound(entity='Resource', name=self.name) return image_data