summaryrefslogtreecommitdiffstats
path: root/azure/multicloud_azure/swagger/views/infra_workload/views.py
blob: c44eba2617bd944427cda2613abc84a42801f048 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
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)