Example #1
0
def create():
    if not g.user.supervisor:
        return json_response({"errors":["You don't have permissions to create new users"]})

    from app.models import User
    user_attrs = dict([(k, v) for k, v in request.json.items() if k in User.FIELDS])
    if "password_hash" in user_attrs:
        del(user_attrs["password_hash"])

    try:
        passwd = request.json["password_raw"]
        passwd_confirm = request.json["password_raw_confirm"]
        if passwd != passwd_confirm:
            return json_response({"errors":["Passwords don't match"]}, 400)
        user_attrs["password_raw"] = passwd
    except KeyError:
        pass

    try:
        existing = User.get(user_attrs["username"])
        if existing is not None:
            return json_response({"errors":["User with such username already exists"]}, 409)
    except KeyError:
        return json_response({"errors":["You should provide username"]}, 400)

    new_user = User(**user_attrs)
    try:
        new_user.save()
    except Exception as e:
        return json_exception(e, 500)

    return json_response({"data": new_user.to_dict()})
Example #2
0
def set_children(group_id):
    group = _get_group_by_id(group_id)
    if group is None:
        return json_response({ "errors": ["Group not found"] }, 404)
    # TODO: check permissions!
    orig = group.child_ids
    upd = request.json["child_ids"]
    try:
        upd = [ObjectId(x) for x in upd]
    except InvalidId as e:
        return json_exception(e, 400)
    d =  diff(orig, upd)
    exs = []
    for item in d.remove:
        try:
            group.remove_child(item)
        except Exception as e:
            exs.append(e)
    for item in d.add:
        try:
            group.add_child(item)
        except Exception as e:
            exs.append(e)
    if len(exs) > 0:
        return json_response({ "errors": ["%s: %s" % (x.__class__.__name__, x.message) for x in exs] })
    else:
        if "_fields" in request.values:
            fields = request.values["_fields"].split(",")
        else:
            fields = None
        return json_response({ "data": group.to_dict(fields), "status": "ok" })
Example #3
0
def create():
    from app.models import Host
    hosts_attrs = dict(
        [x for x in request.json.items() if x[0] in Host.FIELDS])

    if "fqdn_pattern" in request.json:
        if "fqdn" in hosts_attrs:
            return json_response({
                "errors": [
                    "fqdn field is not allowed due to fqdn_pattern param presence"
                ]
            })
        try:
            hostnames = list(expand_pattern(request.json["fqdn_pattern"]))
        except Exception as e:
            return json_exception(e)
    else:
        hostnames = [hosts_attrs["fqdn"]]
        del (hosts_attrs["fqdn"])

    try:
        hosts_attrs["group_id"] = ObjectId(hosts_attrs["group_id"])
    except (KeyError, InvalidId):
        hosts_attrs["group_id"] = None

    try:
        hosts_attrs["datacenter_id"] = ObjectId(hosts_attrs["datacenter_id"])
    except (KeyError, InvalidId):
        hosts_attrs["datacenter_id"] = None

    for fqdn in hostnames:
        attrs = hosts_attrs
        attrs["fqdn"] = fqdn
        host = Host(**attrs)
        try:
            host.save()
        except Exception as e:
            return json_exception(e, 500)

    hosts = Host.find({"fqdn": {"$in": list(hostnames)}})
    try:
        data = paginated_data(hosts.sort("fqdn"))
    except AttributeError as e:
        return json_exception(e, 500)
    return json_response(data, 201)
Example #4
0
def set_parent(dc_id):
    dc = _get_dc_by_id(dc_id)
    if dc is None:
        return json_response({"errors": ["Datacenter not found"]}, 404)
    # TODO: check permissions!
    parent_id = request.json.get("parent_id")
    if dc.parent:
        try:
            dc.unset_parent()
        except Exception as e:
            return json_exception(e, 500)
    if parent_id is not None:
        try:
            parent_id = resolve_id(parent_id)
            dc.set_parent(parent_id)
        except Exception as e:
            return json_exception(e, 500)
    return json_response({"data": dc.to_dict()})
Example #5
0
def delete(group_id):
    group = _get_group_by_id(group_id)
    if group is None:
        return json_response({ "errors": ["Group not found"] }, 404)
    # TODO: check permissions!
    try:
        group.destroy()
    except Exception as e:
        return json_exception(e, 500)
    return json_response({ "data": group.to_dict() })
Example #6
0
def delete(dc_id):
    dc = _get_dc_by_id(dc_id)
    if dc is None:
        return json_response({"errors": ["Datacenter not found"]}, 404)
    # TODO: check permissions!
    try:
        dc.destroy()
    except Exception as e:
        return json_exception(e, 500)
    return json_response({"data": dc.to_dict()})
Example #7
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)
Example #8
0
def set_members(id):
    from app.models import Project, User
    from bson.objectid import ObjectId, InvalidId

    project = Project.get(id)

    if project is None:
        return json_response({"errors": ["Project not found"]}, 404)
    if not project.member_list_modification_allowed:
        return json_response(
            {
                "errors": [
                    "You don't have permissions to modify the project member list"
                ]
            }, 403)
    if not "member_ids" in request.json:
        return json_response(
            {"errors": ["You should provide member_ids field"]}, 400)
    if type(request.json["member_ids"]) != list:
        return json_response({"errors": ["Invalid member_ids field type"]},
                             400)

    try:
        member_ids = [ObjectId(x) for x in request.json["member_ids"]]
    except InvalidId as e:
        return json_exception(e, 500)

    failed_ids = []
    for member_id in member_ids:
        member = User.get(member_id)
        if member is None:
            failed_ids.append(member_id)

    if len(failed_ids) > 0:
        return json_response({
            "errors": [
                "Users not found with the following ids: %s" %
                ", ".join([str(x) for x in failed_ids])
            ]
        })

    if project.owner_id in member_ids:
        member_ids.remove(project.owner_id)

    project.member_ids = member_ids
    project.save()
    if "_fields" in request.values:
        fields = request.values["_fields"].split(",")
    else:
        fields = None

    return json_response({"data": project.to_dict(fields), "status": "ok"})
Example #9
0
def create():
    from app.models import Datacenter
    dc_attrs = dict(
        [x for x in request.json.items() if x[0] in Datacenter.FIELDS])
    dc = Datacenter(**dc_attrs)
    # TODO: check permissions!
    try:
        dc.save()
    except Exception as e:
        return json_exception(e, 500)
    if "parent_id" in dc_attrs and dc_attrs["parent_id"]:
        return set_parent(dc._id)
    return json_response({"data": dc.to_dict()}, 201)
Example #10
0
def update(dc_id):
    dc = _get_dc_by_id(dc_id)
    if dc is None:
        return json_response({"errors": ["Datacenter not found"]}, 404)
    # TODO: check permissions!
    try:
        dc.update(request.json)
    except Exception as e:
        return json_exception(e, 500)
    if "parent_id" in request.json:
        parent_id = resolve_id(request.json["parent_id"])
        if parent_id != dc.parent_id:
            return set_parent(dc._id)
    return json_response({"data": dc.to_dict()})
Example #11
0
def update(group_id):
    group = _get_group_by_id(group_id)
    if group is None:
        return json_response({ "errors": ["Group not found"] }, 404)
    # TODO: check permissions!
    try:
        group.update(request.json)
    except Exception as e:
        return json_exception(e, 500)
    if "_fields" in request.values:
        fields = request.values["_fields"].split(",")
    else:
        fields = None
    return json_response({ "data": group.to_dict(fields) })
Example #12
0
def create():
    from app.models import Project
    data = clear_aux_fields(request.json)
    owner_id = g.user._id
    project = Project(name=data.get("name"),
                      email=data.get("email"),
                      root_email=data.get("root_email"),
                      description=data.get('description'),
                      owner_id=owner_id)
    try:
        project.save()
    except Exception as e:
        return json_exception(e, 500)
    return json_response({"data": project.to_dict()})
Example #13
0
def update(user_id):
    from app.models import User
    user = User.get(user_id)
    if user is None:
        return json_response({"errors":["User not found"]}, 404)
    if not user.modification_allowed:
        return json_response({"errors":["You don't have permissions to modificate this user"]}, 403)

    user_attrs = dict([(k, v) for k, v in request.json.items() if k in User.FIELDS])
    try:
        user.update(user_attrs)
    except Exception as e:
        return json_exception(e, 500)

    return json_response({"data":user.to_dict()})
Example #14
0
def delete(user_id):
    from app.models import User
    user = User.get(user_id)

    if user is None:
        return json_response({"errors":["User not found"]}, 404)
    if not g.user.supervisor:
        return json_response({"errors":["You don't have permissions to delete this user"]}, 403)

    try:
        user.destroy()
    except Exception as e:
        return json_exception(e, 500)

    return json_response({"data":user.to_dict()})
Example #15
0
def delete(id):
    from app.models import Project
    id = resolve_id(id)
    project = Project.find_one({ "$or": [
        { "_id": id },
        { "name": id }
    ] })
    if project is None:
        return json_response({ "errors": [ "Project not found" ]}, 404)
    # TODO check user supervisor privileges
    try:
        project.destroy()
    except Exception as e:
        return json_exception(e, 400)
    return json_response({ "data": project.to_dict(), "status": "deleted" })
Example #16
0
def create():
    from app.models import Group
    group_attrs = dict(
        [x for x in request.json.items() if x[0] in Group.FIELDS])
    group_attrs["project_id"] = ObjectId(group_attrs["project_id"])
    # TODO: check permissions!
    group = Group(**group_attrs)
    try:
        group.save()
    except Exception as e:
        return json_exception(e, 500)
    if "_fields" in request.values:
        fields = request.values["_fields"].split(",")
    else:
        fields = None
    return json_response({"data": group.to_dict(fields)}, 201)
Example #17
0
def update(id):
    from app.models import Project
    data = clear_aux_fields(request.json)
    # TODO check user ownership of project
    id = resolve_id(id)
    project = Project.find_one({ "$or": [
        { "_id": id },
        { "name": id }
    ] })
    if project is None:
        return json_response({ "errors": [ "Project not found" ]}, 404)
    try:
        project.update(data)
    except Exception as e:
        return json_exception(e, 500)
    return json_response({ "data": project.to_dict(), "status": "updated" })
Example #18
0
def delete(id):
    from app.models import Project

    project = Project.get(id)

    if project is None:
        return json_response({"errors": ["Project not found"]}, 404)
    if not project.modification_allowed:
        return json_response(
            {"errors": ["You don't have permissions to modify the project"]},
            403)
    try:
        project.destroy()
    except Exception as e:
        return json_exception(e, 400)
    return json_response({"data": project.to_dict(), "status": "deleted"})
Example #19
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()})
Example #20
0
def update(id):
    from app.models import Project
    data = clear_aux_fields(request.json)

    project = Project.get(id)

    if project is None:
        return json_response({"errors": ["Project not found"]}, 404)
    if not project.modification_allowed:
        return json_response(
            {"errors": ["You don't have permissions to modify the project"]},
            403)
    try:
        project.update(data)
    except Exception as e:
        return json_exception(e, 500)
    return json_response({"data": project.to_dict(), "status": "updated"})
Example #21
0
def create():
    from app.models import Group
    group_attrs = dict([x for x in request.json.items() if x[0] in Group.FIELDS])
    if "project_id" not in group_attrs or group_attrs["project_id"] is None:
        return json_response({ "errors": [ "No project provided for the group" ] }, 400)
    try:
        group_attrs["project_id"] = ObjectId(group_attrs["project_id"])
    except InvalidId as e:
        return json_response({ "errors": [ "Invalid project_id provided" ] }, 400)
    # TODO: check permissions!
    group = Group(**group_attrs)
    try:
        group.save()
    except Exception as e:
        return json_exception(e, 500)
    if "_fields" in request.values:
        fields = request.values["_fields"].split(",")
    else:
        fields = None
    return json_response({ "data": group.to_dict(fields) }, 201)
Example #22
0
def set_hosts(group_id):
    from app.models import Host
    group = _get_group_by_id(group_id)
    if group is None:
        return json_response({"errors": ["Group not found"]}, 404)
    # TODO: check permissions!
    orig = group.host_ids
    upd = request.json["host_ids"]
    try:
        upd = [ObjectId(x) for x in upd]
    except InvalidId as e:
        return json_exception(e, 400)
    d = diff(orig, upd)
    exs = []
    for item in d.remove:
        try:
            h = Host.find_one({"_id": item})
            if h is not None:
                h.group_id = None
                h.save()
        except Exception as e:
            exs.append(e)
    for item in d.add:
        try:
            h = Host.find_one({"_id": item})
            if h is not None:
                h.group_id = group._id
                h.save()
        except Exception as e:
            exs.append(e)
    if len(exs) > 0:
        return json_response({
            "errors":
            ["%s: %s" % (x.__class__.__name__, x.message) for x in exs]
        })
    else:
        if "_fields" in request.values:
            fields = request.values["_fields"].split(",")
        else:
            fields = None
        return json_response({"data": group.to_dict(fields), "status": "ok"})
Example #23
0
def show(host_id=None):
    from app.models import Host
    if host_id is None:
        query = {}
        if "_filter" in request.values:
            name_filter = request.values["_filter"]
            if len(name_filter) >= 2:
                query["fqdn"] = {"$regex": "^%s" % name_filter}
        if "group_id" in request.values:
            group_id = resolve_id(request.values["group_id"])
            query["group_id"] = group_id
        hosts = Host.find(query)
    else:
        host_id = resolve_id(host_id)
        hosts = Host.find({"$or": [{"_id": host_id}, {"fqdn": host_id}]})
        if hosts.count() == 0:
            return json_response({"errors": ["Host not found"]}, 404)
    try:
        data = paginated_data(hosts.sort("fqdn"))
    except AttributeError as e:
        return json_exception(e, 500)
    return json_response(data)
Example #24
0
def show(user_id=None):
    from app.models import User
    if user_id is None:
        query = {}
        if "_filter" in request.values:
            name_filter = request.values["_filter"]
            if len(name_filter) >= 2:
                query["username"] = { "$regex": "^%s" % name_filter }
        users = User.find(query)
    else:
        user_id = resolve_id(user_id)
        users = User.find({
            "$or": [
                { "_id": user_id },
                { "username": user_id }
            ]
        })
        if users.count() == 0:
            return json_response({"errors":["User not found"]}, 404)
    try:
        data = paginated_data(users.sort("username"))
    except AttributeError as e:
        return json_exception(e, 500)
    return json_response(data)