Adding policy manager and a1 sdnc vth
[it/otf.git] / a1-policy-manager-vth / app / helpers / action_helper.py
diff --git a/a1-policy-manager-vth/app/helpers/action_helper.py b/a1-policy-manager-vth/app/helpers/action_helper.py
new file mode 100644 (file)
index 0000000..f952e76
--- /dev/null
@@ -0,0 +1,54 @@
+import json\r
+import ast\r
+from app.helpers import response_helper as ResponseHelper\r
+from flask import current_app\r
+from app.errors.bad_request_exception import BadRequestException\r
+import requests\r
+\r
+def execute_action(request, response_dict, config):\r
+    headers = ResponseHelper.create_headers();\r
+    request_data = request.json\r
+    action_request = request_data.get("action").lower()\r
+    method = request_data.get("method").upper()\r
+    creds = ResponseHelper.get_credentials(request_data, config)\r
+\r
+    proxies = ResponseHelper.get_proxies(config)\r
+    action = "services/keepalive" if action_request == "keepalive" else action_request\r
+    url = ResponseHelper.create_url(config=config, uri_path="/"+action)\r
+#    ret_url = request.args.get('retURL')\r
+\r
+\r
+    json_req = ast.literal_eval(request_data["action_data"]["jsonBody"])\r
+    query_params = ast.literal_eval(request_data["action_data"]["query"])\r
+    current_app.logger.info("Requesting Url: {}, params: {}, body: {}, auth: {}, proxies: {}".format(url, query_params, json_req, creds, proxies))\r
+    try:\r
+        if(method == "GET"):\r
+            res = requests.get(url, proxies=proxies, auth=creds, headers=headers, params=query_params, json=json_req)\r
+        elif(method == "POST"):\r
+            res = requests.post(url, proxies=proxies, auth=creds, headers=headers, params=query_params, json=json_req)\r
+        elif(method == "PUT"):\r
+            res = requests.put(url, proxies=proxies, auth=creds, headers=headers, params=query_params, json=json_req)\r
+        elif(method == "DELETE"):\r
+            res = requests.delete(url, proxies=proxies, auth=creds, headers=headers, params=query_params, json=json_req)\r
+        else: \r
+            raise BadRequestException(406, "Method Not Supported")\r
+        response = {\r
+                "status_code":res.status_code,\r
+                "result": res.json()\r
+                }\r
+    except(json.decoder.JSONDecodeError):\r
+        response = {\r
+                "status_code":res.status_code,\r
+                "result": res.reason\r
+                }\r
+    except requests.exceptions.RequestException:\r
+        response = {\r
+                "status_code":504,\r
+                "result": "Something Happned"\r
+                }\r
+    finally:\r
+        response_dict['vthResponse']['resultData'] = response\r
+ #       if ret_url is not None:\r
+ #           ResponseHelper.sendCallback(ret_url,response_dict)\r
+ #           return '',200\r
+        return response_dict\r