1 from robot.libraries.BuiltIn import BuiltIn
5 class OpenstackLibrary:
6 """OpenstackLibrary manages the connection state and service catalog of an openstack instance."""
8 ROBOT_LIBRARY_SCOPE = 'Global'
12 self._cache = robot.utils.ConnectionCache('No connections created')
13 self.builtin = BuiltIn()
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)
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, basestring):
27 jsonResponse = json.loads(response);
29 jsonResponse = response;
30 if jsonResponse['keystone_api_version'] == 'v2.0':
31 return jsonResponse['access']['token']['id']
33 return jsonResponse['auth_token']
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, basestring):
39 jsonResponse = json.loads(response);
41 jsonResponse = response;
42 if jsonResponse['keystone_api_version'] == 'v2.0':
43 return jsonResponse['access']['serviceCatalog']
45 return jsonResponse['token']['catalog']
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, basestring):
52 jsonResponse = json.loads(response);
54 jsonResponse = response;
55 if jsonResponse['keystone_api_version'] == 'v2.0':
56 return jsonResponse['access']['token']['tenant']
58 return jsonResponse['token']['project']
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);
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, basestring):
69 jsonResponse = json.loads(response);
71 jsonResponse = response;
73 if jsonResponse['keystone_api_version'] == 'v2.0':
74 resp = jsonResponse['access']['serviceCatalog']
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'])
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, basestring):
89 jsonResponse = json.loads(response);
91 jsonResponse = response;
93 if jsonResponse['keystone_api_version'] == 'v2.0':
94 resp = jsonResponse['access']['serviceCatalog']
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'];
113 endPoint = listOfEndpoints[0]['url'];
115 self.builtin.should_not_be_empty("", "Service Endpoint Url should not be empty")
118 def __determine_match(self, listItem, item):
121 elif listItem == item: