DR695H | 5fa0146 | 2017-02-15 18:21:12 -0500 | [diff] [blame] | 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.""" |
Jerry Flood | c89a163 | 2017-10-11 10:14:58 -0400 | [diff] [blame] | 7 | |
DR695H | 5fa0146 | 2017-02-15 18:21:12 -0500 | [diff] [blame] | 8 | ROBOT_LIBRARY_SCOPE = 'Global' |
Jerry Flood | c89a163 | 2017-10-11 10:14:58 -0400 | [diff] [blame] | 9 | |
| 10 | |
DR695H | 5fa0146 | 2017-02-15 18:21:12 -0500 | [diff] [blame] | 11 | def __init__(self): |
| 12 | self._cache = robot.utils.ConnectionCache('No connections created') |
| 13 | self.builtin = BuiltIn() |
| 14 | |
Ah | 21fcef5 | 2018-10-01 16:50:04 +0530 | [diff] [blame] | 15 | def save_openstack_auth(self, alias, response,token, version='v2.0'): |
DR695H | 5fa0146 | 2017-02-15 18:21:12 -0500 | [diff] [blame] | 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') |
Ah | 8d4eb66 | 2018-09-28 12:29:38 +0530 | [diff] [blame] | 18 | jsonResponse = json.loads(response); |
| 19 | jsonResponse['auth_token'] = token |
| 20 | jsonResponse['keystone_api_version'] = version |
| 21 | self._cache.register(jsonResponse, alias=alias) |
Jerry Flood | c89a163 | 2017-10-11 10:14:58 -0400 | [diff] [blame] | 22 | |
DR695H | 5fa0146 | 2017-02-15 18:21:12 -0500 | [diff] [blame] | 23 | def get_openstack_token(self, alias): |
| 24 | """Get Openstack auth token from the current alias""" |
| 25 | response = self._cache.switch(alias) |
DR695H | ae6fedd | 2019-05-01 18:52:33 -0400 | [diff] [blame] | 26 | if isinstance(response, str): |
DR695H | 5fa0146 | 2017-02-15 18:21:12 -0500 | [diff] [blame] | 27 | jsonResponse = json.loads(response); |
| 28 | else: |
| 29 | jsonResponse = response; |
Ah | 8d4eb66 | 2018-09-28 12:29:38 +0530 | [diff] [blame] | 30 | if jsonResponse['keystone_api_version'] == 'v2.0': |
| 31 | return jsonResponse['access']['token']['id'] |
| 32 | else: |
| 33 | return jsonResponse['auth_token'] |
Jerry Flood | c89a163 | 2017-10-11 10:14:58 -0400 | [diff] [blame] | 34 | |
DR695H | 5fa0146 | 2017-02-15 18:21:12 -0500 | [diff] [blame] | 35 | def get_openstack_catalog(self, alias): |
| 36 | """Get Openstack service catalog from the current alias""" |
| 37 | response = self._cache.switch(alias) |
DR695H | ae6fedd | 2019-05-01 18:52:33 -0400 | [diff] [blame] | 38 | if isinstance(response, str): |
DR695H | 5fa0146 | 2017-02-15 18:21:12 -0500 | [diff] [blame] | 39 | jsonResponse = json.loads(response); |
| 40 | else: |
| 41 | jsonResponse = response; |
Ah | 8d4eb66 | 2018-09-28 12:29:38 +0530 | [diff] [blame] | 42 | if jsonResponse['keystone_api_version'] == 'v2.0': |
| 43 | return jsonResponse['access']['serviceCatalog'] |
| 44 | else: |
| 45 | return jsonResponse['token']['catalog'] |
| 46 | |
Jerry Flood | c89a163 | 2017-10-11 10:14:58 -0400 | [diff] [blame] | 47 | |
DR695H | 5fa0146 | 2017-02-15 18:21:12 -0500 | [diff] [blame] | 48 | def get_current_openstack_tenant(self, alias): |
| 49 | """Get Openstack tenant from the current alias""" |
| 50 | response = self._cache.switch(alias) |
DR695H | ae6fedd | 2019-05-01 18:52:33 -0400 | [diff] [blame] | 51 | if isinstance(response, str): |
DR695H | 5fa0146 | 2017-02-15 18:21:12 -0500 | [diff] [blame] | 52 | jsonResponse = json.loads(response); |
| 53 | else: |
| 54 | jsonResponse = response; |
Ah | 8d4eb66 | 2018-09-28 12:29:38 +0530 | [diff] [blame] | 55 | if jsonResponse['keystone_api_version'] == 'v2.0': |
| 56 | return jsonResponse['access']['token']['tenant'] |
| 57 | else: |
| 58 | return jsonResponse['token']['project'] |
Jerry Flood | c89a163 | 2017-10-11 10:14:58 -0400 | [diff] [blame] | 59 | |
DR695H | 5fa0146 | 2017-02-15 18:21:12 -0500 | [diff] [blame] | 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'] |
Jerry Flood | c89a163 | 2017-10-11 10:14:58 -0400 | [diff] [blame] | 64 | |
DR695H | 5fa0146 | 2017-02-15 18:21:12 -0500 | [diff] [blame] | 65 | def get_openstack_regions(self, alias): |
| 66 | """Get all Openstack regions from the current alias""" |
| 67 | response = self._cache.switch(alias) |
DR695H | ae6fedd | 2019-05-01 18:52:33 -0400 | [diff] [blame] | 68 | if isinstance(response, str): |
DR695H | 5fa0146 | 2017-02-15 18:21:12 -0500 | [diff] [blame] | 69 | jsonResponse = json.loads(response); |
| 70 | else: |
| 71 | jsonResponse = response; |
| 72 | regions = []; |
Ah | 8d4eb66 | 2018-09-28 12:29:38 +0530 | [diff] [blame] | 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: |
DR695H | 5fa0146 | 2017-02-15 18:21:12 -0500 | [diff] [blame] | 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; |
Jerry Flood | c89a163 | 2017-10-11 10:14:58 -0400 | [diff] [blame] | 84 | |
DR695H | 5fa0146 | 2017-02-15 18:21:12 -0500 | [diff] [blame] | 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) |
DR695H | ae6fedd | 2019-05-01 18:52:33 -0400 | [diff] [blame] | 88 | if isinstance(response, str): |
DR695H | 5fa0146 | 2017-02-15 18:21:12 -0500 | [diff] [blame] | 89 | jsonResponse = json.loads(response); |
| 90 | else: |
| 91 | jsonResponse = response; |
| 92 | endPoint = None; |
Ah | 8d4eb66 | 2018-09-28 12:29:38 +0530 | [diff] [blame] | 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: |
DR695H | 5fa0146 | 2017-02-15 18:21:12 -0500 | [diff] [blame] | 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 |
Jerry Flood | c89a163 | 2017-10-11 10:14:58 -0400 | [diff] [blame] | 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: |
DR695H | ae6fedd | 2019-05-01 18:52:33 -0400 | [diff] [blame] | 106 | listOfEndpoints[:] = [y for y in listOfEndpoints if self.__determine_match(y['tenantId'], tenant_id)]; |
Ah | 8d4eb66 | 2018-09-28 12:29:38 +0530 | [diff] [blame] | 107 | if jsonResponse['keystone_api_version'] == 'v3': |
| 108 | listOfEndpoints[:] = [z for z in listOfEndpoints if self.__determine_match(z['interface'], 'public')]; |
DR695H | 5fa0146 | 2017-02-15 18:21:12 -0500 | [diff] [blame] | 109 | if len(listOfEndpoints) > 0: |
Ah | 8d4eb66 | 2018-09-28 12:29:38 +0530 | [diff] [blame] | 110 | if jsonResponse['keystone_api_version'] == 'v2.0': |
| 111 | endPoint = listOfEndpoints[0]['publicURL']; |
| 112 | else: |
| 113 | endPoint = listOfEndpoints[0]['url']; |
DR695H | 5fa0146 | 2017-02-15 18:21:12 -0500 | [diff] [blame] | 114 | if endPoint == None: |
| 115 | self.builtin.should_not_be_empty("", "Service Endpoint Url should not be empty") |
| 116 | return endPoint; |
Jerry Flood | c89a163 | 2017-10-11 10:14:58 -0400 | [diff] [blame] | 117 | |
DR695H | 5fa0146 | 2017-02-15 18:21:12 -0500 | [diff] [blame] | 118 | def __determine_match(self, listItem, item): |
| 119 | if item is None: |
| 120 | return True; |
| 121 | elif listItem == item: |
| 122 | return True; |
| 123 | else: |
Jerry Flood | c89a163 | 2017-10-11 10:14:58 -0400 | [diff] [blame] | 124 | return False; |