Ejemplo n.º 1
0
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)
Ejemplo n.º 2
0
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)
Ejemplo n.º 3
0
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)
Ejemplo n.º 4
0
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)
Ejemplo n.º 5
0
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)
Ejemplo n.º 6
0
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)
Ejemplo n.º 7
0
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)
Ejemplo n.º 8
0
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)
Ejemplo n.º 9
0
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
Ejemplo n.º 10
0
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)
Ejemplo n.º 11
0
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(),
        ))
Ejemplo n.º 12
0
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)
Ejemplo n.º 13
0
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)
Ejemplo n.º 14
0
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)
Ejemplo n.º 15
0
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)
Ejemplo n.º 16
0
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(),
        ))
Ejemplo n.º 17
0
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
Ejemplo n.º 18
0
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()
Ejemplo n.º 19
0
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