Consolidate client version checks in an utility method

Change-Id: I8da6f940a5983ce38a4a5bdc054f203088c51c48
This commit is contained in:
Samuel de Medeiros Queiroz 2017-08-14 13:26:46 -04:00
parent 697cf58c82
commit 7466aaedfa
2 changed files with 41 additions and 36 deletions

View File

@ -362,6 +362,10 @@ class OpenStackCloud(
region_name=self.cloud_config.region,
shade_logger=self.log)
def _is_client_version(self, client, version):
api_version = self.cloud_config.get_api_version(client)
return api_version.startswith(str(version))
@property
def _application_catalog_client(self):
if 'application-catalog' not in self._raw_clients:
@ -718,7 +722,7 @@ class OpenStackCloud(
# Keystone v2 calls this attribute tenants
# Keystone v3 calls it projects
# Yay for usable APIs!
if self.cloud_config.get_api_version('identity').startswith('2'):
if self._is_client_version('identity', 2):
return self.keystone_client.tenants
return self.keystone_client.projects
@ -727,7 +731,7 @@ class OpenStackCloud(
project = self.get_project(name_or_id)
if not project:
return {}
if self.cloud_config.get_api_version('identity') == '3':
if self._is_client_version('identity', 3):
return {'default_project_id': project['id']}
else:
return {'tenant_id': project['id']}
@ -741,7 +745,7 @@ class OpenStackCloud(
# not. However, keystone v2 does not allow user creation by non-admin
# users, so we can throw an error to the user that does not need to
# mention api versions
if self.cloud_config.get_api_version('identity') == '3':
if self._is_client_version('identity', 3):
if not domain_id:
raise OpenStackCloudException(
"User or project creation requires an explicit"
@ -840,14 +844,16 @@ class OpenStackCloud(
kwargs = dict(
filters=filters,
domain_id=domain_id)
if self.cloud_config.get_api_version('identity') == '3':
if self._is_client_version('identity', 3):
kwargs['obj_name'] = 'project'
pushdown, filters = _normalize._split_filters(**kwargs)
try:
api_version = self.cloud_config.get_api_version('identity')
key = 'projects' if api_version == '3' else 'tenants'
if self._is_client_version('identity', 3):
key = 'projects'
else:
key = 'tenants'
data = self._identity_client.get(
'/{endpoint}'.format(endpoint=key), params=pushdown)
projects = self._normalize_projects(
@ -897,7 +903,7 @@ class OpenStackCloud(
kwargs.update({'enabled': enabled})
# NOTE(samueldmq): Current code only allow updates of description
# or enabled fields.
if self.cloud_config.get_api_version('identity') == '3':
if self._is_client_version('identity', 3):
data = self._identity_client.patch(
'/projects/' + proj['id'], json={'project': kwargs})
project = self._get_and_munchify('project', data)
@ -919,7 +925,7 @@ class OpenStackCloud(
'description': description,
'enabled': enabled})
endpoint, key = ('tenants', 'tenant')
if self.cloud_config.get_api_version('identity') == '3':
if self._is_client_version('identity', 3):
endpoint, key = ('projects', 'project')
data = self._identity_client.post(
'/{endpoint}'.format(endpoint=endpoint),
@ -951,7 +957,7 @@ class OpenStackCloud(
"Project %s not found for deleting", name_or_id)
return False
if self.cloud_config.get_api_version('identity') == '3':
if self._is_client_version('identity', 3):
self._identity_client.delete('/projects/' + project['id'])
else:
self._identity_client.delete('/tenants/' + project['id'])
@ -1034,7 +1040,7 @@ class OpenStackCloud(
# TODO(mordred) When this changes to REST, force interface=admin
# in the adapter call if it's an admin force call (and figure out how
# to make that disctinction)
if self.cloud_config.get_api_version('identity') != '3':
if not self._is_client_version('identity', 3):
# Do not pass v3 args to a v2 keystone.
kwargs.pop('domain_id', None)
kwargs.pop('description', None)
@ -1065,7 +1071,7 @@ class OpenStackCloud(
params = self._get_identity_params(domain_id, default_project)
params.update({'name': name, 'password': password, 'email': email,
'enabled': enabled})
if self.cloud_config.get_api_version('identity') == '3':
if self._is_client_version('identity', 3):
params['description'] = description
elif description is not None:
self.log.info(
@ -2161,7 +2167,7 @@ class OpenStackCloud(
params = {}
image_list = []
try:
if self.cloud_config.get_api_version('image') == '2':
if self._is_client_version('image', 2):
endpoint = '/images'
if show_all:
params['member_status'] = 'all'
@ -3020,7 +3026,7 @@ class OpenStackCloud(
if len(image) == 0:
raise OpenStackCloudResourceNotFound(
"No images with name or ID %s were found" % name_or_id, None)
if self.cloud_config.get_api_version('image') == '2':
if self._is_client_version('image', 2):
endpoint = '/images/{id}/file'.format(id=image[0]['id'])
else:
endpoint = '/images/{id}'.format(id=image[0]['id'])
@ -4388,7 +4394,7 @@ class OpenStackCloud(
# boolean. Glance v2 takes "visibility". If the user gives us
# is_public, we know what they mean. If they give us visibility, they
# know that they mean.
if self.cloud_config.get_api_version('image') == '2':
if self._is_client_version('image', 2):
if 'is_public' in kwargs:
is_public = kwargs.pop('is_public')
if is_public:
@ -4537,7 +4543,7 @@ class OpenStackCloud(
self, name, filename, meta, wait, timeout, **image_kwargs):
image_data = open(filename, 'rb')
# Because reasons and crying bunnies
if self.cloud_config.get_api_version('image') == '2':
if self._is_client_version('image', 2):
image = self._upload_image_put_v2(
name, image_data, meta, **image_kwargs)
else:
@ -4652,7 +4658,7 @@ class OpenStackCloud(
img_props[k] = v
# This makes me want to die inside
if self.cloud_config.get_api_version('image') == '2':
if self._is_client_version('image', 2):
return self._update_image_properties_v2(image, meta, img_props)
else:
return self._update_image_properties_v1(image, meta, img_props)
@ -4946,12 +4952,12 @@ class OpenStackCloud(
description = kwargs.pop('description',
kwargs.pop('display_description', None))
if name:
if self.cloud_config.get_api_version('volume').startswith('2'):
if self._is_client_version('volume', 2):
kwargs['name'] = name
else:
kwargs['display_name'] = name
if description:
if self.cloud_config.get_api_version('volume').startswith('2'):
if self._is_client_version('volume', 2):
kwargs['description'] = description
else:
kwargs['display_description'] = description

View File

@ -754,7 +754,7 @@ class OperatorCloud(openstackcloud.OpenStackCloud):
# TODO(mordred) When this changes to REST, force interface=admin
# in the adapter call
if self.cloud_config.get_api_version('identity').startswith('2'):
if self._is_client_version('identity', 2):
url, key = '/OS-KSADM/services', 'OS-KSADM:service'
kwargs['type'] = type_ or service_type
else:
@ -773,7 +773,7 @@ class OperatorCloud(openstackcloud.OpenStackCloud):
'description')
def update_service(self, name_or_id, **kwargs):
# NOTE(SamYaple): Service updates are only available on v3 api
if self.cloud_config.get_api_version('identity').startswith('2'):
if self._is_client_version('identity', 2):
raise OpenStackCloudUnavailableFeature(
'Unavailable Feature: Service update requires Identity v3'
)
@ -860,7 +860,7 @@ class OperatorCloud(openstackcloud.OpenStackCloud):
self.log.debug("Service %s not found for deleting", name_or_id)
return False
if self.cloud_config.get_api_version('identity').startswith('2'):
if self._is_client_version('identity', 2):
service_kwargs = {'id': service['id']}
else:
service_kwargs = {'service': service['id']}
@ -913,7 +913,7 @@ class OperatorCloud(openstackcloud.OpenStackCloud):
endpoint_args = []
if url:
urlkwargs = {}
if self.cloud_config.get_api_version('identity').startswith('2'):
if self._is_client_version('identity', 2):
if interface != 'public':
raise OpenStackCloudException(
"Error adding endpoint for service {service}."
@ -935,7 +935,7 @@ class OperatorCloud(openstackcloud.OpenStackCloud):
expected_endpoints = [('public', public_url),
('internal', internal_url),
('admin', admin_url)]
if self.cloud_config.get_api_version('identity').startswith('2'):
if self._is_client_version('identity', 2):
urlkwargs = {}
for interface, url in expected_endpoints:
if url:
@ -949,7 +949,7 @@ class OperatorCloud(openstackcloud.OpenStackCloud):
urlkwargs['interface'] = interface
endpoint_args.append(urlkwargs)
if self.cloud_config.get_api_version('identity').startswith('2'):
if self._is_client_version('identity', 2):
kwargs['service_id'] = service['id']
# Keystone v2 requires 'region' arg even if it is None
kwargs['region'] = region
@ -978,7 +978,7 @@ class OperatorCloud(openstackcloud.OpenStackCloud):
'region')
def update_endpoint(self, endpoint_id, **kwargs):
# NOTE(SamYaple): Endpoint updates are only available on v3 api
if self.cloud_config.get_api_version('identity').startswith('2'):
if self._is_client_version('identity', 2):
raise OpenStackCloudUnavailableFeature(
'Unavailable Feature: Endpoint update'
)
@ -1070,7 +1070,7 @@ class OperatorCloud(openstackcloud.OpenStackCloud):
# TODO(mordred) When this changes to REST, force interface=admin
# in the adapter call
if self.cloud_config.get_api_version('identity').startswith('2'):
if self._is_client_version('identity', 2):
endpoint_kwargs = {'id': endpoint['id']}
else:
endpoint_kwargs = {'endpoint': endpoint['id']}
@ -1357,7 +1357,7 @@ class OperatorCloud(openstackcloud.OpenStackCloud):
:raises: ``OpenStackCloudException``: if something goes wrong during
the openstack API call.
"""
v2 = self.cloud_config.get_api_version('identity').startswith('2')
v2 = self._is_client_version('identity', 2)
url = '/OS-KSADM/roles' if v2 else '/roles'
data = self._identity_client.get(
url, error_message="Failed to list roles")
@ -1459,7 +1459,7 @@ class OperatorCloud(openstackcloud.OpenStackCloud):
if not filters:
filters = {}
if self.cloud_config.get_api_version('identity').startswith('2'):
if self._is_client_version('identity', 2):
if filters.get('project') is None or filters.get('user') is None:
raise OpenStackCloudException(
"Must provide project and user for keystone v2"
@ -1629,7 +1629,7 @@ class OperatorCloud(openstackcloud.OpenStackCloud):
:raise OpenStackCloudException: if the role cannot be created
"""
v2 = self.cloud_config.get_api_version('identity').startswith('2')
v2 = self._is_client_version('identity', 2)
url = '/OS-KSADM/roles' if v2 else '/roles'
msg = 'Failed to create role {name}'.format(name=name)
data = self._identity_client.post(
@ -1653,7 +1653,7 @@ class OperatorCloud(openstackcloud.OpenStackCloud):
"Role %s not found for deleting", name_or_id)
return False
v2 = self.cloud_config.get_api_version('identity').startswith('2')
v2 = self._is_client_version('identity', 2)
url = '{preffix}/{id}'.format(
preffix='/OS-KSADM/roles' if v2 else '/roles', id=role['id'])
error_msg = "Unable to delete role {name}".format(name=name_or_id)
@ -1669,8 +1669,7 @@ class OperatorCloud(openstackcloud.OpenStackCloud):
data = {'role': role.id}
# domain and group not available in keystone v2.0
keystone_version = self.cloud_config.get_api_version('identity')
is_keystone_v2 = keystone_version.startswith('2')
is_keystone_v2 = self._is_client_version('identity', 2)
filters = {}
if not is_keystone_v2 and domain:
@ -1729,7 +1728,7 @@ class OperatorCloud(openstackcloud.OpenStackCloud):
if data.get('user') is None and data.get('group') is None:
raise OpenStackCloudException(
'Must specify either a user or a group')
if self.cloud_config.get_api_version('identity').startswith('2') and \
if self._is_client_version('identity', 2) and \
data.get('project') is None:
raise OpenStackCloudException(
'Must specify project for keystone v2')
@ -1741,7 +1740,7 @@ class OperatorCloud(openstackcloud.OpenStackCloud):
with _utils.shade_exceptions(
"Error granting access to role: {0}".format(
data)):
if self.cloud_config.get_api_version('identity').startswith('2'):
if self._is_client_version('identity', 2):
data['tenant'] = data.pop('project')
self.manager.submit_task(_tasks.RoleAddUser(**data))
else:
@ -1792,7 +1791,7 @@ class OperatorCloud(openstackcloud.OpenStackCloud):
if data.get('user') is None and data.get('group') is None:
raise OpenStackCloudException(
'Must specify either a user or a group')
if self.cloud_config.get_api_version('identity').startswith('2') and \
if self._is_client_version('identity', 2) and \
data.get('project') is None:
raise OpenStackCloudException(
'Must specify project for keystone v2')
@ -1804,7 +1803,7 @@ class OperatorCloud(openstackcloud.OpenStackCloud):
with _utils.shade_exceptions(
"Error revoking access to role: {0}".format(
data)):
if self.cloud_config.get_api_version('identity').startswith('2'):
if self._is_client_version('identity', 2):
data['tenant'] = data.pop('project')
self.manager.submit_task(_tasks.RoleRemoveUser(**data))
else: