def api_node_get_vncinfo(nodeid):
    status = "success"
    answ = {"data": {}}
    try:
        nodeinfo = cloud_api_main.get_node_db_for_output(nodeid)
        if nodeinfo["state"] == "UNKNOWN":
            raise cloud_api_main.NoSuchObjectException("underlying nebula node probably doesn't exist ")
        if cloud_api_main.get_node_state(nodeid, selector="lcm_state") != "RUNNING":
            raise cloud_api_main.WrongStateForActionException("that node isn't running")
        internal = cloud_api_main.info_node_nebula(nodeinfo["internal_id"])
        info = {"host": internal["HISTORY_RECORDS"]["HISTORY"]["HOSTNAME"],
                "port": internal["TEMPLATE"]["GRAPHICS"]["PORT"],
                "password": internal["TEMPLATE"]["GRAPHICS"]["PASSWD"]}

        answ["data"]["vncinfo"] = info
    except (cloud_api_main.PrivilegeException, cloud_api_main.NoSuchObjectException) as e:
        status = "fail"
        answ["message"] = repr(e)
        answ["data"]["description"] = cloud_api_main.get_user_problem_msg()
    except cloud_api_main.GeneralAPIError as e:
        status = "fail"
        answ["message"] = repr(e)
        answ["data"]["description"] =  cloud_api_main.get_user_problem_msg()
    except Exception as e:
        status = "error"
        answ["message"] = repr(e)
        answ["data"]["description"] = cloud_api_main.get_user_problem_msg()

    answ["status"] = status

    return make_response(jsonify(answ))
def api_node_tags_delete(nodeid,tag):
    status = "success"
    answ = {"data": {}}
    cloud_api_main.debug_log_print("Change tags", request.data)

    try:
        cloud_api_main.check_user_privilege_node(nodeid)

        if tag == cloud_api_main.tdata.usertag:
            cloud_api_main.add_to_user_problem_msg("you can't remove your own usertag")
            raise cloud_api_main.PrivilegeException("can't remove this tag")

        tags = set((tag,))

        currtags = set(cloud_api_main.get_tags_node(nodeid))

        to_remove = tags.intersection(currtags)

        cloud_api_main.remove_tag_node(to_remove, nodeid)

        answ["data"]["deleted"] = True
    except (cloud_api_main.PrivilegeException, cloud_api_main.NoSuchObjectException) as e:
        status = "fail"
        answ["message"] = repr(e)
        answ["data"]["description"] = cloud_api_main.get_user_problem_msg()

    except Exception as e:
        status = "error"
        answ["message"] = repr(e)
        answ["data"]["description"] = cloud_api_main.get_user_problem_msg()

    answ["status"] = status

    return make_response(jsonify(answ))
def api_node_tags_add(nodeid):
    status = "success"
    answ = {"data": {}}
    cloud_api_main.debug_log_print("Add tags", request.data)

    try:
        cloud_api_main.check_user_privilege_node(nodeid)
        added = []
        reqdata = json.loads(request.data)
        tags = reqdata["tags"]
        alltags = cloud_api_main.get_tags()
        if isinstance(tags, basestring):
            tags = [tags]
        tags = set(tags)
        for t in tags:
            if t not in alltags:
                cloud_api_main.create_tag(t)
            cloud_api_main.add_tag_node(t, nodeid)
        answ["data"]["tags"] = list(tags)
    except (cloud_api_main.PrivilegeException, cloud_api_main.NoSuchObjectException) as e:
        status = "fail"
        answ["message"] = repr(e)
        answ["data"]["description"] = cloud_api_main.get_user_problem_msg()
    except Exception as e:
        status = "error"
        answ["message"] = repr(e)
        answ["data"]["description"] = cloud_api_main.get_user_problem_msg()

    answ["status"] = status

    return make_response(jsonify(answ))
def api_user_add():
    status = "success"
    answ = {"data": {}}

    try:

        reqdata = json.loads(request.data)

        try:
            username = reqdata.pop("name")
            password = reqdata.pop("password")
            isadmin = reqdata.get("isadmin", False)
            nebulauser = reqdata.get("nebulauser", None)
        except KeyError:
            raise cloud_api_main.WrongRequestException("name or password parameters missing")

        cloud_api_main.add_user(username,password,isadmin,nebulauser)

        data = cloud_api_main.get_user(username)
        answ["data"]["user"] = data.values()

    except (cloud_api_main.PrivilegeException, cloud_api_main.NoSuchObjectException, cloud_api_main.WrongRequestException) as e:
        status = "fail"
        answ["message"] = repr(e)
        answ["data"]["description"] = cloud_api_main.get_user_problem_msg()

    except Exception as e:
        status = "error"
        answ["message"] = repr(e)
        answ["data"]["description"] = cloud_api_main.get_user_problem_msg()


    answ["status"] = status

    return make_response(jsonify(answ))
def api_node_tags_add(nodeid):
    status = "success"
    answ = {"data": {}}
    cloud_api_main.debug_log_print("Add tags", request.data)

    try:
        cloud_api_main.check_user_privilege_node(nodeid)
        added = []
        reqdata = json.loads(request.data)
        tags = reqdata["tags"]
        alltags = cloud_api_main.get_tags()
        if isinstance(tags, basestring):
            tags = [tags]
        tags = set(tags)
        for t in tags:
            if t not in alltags:
                cloud_api_main.create_tag(t)
            cloud_api_main.add_tag_node(t, nodeid)
        answ["data"]["tags"] = list(tags)
    except (cloud_api_main.PrivilegeException,
            cloud_api_main.NoSuchObjectException) as e:
        status = "fail"
        answ["message"] = repr(e)
        answ["data"]["description"] = cloud_api_main.get_user_problem_msg()
    except Exception as e:
        status = "error"
        answ["message"] = repr(e)
        answ["data"]["description"] = cloud_api_main.get_user_problem_msg()

    answ["status"] = status

    return make_response(jsonify(answ))
def api_node_sshcmd(nodeid):
    status = "success"
    answ = {"data": {}}
    cloud_api_main.debug_log_print("Create node", request.data)

    try:
        reqdata = json.loads(request.data)
        cmd = reqdata.get("cmd")

        result = cloud_api_main.run_ssh_on_node(nodeid, cmd)

        answ["data"]["result"] = result

    except cloud_api_main.GeneralAPIError as e:
        status = "fail"
        answ["message"] = repr(e)
        answ["data"]["description"] =  cloud_api_main.get_user_problem_msg()
    except Exception as e:
        status = "error"
        answ["message"] = repr(e)
        answ["data"]["description"] = cloud_api_main.get_user_problem_msg()

    answ["status"] = status

    return make_response(jsonify(answ))
def api_node_sshcmd(nodeid):
    status = "success"
    answ = {"data": {}}
    cloud_api_main.debug_log_print("Create node", request.data)

    try:
        reqdata = json.loads(request.data)
        cmd = reqdata.get("cmd")

        result = cloud_api_main.run_ssh_on_node(nodeid, cmd)

        answ["data"]["result"] = result

    except cloud_api_main.GeneralAPIError as e:
        status = "fail"
        answ["message"] = repr(e)
        answ["data"]["description"] = cloud_api_main.get_user_problem_msg()
    except Exception as e:
        status = "error"
        answ["message"] = repr(e)
        answ["data"]["description"] = cloud_api_main.get_user_problem_msg()

    answ["status"] = status

    return make_response(jsonify(answ))
def api_node_set_status(nodeid):
    status = "success"
    answ = {"data": {}}
    try:
        cloud_api_main.check_user_privilege_node(nodeid)
        reqdata = json.loads(request.data)

        wantedstate = reqdata.get("status")
        if not wantedstate:
            cloud_api_main.add_to_user_problem_msg("no status to change to")
            raise cloud_api_main.WrongRequestException("wrong request")

        data = cloud_api_main.change_node_state_cruder(nodeid,wantedstate)
        answ["data"] = data
    except (cloud_api_main.PrivilegeException, cloud_api_main.NoSuchObjectException, cloud_api_main.WrongRequestException) as e:
        status = "fail"
        answ["message"] = repr(e)
        answ["data"]["description"] = cloud_api_main.get_user_problem_msg()
    except BaseException as e:
        status = "error"
        cloud_api_main.debug_log_print(e)
        answ["message"] = repr(e)
        answ["data"]["description"] = cloud_api_main.get_user_problem_msg()

    answ["status"] = status

    return make_response(jsonify(answ))
def api_node_set_status(nodeid):
    status = "success"
    answ = {"data": {}}
    try:
        cloud_api_main.check_user_privilege_node(nodeid)
        reqdata = json.loads(request.data)

        wantedstate = reqdata.get("status")
        if not wantedstate:
            cloud_api_main.add_to_user_problem_msg("no status to change to")
            raise cloud_api_main.WrongRequestException("wrong request")

        data = cloud_api_main.change_node_state_cruder(nodeid, wantedstate)
        answ["data"] = data
    except (cloud_api_main.PrivilegeException,
            cloud_api_main.NoSuchObjectException,
            cloud_api_main.WrongRequestException) as e:
        status = "fail"
        answ["message"] = repr(e)
        answ["data"]["description"] = cloud_api_main.get_user_problem_msg()
    except BaseException as e:
        status = "error"
        cloud_api_main.debug_log_print(e)
        answ["message"] = repr(e)
        answ["data"]["description"] = cloud_api_main.get_user_problem_msg()

    answ["status"] = status

    return make_response(jsonify(answ))
def api_user_change(username):
    status = "success"
    answ = {"data": {}}

    try:
        reqdata = json.loads(request.data)

        try:
            user_d = reqdata["user"]
        except KeyError:
            raise cloud_api_main.WrongRequestException("dictionary defining new user properties missing")

        cloud_api_main.change_user(username, user_d)


        data = cloud_api_main.get_user(username)
        answ["data"]["user"] = data

    except (cloud_api_main.PrivilegeException, cloud_api_main.NoSuchObjectException, cloud_api_main.WrongRequestException) as e:
        status = "fail"
        answ["message"] = repr(e)
        answ["data"]["description"] = cloud_api_main.get_user_problem_msg()


    except BaseException as e:
        status = "error"
        answ["message"] = repr(e)
        answ["data"]["description"] = cloud_api_main.tdata.extended_error_log

    answ["status"] = status

    return make_response(jsonify(answ))
def api_user_change(username):
    status = "success"
    answ = {"data": {}}

    try:
        reqdata = json.loads(request.data)

        try:
            user_d = reqdata["user"]
        except KeyError:
            raise cloud_api_main.WrongRequestException(
                "dictionary defining new user properties missing")

        cloud_api_main.change_user(username, user_d)

        data = cloud_api_main.get_user(username)
        answ["data"]["user"] = data

    except (cloud_api_main.PrivilegeException,
            cloud_api_main.NoSuchObjectException,
            cloud_api_main.WrongRequestException) as e:
        status = "fail"
        answ["message"] = repr(e)
        answ["data"]["description"] = cloud_api_main.get_user_problem_msg()

    except BaseException as e:
        status = "error"
        answ["message"] = repr(e)
        answ["data"]["description"] = cloud_api_main.tdata.extended_error_log

    answ["status"] = status

    return make_response(jsonify(answ))
def unauthorized():
    return make_response(
        jsonify({
            "status": "fail",
            'message': 'Unauthorized access',
            "description": cloud_api_main.get_user_problem_msg()
        }), 403)
def api_net_create_link():
    status = "success"
    answ = {"data": {}}
    # debug_log_print(request.data)

    try:
        cloud_api_main.debug_log_print_ext("attmpting to load from json",
                                           request.data)
        reqdata = cloud_api_main.json.loads(request.data)

        netid = None
        if "node1" in reqdata and "node2" in reqdata:
            n1 = reqdata.pop("node1")
            n2 = reqdata.pop("node2")

            data = cloud_api_main.connect_nodes_nebula(n1, n2, **reqdata)
            answ["data"]["connection"] = data
            netid = data["id"]
        elif "mac1" in reqdata and "mac2" in reqdata:
            mac1 = reqdata.pop("mac1")
            mac2 = reqdata.pop("mac1")

            netid = cloud_api_main.create_link(mac1, mac2)

        else:
            pass

        answ["data"]["net"] = cloud_api_main.get_network_db_for_output()
    except (cloud_api_main.PrivilegeException,
            cloud_api_main.NoSuchObjectException) as e:
        status = "fail"
        answ["message"] = repr(e)
        answ["data"]["description"] = cloud_api_main.get_user_problem_msg()

    except cloud_api_main.GeneralAPIError as e:
        status = "fail"
        answ["message"] = repr(e)
        answ["data"]["description"] = cloud_api_main.get_user_problem_msg()
    except Exception as e:
        status = "error"
        answ["message"] = repr(e)
        answ["data"]["description"] = cloud_api_main.get_user_problem_msg()

    answ["status"] = status

    return make_response(jsonify(answ))
def api_hook_create():
    status = "success"
    answ = {"data": {}}

    try:
        reqdata = json.loads(request.data)

        try:
            selector = reqdata["selector"]
            towhat = reqdata.get("goalstate", None)
            ids = reqdata["node_ids"]
            howlong = reqdata.get("howlong", None)
            notifyaddr = reqdata["notify_address"]
            howmany = reqdata.get("howmany")

        except KeyError:
            raise cloud_api_main.WrongRequestException(
                "dictionary defining new callback incomplete")

        hook = cloud_api_main.NotifyTask(ids,
                                         selector,
                                         towhat,
                                         howmany,
                                         howlong,
                                         notifyaddr=notifyaddr)
        taskid = hook.taskid

        answ["data"]["callback"] = hook.as_repr_dict()

    except (cloud_api_main.PrivilegeException,
            cloud_api_main.NoSuchObjectException,
            cloud_api_main.WrongRequestException) as e:
        status = "fail"
        answ["message"] = repr(e)
        answ["data"]["description"] = cloud_api_main.get_user_problem_msg()

    except Exception as e:
        status = "error"
        answ["message"] = repr(e)
        answ["data"]["description"] = cloud_api_main.get_user_problem_msg()

    answ["status"] = status

    return make_response(jsonify(answ))
def api_net_create_link():
    status = "success"
    answ = {"data": {}}
    # debug_log_print(request.data)

    try:
        cloud_api_main.debug_log_print_ext("attmpting to load from json", request.data)
        reqdata = cloud_api_main.json.loads(request.data)

        netid = None
        if "node1" in reqdata and "node2" in reqdata:
            n1 = reqdata.pop("node1")
            n2 = reqdata.pop("node2")

            data = cloud_api_main.connect_nodes_nebula(n1, n2, **reqdata)
            answ["data"]["connection"] = data
            netid = data["id"]
        elif "mac1" in reqdata and "mac2" in reqdata:
            mac1 = reqdata.pop("mac1")
            mac2 = reqdata.pop("mac1")

            netid = cloud_api_main.create_link(mac1, mac2)

        else:
            pass

        answ["data"]["net"] = cloud_api_main.get_network_db_for_output()
    except (cloud_api_main.PrivilegeException, cloud_api_main.NoSuchObjectException) as e:
        status = "fail"
        answ["message"] = repr(e)
        answ["data"]["description"] = cloud_api_main.get_user_problem_msg()

    except cloud_api_main.GeneralAPIError as e:
        status = "fail"
        answ["message"] = repr(e)
        answ["data"]["description"] =  cloud_api_main.get_user_problem_msg()
    except Exception as e:
        status = "error"
        answ["message"] = repr(e)
        answ["data"]["description"] = cloud_api_main.get_user_problem_msg()

    answ["status"] = status

    return make_response(jsonify(answ))
def api_nets_list():
    status = "success"
    answ = {"data": {}}

    try:
        data = cloud_api_main.get_networks_db_for_output()
        answ["data"]["nets"] = data.values()
    except cloud_api_main.GeneralAPIError as e:
        status = "fail"
        answ["message"] = repr(e)
        answ["data"]["description"] =  cloud_api_main.get_user_problem_msg()
    except Exception as e:
        status = "error"
        answ["message"] = repr(e)
        answ["data"]["description"] = cloud_api_main.get_user_problem_msg()

    answ["status"] = status

    return make_response(jsonify(answ))
def api_node_tags_change(nodeid):
    status = "success"
    answ = {"data": {}}

    try:
        cloud_api_main.debug_log_print("Change tags", request.data)

        cloud_api_main.check_user_privilege_node(nodeid)

        reqdata = json.loads(request.data)
        tags = reqdata["tags"]
        if isinstance(tags, basestring):
            tags = [tags]
        tags = set(tags)
        tags.add(cloud_api_main.tdata.ownertag)

        alltags = cloud_api_main.get_tags()
        currtags = set(cloud_api_main.get_tags_node(nodeid))

        to_remove = currtags.difference(tags).intersection(alltags)
        to_add = tags.difference(currtags)

        for t in to_add:
            if t not in alltags:
                cloud_api_main.create_tag(t)
            cloud_api_main.add_tag_node(t, nodeid)
        cloud_api_main.remove_tag_node(to_remove, nodeid)

        answ["data"]["tags"] = cloud_api_main.get_tags_node(nodeid)
    except (cloud_api_main.PrivilegeException,
            cloud_api_main.NoSuchObjectException) as e:
        status = "fail"
        answ["message"] = repr(e)
        answ["data"]["description"] = cloud_api_main.get_user_problem_msg()

    except Exception as e:
        status = "error"
        answ["message"] = repr(e)
        answ["data"]["description"] = cloud_api_main.get_user_problem_msg()

    answ["status"] = status

    return make_response(jsonify(answ))
def api_nets_list():
    status = "success"
    answ = {"data": {}}

    try:
        data = cloud_api_main.get_networks_db_for_output()
        answ["data"]["nets"] = data.values()
    except cloud_api_main.GeneralAPIError as e:
        status = "fail"
        answ["message"] = repr(e)
        answ["data"]["description"] = cloud_api_main.get_user_problem_msg()
    except Exception as e:
        status = "error"
        answ["message"] = repr(e)
        answ["data"]["description"] = cloud_api_main.get_user_problem_msg()

    answ["status"] = status

    return make_response(jsonify(answ))
def api_node_tags_change(nodeid):
    status = "success"
    answ = {"data": {}}

    try:
        cloud_api_main.debug_log_print("Change tags", request.data)

        cloud_api_main.check_user_privilege_node(nodeid)

        reqdata = json.loads(request.data)
        tags = reqdata["tags"]
        if isinstance(tags, basestring):
            tags = [tags]
        tags = set(tags)
        tags.add(cloud_api_main.tdata.ownertag)

        alltags = cloud_api_main.get_tags()
        currtags = set(cloud_api_main.get_tags_node(nodeid))

        to_remove = currtags.difference(tags).intersection(alltags)
        to_add = tags.difference(currtags)

        for t in to_add:
            if t not in alltags:
                cloud_api_main.create_tag(t)
            cloud_api_main.add_tag_node(t, nodeid)
        cloud_api_main.remove_tag_node(to_remove, nodeid)

        answ["data"]["tags"] = cloud_api_main.get_tags_node(nodeid)
    except (cloud_api_main.PrivilegeException, cloud_api_main.NoSuchObjectException) as e:
        status = "fail"
        answ["message"] = repr(e)
        answ["data"]["description"] = cloud_api_main.get_user_problem_msg()

    except Exception as e:
        status = "error"
        answ["message"] = repr(e)
        answ["data"]["description"] = cloud_api_main.get_user_problem_msg()

    answ["status"] = status

    return make_response(jsonify(answ))
def api_node_get_status(nodeid):
    status = "success"
    answ = {"data": {}}
    try:
        data = cloud_api_main.get_node_state(nodeid)
        answ["data"] = data
    except (cloud_api_main.PrivilegeException, cloud_api_main.NoSuchObjectException) as e:
        status = "fail"
        cloud_api_main.debug_log_print(e)
        answ["message"] = repr(e)
        answ["data"]["description"] = cloud_api_main.get_user_problem_msg()
    except BaseException as e:
        status = "error"
        cloud_api_main.debug_log_print(e)
        answ["message"] = repr(e)
        answ["data"]["description"] = cloud_api_main.get_user_problem_msg()

    answ["status"] = status

    return make_response(jsonify(answ))
def api_node_delete(nodeid):
    status = "success"
    answ = {"data": {}}
    # debug_log_print(request.data)

    try:
        cloud_api_main.delete_node(nodeid)
        answ["data"]["deleted"] = True

    except cloud_api_main.GeneralAPIError as e:
        status = "fail"
        answ["message"] = repr(e)
        answ["data"]["description"] = cloud_api_main.get_user_problem_msg()
    except Exception as e:
        status = "error"
        answ["message"] = repr(e)
        answ["data"]["description"] = cloud_api_main.get_user_problem_msg()

    answ["status"] = status

    return make_response(jsonify(answ))
def api_node_get_status(nodeid):
    status = "success"
    answ = {"data": {}}
    try:
        data = cloud_api_main.get_node_state(nodeid)
        answ["data"] = data
    except (cloud_api_main.PrivilegeException,
            cloud_api_main.NoSuchObjectException) as e:
        status = "fail"
        cloud_api_main.debug_log_print(e)
        answ["message"] = repr(e)
        answ["data"]["description"] = cloud_api_main.get_user_problem_msg()
    except BaseException as e:
        status = "error"
        cloud_api_main.debug_log_print(e)
        answ["message"] = repr(e)
        answ["data"]["description"] = cloud_api_main.get_user_problem_msg()

    answ["status"] = status

    return make_response(jsonify(answ))
def api_node_delete(nodeid):
    status = "success"
    answ = {"data": {}}
    # debug_log_print(request.data)

    try:
        cloud_api_main.delete_node(nodeid)
        answ["data"]["deleted"] = True

    except cloud_api_main.GeneralAPIError as e:
        status = "fail"
        answ["message"] = repr(e)
        answ["data"]["description"] =  cloud_api_main.get_user_problem_msg()
    except Exception as e:
        status = "error"
        answ["message"] = repr(e)
        answ["data"]["description"] = cloud_api_main.get_user_problem_msg()

    answ["status"] = status

    return make_response(jsonify(answ))
def api_interface_delete(intfid):
    status = "success"
    answ = {"data": {}}
    try:
        # TODO odstranit i addr range?
        cloud_api_main.remove_disconnect_interface(intfid)

        answ["data"]["deleted"] = True
    except (cloud_api_main.PrivilegeException, cloud_api_main.NoSuchObjectException) as e:
        status = "fail"
        answ["message"] = repr(e)
        answ["data"]["description"] = cloud_api_main.get_user_problem_msg()
    except BaseException as e:
        status = "error"
        cloud_api_main.debug_log_print(e)
        answ["message"] = repr(e)
        answ["data"]["description"] = cloud_api_main.get_user_problem_msg()

    answ["status"] = status

    return make_response(jsonify(answ))
def api_node_create():
    status = "success"
    answ = {"data": {}}
    cloud_api_main.debug_log_print("Create node", request.data)

    try:
        reqdata = json.loads(request.data)
        templname = reqdata.pop("templatename")
        dbid = cloud_api_main.create_node(templname, **reqdata)
        answ["data"]["node"] = cloud_api_main.get_node_db_for_output(dbid)
    except cloud_api_main.GeneralAPIError as e:
        status = "fail"
        answ["message"] = repr(e)
        answ["data"]["description"] =  cloud_api_main.get_user_problem_msg()
    except Exception as e:
        status = "error"
        answ["message"] = repr(e)
        answ["data"]["description"] = cloud_api_main.get_user_problem_msg()

    answ["status"] = status

    return make_response(jsonify(answ))
def api_hook_create():
    status = "success"
    answ = {"data": {}}

    try:
        reqdata = json.loads(request.data)

        try:
            selector = reqdata["selector"]
            towhat = reqdata.get("goalstate", None)
            ids = reqdata["node_ids"]
            howlong = reqdata.get("howlong", None)
            notifyaddr = reqdata["notify_address"]
            howmany = reqdata.get("howmany")

        except KeyError:
            raise cloud_api_main.WrongRequestException("dictionary defining new callback incomplete")

        hook = cloud_api_main.NotifyTask(ids,selector,towhat,howmany,howlong,notifyaddr=notifyaddr)
        taskid = hook.taskid

        answ["data"]["callback"] = hook.as_repr_dict()



    except (cloud_api_main.PrivilegeException, cloud_api_main.NoSuchObjectException, cloud_api_main.WrongRequestException) as e:
        status = "fail"
        answ["message"] = repr(e)
        answ["data"]["description"] = cloud_api_main.get_user_problem_msg()

    except Exception as e:
        status = "error"
        answ["message"] = repr(e)
        answ["data"]["description"] = cloud_api_main.get_user_problem_msg()


    answ["status"] = status

    return make_response(jsonify(answ))
def api_node_create():
    status = "success"
    answ = {"data": {}}
    cloud_api_main.debug_log_print("Create node", request.data)

    try:
        reqdata = json.loads(request.data)
        templname = reqdata.pop("templatename")
        dbid = cloud_api_main.create_node(templname, **reqdata)
        answ["data"]["node"] = cloud_api_main.get_node_db_for_output(dbid)
    except cloud_api_main.GeneralAPIError as e:
        status = "fail"
        answ["message"] = repr(e)
        answ["data"]["description"] = cloud_api_main.get_user_problem_msg()
    except Exception as e:
        status = "error"
        answ["message"] = repr(e)
        answ["data"]["description"] = cloud_api_main.get_user_problem_msg()

    answ["status"] = status

    return make_response(jsonify(answ))
def api_hooks_list():
    status = "success"
    answ = {"data": {}}

    try:
        data = cloud_api_main.NotifyTask.get_all_hooks()
        answ["data"]["callbacks"] = data.values()

    except (cloud_api_main.PrivilegeException, cloud_api_main.NoSuchObjectException, cloud_api_main.WrongRequestException) as e:
        status = "fail"
        answ["message"] = repr(e)
        answ["data"]["description"] = cloud_api_main.get_user_problem_msg()

    except Exception as e:
        status = "error"
        answ["message"] = repr(e)
        answ["data"]["description"] = cloud_api_main.get_user_problem_msg()


    answ["status"] = status

    return make_response(jsonify(answ))
def api_interface_delete(intfid):
    status = "success"
    answ = {"data": {}}
    try:
        # TODO odstranit i addr range?
        cloud_api_main.remove_disconnect_interface(intfid)

        answ["data"]["deleted"] = True
    except (cloud_api_main.PrivilegeException,
            cloud_api_main.NoSuchObjectException) as e:
        status = "fail"
        answ["message"] = repr(e)
        answ["data"]["description"] = cloud_api_main.get_user_problem_msg()
    except BaseException as e:
        status = "error"
        cloud_api_main.debug_log_print(e)
        answ["message"] = repr(e)
        answ["data"]["description"] = cloud_api_main.get_user_problem_msg()

    answ["status"] = status

    return make_response(jsonify(answ))
def api_hooks_list():
    status = "success"
    answ = {"data": {}}

    try:
        data = cloud_api_main.NotifyTask.get_all_hooks()
        answ["data"]["callbacks"] = data.values()

    except (cloud_api_main.PrivilegeException,
            cloud_api_main.NoSuchObjectException,
            cloud_api_main.WrongRequestException) as e:
        status = "fail"
        answ["message"] = repr(e)
        answ["data"]["description"] = cloud_api_main.get_user_problem_msg()

    except Exception as e:
        status = "error"
        answ["message"] = repr(e)
        answ["data"]["description"] = cloud_api_main.get_user_problem_msg()

    answ["status"] = status

    return make_response(jsonify(answ))
def api_net_delete(netid):
    status = "success"
    answ = {"data": {}}
    try:
        cloud_api_main.remove_network(netid)
        answ["data"]["deleted"] = netid
    except (cloud_api_main.PrivilegeException, cloud_api_main.NoSuchObjectException) as e:
        status = "fail"
        answ["message"] = repr(e)
        answ["data"]["description"] = cloud_api_main.get_user_problem_msg()

    except cloud_api_main.GeneralAPIError as e:
        status = "fail"
        answ["message"] = repr(e)
        answ["data"]["description"] =  cloud_api_main.get_user_problem_msg()
    except Exception as e:
        status = "error"
        answ["message"] = repr(e)
        answ["data"]["description"] = cloud_api_main.get_user_problem_msg()

    answ["status"] = status

    return make_response(jsonify(answ))
def api_node_get_vncinfo(nodeid):
    status = "success"
    answ = {"data": {}}
    try:
        nodeinfo = cloud_api_main.get_node_db_for_output(nodeid)
        if nodeinfo["state"] == "UNKNOWN":
            raise cloud_api_main.NoSuchObjectException(
                "underlying nebula node probably doesn't exist ")
        if cloud_api_main.get_node_state(nodeid,
                                         selector="lcm_state") != "RUNNING":
            raise cloud_api_main.WrongStateForActionException(
                "that node isn't running")
        internal = cloud_api_main.info_node_nebula(nodeinfo["internal_id"])
        info = {
            "host": internal["HISTORY_RECORDS"]["HISTORY"]["HOSTNAME"],
            "port": internal["TEMPLATE"]["GRAPHICS"]["PORT"],
            "password": internal["TEMPLATE"]["GRAPHICS"]["PASSWD"]
        }

        answ["data"]["vncinfo"] = info
    except (cloud_api_main.PrivilegeException,
            cloud_api_main.NoSuchObjectException) as e:
        status = "fail"
        answ["message"] = repr(e)
        answ["data"]["description"] = cloud_api_main.get_user_problem_msg()
    except cloud_api_main.GeneralAPIError as e:
        status = "fail"
        answ["message"] = repr(e)
        answ["data"]["description"] = cloud_api_main.get_user_problem_msg()
    except Exception as e:
        status = "error"
        answ["message"] = repr(e)
        answ["data"]["description"] = cloud_api_main.get_user_problem_msg()

    answ["status"] = status

    return make_response(jsonify(answ))
def api_user_add():
    status = "success"
    answ = {"data": {}}

    try:

        reqdata = json.loads(request.data)

        try:
            username = reqdata.pop("name")
            password = reqdata.pop("password")
            isadmin = reqdata.get("isadmin", False)
            nebulauser = reqdata.get("nebulauser", None)
        except KeyError:
            raise cloud_api_main.WrongRequestException(
                "name or password parameters missing")

        cloud_api_main.add_user(username, password, isadmin, nebulauser)

        data = cloud_api_main.get_user(username)
        answ["data"]["user"] = data.values()

    except (cloud_api_main.PrivilegeException,
            cloud_api_main.NoSuchObjectException,
            cloud_api_main.WrongRequestException) as e:
        status = "fail"
        answ["message"] = repr(e)
        answ["data"]["description"] = cloud_api_main.get_user_problem_msg()

    except Exception as e:
        status = "error"
        answ["message"] = repr(e)
        answ["data"]["description"] = cloud_api_main.get_user_problem_msg()

    answ["status"] = status

    return make_response(jsonify(answ))
def api_net_delete(netid):
    status = "success"
    answ = {"data": {}}
    try:
        cloud_api_main.remove_network(netid)
        answ["data"]["deleted"] = netid
    except (cloud_api_main.PrivilegeException,
            cloud_api_main.NoSuchObjectException) as e:
        status = "fail"
        answ["message"] = repr(e)
        answ["data"]["description"] = cloud_api_main.get_user_problem_msg()

    except cloud_api_main.GeneralAPIError as e:
        status = "fail"
        answ["message"] = repr(e)
        answ["data"]["description"] = cloud_api_main.get_user_problem_msg()
    except Exception as e:
        status = "error"
        answ["message"] = repr(e)
        answ["data"]["description"] = cloud_api_main.get_user_problem_msg()

    answ["status"] = status

    return make_response(jsonify(answ))
def api_node_tags_delete(nodeid, tag):
    status = "success"
    answ = {"data": {}}
    cloud_api_main.debug_log_print("Change tags", request.data)

    try:
        cloud_api_main.check_user_privilege_node(nodeid)

        if tag == cloud_api_main.tdata.usertag:
            cloud_api_main.add_to_user_problem_msg(
                "you can't remove your own usertag")
            raise cloud_api_main.PrivilegeException("can't remove this tag")

        tags = set((tag, ))

        currtags = set(cloud_api_main.get_tags_node(nodeid))

        to_remove = tags.intersection(currtags)

        cloud_api_main.remove_tag_node(to_remove, nodeid)

        answ["data"]["deleted"] = True
    except (cloud_api_main.PrivilegeException,
            cloud_api_main.NoSuchObjectException) as e:
        status = "fail"
        answ["message"] = repr(e)
        answ["data"]["description"] = cloud_api_main.get_user_problem_msg()

    except Exception as e:
        status = "error"
        answ["message"] = repr(e)
        answ["data"]["description"] = cloud_api_main.get_user_problem_msg()

    answ["status"] = status

    return make_response(jsonify(answ))
def api_interface_get(intfid):
    status = "success"
    answ = {"data": {}}
    try:
        data = cloud_api_main.get_interface_db(intfid)
        answ["data"]["interface"] = data
    except (cloud_api_main.PrivilegeException, cloud_api_main.NoSuchObjectException) as e:
        status = "fail"
        answ["message"] = repr(e)
        answ["data"]["description"] = cloud_api_main.get_user_problem_msg()
    except BaseException as e:
        status = "error"
        cloud_api_main.debug_log_print(e)
        answ["message"] = repr(e)
        answ["data"]["description"] = cloud_api_main.tdata.extended_error_log

    answ["status"] = status

    return make_response(jsonify(answ))
def api_interface_get(intfid):
    status = "success"
    answ = {"data": {}}
    try:
        data = cloud_api_main.get_interface_db(intfid)
        answ["data"]["interface"] = data
    except (cloud_api_main.PrivilegeException,
            cloud_api_main.NoSuchObjectException) as e:
        status = "fail"
        answ["message"] = repr(e)
        answ["data"]["description"] = cloud_api_main.get_user_problem_msg()
    except BaseException as e:
        status = "error"
        cloud_api_main.debug_log_print(e)
        answ["message"] = repr(e)
        answ["data"]["description"] = cloud_api_main.tdata.extended_error_log

    answ["status"] = status

    return make_response(jsonify(answ))
def api_user_get(username):
    status = "success"
    answ = {"data": {}}

    try:
        data = cloud_api_main.get_user(username)
        answ["data"]["user"] = data

    except (cloud_api_main.PrivilegeException, cloud_api_main.NoSuchObjectException, cloud_api_main.WrongRequestException) as e:
        status = "fail"
        answ["message"] = repr(e)
        answ["data"]["description"] = cloud_api_main.get_user_problem_msg()


    except BaseException as e:
        status = "error"
        answ["message"] = repr(e)
        answ["data"]["description"] = cloud_api_main.tdata.extended_error_log

    answ["status"] = status

    return make_response(jsonify(answ))
def api_user_get(username):
    status = "success"
    answ = {"data": {}}

    try:
        data = cloud_api_main.get_user(username)
        answ["data"]["user"] = data

    except (cloud_api_main.PrivilegeException,
            cloud_api_main.NoSuchObjectException,
            cloud_api_main.WrongRequestException) as e:
        status = "fail"
        answ["message"] = repr(e)
        answ["data"]["description"] = cloud_api_main.get_user_problem_msg()

    except BaseException as e:
        status = "error"
        answ["message"] = repr(e)
        answ["data"]["description"] = cloud_api_main.tdata.extended_error_log

    answ["status"] = status

    return make_response(jsonify(answ))
def unauthorized():
    return make_response(jsonify({"status": "fail", 'message': 'Unauthorized access',
                                  "description": cloud_api_main.get_user_problem_msg()}), 403)