vFW and vDNS support added to azure-plugin
[multicloud/azure.git] / azure / multicloud_azure / swagger / views / infra_workload / views.py
1 # Copyright (c) 2018 Amdocs
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at:
6 #
7 #       http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
13 import logging
14 import json
15
16 from rest_framework import status
17 from rest_framework.response import Response
18 from rest_framework.views import APIView
19
20 from multicloud_azure.pub.aria.service import AriaServiceImpl
21
22 logger = logging.getLogger(__name__)
23
24
25 class InfraWorkload(APIView):
26
27     def post(self, request, cloud_owner, cloud_region_id):
28         data = request.data
29         template_data = data["infra-template"]
30         payload = data["infra-payload"]
31         inputs = json.loads(payload)
32         template_name = inputs['template_data']['stack_name']
33         service_op = AriaServiceImpl()
34         try:
35             stack = service_op.deploy_service(template_name, template_data,
36                                               inputs, logger)
37             if stack[1] != 200:
38                 return Response(data=stack[0], status=stack[1])
39         except Exception as e:
40
41             if hasattr(e, "http_status"):
42                 return Response(data={'error': str(e)}, status=e.http_status)
43             else:
44                 return Response(data={'error': str(e)},
45                                 status=status.HTTP_500_INTERNAL_SERVER_ERROR)
46         rsp = {
47             "template_type": "heat",
48             "workload_id": stack[0]
49         }
50         return Response(data=rsp, status=status.HTTP_202_ACCEPTED)
51
52
53 class GetStackView(APIView):
54
55     def get(self, request, cloud_owner, cloud_region_id, workload_id):
56         service_op = AriaServiceImpl()
57         try:
58             stack = service_op.show_execution(workload_id)
59             if stack[1] != 200:
60                 return Response(data=stack[0], status=stack[1])
61             body = json.loads(stack[0])
62             stack_status = body["status"]
63             response = "unknown"
64             if stack_status == "pending" or stack_status == "started":
65                 response = "CREATE_IN_PROGRESS"
66             elif stack_status == "succeeded":
67                 response = "CREATE_COMPLETE"
68             elif stack_status == "failed" or stack_status == "cancelled":
69                 response = "CREATE_FAILED"
70             rsp = {
71                 "template_type": "heat",
72                 "workload_id": workload_id,
73                 "workload_status": response
74             }
75             return Response(data=rsp, status=stack[1])
76         except Exception as e:
77
78             if hasattr(e, "http_status"):
79                 return Response(data={'error': str(e)}, status=e.http_status)
80             else:
81                 return Response(data={'error': str(e)},
82                                 status=status.HTTP_500_INTERNAL_SERVER_ERROR)