Added V0 Registry API
[multicloud/azure.git] / azure / multicloud_azure / pub / utils / syscomm.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 inspect
14 import json
15 from collections import defaultdict
16 from rest_framework import status
17
18
19 keystoneV2Json = \
20     {
21         "auth": {
22             "tenantName": "",
23             "passwordCredentials": {
24                 "username": "",
25                 "password": ""
26             }
27         }
28     }
29
30
31 SUCCESS_STATE = [status.HTTP_200_OK, status.HTTP_201_CREATED,
32                  status.HTTP_202_ACCEPTED]
33
34
35 def fun_name():
36     return inspect.stack()[1][3]
37
38
39 def jsonResponse(data, encoding='utf-8'):
40
41     content_type = "application/json"
42     try:
43         res = json.loads(data, encoding=encoding)
44     except Exception:
45         res = data
46         content_type = "text/plain"
47     return (res, content_type)
48
49
50 class Catalogs(object):
51
52     def __init__(self):
53         self.ct = defaultdict(dict)
54
55     def storeEndpoint(self, vimid, endpoints):
56         if vimid in self.ct:
57             self.ct[vimid].update(endpoints)
58         else:
59             self.ct.setdefault(vimid, endpoints)
60
61     def getEndpointBy(self, vimid, serverType, interface='public'):
62
63         vim = self.ct.get(vimid)
64         return vim.get(serverType).get(interface, "") if vim else ""
65
66
67 def verifyKeystoneV2(param):
68
69     return _walk_json(param, keystoneV2Json)
70
71
72 # comapare two json by key
73 def _walk_json(data, data2):
74     if isinstance(data, dict) and isinstance(data2, dict):
75         if set(data.keys()) != set(data2.keys()):
76             return False
77         else:
78             v1 = data.values()
79             v2 = data2.values()
80             v1.sort()
81             v2.sort()
82             if len(v1) != len(v2):
83                 return False
84             for (i, j) in zip(v1, v2):
85                 # continue compare key
86                 if isinstance(i, dict) and isinstance(j, dict):
87                     if not _walk_json(i, j):
88                         return False
89                 # ignore value
90                 else:
91                     continue
92
93             return True
94
95     return False
96
97
98 def keystoneVersion(url, version="v3"):
99
100     tmp = url.split("/")
101     v = tmp[-1]
102     if v not in ["v2.0", "v3"]:
103         url += "/" + version
104     else:
105         tmp[-1] = version
106         url = "/".join(tmp)
107
108     return url
109
110
111 catalog = Catalogs()