Added v1 registry API
[multicloud/azure.git] / azure / multicloud_azure / pub / msapi / extsys.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 logging
14
15 from multicloud_azure.pub.utils.restcall import AAIClient
16
17 logger = logging.getLogger(__name__)
18
19
20 def encode_vim_id(cloud_owner, cloud_region_id):
21     '''
22     compose vim_id by cloud_owner and cloud_region, make sure the vimid can be
23     converted back when talking to AAI,etc.
24     This is a backward compatibility design to reuse the existing
25     implementation code
26     :param cloud_owner:
27     :param cloud_region:
28     :return:
29     '''
30
31     # since the {cloud_owner}/{cloud_region_id"} is globally unique, the
32     # concatenated one as below will be unique as well.
33
34     vim_id = cloud_owner + "_" + cloud_region_id
35
36     # other options:
37     # 1, store it into cache so the decode and just look up the cache for
38     # decoding
39     # 2, use other delimiter in case that '_' was used by
40     # cloud owner/cloud region id,
41     # e.g. '.', '#', hence the decode need to try more than one time
42
43     return vim_id
44
45
46 def decode_vim_id(vim_id):
47     # m = re.search(r'^([0-9a-zA-Z-]+)_([0-9a-zA-Z_-]+)$', vim_id)
48     # cloud_owner, cloud_region_id = m.group(1), m.group(2)
49     return split_vim_to_owner_region(vim_id)
50
51
52 def split_vim_to_owner_region(vim_id):
53     split_vim = vim_id.split('_')
54     cloud_owner = split_vim[0]
55     cloud_region = "".join(split_vim[1:])
56     return cloud_owner, cloud_region
57
58
59 def get_vim_by_id(vim_id):
60     cloud_owner, cloud_region = split_vim_to_owner_region(vim_id)
61     client = AAIClient(cloud_owner, cloud_region)
62     ret = client.get_vim(get_all=True)
63     esrInfo = ret['esr-system-info-list']['esr-system-info'][0]
64     data = {
65         'type': ret['cloud-type'],
66         'version': ret['cloud-region-version'],
67         'cloud_extra_info': ret['cloud-extra-info'],
68         'cloud_region_id': ret['cloud-region-id'],
69         'name': vim_id,
70         'username': esrInfo['user-name'],
71         'password': esrInfo['password'],
72         'default_tenant': esrInfo['default-tenant'],
73         'url': esrInfo['service-url'],
74         'domain': esrInfo['cloud-domain'],
75         'cacert': esrInfo.get('ssl-cacert', ""),
76         'insecure': esrInfo.get('ssl-insecure', False)
77     }
78     ret.update(data)
79     return ret