示例#1
0
def delete(host_id):
    from app.models import Host
    host = Host.get(host_id)

    if host is None:
        return json_response({"errors": ["Host not found"]}, 404)

    if not host.destruction_allowed:
        return json_response(
            {"errors": ["You don't have permissions to delete this host"]},
            403)

    try:
        host.destroy()
    except Exception as e:
        return json_exception(e, 500)
    return json_response({"data": host.to_dict()})
示例#2
0
def update(host_id):
    from app.models import Host
    host = Host.get(host_id)
    if host is None:
        return json_response({"errors": ["Host not found"]}, 404)

    if not host.modification_allowed:
        return json_response(
            {"errors": ["You don't have permissions to modify this host"]},
            403)

    from app.models import Host
    hosts_attrs = dict(
        [x for x in request.json.items() if x[0] in Host.FIELDS])

    if "group_id" in hosts_attrs and hosts_attrs["group_id"] is not None:
        try:
            hosts_attrs["group_id"] = ObjectId(hosts_attrs["group_id"])
        except InvalidId:
            hosts_attrs["group_id"] = None
    if "datacenter_id" in hosts_attrs and hosts_attrs[
            "datacenter_id"] is not None:
        try:
            hosts_attrs["datacenter_id"] = ObjectId(
                hosts_attrs["datacenter_id"])
        except InvalidId:
            hosts_attrs["datacenter_id"] = None

    try:
        host.update(hosts_attrs)
    except Exception as e:
        return json_exception(e, 500)
    if "_fields" in request.values:
        fields = request.values["_fields"].split(",")
    else:
        fields = None

    try:
        data = {"data": host.to_dict(fields)}
    except AttributeError as e:
        return json_exception(e, 500)
    return json_response(data)