Fix tox issue
[multicloud/azure.git] / azure / multicloud_azure / swagger / views / flavor / 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 json
14
15 from rest_framework import status
16 from rest_framework.response import Response
17 from rest_framework.views import APIView
18
19 from multicloud_azure.pub.msapi import extsys
20 from multicloud_azure.pub.vim.vimapi.compute import OperateFlavors
21 from multicloud_azure.swagger import compute_utils
22 from multicloud_azure.pub.exceptions import VimDriverAzureException
23
24
25 class FlavorsView(APIView):
26
27     def post(self, request, vimid):
28         try:
29             create_req = json.loads(request.body)
30         except Exception:
31             return Response(data={'error': 'Fail to decode request body.'},
32                             status=status.HTTP_500_INTERNAL_SERVER_ERROR)
33
34         try:
35             vim_info = extsys.get_vim_by_id(vimid)
36         except VimDriverAzureException as e:
37             return Response(data={'error': str(e)}, status=e.status_code)
38
39         data = {'subscription_id': vim_info['cloud_extra_info'],
40                 'username': vim_info['username'],
41                 'password': vim_info['password'],
42                 'tenant_id': vim_info['default_tenant'],
43                 'region_id': vim_info['cloud_region']}
44         rsp = {'vimId': vim_info['cloud_extra_info'],
45                'vimName': vim_info['name']}
46         flavor_name = create_req.get('name', None)
47         flavor_id = create_req.get('id', None)
48         flavors_op = OperateFlavors.OperateFlavors()
49         try:
50             target = flavor_id or flavor_name
51             flavor = flavors_op.find_flavor(data, target)
52             if flavor:
53                 flavor, extra_specs = flavors_op.get_flavor(
54                     data, flavor.id)
55                 rsp['returnCode'] = 0
56             else:
57                 rsp['returnCode'] = 1
58                 flavor, extra_specs = flavors_op.create_flavor(
59                     data, create_req)
60             flavor_dict = compute_utils.flavor_formatter(flavor, extra_specs)
61         except Exception as e:
62             if hasattr(e, "http_status"):
63                 return Response(data={'error': str(e)}, status=e.http_status)
64             else:
65                 return Response(data={'error': str(e)},
66                                 status=status.HTTP_500_INTERNAL_SERVER_ERROR)
67         rsp.update(flavor_dict)
68         return Response(data=rsp, status=status.HTTP_200_OK)
69
70     def get(self, request, vimid):
71         try:
72             vim_info = extsys.get_vim_by_id(vimid)
73         except VimDriverAzureException as e:
74             return Response(data={'error': str(e)}, status=e.status_code)
75
76         data = {'subscription_id': vim_info['cloud_extra_info'],
77                 'username': vim_info['username'],
78                 'password': vim_info['password'],
79                 'tenant_id': vim_info['default_tenant'],
80                 'region_id': vim_info['cloud_region_id']}
81         query = dict(request.query_params)
82         flavors_op = OperateFlavors.OperateFlavors()
83         try:
84             flavors_result = flavors_op.list_flavors(data, **query)
85             flavors_dict = [compute_utils.flavor_formatter(flavor, extra)
86                             for flavor, extra in flavors_result]
87         except Exception as e:
88             if hasattr(e, "http_status"):
89                 return Response(data={'error': str(e)}, status=e.http_status)
90             else:
91                 return Response(data={'error': str(e)},
92                                 status=status.HTTP_500_INTERNAL_SERVER_ERROR)
93
94         rsp = {'vimId': vim_info['cloud_extra_info'],
95                'vimName': vim_info['name'],
96                'flavors': flavors_dict}
97
98         return Response(data=rsp, status=status.HTTP_200_OK)