Update Keystone resource search

Change-Id: I7a502dc202afed07c5f129fb26150958d84d0b81
This commit is contained in:
Federico Ressi 2019-07-26 16:25:34 +02:00
parent 211e1a8c27
commit fd25eb61d0
4 changed files with 65 additions and 94 deletions

View File

@ -26,8 +26,6 @@ find_service_endpoint = _client.find_service_endpoint
list_endpoints = _client.list_endpoints
list_services = _client.list_services
KeystoneClientFixture = _client.KeystoneClientFixture
KeystoneResourceNotFound = _client.KeystoneResourceNotFound
MultipleKeystoneResourcesFound = _client.MultipleKeystoneResourcesFound
keystone_credentials = _credentials.keystone_credentials
get_keystone_credentials = _credentials.get_keystone_credentials

View File

@ -65,31 +65,43 @@ def get_keystone_client(session=None, shared=True, init_client=None,
return client.client
def find_endpoint(client=None, check_found=True, check_unique=False,
**params):
endpoints = list_endpoints(client=client, **params)
return find_resource(resources=endpoints, check_found=check_found,
check_unique=check_unique)
_RAISE_ERROR = object()
def find_service(client=None, check_found=True, check_unique=False,
**params):
services = list_services(client=client, **params)
return find_resource(resources=services, check_found=check_found,
check_unique=check_unique)
def find_endpoint(client=None, unique=False, default=_RAISE_ERROR,
**attributes):
endpoints = list_endpoints(client=client, **attributes)
if default is _RAISE_ERROR or endpoints:
if unique:
return endpoints.unique
else:
return endpoints.first
else:
return default
def find_service(client=None, unique=False, default=_RAISE_ERROR, **attribute):
services = list_services(client=client, **attribute)
if default is _RAISE_ERROR or services:
if unique:
return services.unique
else:
return services.first
else:
return default
def list_endpoints(client=None, service=None, interface=None, region=None,
translate=True, **params):
translate=True, **attributes):
client = keystone_client(client)
service = service or params.pop('service_id', None)
service = service or attributes.pop('service_id', None)
if service:
params['service_id'] = base.getid(service)
attributes['service_id'] = base.getid(service)
region = region or params.pop('region_id', None)
region = region or attributes.pop('region_id', None)
if region:
params['region_id'] = base.getid(region)
attributes['region_id'] = base.getid(region)
if client.version == 'v2.0':
endpoints = client.endpoints.list()
@ -100,30 +112,31 @@ def list_endpoints(client=None, service=None, interface=None, region=None,
endpoints = client.endpoints.list(service=service,
interface=interface,
region=region)
if params:
endpoints = find_resources(endpoints, **params)
return list(endpoints)
endpoints = tobiko.select(endpoints)
if attributes:
endpoints = endpoints.with_attributes(**attributes)
return endpoints
def list_services(client=None, name=None, service_type=None, **params):
def list_services(client=None, name=None, service_type=None, **attributes):
client = keystone_client(client)
service_type = service_type or params.pop('type', None)
service_type = service_type or attributes.pop('type', None)
if service_type:
params['type'] = base.getid(service_type)
attributes['type'] = base.getid(service_type)
if name:
params['name'] = name
attributes['name'] = name
if client.version == 'v2.0':
services = client.services.list()
else:
services = client.services.list(name=name,
service_type=service_type)
if params:
services = find_resources(services, **params)
return list(services)
services = tobiko.select(services)
if attributes:
services = services.with_attributes(**attributes)
return services
def translate_v2_endpoints(v2_endpoints, interface=None):
@ -143,48 +156,9 @@ def translate_v2_endpoints(v2_endpoints, interface=None):
return endpoints
def find_resource(resources, check_found=True, check_unique=True, **params):
"""Look for a service matching some property values"""
resource_it = find_resources(resources, **params)
try:
resource = next(resource_it)
except StopIteration:
resource = None
if check_found and resource is None:
raise KeystoneResourceNotFound(params=params)
if check_unique:
duplicate_ids = [s.id for s in resource_it]
if duplicate_ids:
raise MultipleKeystoneResourcesFound(params=params)
return resource
def find_resources(resources, **params):
"""Look for a service matching some property values"""
# Remove parameters with None value
for resource in resources:
for name, match in params.items():
value = getattr(resource, name)
if match is not None and match != value:
break
else:
yield resource
def find_service_endpoint(enabled=True, interface='public', client=None,
**params):
client = keystone_client(client)
service = find_service(client=client, enabled=enabled, **params)
return find_endpoint(client=client, service=service, interface=interface,
enabled=enabled)
class KeystoneResourceNotFound(tobiko.TobikoException):
message = 'No such resource found with parameters {params!r}'
class MultipleKeystoneResourcesFound(tobiko.TobikoException):
message = 'Multiple resources found with parameters {params!r}'

View File

@ -25,21 +25,16 @@ class ServiceListFixture(tobiko.SharedFixture):
def setup_fixture(self):
self.services = _client.list_services()
def has_service(self, **params):
try:
_client.find_resource(self.services,
check_found=True,
check_unique=False,
**params)
except _client.KeystoneResourceNotFound:
return False
else:
return True
def has_service(self, **attributes):
services = self.services
if services and attributes:
services = services.with_attributes(**attributes)
return bool(services)
def has_service(**params):
def has_service(**attributes):
fixture = tobiko.setup_fixture(ServiceListFixture)
return fixture.has_service(**params)
return fixture.has_service(**attributes)
def is_service_missing(**params):

View File

@ -21,6 +21,7 @@ from oslo_log import log
import testtools
import yaml
import tobiko
from tobiko.openstack import keystone
from tobiko.shell import sh
@ -66,17 +67,19 @@ class KeystoneClientAPITest(testtools.TestCase):
service = keystone.find_service()
self.assertTrue(service.id)
def test_find_service_with_check_unique(self):
self.assertRaises(keystone.MultipleKeystoneResourcesFound,
keystone.find_service, check_unique=True)
def test_find_service_with_unique(self):
self.assertRaises(tobiko.MultipleObjectsFound,
keystone.find_service,
unique=True)
def test_find_service_not_found(self):
self.assertRaises(keystone.KeystoneResourceNotFound,
keystone.find_service, name='never-never-land')
self.assertRaises(tobiko.ObjectNotFound,
keystone.find_service,
name='never-never-land')
def test_find_service_without_check_found(self):
service = keystone.find_service(check_found=False,
name='never-never-land')
def test_find_service_with_defaulkt(self):
service = keystone.find_service(name='never-never-land',
default=None)
self.assertIsNone(service)
def test_find_service_by_name(self):
@ -123,18 +126,19 @@ class KeystoneClientAPITest(testtools.TestCase):
endpoint = keystone.find_endpoint()
self.assertTrue(endpoint.id)
def test_find_endpoint_with_check_unique(self):
self.assertRaises(keystone.MultipleKeystoneResourcesFound,
keystone.find_endpoint, check_unique=True)
def test_find_endpoint_with_unique(self):
self.assertRaises(tobiko.MultipleObjectsFound,
keystone.find_endpoint,
unique=True)
def test_find_endpoint_not_found(self):
self.assertRaises(keystone.KeystoneResourceNotFound,
self.assertRaises(tobiko.ObjectNotFound,
keystone.find_endpoint,
service='never-never-land')
def test_find_endpoint_without_check_found(self):
service = keystone.find_endpoint(check_found=False,
service='never-never-land')
def test_find_endpoint_with_default(self):
service = keystone.find_endpoint(service='never-never-land',
default=None)
self.assertIsNone(service)
def test_find_endpoint_by_service(self):