def _delete(self, request): site_mgmt = watolib.SiteManagementFactory().factory() all_sites = site_mgmt.load_sites() existing_site = all_sites.get(request["site_id"]) if existing_site and "configuration_hash" in request: validate_config_hash(request["configuration_hash"], existing_site) site_mgmt.delete_site(request["site_id"])
def _set_all(self, request): site_mgmt = watolib.SiteManagementFactory().factory() all_sites = site_mgmt.load_sites() if "configuration_hash" in request: validate_config_hash(request["configuration_hash"], all_sites) for site_id, site_config in request["sites"].items(): site_mgmt.validate_configuration(site_id, site_config, request["sites"]) site_mgmt.save_sites(prepare_raw_site_config(request["sites"]))
def _delete(self, request): folder_path = request["folder"] if not watolib.Folder.folder_exists(folder_path): raise MKUserError(None, _("Folder %s does not exist") % folder_path) folder = watolib.Folder.folder(folder_path) if "configuration_hash" in request: validate_config_hash(request["configuration_hash"], folder.attributes()) if folder.is_root(): raise MKUserError(None, _("Unable to delete root folder")) folder.parent().delete_subfolder(folder.name())
def _set(self, request): site_mgmt = watolib.SiteManagementFactory().factory() all_sites = site_mgmt.load_sites() existing_site = all_sites.get(request["site_id"]) if existing_site and "configuration_hash" in request: validate_config_hash(request["configuration_hash"], existing_site) site_mgmt.validate_configuration(request["site_id"], request["site_config"], all_sites) sites = prepare_raw_site_config({request["site_id"]: request["site_config"]}) all_sites.update(sites) site_mgmt.save_sites(all_sites)
def _set(self, request): tag_config_file = TagConfigFile() hosttags_config = cmk.utils.tags.TagConfig() hosttags_config.parse_config(tag_config_file.load_for_modification()) hosttags_dict = hosttags_config.get_dict_format() if "configuration_hash" in request: validate_config_hash(request["configuration_hash"], hosttags_dict) del request["configuration_hash"] changed_hosttags_config = cmk.utils.tags.TagConfig() changed_hosttags_config.parse_config(request) changed_hosttags_config.validate_config() self._verify_no_used_tags_missing(changed_hosttags_config) tag_config_file.save(changed_hosttags_config.get_dict_format()) watolib.add_change("edit-hosttags", _("Updated host tags through Web-API"))
def _edit(self, request): folder_path = request["folder"] if not watolib.Folder.folder_exists(folder_path): raise MKUserError(None, _("Folder %s does not exist") % folder_path) folder = watolib.Folder.folder(folder_path) if "configuration_hash" in request: validate_config_hash(request["configuration_hash"], folder.attributes()) folder_attributes = request.get("attributes", {}) if "alias" in folder_attributes: folder_alias = folder_attributes.pop("alias") or os.path.basename(folder_path) else: folder_alias = os.path.basename(folder_path) # Validates host and folder attributes, since there are no real folder attributes, at all... validate_host_attributes(folder_attributes, new=False) folder.edit(folder_alias, folder_attributes)
def _set(self, request): tag_config_file = TagConfigFile() hosttags_config = cmk.utils.tags.TagConfig() hosttags_config.parse_config(tag_config_file.load_for_modification()) hosttags_dict = hosttags_config.get_dict_format() if "configuration_hash" in request: validate_config_hash(request["configuration_hash"], hosttags_dict) del request["configuration_hash"] # Check for conflicts with existing configuration # Tags may be either specified grouped in a host/folder configuration, e.g agent/cmk-agent, # or specified as the plain id in rules. We need to check both variants.. used_tags = self._get_used_grouped_tags() used_tags.update(self._get_used_rule_tags()) changed_hosttags_config = cmk.utils.tags.TagConfig() changed_hosttags_config.parse_config(request) changed_hosttags_config.validate_config() new_tags = changed_hosttags_config.get_tag_ids() new_tags.update( changed_hosttags_config.get_tag_ids_with_group_prefix()) # Remove the builtin hoststags from the list of used_tags builtin_config = cmk.utils.tags.BuiltinTagConfig() used_tags.discard(builtin_config.get_tag_ids_with_group_prefix()) missing_tags = used_tags - new_tags if missing_tags: raise MKUserError( None, _("Unable to apply new hosttag configuration. The following tags " "are still in use, but not mentioned in the updated " "configuration: %s") % ", ".join(missing_tags)) tag_config_file.save(changed_hosttags_config.get_dict_format()) watolib.add_change("edit-hosttags", _("Updated host tags through Web-API"))
def _set(self, request): # Py2: This encoding here should be kept Otherwise and unicode encoded text will be written # into the configuration file with unknown side effects ruleset_name = ensure_str(request["ruleset_name"]) # Future validation, currently the rule API actions are admin only, so the check is pointless # may_edit_ruleset(ruleset_name) # Check if configuration hash has changed in the meantime ruleset_dict = self._get_ruleset_configuration(ruleset_name) if "configuration_hash" in request: validate_config_hash(request["configuration_hash"], ruleset_dict) # Check permissions of new rules and rules we are going to delete new_ruleset = request["ruleset"] folders_set_ruleset = set(new_ruleset.keys()) folders_obsolete_ruleset = set(ruleset_dict.keys()) - folders_set_ruleset for check_folders in [folders_set_ruleset, folders_obsolete_ruleset]: for folder_path in check_folders: if not watolib.Folder.folder_exists(folder_path): raise MKUserError(None, _("Folder %s does not exist") % folder_path) rule_folder = watolib.Folder.folder(folder_path) rule_folder.need_permission("write") tag_to_group_map = ruleset_matcher.get_tag_to_group_map(config.tags) # Verify all rules rule_vs = watolib.Ruleset(ruleset_name, tag_to_group_map).rulespec.valuespec for folder_path, rules in new_ruleset.items(): for rule in rules: value = rule["value"] try: rule_vs.validate_datatype(value, "test_value") rule_vs.validate_value(value, "test_value") except MKException as e: raise MKGeneralException("ERROR: %s. Affected Rule %r" % (str(e), rule)) # Add new rulesets for folder_path, rules in new_ruleset.items(): folder = watolib.Folder.folder(folder_path) new_ruleset = watolib.Ruleset(ruleset_name, tag_to_group_map) new_ruleset.from_config(folder, rules) folder_rulesets = watolib.FolderRulesets(folder) folder_rulesets.load() # TODO: This add_change() call should be made by the data classes watolib.add_change("edit-ruleset", _("Set ruleset '%s' for '%s' with %d rules") % ( new_ruleset.title(), folder.title(), len(rules), ), sites=folder.all_site_ids(), object_ref=new_ruleset.object_ref()) folder_rulesets.set(ruleset_name, new_ruleset) folder_rulesets.save() # Remove obsolete rulesets for folder_path in folders_obsolete_ruleset: folder = watolib.Folder.folder(folder_path) folder_rulesets = watolib.FolderRulesets(folder) folder_rulesets.load() new_ruleset = watolib.Ruleset(ruleset_name, tag_to_group_map) new_ruleset.from_config(folder, []) # TODO: This add_change() call should be made by the data classes watolib.add_change("edit-ruleset", _("Deleted ruleset '%s' for '%s'") % ( new_ruleset.title(), folder.title(), ), sites=folder.all_site_ids(), object_ref=new_ruleset.object_ref()) folder_rulesets.set(ruleset_name, new_ruleset) folder_rulesets.save()
def _set(self, request): # NOTE: This encoding here should be kept # Otherwise and unicode encoded text will be written into the # configuration file with unknown side effects ruleset_name = request["ruleset_name"].encode("utf-8") # Future validation, currently the rule API actions are admin only, so the check is pointless # may_edit_ruleset(ruleset_name) # Check if configuration hash has changed in the meantime ruleset_dict = self._get_ruleset_configuration(ruleset_name) if "configuration_hash" in request: validate_config_hash(request["configuration_hash"], ruleset_dict) # Check permissions of new rules and rules we are going to delete new_ruleset = request["ruleset"] folders_set_ruleset = set(new_ruleset.keys()) folders_obsolete_ruleset = set( ruleset_dict.keys()) - folders_set_ruleset for check_folders in [folders_set_ruleset, folders_obsolete_ruleset]: for folder_path in check_folders: if not watolib.Folder.folder_exists(folder_path): raise MKUserError( None, _("Folder %s does not exist") % folder_path) rule_folder = watolib.Folder.folder(folder_path) rule_folder.need_permission("write") # Verify all rules rule_vs = watolib.Ruleset(ruleset_name).rulespec.valuespec for folder_path, rules in new_ruleset.items(): for rule in rules: if "negate" in rule: continue # ugly, rules with a boolean value have a different representation value = rule["value"] try: rule_vs.validate_datatype(value, "test_value") rule_vs.validate_value(value, "test_value") except MKException as e: # TODO: The abstract MKException should never be instanciated directly # Change this call site and make MKException an abstract base class raise MKException("ERROR: %s. Affected Rule %r" % (str(e), rule)) # Add new rulesets for folder_path, rules in new_ruleset.items(): folder = watolib.Folder.folder(folder_path) new_ruleset = watolib.Ruleset(ruleset_name) new_ruleset.from_config(folder, rules) folder_rulesets = watolib.FolderRulesets(folder) folder_rulesets.load() # TODO: This add_change() call should be made by the data classes watolib.add_change("edit-ruleset", _("Set ruleset '%s' for '%s' with %d rules") % ( new_ruleset.title(), folder.title(), len(rules), ), sites=folder.all_site_ids()) folder_rulesets.set(ruleset_name, new_ruleset) folder_rulesets.save() # Remove obsolete rulesets for folder_path in folders_obsolete_ruleset: folder = watolib.Folder.folder(folder_path) folder_rulesets = watolib.FolderRulesets(folder) folder_rulesets.load() # TODO: This add_change() call should be made by the data classes watolib.add_change("edit-ruleset", _("Deleted ruleset '%s' for '%s'") % ( watolib.Ruleset(ruleset_name).title(), folder.title(), ), sites=folder.all_site_ids()) new_ruleset = watolib.Ruleset(ruleset_name) new_ruleset.from_config(folder, []) folder_rulesets.set(ruleset_name, new_ruleset) folder_rulesets.save()