예제 #1
0
def update_host(old_host_name):
    if not is_json_request(request):
        return abort(400)

    req = request.json
    if not is_valid_keys(req, ["host_name", "ip", "role"]):
        abort(400)

    try:
        role_maps = RoleMap.get_by_host_name(db.session.query, old_host_name)
        for r in role_maps:
            rm = RoleMap.get_by_host_name_and_role_name(db.session.query, old_host_name, r.role_name)
            db.session.delete(rm)
            db.session.flush()

        host = Host.get_one_by_host_name(db.session.query, old_host_name)
        host.host_name = req["host_name"]
        old_ip = host.ip
        host.ip = req["ip"]
        host.role = [RoleMap(role_name=role_name) for role_name in req["role"]]
        db.session.add(host)

        if req["ip"] != old_ip:
            ip = IP.get_one(db.session.query, req["ip"])
            ip.is_used = 1
            ip = IP.get_one(db.session.query, old_ip)
            ip.is_used = 0

        db.session.commit()
    except Exception as e:
        abort(500)
    return jsonify(result=[dict(message="OK", request="update host", payload=str(request.json))])
예제 #2
0
def update_role(old_role_name):
    if not is_json_request(request) or not is_valid_keys(request.json, ["role"]):
        abort(400)
    new_role_name = request.json["role"]

    try:
        role = Role.get_one(db.session.query, old_role_name)
        role.role_name = new_role_name
        db.session.commit()
    except Exception as e:
        abort(500)
    return jsonify(result=[dict(message="OK", request="update role", payload=str(request.json))])
예제 #3
0
def add_role():
    if not is_json_request(request) and not is_valid_keys(request.json, ["role"]):
        abort(400)

    role_name = request.json["role"]
    try:
        role = Role(role_name=role_name)
        db.session.add(role)
        db.session.commit()
    except Exception as e:
        abort(500)
    return jsonify(result=dict(message="OK", request="add role", payload=str(request.json)))
예제 #4
0
def add_host():
    if not is_json_request(request):
        return abort(400)

    req = request.json
    if not is_valid_keys(req, ["host_name", "ip", "role"]):
        abort(400)
    try:
        register_new_host(req["host_name"], req["ip"], req["role"])
        db.session.commit()
    except Exception as e:
        abort(500)

    return jsonify(result=dict(message="OK", request="add host", payload=str(request.json)))
예제 #5
0
def add_host():
    if not is_json_request(request):
        return abort(400)

    req = request.json
    if not is_valid_keys(req, ['host_name', 'ip', 'role']):
        abort(400)
    try:
        register_new_host(req['host_name'], req['ip'], req['role'])
        db.session.commit()
    except Exception as e:
        abort(500)

    return jsonify(result=dict(message='OK', request='add host', payload=str(request.json)))
예제 #6
0
def add_ip():
    if not is_json_request(request) or not is_valid_keys(request.json, ["ip"]):
        abort(400)
    ipaddr = request.json["ip"]

    if not is_valid_ip(ipaddr):
        abort(400)
    try:
        check_registerable_ip(ipaddr)
        ip = IP(ip=ipaddr)
        db.session.add(ip)
        db.session.commit()
    except Exception as e:
        abort(500)
    return jsonify(result=[dict(message="OK", request="add ip", payload=str(request.json))])
예제 #7
0
def update_ip(old_ipaddr):
    if not is_json_request(request) or not is_valid_keys(request.json, ["ip"]):
        abort(400)
    new_ipaddr = request.json["ip"]

    if not is_valid_ip(new_ipaddr):
        abort(400)
    try:
        check_registerable_ip(new_ipaddr)
        ip = IP.get_one(db.session.query, old_ipaddr)
        if ip.is_used == 1:
            ip.is_used = 1
        ip.ip = new_ipaddr
        db.session.commit()
    except Exception as e:
        abort(500)
    return jsonify(result=[dict(message="OK", request="update ip", payload=str(request.json))])