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 multicloud_azure.pub.utils.restcall import AAIClient
17 logger = logging.getLogger(__name__)
20 def encode_vim_id(cloud_owner, cloud_region_id):
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
31 # since the {cloud_owner}/{cloud_region_id"} is globally unique, the
32 # concatenated one as below will be unique as well.
34 vim_id = cloud_owner + "_" + cloud_region_id
37 # 1, store it into cache so the decode and just look up the cache for
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
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)
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
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]
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'],
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)