blob: 8791450f94dff1f6bda95a16ce04cb3d0229dcbd [file] [log] [blame]
DR695H5fa01462017-02-15 18:21:12 -05001from robot.libraries.BuiltIn import BuiltIn
2import robot.utils
3import json
4
5class OpenstackLibrary:
6 """OpenstackLibrary manages the connection state and service catalog of an openstack instance."""
Jerry Floodc89a1632017-10-11 10:14:58 -04007
DR695H5fa01462017-02-15 18:21:12 -05008 ROBOT_LIBRARY_SCOPE = 'Global'
Jerry Floodc89a1632017-10-11 10:14:58 -04009
10
DR695H5fa01462017-02-15 18:21:12 -050011 def __init__(self):
12 self._cache = robot.utils.ConnectionCache('No connections created')
13 self.builtin = BuiltIn()
14
Ah21fcef52018-10-01 16:50:04 +053015 def save_openstack_auth(self, alias, response,token, version='v2.0'):
DR695H5fa01462017-02-15 18:21:12 -050016 """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')
Ah8d4eb662018-09-28 12:29:38 +053018 jsonResponse = json.loads(response);
19 jsonResponse['auth_token'] = token
20 jsonResponse['keystone_api_version'] = version
21 self._cache.register(jsonResponse, alias=alias)
Jerry Floodc89a1632017-10-11 10:14:58 -040022
DR695H5fa01462017-02-15 18:21:12 -050023 def get_openstack_token(self, alias):
24 """Get Openstack auth token from the current alias"""
25 response = self._cache.switch(alias)
DR695Hae6fedd2019-05-01 18:52:33 -040026 if isinstance(response, str):
DR695H5fa01462017-02-15 18:21:12 -050027 jsonResponse = json.loads(response);
28 else:
29 jsonResponse = response;
Ah8d4eb662018-09-28 12:29:38 +053030 if jsonResponse['keystone_api_version'] == 'v2.0':
31 return jsonResponse['access']['token']['id']
32 else:
33 return jsonResponse['auth_token']
Jerry Floodc89a1632017-10-11 10:14:58 -040034
DR695H5fa01462017-02-15 18:21:12 -050035 def get_openstack_catalog(self, alias):
36 """Get Openstack service catalog from the current alias"""
37 response = self._cache.switch(alias)
DR695Hae6fedd2019-05-01 18:52:33 -040038 if isinstance(response, str):
DR695H5fa01462017-02-15 18:21:12 -050039 jsonResponse = json.loads(response);
40 else:
41 jsonResponse = response;
Ah8d4eb662018-09-28 12:29:38 +053042 if jsonResponse['keystone_api_version'] == 'v2.0':
43 return jsonResponse['access']['serviceCatalog']
44 else:
45 return jsonResponse['token']['catalog']
46
Jerry Floodc89a1632017-10-11 10:14:58 -040047
DR695H5fa01462017-02-15 18:21:12 -050048 def get_current_openstack_tenant(self, alias):
49 """Get Openstack tenant from the current alias"""
50 response = self._cache.switch(alias)
DR695Hae6fedd2019-05-01 18:52:33 -040051 if isinstance(response, str):
DR695H5fa01462017-02-15 18:21:12 -050052 jsonResponse = json.loads(response);
53 else:
54 jsonResponse = response;
Ah8d4eb662018-09-28 12:29:38 +053055 if jsonResponse['keystone_api_version'] == 'v2.0':
56 return jsonResponse['access']['token']['tenant']
57 else:
58 return jsonResponse['token']['project']
Jerry Floodc89a1632017-10-11 10:14:58 -040059
DR695H5fa01462017-02-15 18:21:12 -050060 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']
Jerry Floodc89a1632017-10-11 10:14:58 -040064
DR695H5fa01462017-02-15 18:21:12 -050065 def get_openstack_regions(self, alias):
66 """Get all Openstack regions from the current alias"""
67 response = self._cache.switch(alias)
DR695Hae6fedd2019-05-01 18:52:33 -040068 if isinstance(response, str):
DR695H5fa01462017-02-15 18:21:12 -050069 jsonResponse = json.loads(response);
70 else:
71 jsonResponse = response;
72 regions = [];
Ah8d4eb662018-09-28 12:29:38 +053073 if jsonResponse['keystone_api_version'] == 'v2.0':
74 resp = jsonResponse['access']['serviceCatalog']
75 else:
76 resp = jsonResponse['token']['catalog']
77 for catalogEntry in resp:
DR695H5fa01462017-02-15 18:21:12 -050078 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;
Jerry Floodc89a1632017-10-11 10:14:58 -040084
DR695H5fa01462017-02-15 18:21:12 -050085 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)
DR695Hae6fedd2019-05-01 18:52:33 -040088 if isinstance(response, str):
DR695H5fa01462017-02-15 18:21:12 -050089 jsonResponse = json.loads(response);
90 else:
91 jsonResponse = response;
92 endPoint = None;
Ah8d4eb662018-09-28 12:29:38 +053093 if jsonResponse['keystone_api_version'] == 'v2.0':
94 resp = jsonResponse['access']['serviceCatalog']
95 else:
96 resp = jsonResponse['token']['catalog']
97 for catalogEntry in resp:
DR695H5fa01462017-02-15 18:21:12 -050098 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
Jerry Floodc89a1632017-10-11 10:14:58 -0400103 # 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:
DR695Hae6fedd2019-05-01 18:52:33 -0400106 listOfEndpoints[:] = [y for y in listOfEndpoints if self.__determine_match(y['tenantId'], tenant_id)];
Ah8d4eb662018-09-28 12:29:38 +0530107 if jsonResponse['keystone_api_version'] == 'v3':
108 listOfEndpoints[:] = [z for z in listOfEndpoints if self.__determine_match(z['interface'], 'public')];
DR695H5fa01462017-02-15 18:21:12 -0500109 if len(listOfEndpoints) > 0:
Ah8d4eb662018-09-28 12:29:38 +0530110 if jsonResponse['keystone_api_version'] == 'v2.0':
111 endPoint = listOfEndpoints[0]['publicURL'];
112 else:
113 endPoint = listOfEndpoints[0]['url'];
DR695H5fa01462017-02-15 18:21:12 -0500114 if endPoint == None:
115 self.builtin.should_not_be_empty("", "Service Endpoint Url should not be empty")
116 return endPoint;
Jerry Floodc89a1632017-10-11 10:14:58 -0400117
DR695H5fa01462017-02-15 18:21:12 -0500118 def __determine_match(self, listItem, item):
119 if item is None:
120 return True;
121 elif listItem == item:
122 return True;
123 else:
Jerry Floodc89a1632017-10-11 10:14:58 -0400124 return False;