8464ce40446f8d01174b686cfe60a9ff9f08c8f5
[multicloud/azure.git] / azure / multicloud_azure / swagger / views / registry / 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
14 import logging
15 import json
16
17 from rest_framework import status
18 from rest_framework.response import Response
19 from rest_framework.views import APIView
20
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
25
26
27 logger = logging.getLogger(__name__)
28
29
30 class Registry(APIView):
31     def _get_flavors(self, auth_info):
32         flavors_op = OperateFlavors.OperateFlavors()
33         try:
34             flavors = flavors_op.list_flavors(auth_info)
35         except Exception as e:
36             logger.exception("get flavors error %(e)s", {"e": e})
37             raise e
38
39         rsp = {"flavors": flavors}
40         return rsp
41
42     def post(self, request, vimid):
43         try:
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'])
48         data = {
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']
54         }
55         rsp = {}
56         try:
57             logger.debug('Getting flavors')
58             flavors = self._get_flavors(data)
59             rsp.update(flavors)
60             # update A&AI
61             logger.debug('Put data into A&AI')
62             cloud_owner, cloud_region = extsys.split_vim_to_owner_region(
63                 vimid)
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)
69             else:
70                 return Response(data={'error': str(e)},
71                                 status=status.HTTP_500_INTERNAL_SERVER_ERROR)
72
73         return Response(data="", status=status.HTTP_200_OK)
74
75
76 class UnRegistry(APIView):
77
78     def delete(self, request, vimid):
79         try:
80             cloud_owner, cloud_region = extsys.split_vim_to_owner_region(
81                     vimid)
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)