add sasl support to kafka
[testsuite/python-testing-utils.git] / robotframework-onap / eteutils / OpenstackLibrary.py
1 from robot.libraries.BuiltIn import BuiltIn
2 import robot.utils
3 import json
4
5 class OpenstackLibrary:
6     """OpenstackLibrary manages the connection state and service catalog of an openstack instance."""
7
8     ROBOT_LIBRARY_SCOPE = 'Global'
9
10
11     def __init__(self):
12         self._cache = robot.utils.ConnectionCache('No connections created')
13         self.builtin = BuiltIn()
14
15     def save_openstack_auth(self, alias, response,token, version='v2.0'):
16         """Save Openstack Auth takes in an openstack auth response and saves it to allow easy retrival of token and service catalog"""
17         self.builtin.log('Creating connection: %s' % alias, 'DEBUG')
18         jsonResponse = json.loads(response);
19         jsonResponse['auth_token'] = token
20         jsonResponse['keystone_api_version'] = version
21         self._cache.register(jsonResponse, alias=alias)
22
23     def get_openstack_token(self, alias):
24         """Get Openstack auth token from the current alias"""
25         response = self._cache.switch(alias)
26         if isinstance(response, str):
27             jsonResponse = json.loads(response);
28         else:
29             jsonResponse = response;
30         if jsonResponse['keystone_api_version'] == 'v2.0':
31             return  jsonResponse['access']['token']['id']
32         else:
33             return  jsonResponse['auth_token']
34
35     def get_openstack_catalog(self, alias):
36         """Get Openstack service catalog from the current alias"""
37         response = self._cache.switch(alias)
38         if isinstance(response, str):
39             jsonResponse = json.loads(response);
40         else:
41             jsonResponse = response;
42         if jsonResponse['keystone_api_version'] == 'v2.0':
43             return  jsonResponse['access']['serviceCatalog']
44         else:
45             return  jsonResponse['token']['catalog']
46  
47
48     def get_current_openstack_tenant(self, alias):
49         """Get Openstack tenant from the current alias"""
50         response = self._cache.switch(alias)
51         if isinstance(response, str):
52             jsonResponse = json.loads(response);
53         else:
54             jsonResponse = response;
55         if jsonResponse['keystone_api_version'] == 'v2.0':
56             return  jsonResponse['access']['token']['tenant']
57         else:
58             return  jsonResponse['token']['project']
59
60     def get_current_openstack_tenant_id(self, alias):
61         """Get Openstack tenant id from the current alias"""
62         tenant = self.get_current_openstack_tenant(alias);
63         return  tenant['id']
64
65     def get_openstack_regions(self, alias):
66         """Get all Openstack regions from the current alias"""
67         response = self._cache.switch(alias)
68         if isinstance(response, str):
69             jsonResponse = json.loads(response);
70         else:
71             jsonResponse = response;
72         regions = [];
73         if jsonResponse['keystone_api_version'] == 'v2.0':
74             resp = jsonResponse['access']['serviceCatalog']
75         else:
76             resp = jsonResponse['token']['catalog']
77         for catalogEntry in resp:
78             listOfEndpoints = catalogEntry['endpoints'];
79             for endpoint in listOfEndpoints:
80                 if 'region'in endpoint:
81                     if endpoint['region'] not in regions:
82                         regions.append(endpoint['region'])
83         return regions;
84
85     def get_openstack_service_url(self, alias, servicetype, region =  None, tenant_id = None):
86         """Get Openstack service catalog from the current alias"""
87         response = self._cache.switch(alias)
88         if isinstance(response, str):
89             jsonResponse = json.loads(response);
90         else:
91             jsonResponse = response;
92         endPoint = None;
93         if jsonResponse['keystone_api_version'] == 'v2.0':
94             resp = jsonResponse['access']['serviceCatalog']
95         else:
96             resp = jsonResponse['token']['catalog']
97         for catalogEntry in resp:    
98             if self.__determine_match(catalogEntry['type'], servicetype):
99                 listOfEndpoints = catalogEntry['endpoints'];
100                 # filter out non matching regions if provided
101                 listOfEndpoints[:] = [x for x in listOfEndpoints if self.__determine_match(x['region'], region)];
102                 # filter out non matching tenants if provided
103                 # Only provide tenant id when authorizing without qualifying with tenant id
104                 # WindRiver does not return the tenantId on the endpoint in this case.
105                 if tenant_id is not None:
106                     listOfEndpoints[:] = [y for y in listOfEndpoints if self.__determine_match(y['tenantId'], tenant_id)];
107                 if jsonResponse['keystone_api_version'] == 'v3':
108                         listOfEndpoints[:] = [z for z in listOfEndpoints if self.__determine_match(z['interface'], 'public')];
109                 if len(listOfEndpoints) > 0:
110                     if jsonResponse['keystone_api_version'] == 'v2.0':
111                         endPoint = listOfEndpoints[0]['publicURL'];
112                     else:
113                         endPoint = listOfEndpoints[0]['url'];
114         if endPoint == None:
115             self.builtin.should_not_be_empty("", "Service Endpoint Url should not be empty")
116         return endPoint;
117
118     def __determine_match(self, listItem, item):
119         if item is None:
120             return True;
121         elif listItem == item:
122             return True;
123         else:
124             return False;