Skip to content

Commit

Permalink
rulesets as sequences instead of iterables
Browse files Browse the repository at this point in the history
We will need to iterate over the rulesets twice at least in
order to fix the cache ID.  So let's type the structure as
a `Sequence`.

CMK-14467

Change-Id: Iecdf6616c7f1bc2da63deffc4063209a37dc2c92
  • Loading branch information
Synss committed Oct 25, 2023
1 parent e35e2b7 commit f84f2af
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
2 changes: 1 addition & 1 deletion cmk/base/default_config/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ class _PeriodicDiscovery(TypedDict):
clusters: dict[HostName, list[HostName]] = {}
clustered_services: list[RuleSpec[object]] = []
# new in 1.1.4
clustered_services_of: dict[HostAddress, Iterable[RuleSpec[object]]] = {}
clustered_services_of: dict[HostAddress, Sequence[RuleSpec[object]]] = {}
# new for 1.2.5i1 Wato Rule
clustered_services_mapping: list[RuleSpec[HostAddress]] = []
clustered_services_configuration: list[
Expand Down
20 changes: 10 additions & 10 deletions cmk/utils/rulesets/ruleset_matcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ def __init__(
tuple[HostName, ServiceName, Item | None], RulesetMatchObject
] = {}

def get_host_bool_value(self, hostname: HostName, ruleset: Iterable[RuleSpec[bool]]) -> bool:
def get_host_bool_value(self, hostname: HostName, ruleset: Sequence[RuleSpec[bool]]) -> bool:
"""Compute outcome of a ruleset set that just says yes/no
The binary match only cares about the first matching rule of an object.
Expand All @@ -189,7 +189,7 @@ def get_host_bool_value(self, hostname: HostName, ruleset: Iterable[RuleSpec[boo
def get_host_merged_dict(
self,
hostname: HostName,
ruleset: Iterable[RuleSpec[Mapping[str, TRuleValue]]],
ruleset: Sequence[RuleSpec[Mapping[str, TRuleValue]]],
) -> Mapping[str, TRuleValue]:
"""Returns a dictionary of the merged dict values of the matched rules
The first dict setting a key defines the final value.
Expand All @@ -203,7 +203,7 @@ def get_host_merged_dict(
def get_host_values(
self,
hostname: HostName | HostAddress,
ruleset: Iterable[RuleSpec[TRuleValue]],
ruleset: Sequence[RuleSpec[TRuleValue]],
) -> Sequence[TRuleValue]:
"""Returns a generator of the values of the matched rules."""

Expand Down Expand Up @@ -258,7 +258,7 @@ def _checkgroup_match_object(
)

def get_service_bool_value(
self, hostname: HostName, description: ServiceName, ruleset: Iterable[RuleSpec[TRuleValue]]
self, hostname: HostName, description: ServiceName, ruleset: Sequence[RuleSpec[TRuleValue]]
) -> bool:
"""Compute outcome of a ruleset set that just says yes/no
Expand All @@ -278,7 +278,7 @@ def get_service_merged_dict(
self,
hostname: HostName,
description: ServiceName,
ruleset: Iterable[RuleSpec[Mapping[str, TRuleValue]]],
ruleset: Sequence[RuleSpec[Mapping[str, TRuleValue]]],
) -> Mapping[str, TRuleValue]:
"""Returns a dictionary of the merged dict values of the matched rules
The first dict setting a key defines the final value.
Expand All @@ -295,7 +295,7 @@ def get_service_merged_dict(
return merged

def service_extra_conf(
self, hostname: HostName, description: ServiceName, ruleset: Iterable[RuleSpec[TRuleValue]]
self, hostname: HostName, description: ServiceName, ruleset: Sequence[RuleSpec[TRuleValue]]
) -> list[TRuleValue]:
"""Compute outcome of a service rule set that has an item."""
return list(
Expand All @@ -309,7 +309,7 @@ def get_checkgroup_ruleset_values(
hostname: HostName,
description: ServiceName,
item: Item,
ruleset: Iterable[RuleSpec[TRuleValue]],
ruleset: Sequence[RuleSpec[TRuleValue]],
) -> list[TRuleValue]:
return list(
self.get_service_ruleset_values(
Expand All @@ -320,7 +320,7 @@ def get_checkgroup_ruleset_values(
def get_service_ruleset_values(
self,
match_object: RulesetMatchObject,
ruleset: Iterable[RuleSpec[TRuleValue]],
ruleset: Sequence[RuleSpec[TRuleValue]],
) -> Iterator[TRuleValue]:
"""Returns a generator of the values of the matched rules"""
with_foreign_hosts = (
Expand Down Expand Up @@ -472,7 +472,7 @@ def set_all_processed_hosts(self, all_processed_hosts: Iterable[HostName]) -> No
)

def get_host_ruleset(
self, ruleset: Iterable[RuleSpec[TRuleValue]], with_foreign_hosts: bool
self, ruleset: Sequence[RuleSpec[TRuleValue]], with_foreign_hosts: bool
) -> Mapping[HostAddress, Sequence[TRuleValue]]:
def _impl(
ruleset: Iterable[RuleSpec[TRuleValue]], with_foreign_hosts: bool
Expand All @@ -494,7 +494,7 @@ def _impl(
return self.__host_ruleset_cache.setdefault(cache_id, _impl(ruleset, with_foreign_hosts))

def get_service_ruleset(
self, ruleset: Iterable[RuleSpec[TRuleValue]], with_foreign_hosts: bool
self, ruleset: Sequence[RuleSpec[TRuleValue]], with_foreign_hosts: bool
) -> PreprocessedServiceRuleset[TRuleValue]:
def _impl(
ruleset: Iterable[RuleSpec[TRuleValue]], with_foreign_hosts: bool
Expand Down

0 comments on commit f84f2af

Please sign in to comment.