1 # Copyright (c) 2018 Amdocs
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:
7 # http://www.apache.org/licenses/LICENSE-2.0
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.
15 from rest_framework import status
16 from rest_framework.response import Response
17 from rest_framework.views import APIView
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
25 class FlavorsView(APIView):
27 def post(self, request, vimid):
29 create_req = json.loads(request.body)
30 except Exception as e:
31 return Response(data={'error': 'Fail to decode request body.'},
32 status=status.HTTP_500_INTERNAL_SERVER_ERROR)
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)
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()
50 target = flavor_id or flavor_name
51 flavor = flavors_op.find_flavor(data, target)
53 flavor, extra_specs = flavors_op.get_flavor(
58 flavor, extra_specs = flavors_op.create_flavor(
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)
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)
70 def get(self, request, vimid):
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)
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()
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)
91 return Response(data={'error': str(e)},
92 status=status.HTTP_500_INTERNAL_SERVER_ERROR)
94 rsp = {'vimId': vim_info['cloud_extra_info'],
95 'vimName': vim_info['name'],
96 'flavors': flavors_dict}
98 return Response(data=rsp, status=status.HTTP_200_OK)