Skip to content

Commit ba73489

Browse files
LarsMichelsenJenkins
authored andcommitted
watolib: pass the page context into WatoMode constructors
WatoMode.__init__ now takes the PageContext alongside the edition and stores it as self._ctx, making the request's config and request objects an explicit constructor dependency rather than implicit reads of the global proxies. The page handler and every mode construction site thread the context through; modes with custom constructors (key management, simple config modes, backup, OTel, DCD, ...) forward it to super().__init__. The base mode stays generic and does not depend on the folder tree: modes that need the tree keep building it themselves. The two Setup search match-item generators that build a mode outside a page request construct a PageContext from active_config at call time. CMK-35767 Change-Id: I5950c99c8b213fab8a4d11e3a35c6543d9530d60
1 parent 10d8774 commit ba73489

44 files changed

Lines changed: 323 additions & 203 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

cmk/gui/background_job/wato/_modes.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ def __init__(self, edition: Edition) -> None:
202202

203203
@override
204204
def handle_page(self, ctx: PageContext) -> None:
205-
self.action()
205+
self.action(ctx)
206206
super().handle_page(ctx)
207207

208208
@override
@@ -231,8 +231,8 @@ def _show_details_page(self, job_id: str) -> BackgroundStatusSnapshot | None:
231231
job_manager.show_job_details_from_snapshot(job_snapshot)
232232
return job_snapshot
233233

234-
def action(self) -> None:
235-
job_details_page = ModeBackgroundJobDetails(self._edition)
234+
def action(self, ctx: PageContext) -> None:
235+
job_details_page = ModeBackgroundJobDetails(self._edition, ctx)
236236
action_handler = ActionHandler(job_details_page.breadcrumb())
237237
action_handler.handle_actions()
238238
if action_handler.did_delete_job():

cmk/gui/bi/_config.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -219,8 +219,8 @@ def is_show_more(self) -> bool:
219219

220220

221221
class ABCBIMode(WatoMode):
222-
def __init__(self, edition: Edition) -> None:
223-
super().__init__(edition)
222+
def __init__(self, edition: Edition, ctx: PageContext) -> None:
223+
super().__init__(edition, ctx)
224224
self._bi_packs = get_cached_bi_packs()
225225
self._bi_pack = None
226226

@@ -508,8 +508,8 @@ def _vs_pack(self) -> Dictionary:
508508

509509

510510
class ModeBIPacks(ABCBIMode):
511-
def __init__(self, edition: Edition) -> None:
512-
super().__init__(edition)
511+
def __init__(self, edition: Edition, ctx: PageContext) -> None:
512+
super().__init__(edition, ctx)
513513
self._contact_group_names = load_contact_group_information()
514514

515515
@classmethod
@@ -727,8 +727,8 @@ def mode_url(cls, **kwargs: str) -> str: ...
727727
def mode_url(cls, **kwargs: str) -> str:
728728
return super().mode_url(**kwargs)
729729

730-
def __init__(self, edition: Edition) -> None:
731-
super().__init__(edition)
730+
def __init__(self, edition: Edition, ctx: PageContext) -> None:
731+
super().__init__(edition, ctx)
732732
self._view_type = request.var("view", "list")
733733

734734
@classmethod
@@ -1226,8 +1226,8 @@ def name(cls) -> str:
12261226
def static_permissions() -> Collection[PermissionName]:
12271227
return ["bi_rules"]
12281228

1229-
def __init__(self, edition: Edition) -> None:
1230-
super().__init__(edition)
1229+
def __init__(self, edition: Edition, ctx: PageContext) -> None:
1230+
super().__init__(edition, ctx)
12311231
self._rule_id = request.get_str_input("id")
12321232
self._new = self._rule_id is None
12331233

@@ -1828,8 +1828,8 @@ def name(cls) -> str:
18281828
def static_permissions() -> Collection[PermissionName]:
18291829
return ["bi_rules"]
18301830

1831-
def __init__(self, edition: Edition) -> None:
1832-
super().__init__(edition)
1831+
def __init__(self, edition: Edition, ctx: PageContext) -> None:
1832+
super().__init__(edition, ctx)
18331833
aggr_id = request.get_str_input_mandatory("id", "")
18341834
clone_id = request.get_str_input_mandatory("clone", "")
18351835
self._new = False
@@ -2598,8 +2598,8 @@ def static_permissions() -> Collection[PermissionName]:
25982598
def parent_mode(cls) -> type[WatoMode] | None:
25992599
return ModeBIPacks
26002600

2601-
def __init__(self, edition: Edition) -> None:
2602-
super().__init__(edition)
2601+
def __init__(self, edition: Edition, ctx: PageContext) -> None:
2602+
super().__init__(edition, ctx)
26032603
self._rule_id = request.get_str_input_mandatory("id")
26042604
if not (rule_tree_bi_pack := self._bi_packs.get_pack_of_rule(self._rule_id)):
26052605
raise MKUserError("id", _("This BI rule does not exist"))

cmk/gui/key_mgmt.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
PageMenuEntry,
3232
PageMenuTopic,
3333
)
34+
from cmk.gui.pages import PageContext
3435
from cmk.gui.table import table_element
3536
from cmk.gui.type_defs import ActionResult, IconNames, StaticIcon
3637
from cmk.gui.utils.csrf_token import check_csrf_token
@@ -55,8 +56,8 @@ class ModeKeyManagement(WatoMode[object]):
5556
upload_mode = "upload_key"
5657
download_mode = "download_key"
5758

58-
def __init__(self, edition: Edition, key_store: KeypairStore) -> None:
59-
super().__init__(edition)
59+
def __init__(self, edition: Edition, ctx: PageContext, key_store: KeypairStore) -> None:
60+
super().__init__(edition, ctx)
6061
self.key_store = key_store
6162

6263
@override
@@ -206,8 +207,8 @@ def page(self, config: Config) -> None:
206207
class ModeEditKey(WatoMode[object]):
207208
back_mode: str
208209

209-
def __init__(self, edition: Edition, key_store: KeypairStore) -> None:
210-
super().__init__(edition)
210+
def __init__(self, edition: Edition, ctx: PageContext, key_store: KeypairStore) -> None:
211+
super().__init__(edition, ctx)
211212
self._minlen = 12
212213
self.key_store = key_store
213214

@@ -309,8 +310,8 @@ def _passphrase_help(self) -> str:
309310
class ModeUploadKey(WatoMode[object]):
310311
back_mode: str
311312

312-
def __init__(self, edition: Edition, key_store: KeypairStore) -> None:
313-
super().__init__(edition)
313+
def __init__(self, edition: Edition, ctx: PageContext, key_store: KeypairStore) -> None:
314+
super().__init__(edition, ctx)
314315
self.key_store = key_store
315316

316317
@override
@@ -479,8 +480,8 @@ def _passphrase_help(self) -> str:
479480
class ModeDownloadKey(WatoMode[object]):
480481
back_mode: str
481482

482-
def __init__(self, edition: Edition, key_store: KeypairStore) -> None:
483-
super().__init__(edition)
483+
def __init__(self, edition: Edition, ctx: PageContext, key_store: KeypairStore) -> None:
484+
super().__init__(edition, ctx)
484485
self.key_store = key_store
485486

486487
@override

cmk/gui/mkeventd/wato.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
PageMenuSearch,
6363
PageMenuTopic,
6464
)
65+
from cmk.gui.pages import PageContext
6566
from cmk.gui.permissions import Permission, PermissionRegistry
6667
from cmk.gui.rule_specs.legacy_converter import convert_dictionary_formspec_to_valuespec
6768
from cmk.gui.search.matchers import (
@@ -305,7 +306,9 @@ def register(
305306
MatchItemGeneratorSettings(
306307
"event_console_settings",
307308
_("Event Console settings"),
308-
lambda: ModeEventConsoleSettings(edition),
309+
lambda: ModeEventConsoleSettings(
310+
edition, PageContext(config=active_config, request=request)
311+
),
309312
)
310313
)
311314

@@ -1548,13 +1551,13 @@ def generate(self, tree: FolderTree) -> None:
15481551

15491552

15501553
class ABCEventConsoleMode(WatoMode, abc.ABC):
1551-
def __init__(self, edition: Edition) -> None:
1554+
def __init__(self, edition: Edition, ctx: PageContext) -> None:
15521555
config_domain = config_domain_registry[EVENT_CONSOLE]
15531556
assert isinstance(config_domain, ConfigDomainEventConsole)
15541557
self._config_domain = config_domain
15551558
self._paths = ec.create_paths(cmk.utils.paths.omd_root)
15561559
self._rule_packs = list(ec.load_rule_packs(self._paths))
1557-
super().__init__(edition)
1560+
super().__init__(edition, ctx)
15581561

15591562
def _mib_dirs(self) -> Sequence[tuple[Path, str, bool]]:
15601563
# ASN1 MIB source directory candidates. Non existing dirs are ok.
@@ -3278,8 +3281,8 @@ def static_permissions() -> Collection[PermissionName]:
32783281
def parent_mode(cls) -> type[WatoMode] | None:
32793282
return ModeEventConsoleRulePacks
32803283

3281-
def __init__(self, edition: Edition) -> None:
3282-
super().__init__(edition)
3284+
def __init__(self, edition: Edition, ctx: PageContext) -> None:
3285+
super().__init__(edition, ctx)
32833286

32843287
self._default_values = self._config_domain.default_globals()
32853288
self._current_settings = dict(load_configuration_settings())
@@ -3418,8 +3421,8 @@ def static_permissions() -> Collection[PermissionName]:
34183421
def parent_mode(cls) -> type[WatoMode] | None:
34193422
return ModeEventConsoleSettings
34203423

3421-
def __init__(self, edition: Edition) -> None:
3422-
super().__init__(edition)
3424+
def __init__(self, edition: Edition, ctx: PageContext) -> None:
3425+
super().__init__(edition, ctx)
34233426
self._need_restart = None
34243427

34253428
@override

cmk/gui/oauth2_connections/wato/_modes.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
PageMenuSearch,
3535
PageMenuTopic,
3636
)
37+
from cmk.gui.pages import PageContext
3738
from cmk.gui.site_config import site_is_local
3839
from cmk.gui.table import Table
3940
from cmk.gui.type_defs import ActionResult, IconNames, PermissionName, StaticIcon
@@ -477,10 +478,14 @@ def _connector_type(cls) -> str | None:
477478
return None
478479

479480
def __init__(
480-
self, edition: Edition, mode_type: SimpleModeType[OAuth2Connection] | None = None
481+
self,
482+
edition: Edition,
483+
ctx: PageContext,
484+
mode_type: SimpleModeType[OAuth2Connection] | None = None,
481485
) -> None:
482486
super().__init__(
483487
edition,
488+
ctx,
484489
mode_type=mode_type or OAuth2ModeType(),
485490
store=OAuth2ConnectionsConfigFile(),
486491
)
@@ -663,8 +668,8 @@ def _delete_passwords(
663668

664669

665670
class ModeMicrosoftEntraIdConnections(ModeOAuth2Connections):
666-
def __init__(self, edition: Edition) -> None:
667-
super().__init__(edition, mode_type=MicrosoftEntraIdModeType())
671+
def __init__(self, edition: Edition, ctx: PageContext) -> None:
672+
super().__init__(edition, ctx, mode_type=MicrosoftEntraIdModeType())
668673

669674
@classmethod
670675
@override
@@ -693,10 +698,14 @@ def name(cls) -> str:
693698
return "edit_oauth2_connection"
694699

695700
def __init__(
696-
self, edition: Edition, mode_type: SimpleModeType[OAuth2Connection] | None = None
701+
self,
702+
edition: Edition,
703+
ctx: PageContext,
704+
mode_type: SimpleModeType[OAuth2Connection] | None = None,
697705
) -> None:
698706
super().__init__(
699707
edition,
708+
ctx,
700709
mode_type=mode_type or OAuth2ModeType(),
701710
store=OAuth2ConnectionsConfigFile(),
702711
)
@@ -829,8 +838,8 @@ def page(self, config: Config, form_name: str = "edit") -> None:
829838

830839

831840
class ModeCreateMicrosoftEntraIdConnection(ModeCreateOAuth2Connection):
832-
def __init__(self, edition: Edition) -> None:
833-
super().__init__(edition, mode_type=MicrosoftEntraIdModeType())
841+
def __init__(self, edition: Edition, ctx: PageContext) -> None:
842+
super().__init__(edition, ctx, mode_type=MicrosoftEntraIdModeType())
834843

835844
@classmethod
836845
@override

cmk/gui/wato/_search_permissions.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
class SetupPermissionsHandler:
2121
def __init__(self, edition: Edition, ctx: PageContext) -> None:
2222
self._edition = edition
23+
self._ctx = ctx
2324
self._config = ctx.config
2425
self._request = ctx.request
2526
self._category_permissions = {
@@ -70,7 +71,7 @@ def _check_page_handler(self, url: str) -> bool:
7071

7172
try:
7273
if mode:
73-
mode_registry[mode](self._edition).ensure_permissions()
74+
mode_registry[mode](self._edition, self._ctx).ensure_permissions()
7475
else:
7576
self._check_if_handling_page_triggers_exception(file_name)
7677
return True

cmk/gui/wato/page_handler.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ def page_handler(edition: Edition, ctx: PageContext) -> None:
7474
):
7575
raise MKGeneralException(_("Checkmk can only be configured on the managers central site."))
7676

77-
mode_instance = mode_registry.get(current_mode, ModeNotImplemented)(edition)
77+
mode_instance = mode_registry.get(current_mode, ModeNotImplemented)(edition, ctx)
7878
mode_instance.ensure_permissions()
7979

8080
display_options.load_from_html(request, html)

cmk/gui/wato/pages/_simple_modes.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
PageMenuSearch,
5959
PageMenuTopic,
6060
)
61+
from cmk.gui.pages import PageContext
6162
from cmk.gui.table import Table, table_element
6263
from cmk.gui.type_defs import ActionResult, IconNames, RenderMode, StaticIcon
6364
from cmk.gui.user_sites import activation_sites
@@ -180,7 +181,11 @@ class _SimpleWatoModeBase[T: Mapping[str, Any]](WatoMode):
180181
"""
181182

182183
def __init__(
183-
self, edition: Edition, mode_type: SimpleModeType[T], store: WatoSimpleConfigFile[T]
184+
self,
185+
edition: Edition,
186+
ctx: PageContext,
187+
mode_type: SimpleModeType[T],
188+
store: WatoSimpleConfigFile[T],
184189
) -> None:
185190
self._mode_type = mode_type
186191
self._store = store
@@ -189,7 +194,7 @@ def __init__(
189194
# to be set before it is executed. Therefore we execute the super constructor
190195
# here.
191196
# TODO: Make the _from_vars() mechanism more explicit
192-
super().__init__(edition)
197+
super().__init__(edition, ctx)
193198

194199
def _add_change(
195200
self,
@@ -416,12 +421,16 @@ class SimpleEditMode[T: Mapping[str, Any]](_SimpleWatoModeBase[T]):
416421
"""Base class for edit modes"""
417422

418423
def __init__(
419-
self, edition: Edition, mode_type: SimpleModeType[T], store: WatoSimpleConfigFile[T]
424+
self,
425+
edition: Edition,
426+
ctx: PageContext,
427+
mode_type: SimpleModeType[T],
428+
store: WatoSimpleConfigFile[T],
420429
):
421430
self._ident: str | None = None
422431
self._clone: str | None = None
423432
self._new: bool = True
424-
super().__init__(edition, mode_type, store)
433+
super().__init__(edition, ctx, mode_type, store)
425434

426435
def _vs_individual_elements(self) -> list[DictionaryEntry]:
427436
raise NotImplementedError

cmk/gui/wato/pages/activate_changes.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,8 @@ def name(cls) -> str:
152152
def static_permissions() -> Collection[PermissionName]:
153153
return ["discard"]
154154

155-
def __init__(self, edition: Edition) -> None:
156-
super().__init__(edition)
155+
def __init__(self, edition: Edition, ctx: PageContext) -> None:
156+
super().__init__(edition, ctx)
157157
self._changes = activate_changes.ActivateChanges()
158158
self._changes.load(list(activation_sites(active_config.sites)))
159159

@@ -400,8 +400,8 @@ def name(cls) -> str:
400400
def static_permissions() -> Collection[PermissionName]:
401401
return []
402402

403-
def __init__(self, edition: Edition) -> None:
404-
super().__init__(edition)
403+
def __init__(self, edition: Edition, ctx: PageContext) -> None:
404+
super().__init__(edition, ctx)
405405
self._changes = activate_changes.ActivateChanges()
406406
self._changes.load(list(activation_sites(active_config.sites)))
407407
self._license_usage_report_validity = get_license_usage_report_validity(

cmk/gui/wato/pages/analyze_configuration.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
PageMenuEntry,
3838
PageMenuTopic,
3939
)
40+
from cmk.gui.pages import PageContext
4041
from cmk.gui.table import Table, table_element
4142
from cmk.gui.type_defs import ActionResult, IconNames, PermissionName, StaticIcon
4243
from cmk.gui.user_sites import activation_sites
@@ -70,8 +71,8 @@ def name(cls) -> str:
7071
def static_permissions() -> Collection[PermissionName]:
7172
return ["analyze_config"]
7273

73-
def __init__(self, edition: Edition) -> None:
74-
super().__init__(edition)
74+
def __init__(self, edition: Edition, ctx: PageContext) -> None:
75+
super().__init__(edition, ctx)
7576
self._logger = logger.getChild("analyze-config")
7677
self._acks = self._load_acknowledgements()
7778

0 commit comments

Comments
 (0)