def obj_from_id(resource_config, resources, typename): """Return the resource whose name matches the id. resource_config has to contain `id`, as it is used to lookup a resource. :param resource_config: resource to be transformed :param resources: iterable containing all resources :param typename: name which describes the type of resource :returns: resource object mapped to `id` """ if "id" in resource_config: matching = [ resource for resource in resources if resource.id == resource_config["id"] ] if len(matching) == 1: return matching[0] elif len(matching) > 1: raise exceptions.MultipleMatchesFound( needle="{typename} with id '{id}'".format( typename=typename.title(), id=resource_config["id"]), haystack=matching) else: raise exceptions.InvalidScenarioArgument( "{typename} with id '{id}' not found".format( typename=typename.title(), id=resource_config["id"])) else: raise exceptions.InvalidScenarioArgument( "{typename} 'id' not found in '{resource_config}'".format( typename=typename.title(), resource_config=resource_config))
def get(cls, name, namespace=None): """Return plugin by its name from specified namespace. This method iterates over all subclasses of cls and returns plugin by name from specified namespace. If namespace is not specified it will return first found plugin from any of namespaces. :param name: Plugin's name :param namespace: Namespace where to search for plugins """ potential_result = [] for p in cls.get_all(namespace=namespace): if p.get_name() == name: potential_result.append(p) if len(potential_result) == 1: return potential_result[0] elif potential_result: hint = _LE("Try to choose the correct Plugin base or namespace to " "search in.") if namespace: needle = "%s at %s namespace" % (name, namespace) else: needle = "%s at any of namespaces" % name raise exceptions.MultipleMatchesFound( needle=needle, haystack=", ".join(p.get_name() for p in potential_result), hint=hint) raise exceptions.PluginNotFound( name=name, namespace=namespace or "any of")
def get(cls, name, namespace=None, allow_hidden=False, fallback_to_default=True): """Return plugin by its name from specified namespace. This method iterates over all subclasses of cls and returns plugin by name from specified namespace. If namespace is not specified, it will return first found plugin from any of namespaces. :param name: Plugin's name :param namespace: Namespace where to search for plugins :param allow_hidden: if False and found plugin is hidden then PluginNotFound will be raised :param fallback_to_default: if True, then it tries to find plugin within "default" namespace """ potential_result = cls.get_all(name=name, namespace=namespace, allow_hidden=True) if fallback_to_default and len(potential_result) == 0: # try to find in default namespace potential_result = cls.get_all(name=name, namespace="default", allow_hidden=True) if len(potential_result) == 1: plugin = potential_result[0] if allow_hidden or not plugin.is_hidden(): return plugin elif potential_result: hint = _LE("Try to choose the correct Plugin base or namespace to " "search in.") if namespace: needle = "%s at %s namespace" % (name, namespace) else: needle = "%s at any of namespaces" % name raise exceptions.MultipleMatchesFound( needle=needle, haystack=", ".join(p.get_name() for p in potential_result), hint=hint) raise exceptions.PluginNotFound(name=name, namespace=namespace or "any of")
def base_repo(self): """Get directory to clone tempest to old: _ rally/tempest |_base -> clone from source to here |_for-deployment-<UUID1> -> copy from relevant tempest base |_for-deployment-<UUID2> -> copy from relevant tempest base new: _ rally/tempest |_base ||_ tempest_base-<rand suffix specific for source> -> clone || from source to here ||_ tempest_base-<rand suffix 2> |_for-deployment-<UUID1> -> copy from relevant tempest base |_for-deployment-<UUID2> -> copy from relevant tempest base """ if os.path.exists(Tempest.base_repo_dir): if self._is_git_repo(Tempest.base_repo_dir): # this is the old dir structure and needs to be upgraded directory = tempfile.mkdtemp(prefix=os.path.join( Tempest.base_repo_dir, "tempest_base-")) LOG.debug("Upgrading Tempest directory tree: " "Moving Tempest base dir %s into subdirectory %s" % (Tempest.base_repo_dir, directory)) self._move_contents_to_dir(Tempest.base_repo_dir, directory) if not self._base_repo: # Search existing tempest bases for a matching source repos = [ d for d in os.listdir(Tempest.base_repo_dir) if self._is_git_repo(d) and self.tempest_source == self._get_remote_origin(d) ] if len(repos) > 1: raise exceptions.MultipleMatchesFound( needle="git directory", haystack=repos) if repos: # Use existing base with relevant source self._base_repo = repos.pop() else: os.makedirs(Tempest.base_repo_dir) if not self._base_repo: self._base_repo = tempfile.mkdtemp(prefix=os.path.join( os.path.abspath(Tempest.base_repo_dir), "tempest_base-")) return self._base_repo