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.
17 from rest_framework import status
18 from rest_framework.response import Response
19 from rest_framework.views import APIView
21 from multicloud_azure.pub.exceptions import VimDriverAzureException
22 from multicloud_azure.pub.msapi import extsys
23 from multicloud_azure.pub.utils.restcall import AAIClient
24 from multicloud_azure.pub.vim.vimapi.compute import OperateFlavors
27 logger = logging.getLogger(__name__)
30 class Registry(APIView):
31 def _get_flavors(self, auth_info):
32 flavors_op = OperateFlavors.OperateFlavors()
34 flavors = flavors_op.list_flavors(auth_info)
35 except Exception as e:
36 logger.exception("get flavors error %(e)s", {"e": e})
39 rsp = {"flavors": flavors}
42 def post(self, request, vimid):
44 vim_info = extsys.get_vim_by_id(vimid)
45 except VimDriverAzureException as e:
46 return Response(data={'error': str(e)}, status=e.status_code)
47 cloud_extra_info = json.loads(vim_info['cloud_extra_info'])
49 'subscription_id': cloud_extra_info['subscription_id'],
50 'username': vim_info['username'],
51 'password': vim_info['password'],
52 'tenant_id': vim_info['default_tenant'],
53 'region_id': vim_info['cloud-region-id']
57 logger.debug('Getting flavors')
58 flavors = self._get_flavors(data)
61 logger.debug('Put data into A&AI')
62 cloud_owner, cloud_region = extsys.split_vim_to_owner_region(
64 aai_adapter = AAIClient(cloud_owner, cloud_region)
65 aai_adapter.update_vim(rsp)
66 except Exception as e:
67 if hasattr(e, "http_status"):
68 return Response(data={'error': str(e)}, status=e.http_status)
70 return Response(data={'error': str(e)},
71 status=status.HTTP_500_INTERNAL_SERVER_ERROR)
73 return Response(data="", status=status.HTTP_200_OK)
76 class UnRegistry(APIView):
78 def delete(self, request, vimid):
80 cloud_owner, cloud_region = extsys.split_vim_to_owner_region(
82 aai_adapter = AAIClient(cloud_owner, cloud_region)
83 aai_adapter.delete_vim()
84 except Exception as e:
85 return Response(data=e.message,
86 status=status.HTTP_500_INTERNAL_SERVER_ERROR)
87 return Response(data="", status=status.HTTP_204_NO_CONTENT)
90 class APIv1Registry(Registry):
92 def post(self, request, cloud_owner, cloud_region_id):
93 vimid = extsys.encode_vim_id(cloud_owner, cloud_region_id)
94 return super(APIv1Registry, self).post(request, vimid)
97 class APIv1UnRegistry(UnRegistry):
99 def delete(self, request, cloud_owner, cloud_region_id):
100 vimid = extsys.encode_vim_id(cloud_owner, cloud_region_id)
101 return super(APIv1UnRegistry, self).delete(request, vimid)