Skip to content

Commit

Permalink
Improve simple group behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
ezh committed Feb 18, 2020
1 parent ea06a7c commit 85a6489
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 38 deletions.
18 changes: 11 additions & 7 deletions cloudselect/cloudselect.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def __init__(self, configpath=None):
"""Class constructor."""
if configpath:
self.configpath = str(configpath)
self.logger = None
self.log = None

def configuration_exists(self, name):
"""Check if configuration/profile exists."""
Expand Down Expand Up @@ -104,9 +104,9 @@ def configuration_read(self, name=None):
return safe_load(f)
except Exception as e:
message = "Unable to read configuration {}: {}".format(full_path, str(e))
if self.logger:
self.logger.debug(traceback.format_exc())
self.logger.error(message)
if self.log:
self.log.debug(traceback.format_exc())
self.log.error(message)
else:
traceback.print_exc()
print(message)
Expand All @@ -119,9 +119,9 @@ def fabric(self, configuration, args):
elif args.verbose > 1:
configuration.get("log", {}).get("root", {})["level"] = logging.DEBUG
dictConfig(configuration.get("log", {}))
self.logger = logging.getLogger("cloudselect.CloudSelect")
self.logger.debug("Logging is initialized")
self.logger.debug(
self.log = logging.getLogger("cloudselect.CloudSelect")
self.log.debug("Logging is initialized")
self.log.debug(
"Configuration:\n%s",
json.dumps(configuration, sort_keys=True, indent=4, separators=(",", ": ")),
)
Expand Down Expand Up @@ -200,6 +200,10 @@ def options(self, name, metadata=None):
group = Container.group()
base = copy.deepcopy(Container.config().get(name, {}))
override = group.run(name, metadata)
if override is None:
return base
if override == {}:
return {}
return self.merge(base, override)

@staticmethod
Expand Down
2 changes: 1 addition & 1 deletion cloudselect/group/group.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class GroupService:
@staticmethod
def run(name, metadata): # pylint: disable=unused-argument
"""Get options for name regard with metadata."""
return {}
return None

@staticmethod
def config():
Expand Down
72 changes: 42 additions & 30 deletions cloudselect/group/simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,38 +18,50 @@ class Simple(GroupService):
def __init__(self):
"""Class constructor."""
self.default_priority = 1000
self.logger = logging.getLogger("cloudselect.group.Simple")
self.log = logging.getLogger("cloudselect.group.Simple")

def run(self, name, metadata):
"""Return dictionary with options for the group of instances."""
options = self.config().get("options")
if not options:
self.log.warning("'options' block not found in %s", self.config())
return None
if not isinstance(options, list):
self.log.warning(
"'options' block should be list of dictionaries in %s", self.config(),
)
return None
for group in self.config().get("options", []):
self.logger.debug("Process group %s", group)
entry = group.get("match")
if not entry:
self.logger.warning("Unable to find 'match' key in %s", group)
continue
if ":" not in entry:
self.logger.warning(
"Unable to parse 'match' value %s for %s", entry, group,
)
continue
key, pattern = entry.split(":", 1)
regex = re.compile(pattern)
value = metadata
for key_part in key.split("."):
try:
value = value.get(key_part)
if not value:
self.logger.debug("Unable to find key %s", key_part)
break
except (AttributeError, NameError):
self.logger.debug("Unable to find key %s", key_part)
break
if isinstance(value, str) and regex.match(value):
self.logger.debug(
"Match pattern %s and value %s", pattern, value,
try:
self.log.debug("Process group %s", group)
entry = group.get("match")
if not entry:
self.log.warning("Unable to find 'match' key in %s", group)
continue
if ":" not in entry:
self.log.warning(
"Unable to parse 'match' value %s for %s", entry, group,
)
result = group.get(name)
if result is not None:
return result
return {}
continue
key, pattern = entry.split(":", 1)
regex = re.compile(pattern)
value = metadata
for key_part in key.split("."):
try:
value = value.get(key_part)
if not value:
self.log.debug("Unable to find key %s", key_part)
break
except (AttributeError, NameError):
self.log.debug("Unable to find key %s", key_part)
break
if isinstance(value, str) and regex.match(value) is not None:
self.log.debug(
"Match pattern %s and value %s", pattern, value,
)
result = group.get(name)
if result is not None:
return result
except AttributeError:
self.log.warning("Unable to find 'match' key in %s", group)
return None

0 comments on commit 85a6489

Please sign in to comment.