summaryrefslogtreecommitdiffstats
path: root/azure/multicloud_azure/swagger/views/flavor/views.py
blob: 8b65cea575796bac59873b0f0e3a6987bf51b846 (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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# 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 json

from rest_framework import status
from rest_framework.response import Response
from rest_framework.views import APIView

from multicloud_azure.pub.msapi import extsys
from multicloud_azure.pub.vim.vimapi.compute import OperateFlavors
from multicloud_azure.swagger import compute_utils
from multicloud_azure.pub.exceptions import VimDriverAzureException


class FlavorsView(APIView):

    def post(self, request, vimid):
        try:
            create_req = json.loads(request.body)
        except Exception:
            return Response(data={'error': 'Fail to decode request body.'},
                            status=status.HTTP_500_INTERNAL_SERVER_ERROR)

        try:
            vim_info = extsys.get_vim_by_id(vimid)
        except VimDriverAzureException as e:
            return Response(data={'error': str(e)}, status=e.status_code)

        data = {'subscription_id': vim_info['cloud_extra_info'],
                'username': vim_info['username'],
                'password': vim_info['password'],
                'tenant_id': vim_info['default_tenant'],
                'region_id': vim_info['cloud_region']}
        rsp = {'vimId': vim_info['cloud_extra_info'],
               'vimName': vim_info['name']}
        flavor_name = create_req.get('name', None)
        flavor_id = create_req.get('id', None)
        flavors_op = OperateFlavors.OperateFlavors()
        try:
            target = flavor_id or flavor_name
            flavor = flavors_op.find_flavor(data, target)
            if flavor:
                flavor, extra_specs = flavors_op.get_flavor(
                    data, flavor.id)
                rsp['returnCode'] = 0
            else:
                rsp['returnCode'] = 1
                flavor, extra_specs = flavors_op.create_flavor(
                    data, create_req)
            flavor_dict = compute_utils.flavor_formatter(flavor, extra_specs)
        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.update(flavor_dict)
        return Response(data=rsp, status=status.HTTP_200_OK)

    def get(self, request, vimid):
        try:
            vim_info = extsys.get_vim_by_id(vimid)
        except VimDriverAzureException as e:
            return Response(data={'error': str(e)}, status=e.status_code)

        data = {'subscription_id': vim_info['cloud_extra_info'],
                'username': vim_info['username'],
                'password': vim_info['password'],
                'tenant_id': vim_info['default_tenant'],
                'region_id': vim_info['cloud_region_id']}
        query = dict(request.query_params)
        flavors_op = OperateFlavors.OperateFlavors()
        try:
            flavors_result = flavors_op.list_flavors(data, **query)
            flavors_dict = [compute_utils.flavor_formatter(flavor, extra)
                            for flavor, extra in flavors_result]
        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 = {'vimId': vim_info['cloud_extra_info'],
               'vimName': vim_info['name'],
               'flavors': flavors_dict}

        return Response(data=rsp, status=status.HTTP_200_OK)