def update_host(params): """Update a host""" host_name = params['host_name'] body = params['body'] new_attributes = body['attributes'] update_attributes = body['update_attributes'] remove_attributes = body['remove_attributes'] check_hostname(host_name, should_exist=True) host: watolib.CREHost = watolib.Host.host(host_name) constructors.require_etag(constructors.etag_of_obj(host)) if new_attributes: host.edit(new_attributes, None) if update_attributes: host.update_attributes(update_attributes) faulty_attributes = [] for attribute in remove_attributes: try: host.remove_attribute(attribute) except KeyError: faulty_attributes.append(attribute) if faulty_attributes: return problem( status=400, title="Some attributes were not removed", detail= f"The following attributes were not removed since they didn't exist: {', '.join(faulty_attributes)}", ) return _serve_host(host, False)
def delete(params): hostname = params['hostname'] check_hostname(hostname, should_exist=True) host = watolib.Host.host(hostname) constructors.require_etag(constructors.etag_of_obj(host)) host.folder().delete_hosts([host.name()]) return Response(status=204)
def update(params): """Update a folder """ folder = params['folder'] constructors.require_etag(constructors.etag_of_obj(folder)) post_body = params['body'] title = post_body['title'] replace_attributes = post_body['attributes'] update_attributes = post_body['update_attributes'] remove_attributes = post_body['remove_attributes'] attributes = folder.attributes().copy() if replace_attributes: attributes = replace_attributes if update_attributes: attributes.update(update_attributes) for attribute in remove_attributes: folder.remove_attribute(attribute) folder.edit(title, attributes) return _serve_folder(folder)
def update(params): """Update a folder """ folder = params['folder'] constructors.require_etag(constructors.etag_of_obj(folder)) post_body = params['body'] title = post_body['title'] replace_attributes = post_body.get('attributes') update_attributes = post_body.get('update_attributes') attributes = folder.attributes().copy() if replace_attributes: attributes = replace_attributes if update_attributes: attributes.update(update_attributes) # FIXME # You can't update the attributes without updating the title, so the title is mandatory. # This shouldn't be the case though. folder.edit(title, attributes) return _serve_folder(folder)
def update(params): """Update a folder Title and attributes can be updated, but there is no checking of the attributes done""" ident = params['ident'] folder = load_folder(ident, status=404) constructors.require_etag(constructors.etag_of_obj(folder)) post_body = params['body'] title = post_body['title'] replace_attributes = post_body.get('attributes') update_attributes = post_body.get('update_attributes') attributes = folder.attributes().copy() if replace_attributes: attributes = replace_attributes if update_attributes: attributes.update(update_attributes) # FIXME # You can't update the attributes without updating the title, so the title is mandatory. # This shouldn't be the case though. folder.edit(title, attributes) return _serve_folder(folder)
def update(params): 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)) host.update_attributes(attributes) return _serve_host(host)
def delete(params): """Delete a host""" host_name = params['host_name'] # Parameters can't be validated through marshmallow yet. check_hostname(host_name, should_exist=True) host = watolib.Host.host(host_name) constructors.require_etag(constructors.etag_of_obj(host)) host.folder().delete_hosts([host.name()]) return Response(status=204)
def update_host(params): """Update a host""" hostname = params['hostname'] body = params['body'] attributes = body['attributes'] host: watolib.CREHost = watolib.Host.host(hostname) constructors.require_etag(constructors.etag_of_obj(host)) validate_host_attributes(attributes, new=False) host.update_attributes(attributes) return _serve_host(host)
def _serve_folder( folder, profile=None, show_hosts=False, ): folder_json = _serialize_folder(folder, show_hosts) response = constructors.serve_json(folder_json, profile=profile) if not folder.is_root(): response.headers.add("ETag", constructors.etag_of_obj(folder).to_header()) return response
def update(params): """Update a folder Title and attributes can be updated, but there is no checking of the attributes done.""" ident = params['ident'] folder = load_folder(ident, status=404) constructors.require_etag(constructors.etag_of_obj(folder)) post_body = params['body'] title = post_body['title'] attributes = folder.attributes() folder.edit(title, attributes) return _serve_folder(folder)
def update_nodes(params): """Update the nodes of a cluster host""" host_name = params['host_name'] body = params['body'] nodes = body['nodes'] host: watolib.CREHost = watolib.Host.host(host_name) constructors.require_etag(constructors.etag_of_obj(host)) host.edit(host.attributes(), nodes) return constructors.serve_json( constructors.object_sub_property( domain_type='host_config', ident=host_name, name='nodes', value=host.cluster_nodes(), ))
def move(params): ident = params['ident'] folder = load_folder(ident, status=404) constructors.require_etag(constructors.etag_of_obj(folder)) dest = params['body']['destination'] if dest == 'root': dest_folder = watolib.Folder.root_folder() else: dest_folder = load_folder(dest, status=400) folder.parent().move_subfolder_to(folder, dest_folder) folder = load_folder(ident, status=500) return _serve_folder(folder)
def update_host(params): """Update a host""" host_name = params['host_name'] body = params['body'] new_attributes = body['attributes'] update_attributes = body['attributes'] check_hostname(host_name, should_exist=True) host: watolib.CREHost = watolib.Host.host(host_name) constructors.require_etag(constructors.etag_of_obj(host)) if new_attributes: host.edit(new_attributes, None) if update_attributes: host.update_attributes(update_attributes) return _serve_host(host)
def move(params): """Move a folder""" folder: watolib.CREFolder = params['folder'] folder_id = folder.id() constructors.require_etag(constructors.etag_of_obj(folder)) dest_folder: watolib.CREFolder = params['body']['destination'] try: folder.parent().move_subfolder_to(folder, dest_folder) except MKUserError as exc: raise ProblemException( title="Problem moving folder.", detail=exc.message, status=400, ) folder = fields.FolderField.load_folder(folder_id) return _serve_folder(folder)
def update(params): """Update a folder """ folder = params['folder'] constructors.require_etag(constructors.etag_of_obj(folder)) post_body = params['body'] title = post_body['title'] replace_attributes = post_body['attributes'] update_attributes = post_body['update_attributes'] remove_attributes = post_body['remove_attributes'] attributes = folder.attributes().copy() if replace_attributes: attributes = replace_attributes if update_attributes: attributes.update(update_attributes) faulty_attributes = [] for attribute in remove_attributes: try: folder.remove_attribute(attribute) except KeyError: faulty_attributes.append(attribute) if faulty_attributes: return problem( status=400, title="The folder was not updated", detail=f"The following attributes did not exist and could therefore" f"not be removed: {', '.join(faulty_attributes)}") folder.edit(title, attributes) return _serve_folder(folder)
def update_nodes(params): """Update the nodes of a cluster host""" host_name = params['host_name'] body = params['body'] nodes = body['nodes'] check_hostname(host_name, should_exist=True) for node in nodes: check_hostname(node, should_exist=True) host: watolib.CREHost = watolib.Host.host(host_name) if not host.is_cluster(): return problem(status=400, title="Trying to change nodes of a regular host.", detail="nodes can only be changed on cluster hosts.") constructors.require_etag(constructors.etag_of_obj(host)) host.edit(host.attributes(), nodes) return constructors.serve_json( constructors.object_sub_property( domain_type='host_config', ident=host_name, name='nodes', value=host.cluster_nodes(), ))
def _serve_folder(folder, profile=None): folder_json = _serialize_folder(folder) response = constructors.serve_json(folder_json, profile=profile) response.headers.add("ETag", constructors.etag_of_obj(folder).to_header()) return response
def _serve_host(host): response.set_data(json.dumps(serialize_host(host))) response.set_content_type('application/json') response.headers.add('ETag', constructors.etag_of_obj(host).to_header()) return response._get_current_object()
def _serve_host(host, effective_attributes=False): response = Response() response.set_data(json.dumps(serialize_host(host, effective_attributes))) response.set_content_type('application/json') response.headers.add('ETag', constructors.etag_of_obj(host).to_header()) return response