def _edit(self, request): hostname = request.get("hostname") attributes = request.get("attributes", {}) unset_attribute_names = request.get("unset_attributes", []) cluster_nodes = request.get("nodes") check_hostname(hostname, should_exist=True) host = watolib.Host.host(hostname) # Deprecated, but still supported # Nodes are now specified in an extra key if ".nodes" in attributes: cluster_nodes = attributes[".nodes"] del attributes[".nodes"] validate_host_attributes(attributes, new=False) # Update existing attributes. Add new, remove unset_attributes current_attributes = host.attributes().copy() for attrname in unset_attribute_names: if attrname in current_attributes: del current_attributes[attrname] current_attributes.update(attributes) if not cluster_nodes: cluster_nodes = host.cluster_nodes() if cluster_nodes: cluster_nodes = list(map(str, cluster_nodes)) host.edit(current_attributes, cluster_nodes)
def _add(self, request): folder_path = request["folder"] watolib.check_wato_foldername(None, os.path.basename(folder_path), just_name=True) 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=True) # Check existance of parent folder, create it when configured create_parent_folders = bool( int(request.get("create_parent_folders", "1"))) if create_parent_folders or watolib.Folder.folder_exists( os.path.dirname(folder_path)): watolib.Folder.root_folder().create_missing_folders(folder_path) watolib.Folder.folder(folder_path).edit(folder_alias, folder_attributes) else: raise MKUserError(None, _("Unable to create parent folder(s)."))
def update_host(params): """Update a host""" hostname = params['hostname'] body = params['body'] attributes = body['attributes'] host = watolib.Host.host(hostname) # type: watolib.CREHost constructors.require_etag(constructors.etag_of_obj(host)) validate_host_attributes(attributes, new=False) host.update_attributes(attributes) return _serve_host(host)
def _validate(self, value): # Special keys: # - site -> validate against config.allsites().keys() # - tag_* -> validate_host_tags # - * -> validate against host_attribute_registry.keys() try: validate_host_attributes(value, new=True) if 'meta_data' in value: self.fail("attribute_forbidden", attribute='meta_data', value=value) except MKUserError as exc: raise ValidationError(str(exc))
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 _add(self, request): create_parent_folders_var = request.get( "create_parent_folders", request.get("create_folders", "1")) create_parent_folders = bool(int(create_parent_folders_var)) # Werk #10863: In 1.6 some hosts / rulesets were saved as unicode # strings. After reading the config into the GUI ensure we really # process the host names as str. TODO: Can be removed with Python 3. hostname = str(request.get("hostname")) folder_path = str(request.get("folder")) attributes = request.get("attributes", {}) cluster_nodes = request.get("nodes") check_hostname(hostname, should_exist=False) # Validate folder if folder_path not in ('', '/'): folders = folder_path.split("/") for foldername in folders: watolib.check_wato_foldername(None, foldername, just_name=True) else: folder_path = "" folders = [""] # Deprecated, but still supported # Nodes are now specified in an extra key if ".nodes" in attributes: cluster_nodes = attributes[".nodes"] del attributes[".nodes"] validate_host_attributes(attributes, new=True) # Create folder(s) if not watolib.Folder.folder_exists(folder_path): if not create_parent_folders: raise MKUserError(None, _("Unable to create parent folder(s).")) watolib.Folder.create_missing_folders(folder_path) # Add host if cluster_nodes: cluster_nodes = list(map(str, cluster_nodes)) watolib.Folder.folder(folder_path).create_hosts([(hostname, attributes, cluster_nodes)])
def create(params): body = params['body'] hostname = body['hostname'] folder_id = body['folder'] attributes = body.get('attributes', {}) cluster_nodes = None check_hostname(hostname, should_exist=False) validate_host_attributes(attributes, new=True) if folder_id == 'root': folder = watolib.Folder.root_folder() else: folder = load_folder(folder_id, status=400) folder.create_hosts([(hostname, attributes, cluster_nodes)]) host = watolib.Host.host(hostname) return _serve_host(host)
def _add(self, request, bake_hosts=True): create_parent_folders_var = request.get( "create_parent_folders", request.get("create_folders", "1") ) create_parent_folders = bool(int(create_parent_folders_var)) hostname = request["hostname"] folder_path = request.get("folder", "") attributes = request.get("attributes", {}) cluster_nodes = request.get("nodes") check_hostname(hostname, should_exist=False) # Validate folder if folder_path not in ("", "/"): folders = folder_path.split("/") for foldername in folders: watolib.check_wato_foldername(None, foldername, just_name=True) else: folder_path = "" folders = [""] # Deprecated, but still supported # Nodes are now specified in an extra key if ".nodes" in attributes: cluster_nodes = attributes[".nodes"] del attributes[".nodes"] validate_host_attributes(attributes, new=True) # Create folder(s) if not watolib.Folder.folder_exists(folder_path): if not create_parent_folders: raise MKUserError(None, _("Unable to create parent folder(s).")) watolib.Folder.create_missing_folders(folder_path) # Add host if cluster_nodes: cluster_nodes = list(map(str, cluster_nodes)) watolib.Folder.folder(folder_path).create_hosts( [(hostname, attributes, cluster_nodes)], bake_hosts=bake_hosts )