def _update_endpoint_from_keystone(self, cxt, is_internal): """Update the database by querying service endpoint url from Keystone :param cxt: context object :param is_internal: if True, this method utilizes pre-configured admin username and password to apply an new admin token, this happens only when auto_refresh_endpoint is set to True. if False, token in cxt is directly used, users should prepare admin token themselves :return: None """ if is_internal: admin_context = tricircle_context.Context() admin_context.auth_token = self._get_admin_token() endpoint_map = self._get_endpoint_from_keystone(admin_context) else: endpoint_map = self._get_endpoint_from_keystone(cxt) for region in endpoint_map: # use region name to query pod pod_filters = [{ 'key': 'pod_name', 'comparator': 'eq', 'value': region }] pod_list = api.list_pods(cxt, pod_filters) # skip region/pod not registered in cascade service if len(pod_list) != 1: continue for service in endpoint_map[region]: pod_id = pod_list[0]['pod_id'] config_filters = [{ 'key': 'pod_id', 'comparator': 'eq', 'value': pod_id }, { 'key': 'service_type', 'comparator': 'eq', 'value': service }] config_list = api.list_pod_service_configurations( cxt, config_filters) if len(config_list) > 1: raise exceptions.EndpointNotUnique(pod_id, service) if len(config_list) == 1: config_id = config_list[0]['service_id'] update_dict = { 'service_url': endpoint_map[region][service] } api.update_pod_service_configuration( cxt, config_id, update_dict) else: config_dict = { 'service_id': str(uuid.uuid4()), 'pod_id': pod_id, 'service_type': service, 'service_url': endpoint_map[region][service] } api.create_pod_service_configuration(cxt, config_dict)
def test_list_endpoint_not_valid(self): cfg.CONF.set_override(name='auto_refresh_endpoint', override=False, group='client') update_dict = {'service_url': FAKE_URL_INVALID} # update url to an invalid one api.update_pod_service_configuration(self.context, FAKE_SERVICE_ID, update_dict) # auto refresh set to False, directly raise exception self.assertRaises(exceptions.EndpointNotAvailable, self.client.list_resources, FAKE_RESOURCE, self.context, [])
def _update_endpoint_from_keystone(self, cxt, is_internal): """Update the database by querying service endpoint url from Keystone :param cxt: context object :param is_internal: if True, this method utilizes pre-configured admin username and password to apply an new admin token, this happens only when auto_refresh_endpoint is set to True. if False, token in cxt is directly used, users should prepare admin token themselves :return: None """ if is_internal: admin_context = tricircle_context.Context() admin_context.auth_token = self._get_admin_token() endpoint_map = self._get_endpoint_from_keystone(admin_context) else: endpoint_map = self._get_endpoint_from_keystone(cxt) for region in endpoint_map: # use region name to query pod pod_filters = [{'key': 'pod_name', 'comparator': 'eq', 'value': region}] pod_list = api.list_pods(cxt, pod_filters) # skip region/pod not registered in cascade service if len(pod_list) != 1: continue for service in endpoint_map[region]: pod_id = pod_list[0]['pod_id'] config_filters = [{'key': 'pod_id', 'comparator': 'eq', 'value': pod_id}, {'key': 'service_type', 'comparator': 'eq', 'value': service}] config_list = api.list_pod_service_configurations( cxt, config_filters) if len(config_list) > 1: continue if len(config_list) == 1: config_id = config_list[0]['service_id'] update_dict = { 'service_url': endpoint_map[region][service]} api.update_pod_service_configuration( cxt, config_id, update_dict) else: config_dict = { 'service_id': str(uuid.uuid4()), 'pod_id': pod_id, 'service_type': service, 'service_url': endpoint_map[region][service] } api.create_pod_service_configuration( cxt, config_dict)
def test_list_endpoint_not_valid_retry(self): cfg.CONF.set_override(name='auto_refresh_endpoint', override=True, group='client') update_dict = {'service_url': FAKE_URL_INVALID} # update url to an invalid one api.update_pod_service_configuration(self.context, FAKE_SERVICE_ID, update_dict) self.client._get_admin_token = mock.Mock() self.client._get_endpoint_from_keystone = mock.Mock() self.client._get_endpoint_from_keystone.return_value = { FAKE_SITE_NAME: {FAKE_TYPE: FAKE_URL} } resources = self.client.list_resources( FAKE_RESOURCE, self.context, []) self.assertEqual(resources, [{'name': 'res1'}, {'name': 'res2'}])
def test_list_endpoint_not_valid_retry(self): cfg.CONF.set_override(name='auto_refresh_endpoint', override=True, group='client') update_dict = {'service_url': FAKE_URL_INVALID} # update url to an invalid one api.update_pod_service_configuration(self.context, FAKE_SERVICE_ID, update_dict) self.client._get_admin_token = mock.Mock() self.client._get_endpoint_from_keystone = mock.Mock() self.client._get_endpoint_from_keystone.return_value = { FAKE_SITE_NAME: { FAKE_TYPE: FAKE_URL } } resources = self.client.list_resources(FAKE_RESOURCE, self.context, []) self.assertEqual(resources, [{'name': 'res1'}, {'name': 'res2'}])