Skip to content

Commit

Permalink
Improve simple group logging
Browse files Browse the repository at this point in the history
  • Loading branch information
ezh committed Feb 18, 2020
1 parent 8f6a650 commit 0e76ae3
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 11 deletions.
28 changes: 19 additions & 9 deletions cloudselect/group/simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,30 @@ 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())
self.log.warning("%s: 'options' block not found in %s", name, self.config())
return None
if not isinstance(options, list):
self.log.warning(
"'options' block should be list of dictionaries in %s", self.config(),
"%s: 'options' block should be list of dictionaries in %s",
name,
self.config(),
)
return None
for group in self.config().get("options", []):
try:
self.log.debug("Process group %s", group)
self.log.debug("%s: Process group %s", name, group)
entry = group.get("match")
if not entry:
self.log.warning("Unable to find 'match' key in %s", group)
self.log.warning(
"%s: Unable to find 'match' key in %s", name, group,
)
continue
if ":" not in entry:
self.log.warning(
"Unable to parse 'match' value %s for %s", entry, group,
"%s: Unable to parse 'match' value %s for %s",
name,
entry,
group,
)
continue
key, pattern = entry.split(":", 1)
Expand All @@ -50,18 +57,21 @@ def run(self, name, metadata):
try:
value = value.get(key_part)
if not value:
self.log.debug("Unable to find key %s", key_part)
self.log.debug("%s: Unable to find key %s", name, key_part)
break
except (AttributeError, NameError):
self.log.debug("Unable to find key %s", key_part)
self.log.debug("%s: Unable to find key %s", name, 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,
"%s: Pattern %s and value %s is matched",
name,
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)
self.log.warning("%s: Unable to find 'match' key in %s", name, group)
return None
5 changes: 3 additions & 2 deletions test/group/test_simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,16 @@ def test_options_errors(caplog, cfgdir):
assert len(caplog.records) == 1
assert (
caplog.records[0].getMessage()
== "'options' block not found in {'type': 'simple'}"
== "option: 'options' block not found in {'type': 'simple'}"
)

caplog.clear()
configuration["group"]["options"] = 123
Container.options("option")
assert len(caplog.records) == 1
assert (
caplog.records[0].msg == "'options' block should be list of dictionaries in %s"
caplog.records[0].msg
== "%s: 'options' block should be list of dictionaries in %s"
)


Expand Down

0 comments on commit 0e76ae3

Please sign in to comment.