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.from_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.from_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): # 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( # pylint: disable= six-ensure-str-bin-call 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()