Allow to specify resource name pattern via rally.conf
This patch as well refactors the interface of RandomNameMixin instead of using cls variables we can now use as well method which can dynamically return the pattern name value. This is important because otherwise oslo.conf doesn't work =( Change-Id: Ib6af0c9784590e29634995d57e2e465a7b2dde5e
This commit is contained in:
parent
7165f31726
commit
fc5e66b4b8
@ -270,6 +270,14 @@ class RandomNameGeneratorMixin(object):
|
||||
RESOURCE_NAME_FORMAT = "rally_XXXXXXXX_XXXXXXXX"
|
||||
RESOURCE_NAME_ALLOWED_CHARACTERS = string.ascii_letters + string.digits
|
||||
|
||||
@classmethod
|
||||
def _get_resource_name_format(cls):
|
||||
return cls.RESOURCE_NAME_FORMAT
|
||||
|
||||
@classmethod
|
||||
def _get_resource_name_allowed_characters(cls):
|
||||
return cls.RESOURCE_NAME_ALLOWED_CHARACTERS
|
||||
|
||||
@classmethod
|
||||
def _generate_random_part(cls, length):
|
||||
"""Generate a random string.
|
||||
@ -279,8 +287,9 @@ class RandomNameGeneratorMixin(object):
|
||||
containing only characters from
|
||||
cls.RESOURCE_NAME_ALLOWED_CHARACTERS
|
||||
"""
|
||||
return "".join(random.choice(cls.RESOURCE_NAME_ALLOWED_CHARACTERS)
|
||||
for i in range(length))
|
||||
return "".join(
|
||||
random.choice(cls._get_resource_name_allowed_characters())
|
||||
for i in range(length))
|
||||
|
||||
@classmethod
|
||||
def _generate_task_id_part(cls, task_id, length):
|
||||
@ -292,14 +301,14 @@ class RandomNameGeneratorMixin(object):
|
||||
LOG.debug("Task ID %(task_id)s cannot be included in a random "
|
||||
"name because it is too short. Format: %(format)s"
|
||||
% {"task_id": task_id,
|
||||
"format": cls.RESOURCE_NAME_FORMAT})
|
||||
elif any(char not in cls.RESOURCE_NAME_ALLOWED_CHARACTERS
|
||||
"format": cls._get_resource_name_format()})
|
||||
elif any(char not in cls._get_resource_name_allowed_characters()
|
||||
for char in task_id_part):
|
||||
LOG.debug("Task ID %(task_id)s cannot be included in a random "
|
||||
"name because it includes disallowed characters. "
|
||||
"Allowed characters are: %(chars)s"
|
||||
% {"task_id": task_id,
|
||||
"chars": cls.RESOURCE_NAME_ALLOWED_CHARACTERS})
|
||||
"chars": cls._get_resource_name_allowed_characters()})
|
||||
else:
|
||||
return task_id_part
|
||||
|
||||
@ -338,10 +347,10 @@ class RandomNameGeneratorMixin(object):
|
||||
task_id = self.get_owner_id()
|
||||
|
||||
match = self._resource_name_placeholder_re.match(
|
||||
self.RESOURCE_NAME_FORMAT)
|
||||
self._get_resource_name_format())
|
||||
if match is None:
|
||||
raise ValueError("%s is not a valid resource name format" %
|
||||
self.RESOURCE_NAME_FORMAT)
|
||||
self._get_resource_name_format())
|
||||
parts = match.groupdict()
|
||||
return "".join([
|
||||
parts["prefix"],
|
||||
@ -366,13 +375,13 @@ class RandomNameGeneratorMixin(object):
|
||||
:returns: bool
|
||||
"""
|
||||
match = cls._resource_name_placeholder_re.match(
|
||||
cls.RESOURCE_NAME_FORMAT)
|
||||
cls._get_resource_name_format())
|
||||
parts = match.groupdict()
|
||||
subst = {
|
||||
"prefix": re.escape(parts["prefix"]),
|
||||
"sep": re.escape(parts["sep"]),
|
||||
"suffix": re.escape(parts["suffix"]),
|
||||
"chars": re.escape(cls.RESOURCE_NAME_ALLOWED_CHARACTERS),
|
||||
"chars": re.escape(cls._get_resource_name_allowed_characters()),
|
||||
"rand_length": len(parts["rand"])}
|
||||
if task_id:
|
||||
subst["task_id"] = cls._generate_task_id_part(task_id,
|
||||
@ -408,7 +417,8 @@ def name_matches_object(name, *objects, **kwargs):
|
||||
"""
|
||||
unique_rng_options = {}
|
||||
for obj in objects:
|
||||
key = (obj.RESOURCE_NAME_FORMAT, obj.RESOURCE_NAME_ALLOWED_CHARACTERS)
|
||||
key = (obj._get_resource_name_format(),
|
||||
obj._get_resource_name_allowed_characters())
|
||||
if key not in unique_rng_options:
|
||||
unique_rng_options[key] = obj
|
||||
return any(obj.name_matches_object(name, **kwargs)
|
||||
|
@ -15,6 +15,7 @@
|
||||
|
||||
import abc
|
||||
|
||||
from oslo_config import cfg
|
||||
import six
|
||||
|
||||
from rally.common import logging
|
||||
@ -23,7 +24,16 @@ from rally.common import utils
|
||||
from rally.common import validation
|
||||
from rally.task import functional
|
||||
|
||||
|
||||
LOG = logging.getLogger(__name__)
|
||||
CONF = cfg.CONF
|
||||
CONF_OPTS = [
|
||||
cfg.StrOpt(
|
||||
"context_resource_name_format",
|
||||
help="Template is used to generate random names of resources. X is"
|
||||
"replaced with random latter, amount of X can be adjusted")
|
||||
]
|
||||
CONF.register_opts(CONF_OPTS)
|
||||
|
||||
|
||||
@logging.log_deprecated_args("Use 'platform' arg instead", "0.10.0",
|
||||
@ -161,6 +171,11 @@ class Context(BaseContext, validation.ValidatablePluginMixin):
|
||||
super(Context, self).__init__(ctx)
|
||||
self.task = self.context.get("task", {})
|
||||
|
||||
@classmethod
|
||||
def _get_resource_name_format(cls):
|
||||
return (CONF.context_resource_name_format
|
||||
or super(Context, cls)._get_resource_name_format())
|
||||
|
||||
def get_owner_id(self):
|
||||
if "owner_id" in self.context:
|
||||
return self.context["owner_id"]
|
||||
|
@ -16,6 +16,8 @@
|
||||
import copy
|
||||
import random
|
||||
|
||||
from oslo_config import cfg
|
||||
|
||||
from rally.common import logging
|
||||
from rally.common.objects import task # noqa
|
||||
from rally.common.plugin import plugin
|
||||
@ -28,6 +30,14 @@ from rally.task.processing import charts
|
||||
|
||||
|
||||
LOG = logging.getLogger(__name__)
|
||||
CONF = cfg.CONF
|
||||
CONF_OPTS = [
|
||||
cfg.StrOpt(
|
||||
"scenario_resource_name_format",
|
||||
help="Template is used to generate random names of resources. X is"
|
||||
"replaced with random latter, amount of X can be adjusted")
|
||||
]
|
||||
CONF.register_opts(CONF_OPTS)
|
||||
|
||||
|
||||
@logging.log_deprecated_args("Use 'platform' arg instead", "0.10.0",
|
||||
@ -87,6 +97,11 @@ class Scenario(plugin.Plugin,
|
||||
"""
|
||||
RESOURCE_NAME_FORMAT = "s_rally_XXXXXXXX_XXXXXXXX"
|
||||
|
||||
@classmethod
|
||||
def _get_resource_name_format(cls):
|
||||
return (CONF.scenario_resource_name_format
|
||||
or super(Scenario, cls)._get_resource_name_format())
|
||||
|
||||
def __init__(self, context=None):
|
||||
super(Scenario, self).__init__()
|
||||
self.context = context or {}
|
||||
|
@ -574,6 +574,8 @@ def check_opts_import_path(logical_line, physical_line, filename):
|
||||
"""
|
||||
excluded_files = ["./rally/osclients.py",
|
||||
"./rally/task/engine.py",
|
||||
"./rally/task/context.py",
|
||||
"./rally/task/scenario.py",
|
||||
"./rally/common/opts.py"]
|
||||
forbidden_methods = [".register_opts("]
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user