summaryrefslogtreecommitdiffstats
path: root/roles/openshift_health_checker/openshift_checks/__init__.py
diff options
context:
space:
mode:
authorRodolfo Carvalho <rhcarvalho@gmail.com>2017-02-06 17:06:18 +0100
committerRodolfo Carvalho <rhcarvalho@gmail.com>2017-02-10 19:49:01 +0100
commitf502b09c103b5d8681854b7ab6a3c9655311f73b (patch)
tree92a10933acc42815ed14f9516b41d4028ed771d6 /roles/openshift_health_checker/openshift_checks/__init__.py
parentd6cebd0f5c43762469b1218e1e4e4bdb608cda19 (diff)
downloadopenshift-f502b09c103b5d8681854b7ab6a3c9655311f73b.tar.gz
openshift-f502b09c103b5d8681854b7ab6a3c9655311f73b.tar.bz2
openshift-f502b09c103b5d8681854b7ab6a3c9655311f73b.tar.xz
openshift-f502b09c103b5d8681854b7ab6a3c9655311f73b.zip
Refactor code to access values from task_vars
Diffstat (limited to 'roles/openshift_health_checker/openshift_checks/__init__.py')
-rw-r--r--roles/openshift_health_checker/openshift_checks/__init__.py18
1 files changed, 18 insertions, 0 deletions
diff --git a/roles/openshift_health_checker/openshift_checks/__init__.py b/roles/openshift_health_checker/openshift_checks/__init__.py
index ff99e7b4c..c31242624 100644
--- a/roles/openshift_health_checker/openshift_checks/__init__.py
+++ b/roles/openshift_health_checker/openshift_checks/__init__.py
@@ -5,8 +5,10 @@ Health checks for OpenShift clusters.
import os
from abc import ABCMeta, abstractmethod, abstractproperty
from importlib import import_module
+import operator
import six
+from six.moves import reduce
class OpenShiftCheckException(Exception):
@@ -54,6 +56,22 @@ class OpenShiftCheck(object):
yield subclass
+def get_var(task_vars, *keys, **kwargs):
+ """Helper function to get deeply nested values from task_vars.
+
+ Ansible task_vars structures are Python dicts, often mapping strings to
+ other dicts. This helper makes it easier to get a nested value, raising
+ OpenShiftCheckException when a key is not found.
+ """
+ try:
+ value = reduce(operator.getitem, keys, task_vars)
+ except (KeyError, TypeError):
+ if "default" in kwargs:
+ return kwargs["default"]
+ raise OpenShiftCheckException("'{}' is undefined".format(".".join(map(str, keys))))
+ return value
+
+
# Dynamically import all submodules for the side effect of loading checks.
EXCLUDES = (