diff --git a/src/tirith/providers/common.py b/src/tirith/providers/common.py index cc54805..1f01895 100644 --- a/src/tirith/providers/common.py +++ b/src/tirith/providers/common.py @@ -5,6 +5,11 @@ def create_result_dict(value=None, meta=None, err=None) -> Dict: return dict(value=value, meta=meta, err=err) +def get_path_value_from_dict(key_path: str, input_dict: dict, get_path_value_from_dict_func): + splitted_attribute = key_path.split(".*.") + return get_path_value_from_dict_func(splitted_attribute, input_dict) + + class ProviderError: """ A class to represent an error happening in a provider diff --git a/src/tirith/providers/json/handler.py b/src/tirith/providers/json/handler.py index 173e56d..ce4ced5 100644 --- a/src/tirith/providers/json/handler.py +++ b/src/tirith/providers/json/handler.py @@ -1,19 +1,13 @@ import pydash from typing import Callable, Dict, List -from ..common import create_result_dict, ProviderError +from ..common import create_result_dict, ProviderError, get_path_value_from_dict class PydashPathNotFound: pass -def get_path_value_from_dict(key_path, input_dict): - # TODO: Make this function more general and then move it to common.py - splitted_attribute = key_path.split(".*.") - return _get_path_value_from_dict(splitted_attribute, input_dict) - - def _get_path_value_from_dict(splitted_paths, input_dict): final_data = [] for i, expression in enumerate(splitted_paths): @@ -38,7 +32,7 @@ def get_value(provider_args: Dict, input_data: Dict) -> List[dict]: # Must be validated first whether the provider args are valid for this op type key_path: str = provider_args["key_path"] - values = get_path_value_from_dict(key_path, input_data) + values = get_path_value_from_dict(key_path, input_data, _get_path_value_from_dict) if len(values) == 0: severity_value = 2 diff --git a/src/tirith/providers/kubernetes/handler.py b/src/tirith/providers/kubernetes/handler.py index 62ac576..746b0dc 100644 --- a/src/tirith/providers/kubernetes/handler.py +++ b/src/tirith/providers/kubernetes/handler.py @@ -1,19 +1,13 @@ import pydash from typing import Callable, Dict, List -from ..common import create_result_dict, ProviderError +from ..common import create_result_dict, ProviderError, get_path_value_from_dict class PydashPathNotFound: pass -def get_path_value_from_dict(key_path, input_dict): - # TODO: Make this function more general and then move it to common.py - splitted_attribute = key_path.split(".*.") - return _get_path_value_from_dict(splitted_attribute, input_dict) - - def _get_path_value_from_dict(splitted_paths, input_dict): final_data = [] expression = splitted_paths[0] @@ -55,7 +49,7 @@ def get_value(provider_args: Dict, input_data: Dict, outputs: list) -> Dict: if resource["kind"] != target_kind: continue is_kind_found = True - values = get_path_value_from_dict(attribute_path, resource) + values = get_path_value_from_dict(attribute_path, resource, _get_path_value_from_dict) if ".*." not in attribute_path: # If there's no * in the attribute path, the values always have 1 member values = values[0]