def test_call_exception_handling(mocker): hooks.register_builtin("bli", lambda: 1.0 / 0.0) hook3_mock = mocker.Mock() hooks.register("bli", hook3_mock) with pytest.raises(ZeroDivisionError, match="float division by zero"): hooks.call("bli") hook3_mock.assert_not_called()
def test_builtin_vs_plugin_hooks(): hooks.register_builtin("bla", lambda: True) assert hooks.registered("bla") == True hooks.register_from_plugin("blub", lambda: True) assert hooks.registered("blub") == True hooks.load_plugins(force=True) assert hooks.registered("bla") == True assert hooks.registered("blub") == False
def _register_hooks(): # TODO: Should we not execute this hook also when folders are modified? args: Sequence[Tuple[str, Callable]] = ( ("userdb-job", _auth_php._on_userdb_job), ("users-saved", lambda users: _auth_php._create_auth_file("users-saved", users)), ("roles-saved", lambda x: _auth_php._create_auth_file("roles-saved")), ("contactgroups-saved", lambda x: _auth_php._create_auth_file("contactgroups-saved")), ("activate-changes", lambda x: _auth_php._create_auth_file("activate-changes")), ) for name, func in args: _hooks.register_builtin(name, func)
def test_hook_registration(): assert hooks.hooks == {} # pre 1.6 API hooks.register("bla", lambda: True) assert hooks.get("bla")[0].is_builtin == False hooks.register_builtin("blub", lambda: True) hooks.register_from_plugin("blub", lambda: False) assert hooks.get("blub")[0].is_builtin == True assert hooks.get("blub")[1].is_builtin == False assert hooks.registered("bla") == True assert hooks.registered("blub") == True assert hooks.registered("bli") == False assert len(hooks.get("bla")) == 1 assert len(hooks.get("blub")) == 2 assert len(hooks.get("bli")) == 0
if not parent: raise MKUserError( None, _("You defined the non-existing host '%s' as a parent.") % parent_name) if host.site_id() != parent.site_id(): raise MKUserError( None, _("The parent '%s' is monitored on site '%s' while the host itself " "is monitored on site '%s'. Both must be monitored on the same site. Remember: The parent/child " "relation is used to describe the reachability of hosts by one monitoring daemon." ) % (parent_name, parent.site_id(), host.site_id())) hooks.register_builtin('validate-host', validate_host_parents) @host_attribute_registry.register class HostAttributeNetworkScan(ABCHostAttributeValueSpec): def name(self): return "network_scan" def may_edit(self): return config.user.may("wato.manage_hosts") def topic(self): return HostAttributeTopicNetworkScan def show_in_table(self): return False
# This function is currently only used by the background job # which does not have any request timeout set, just in case... raise except Exception as e: return SynchronizationResult(site_id, error_text="%s" % e, failed=True) # TODO: Why is the logger handed over here? The sync job could simply gather it's own def _handle_ldap_sync_finished(logger, profiles_to_synchronize, changes): _synchronize_profiles_to_sites(logger, profiles_to_synchronize) if changes and config.wato_enabled and not config.is_wato_slave_site(): add_change("edit-users", "<br>".join(changes), add_user=False) hooks.register_builtin("ldap-sync-finished", _handle_ldap_sync_finished) def push_user_profiles_to_site_transitional_wrapper(site, user_profiles): try: return push_user_profiles_to_site(site, user_profiles) except MKAutomationException as e: if "Invalid automation command: push-profiles" in "%s" % e: failed_info = [] for user_id, user in user_profiles.items(): result = _legacy_push_user_profile_to_site(site, user_id, user) if result is not True: failed_info.append(result) if failed_info: return "\n".join(failed_info)
if users is None: users = userdb.load_users() contactgroups = load_contact_group_information() groups = {} for gid, group in contactgroups.items(): if 'nagvis_maps' in group and group['nagvis_maps']: groups[gid] = group['nagvis_maps'] _create_php_file(callee, users, config.get_role_permissions(), groups) def _on_userdb_job(): # Working around the problem that the auth.php file needed for multisite based # authorization of external addons might not exist when setting up a new installation # This is a good place to replace old api based files in the future. if not _auth_php().exists() or _auth_php().stat().st_size == 0: _create_auth_file("page_hook") # TODO: Should we not execute this hook also when folders are modified? hooks.register_builtin('userdb-job', _on_userdb_job) hooks.register_builtin('users-saved', lambda users: _create_auth_file("users-saved", users)) hooks.register_builtin('roles-saved', lambda x: _create_auth_file("roles-saved")) hooks.register_builtin('contactgroups-saved', lambda x: _create_auth_file("contactgroups-saved")) hooks.register_builtin('activate-changes', lambda x: _create_auth_file("activate-changes"))
raise MKUserError( None, _("You defined the non-existing host '%s' as a parent.") % parent_name) if host.site_id() != parent.site_id(): raise MKUserError( None, _("The parent '%s' is monitored on site '%s' while the host itself " "is monitored on site '%s'. Both must be monitored on the same site. Remember: The parent/child " "relation is used to describe the reachability of hosts by one monitoring daemon." ) % (parent_name, parent.site_id(), host.site_id()), ) hooks.register_builtin("validate-host", validate_host_parents) @hooks.request_memoize() def _get_criticality_choices(): """Returns the current configuration of the tag_group criticality""" tags = cmk.utils.tags.TagConfig.from_config( watolib.TagConfigFile().load_for_reading()) criticality_group = tags.get_tag_group("criticality") if not criticality_group: return [] return criticality_group.get_tag_choices() @host_attribute_registry.register