Skip to content

Commit

Permalink
deps module utils: add function failed() to retrieve dependencies che…
Browse files Browse the repository at this point in the history
…ck result (#6383)

* deps module utils: add function failed() to retrieve dependencies check result

* add changelog frag

(cherry picked from commit e7cc996)
  • Loading branch information
russoz authored and patchback[bot] committed Apr 23, 2023
1 parent dbd9188 commit 5c56cce
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
2 changes: 2 additions & 0 deletions changelogs/fragments/6383-deps-failed.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
minor_changes:
- deps module utils - add function ``failed()`` providing the ability to check the dependency check result without triggering an exception (https://github.com/ansible-collections/community.general/pull/6383).
18 changes: 13 additions & 5 deletions plugins/module_utils/deps.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def message(self):
def failed(self):
return self.state == 1

def verify(self, module):
def validate(self, module):
if self.failed:
module.fail_json(msg=self.message, exception=self.trace)

Expand All @@ -71,10 +71,10 @@ def declare(name, *args, **kwargs):
_deps[name] = dep


def validate(module, spec=None):
def _select_names(spec):
dep_names = sorted(_deps)

if spec is not None:
if spec:
if spec.startswith("-"):
spec_split = spec[1:].split(":")
for d in spec_split:
Expand All @@ -86,5 +86,13 @@ def validate(module, spec=None):
_deps[d] # ensure it exists
dep_names.append(d)

for dep in dep_names:
_deps[dep].verify(module)
return dep_names


def validate(module, spec=None):
for dep in _select_names(spec):
_deps[dep].validate(module)


def failed(spec=None):
return any(_deps[d].failed for d in _select_names(spec))

0 comments on commit 5c56cce

Please sign in to comment.