Skip to content

Commit

Permalink
BaseScopeMatcher: abstract ctor -> abstract get_data
Browse files Browse the repository at this point in the history
  • Loading branch information
RobertSzefler committed Apr 1, 2024
1 parent f689663 commit 49ee6b2
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 9 deletions.
3 changes: 3 additions & 0 deletions src/robusta/core/reporting/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,9 @@ class FilterableScopeMatcher(BaseScopeMatcher):
def __init__(self, data):
self.data = data

def get_data(self) -> Dict:
return self.data


class Filterable(ABC):
@property
Expand Down
3 changes: 3 additions & 0 deletions src/robusta/integrations/kubernetes/base_triggers.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ def __init__(self, data):
self.data[meta_key] = meta[meta_key]
self.data["attributes"] = meta

def get_data(self) -> Dict:
return self.data

def scope_match_attributes(self, attr_matcher: str, attr_value: Dict[str, Union[List, Dict]]) -> bool:
# attr_value is the full payload (nested dicts/lists)
# attr_matcher is a comma-separated list of pydash-compatible search path expressions
Expand Down
14 changes: 5 additions & 9 deletions src/robusta/utils/scope.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,8 @@ def check_and_normalize(cls, data: Dict) -> Dict:


class BaseScopeMatcher(ABC):
# The data that will be used for search. This will usually be derived from an event payload.
data: Dict

@abstractmethod
def __init__(self, data):
# In concrete implementations, this should process incoming data and set up self.data in a way
# that will make it possible for the general scope mechanism to work.
def get_data(self) -> Dict:
raise NotImplementedError

def scope_match_attributes(self, attr_matcher: str, attr_value: Dict[str, Union[List, Dict]]) -> bool:
Expand All @@ -62,10 +57,11 @@ def scope_matches(self, scope: ScopeIncludeExcludeParamsT) -> bool:
return True

def scope_attribute_matches(self, attr_name: str, attr_matchers: List[str]) -> bool:
if attr_name not in self.data:
logging.warning(f'Scope match on non-existent attribute "{attr_name}" ({self.data=}')
data = self.get_data()
if attr_name not in data:
logging.warning(f'Scope match on non-existent attribute "{attr_name}" ({data=}')
return False
attr_value = self.data[attr_name]
attr_value = data[attr_name]
for attr_matcher in attr_matchers:
if attr_name == "attributes":
return self.scope_match_attributes(attr_matcher, attr_value)
Expand Down

0 comments on commit 49ee6b2

Please sign in to comment.