Skip to content

Commit

Permalink
Pass checking_section cb to CMKParser
Browse files Browse the repository at this point in the history
removes last dep of this class on ConfigCache

CMK-16672

Change-Id: I6f47db57f93af8e3e0051edea07a259905c984b5
  • Loading branch information
Synss committed May 10, 2024
1 parent 21b95bd commit f8294f5
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 19 deletions.
12 changes: 9 additions & 3 deletions cmk/base/automations/check_mk.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,8 +275,10 @@ def execute(self, args: list[str]) -> ServiceDiscoveryResult:
results: dict[HostName, DiscoveryResult] = {}

parser = CMKParser(
config_cache,
config_cache.parser_factory(),
checking_sections=lambda hostname: config_cache.make_checking_sections(
hostname, selected_sections=NO_SELECTION
),
selected_sections=NO_SELECTION,
keep_outdated=file_cache_options.keep_outdated,
logger=logging.getLogger("cmk.base.discovery"),
Expand Down Expand Up @@ -540,8 +542,10 @@ def _execute_discovery(
hosts_config = config.make_hosts_config()
ruleset_matcher = config_cache.ruleset_matcher
parser = CMKParser(
config_cache,
config_cache.parser_factory(),
checking_sections=lambda hostname: config_cache.make_checking_sections(
hostname, selected_sections=NO_SELECTION
),
selected_sections=NO_SELECTION,
keep_outdated=file_cache_options.keep_outdated,
logger=logging.getLogger("cmk.base.discovery"),
Expand Down Expand Up @@ -662,8 +666,10 @@ def _execute_autodiscovery() -> tuple[Mapping[HostName, DiscoveryResult], bool]:
)
ruleset_matcher = config_cache.ruleset_matcher
parser = CMKParser(
config_cache,
config_cache.parser_factory(),
checking_sections=lambda hostname: config_cache.make_checking_sections(
hostname, selected_sections=NO_SELECTION
),
selected_sections=NO_SELECTION,
keep_outdated=file_cache_options.keep_outdated,
logger=logging.getLogger("cmk.base.discovery"),
Expand Down
8 changes: 3 additions & 5 deletions cmk/base/checkers.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,16 +159,16 @@ def _do_fetch(
class CMKParser:
def __init__(
self,
config_cache: ConfigCache,
factory: ParserFactory,
*,
checking_sections: Callable[[HostName], Iterable[SectionName]],
selected_sections: SectionNameCollection,
keep_outdated: bool,
logger: logging.Logger,
) -> None:
self.config_cache: Final = config_cache
self.factory: Final = factory
self.selected_sections: Final = selected_sections
self.checking_sections: Final = checking_sections
self.keep_outdated: Final = keep_outdated
self.logger: Final = logger

Expand All @@ -193,9 +193,7 @@ def __call__(
self.factory,
source.hostname,
source.fetcher_type,
checking_sections=self.config_cache.make_checking_sections(
source.hostname, selected_sections=NO_SELECTION
),
checking_sections=self.checking_sections(source.hostname),
persisted_section_dir=make_persisted_section_dir(
source.hostname,
fetcher_type=source.fetcher_type,
Expand Down
2 changes: 1 addition & 1 deletion cmk/base/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -4027,7 +4027,7 @@ def make_snmp_parser(
*,
keep_outdated: bool,
logger: logging.Logger,
checking_sections: frozenset[SectionName],
checking_sections: Iterable[SectionName],
) -> SNMPParser:
return SNMPParser(
host_name,
Expand Down
24 changes: 18 additions & 6 deletions cmk/base/modes/check_mk.py
Original file line number Diff line number Diff line change
Expand Up @@ -1814,8 +1814,10 @@ def mode_check_discovery(
password_store_file=cmk.utils.password_store.core_password_store_path(LATEST_CONFIG),
)
parser = CMKParser(
config_cache,
config_cache.parser_factory(),
checking_sections=lambda hostname: config_cache.make_checking_sections(
hostname, selected_sections=NO_SELECTION
),
selected_sections=NO_SELECTION,
keep_outdated=file_cache_options.keep_outdated,
logger=logging.getLogger("cmk.base.discovery"),
Expand Down Expand Up @@ -2124,8 +2126,10 @@ def mode_discover(options: _DiscoveryOptions, args: list[str]) -> None:
selected_sections, run_plugin_names = _extract_plugin_selection(options, CheckPluginName)
config_cache = config.get_config_cache()
parser = CMKParser(
config_cache,
config_cache.parser_factory(),
checking_sections=lambda hostname: config_cache.make_checking_sections(
hostname, selected_sections=NO_SELECTION
),
selected_sections=selected_sections,
keep_outdated=file_cache_options.keep_outdated,
logger=logging.getLogger("cmk.base.discovery"),
Expand Down Expand Up @@ -2308,8 +2312,10 @@ def mode_check(
),
)
parser = CMKParser(
config_cache,
config_cache.parser_factory(),
checking_sections=lambda hostname: config_cache.make_checking_sections(
hostname, selected_sections=NO_SELECTION
),
selected_sections=selected_sections,
keep_outdated=file_cache_options.keep_outdated,
logger=logging.getLogger("cmk.base.checking"),
Expand Down Expand Up @@ -2533,8 +2539,10 @@ def mode_inventory(options: _InventoryOptions, args: list[str]) -> None:
password_store_file=cmk.utils.password_store.pending_password_store_path(),
)
parser = CMKParser(
config_cache,
config_cache.parser_factory(),
checking_sections=lambda hostname: config_cache.make_checking_sections(
hostname, selected_sections=NO_SELECTION
),
selected_sections=selected_sections,
keep_outdated=file_cache_options.keep_outdated,
logger=logging.getLogger("cmk.base.inventory"),
Expand Down Expand Up @@ -2780,8 +2788,10 @@ def mode_inventory_as_check(
password_store_file=cmk.utils.password_store.core_password_store_path(LATEST_CONFIG),
)
parser = CMKParser(
config_cache,
config_cache.parser_factory(),
checking_sections=lambda hostname: config_cache.make_checking_sections(
hostname, selected_sections=NO_SELECTION
),
selected_sections=NO_SELECTION,
keep_outdated=file_cache_options.keep_outdated,
logger=logging.getLogger("cmk.base.inventory"),
Expand Down Expand Up @@ -2918,8 +2928,10 @@ def mode_inventorize_marked_hosts(options: Mapping[str, object]) -> None:
config.load()
config_cache = config.get_config_cache()
parser = CMKParser(
config_cache,
config_cache.parser_factory(),
checking_sections=lambda hostname: config_cache.make_checking_sections(
hostname, selected_sections=NO_SELECTION
),
selected_sections=NO_SELECTION,
keep_outdated=file_cache_options.keep_outdated,
logger=logging.getLogger("cmk.base.inventory"),
Expand Down
6 changes: 3 additions & 3 deletions cmk/base/sources/_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# conditions defined in the file COPYING, which is part of this source code package.

import logging
from collections.abc import Sequence
from collections.abc import Iterable, Sequence
from pathlib import Path
from typing import Protocol

Expand All @@ -25,7 +25,7 @@ def make_snmp_parser(
hostname: HostName,
section_store: SectionStore[SNMPRawDataElem],
*,
checking_sections: frozenset[SectionName],
checking_sections: Iterable[SectionName],
keep_outdated: bool,
logger: logging.Logger,
) -> Parser: ...
Expand All @@ -46,7 +46,7 @@ def make_parser(
fetcher_type: FetcherType,
*,
# Always from NO_SELECTION.
checking_sections: frozenset[SectionName],
checking_sections: Iterable[SectionName],
persisted_section_dir: Path,
keep_outdated: bool,
logger: logging.Logger,
Expand Down
4 changes: 3 additions & 1 deletion tests/unit/cmk/base/test_discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -1331,8 +1331,10 @@ def test_commandline_discovery(monkeypatch: MonkeyPatch) -> None:
with current_host(testhost):
file_cache_options = FileCacheOptions()
parser = CMKParser(
config_cache,
config_cache.parser_factory(),
checking_sections=lambda hostname: config_cache.make_checking_sections(
hostname, selected_sections=NO_SELECTION
),
selected_sections=NO_SELECTION,
keep_outdated=file_cache_options.keep_outdated,
logger=logging.getLogger("tests"),
Expand Down

0 comments on commit f8294f5

Please sign in to comment.