Merge "vFW and vDNS support added to azure-plugin"
[multicloud/azure.git] / azure / multicloud_azure / swagger / views / infra_workload / views.py
diff --git a/azure/multicloud_azure/swagger/views/infra_workload/views.py b/azure/multicloud_azure/swagger/views/infra_workload/views.py
new file mode 100644 (file)
index 0000000..c44eba2
--- /dev/null
@@ -0,0 +1,82 @@
+# Copyright (c) 2018 Amdocs
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at:
+#
+#       http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+
+import logging
+import json
+
+from rest_framework import status
+from rest_framework.response import Response
+from rest_framework.views import APIView
+
+from multicloud_azure.pub.aria.service import AriaServiceImpl
+
+logger = logging.getLogger(__name__)
+
+
+class InfraWorkload(APIView):
+
+    def post(self, request, cloud_owner, cloud_region_id):
+        data = request.data
+        template_data = data["infra-template"]
+        payload = data["infra-payload"]
+        inputs = json.loads(payload)
+        template_name = inputs['template_data']['stack_name']
+        service_op = AriaServiceImpl()
+        try:
+            stack = service_op.deploy_service(template_name, template_data,
+                                              inputs, logger)
+            if stack[1] != 200:
+                return Response(data=stack[0], status=stack[1])
+        except Exception as e:
+
+            if hasattr(e, "http_status"):
+                return Response(data={'error': str(e)}, status=e.http_status)
+            else:
+                return Response(data={'error': str(e)},
+                                status=status.HTTP_500_INTERNAL_SERVER_ERROR)
+        rsp = {
+            "template_type": "heat",
+            "workload_id": stack[0]
+        }
+        return Response(data=rsp, status=status.HTTP_202_ACCEPTED)
+
+
+class GetStackView(APIView):
+
+    def get(self, request, cloud_owner, cloud_region_id, workload_id):
+        service_op = AriaServiceImpl()
+        try:
+            stack = service_op.show_execution(workload_id)
+            if stack[1] != 200:
+                return Response(data=stack[0], status=stack[1])
+            body = json.loads(stack[0])
+            stack_status = body["status"]
+            response = "unknown"
+            if stack_status == "pending" or stack_status == "started":
+                response = "CREATE_IN_PROGRESS"
+            elif stack_status == "succeeded":
+                response = "CREATE_COMPLETE"
+            elif stack_status == "failed" or stack_status == "cancelled":
+                response = "CREATE_FAILED"
+            rsp = {
+                "template_type": "heat",
+                "workload_id": workload_id,
+                "workload_status": response
+            }
+            return Response(data=rsp, status=stack[1])
+        except Exception as e:
+
+            if hasattr(e, "http_status"):
+                return Response(data={'error': str(e)}, status=e.http_status)
+            else:
+                return Response(data={'error': str(e)},
+                                status=status.HTTP_500_INTERNAL_SERVER_ERROR)