Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolved - Generalize the function get_path_value_from_dict in kubernetes and json handler and move it to common.py #145 #159

Merged
merged 1 commit into from
Aug 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/tirith/providers/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 2 additions & 8 deletions src/tirith/providers/json/handler.py
Original file line number Diff line number Diff line change
@@ -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):
Expand All @@ -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
Expand Down
10 changes: 2 additions & 8 deletions src/tirith/providers/kubernetes/handler.py
Original file line number Diff line number Diff line change
@@ -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]
Expand Down Expand Up @@ -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]
Expand Down
Loading