Example #1
0
def acknowledge_werks(werks):
    config.need_permission("general.acknowledge_werks")
    ack_ids = load_acknowledgements()
    for werk in werks:
        ack_ids.append(werk["id"])
        werk["compatible"] = "incomp_ack"
    save_acknowledgements(ack_ids)
Example #2
0
def acknowledge_werks(werks):
    config.need_permission("general.acknowledge_werks")
    ack_ids = load_acknowledgements()
    for werk in werks:
        ack_ids.append(werk["id"])
        werk["compatible"] = "incomp_ack"
    save_acknowledgements(ack_ids)
Example #3
0
def page_api():
    global g_api

    try:
        if not config.user.get("automation_secret"):
            raise MKAuthException("The WATO API is only available for automation users")

        config.need_permission("webapi.api_allowed")

        action = html.var('action')
        if action not in api_actions:
            raise MKUserError(None, "Unknown API action %s" % html.attrencode(action))

        config.need_permission("webapi.%s" % action)

        # Create API instance
        g_api = API()

        # Prepare request_object
        # Most of the time the request is given as json
        # However, the plugin may have an own mechanism to interpret the request
        request_object = {}
        if html.var("request"):
            if api_actions[action].get("dont_eval_request"):
                request_object = html.var("request")
            else:
                eval_function = None
                request = html.var("request")

                try:
                    import json
                    eval_function = json.loads
                except ImportError:
                    eval_function = literal_eval
                    # modify request so it can be read by literal_eval...
                    for old, new in [ (": null",  ": None"),
                                      (": true",  ": True"),
                                      (": false", ": False"), ]:
                        request = request.replace(old, new)
                request_object = eval_function(request)
        else:
            request_object = {}

        if api_actions[action].get("locking", True):
            g_api.lock_wato()

        if html.var("debug_webapi"):
            if api_actions[action]["example_request"]:
                example_request = api_actions[action]["example_request"]
                for entry, description in example_request[0]:
                    key, value = entry.split("=")
                    html.set_var(key, value)
                request_object = example_request[1]

        action_response = api_actions[action]["handler"](request_object)
        response = { "result_code": 0, "result": action_response }
    except Exception, e:
        #import traceback
        #html.debug(traceback.format_exc().replace("\n","<br>"))
        response = { "result_code": 1, "result": str(e) }
Example #4
0
def page_api():
    try:
        # The API uses JSON format by default and python as optional alternative
        output_format = html.var("output_format", "json")
        if output_format not in ["json", "python"]:
            raise MKUserError(
                None,
                "Only \"json\" and \"python\" are supported as output formats")
        else:
            html.set_output_format(output_format)

        if not config.user.get("automation_secret"):
            raise MKAuthException(
                "The WATO API is only available for automation users")

        config.need_permission("wato.use")
        config.need_permission("wato.api_allowed")

        action = html.var('action')
        if action not in api_actions:
            raise MKUserError(
                None, "Unknown API action %s" % html.attrencode(action))

        # Initialize host and site attributes
        init_watolib_datastructures()

        # Prepare request_object
        # Most of the time the request is given as json
        # However, the plugin may have an own mechanism to interpret the request
        request_object = {}
        if html.var("request"):
            if api_actions[action].get("dont_eval_request"):
                request_object = html.var("request")
            else:
                request = html.var("request")
                request_object = json.loads(request)
        else:
            request_object = {}

        if api_actions[action].get("locking", True):
            lock_exclusive()  # unlock is done automatically

        action_response = api_actions[action]["handler"](request_object)
        response = {"result_code": 0, "result": action_response}

    except MKException, e:
        response = {"result_code": 1, "result": str(e)}
Example #5
0
def page_api():
    global g_api

    try:
        if not config.user.get("automation_secret"):
            raise MKAuthException(
                "The WATO API is only available for automation users")

        config.need_permission("wato.use")
        config.need_permission("wato.api_allowed")

        action = html.var('action')
        if action not in api_actions:
            raise MKUserError(
                None, "Unknown API action %s" % html.attrencode(action))

        # Create API instance
        g_api = API()

        # Prepare request_object
        # Most of the time the request is given as json
        # However, the plugin may have an own mechanism to interpret the request
        request_object = {}
        if html.var("request"):
            if api_actions[action].get("dont_eval_request"):
                request_object = html.var("request")
            else:
                request = html.var("request")
                request_object = json.loads(request)
        else:
            request_object = {}

        if api_actions[action].get("locking", True):
            g_api.lock_wato()

        action_response = api_actions[action]["handler"](request_object)
        response = {"result_code": 0, "result": action_response}
    except Exception, e:
        response = {"result_code": 1, "result": str(e)}
Example #6
0
def page_api():
    global g_api

    try:
        if not config.user.get("automation_secret"):
            raise MKAuthException("The WATO API is only available for automation users")

        config.need_permission("wato.use")
        config.need_permission("wato.api_allowed")

        action = html.var('action')
        if action not in api_actions:
            raise MKUserError(None, "Unknown API action %s" % html.attrencode(action))

        # Create API instance
        g_api = API()

        # Prepare request_object
        # Most of the time the request is given as json
        # However, the plugin may have an own mechanism to interpret the request
        request_object = {}
        if html.var("request"):
            if api_actions[action].get("dont_eval_request"):
                request_object = html.var("request")
            else:
                request = html.var("request")
                request_object = json.loads(request)
        else:
            request_object = {}

        if api_actions[action].get("locking", True):
            g_api.lock_wato()


        action_response = api_actions[action]["handler"](request_object)
        response = { "result_code": 0, "result": action_response }
    except Exception, e:
        response = { "result_code": 1, "result": str(e) }