Make VersionData class

We're repeating ourselves a bunch with a plain dict that contains the
version data. Make a class to encapsulate it. Make the class a subclass
of dict so that json translation works.

Change-Id: Ic7d122487174a5b0378a8dfaa39514c5cb2a604b
This commit is contained in:
Monty Taylor 2018-04-26 12:32:06 +02:00
parent d6670ee5c9
commit 57f9736329
No known key found for this signature in database
GPG Key ID: 7BAE94BC7141A594
3 changed files with 94 additions and 105 deletions

View File

@ -470,21 +470,8 @@ class Discover(object):
:param bool reverse: Reverse the list. reverse=true will mean the
returned list is sorted from newest to oldest
version.
:returns: A list of version data dictionaries sorted by version number.
Each data element in the returned list is a dictionary
consisting of:
:version tuple: The normalized version of the endpoint.
:url str: The url for the endpoint.
:collection: The URL for the discovery document. May be None.
:min_microversion: The minimum microversion supported by the
endpoint. May be None.
:max_microversion: The maximum microversion supported by the
endpoint. May be None.
:status str: A canonicalized version of the status. Valid values
are CURRENT, SUPPORTED, DEPRECATED and EXPERIMENTAL
:raw_status str: The status as provided by the server
:rtype: list(dict)
:returns: A list of :class:`VersionData` sorted by version number.
:rtype: list(VersionData)
"""
data = self.raw_version_data(**kwargs)
versions = []
@ -542,15 +529,16 @@ class Discover(object):
'Missing link to endpoint.')
continue
versions.append({'version': version_number,
'url': self_url,
'collection': collection_url,
'min_microversion': min_microversion,
'max_microversion': max_microversion,
'next_min_version': next_min_version,
'not_before': not_before,
'status': normalize_status(v['status']),
'raw_status': v['status']})
versions.append(
VersionData(version=version_number,
url=self_url,
collection=collection_url,
min_microversion=min_microversion,
max_microversion=max_microversion,
next_min_version=next_min_version,
not_before=not_before,
status=normalize_status(v['status']),
raw_status=v['status']))
versions.sort(key=lambda v: v['version'], reverse=reverse)
return versions
@ -563,21 +551,8 @@ class Discover(object):
:param bool reverse: Reverse the list. reverse=true will mean the
returned list is sorted from newest to oldest
version.
:returns: A list of version data dictionaries sorted by version number.
Each data element in the returned list is a dictionary
consisting of:
:version string: The normalized version of the endpoint.
:url str: The url for the endpoint.
:collection: The URL for the discovery document. May be None.
:min_microversion str: The minimum microversion supported by the
endpoint. May be None.
:max_microversion str: The maximum microversion supported by the
endpoint. May be None.
:status str: A canonicalized version of the status. Valid values
are CURRENT, SUPPORTED, DEPRECATED and EXPERIMENTAL
:raw_status str: The status as provided by the server
:rtype: list(dict)
:returns: A list of :class:`VersionData` sorted by version number.
:rtype: list(VersionData)
"""
version_data = self.version_data(reverse=reverse, **kwargs)
for version in version_data:
@ -718,6 +693,79 @@ class Discover(object):
return data['url'] if data else None
class VersionData(dict):
"""Normalized Version Data about an endpoint."""
def __init__(
self,
version,
url,
collection=None,
max_microversion=None,
min_microversion=None,
next_min_version=None,
not_before=None,
status='CURRENT',
raw_status=None):
super(VersionData, self).__init__()
self['version'] = version
self['url'] = url
self['collection'] = collection
self['max_microversion'] = max_microversion
self['min_microversion'] = min_microversion
self['next_min_version'] = next_min_version
self['not_before'] = not_before
self['status'] = status
self['raw_status'] = raw_status
@property
def version(self):
"""The normalized version of the endpoint."""
return self.get('version')
@property
def url(self):
"""The url for the endpoint."""
return self.get('url')
@property
def collection(self):
"""The URL for the discovery document.
May be None.
"""
return self.get('collection')
@property
def min_microversion(self):
"""The minimum microversion supported by the endpoint.
May be None.
"""
return self.get('min_microversion')
@property
def max_microversion(self):
"""The maximum microversion supported by the endpoint.
May be None.
"""
return self.get('max_microversion')
@property
def status(self):
"""A canonicalized version of the status.
Valid values are CURRENT, SUPPORTED, DEPRECATED and EXPERIMENTAL.
"""
return self.get('status')
@property
def raw_status(self):
"""The status as provided by the server."""
return self.get('raw_status')
class EndpointData(object):
"""Normalized information about a discovered endpoint.
@ -906,21 +954,8 @@ class EndpointData(object):
:param string project_id: ID of the currently scoped project. Used for
removing project_id components of URLs from
the catalog. (optional)
:returns: A list of version data dictionaries sorted by version number.
Each data element in the returned list is a dictionary
consisting of:
:version string: The normalized version of the endpoint.
:url str: The url for the endpoint.
:collection: The URL for the discovery document. May be None.
:min_microversion: The minimum microversion supported by the
endpoint. May be None.
:max_microversion: The maximum microversion supported by the
endpoint. May be None.
:status str: A canonicalized version of the status. Valid values
are CURRENT, SUPPORTED, DEPRECATED and EXPERIMENTAL
:raw_status str: The status as provided by the server
:rtype: list(dict)
:returns: A list of :class:`VersionData` sorted by version number.
:rtype: list(VersionData)
"""
versions = []
for vers_url in self._get_discovery_url_choices(project_id=project_id):
@ -943,20 +978,8 @@ class EndpointData(object):
:param string project_id: ID of the currently scoped project. Used for
removing project_id components of URLs from
the catalog. (optional)
:returns: A list of version data dictionaries sorted by version number.
Each data element in the returned list is a dictionary
consisting of:
:version string: The normalized version of the endpoint.
:url str: The url for the endpoint.
:collection: The URL for the discovery document. May be None.
:min_microversion: The minimum microversion supported by the
endpoint. May be None.
:max_microversion: The maximum microversion supported by the
endpoint. May be None.
:status str: A canonicalized version of the status. Valid values
are CURRENT, SUPPORTED, DEPRECATED and EXPERIMENTAL
:raw_status str: The status as provided by the server
:rtype: list(dict)
:returns: A list of :class:`VersionData` sorted by version number.
:rtype: list(VersionData)
"""
version = self.api_version
if version:
@ -967,17 +990,7 @@ class EndpointData(object):
url, _ = self.url.rsplit('/', 1)
url += "/"
return [{
'version': version,
'collection': None,
'max_microversion': None,
'min_microversion': None,
'next_min_version': None,
'not_before': None,
'status': 'CURRENT',
'raw_status': None,
'url': url,
}]
return [VersionData(url=url, version=version)]
def _set_version_info(self, session, allow=None, cache=None,
allow_version_hack=True, project_id=None,

View File

@ -523,19 +523,7 @@ class BaseIdentityPlugin(plugin.BaseAuthPlugin):
to None)
:returns: A dictionary keyed by region_name with values containing
dictionaries keyed by interface with values being a list of
version data dictionaries. Each version data dictionary consists
of:
:version string: The normalized version of the endpoint.
:url str: The url for the endpoint.
:collection: The URL for the discovery document. May be None.
:min_microversion: The minimum microversion supported by the
endpoint. May be None.
:max_microversion: The maximum microversion supported by the
endpoint. May be None.
:status str: A canonicalized version of the status. Valid values
are CURRENT, SUPPORTED, DEPRECATED and EXPERIMENTAL
:raw_status str: The status as provided by the server
:class:`~keystoneauth1.discover.VersionData`.
"""
service_types = discover._SERVICE_TYPES
catalog = self.get_access(session).service_catalog

View File

@ -1066,19 +1066,7 @@ class Session(object):
to None)
:returns: A dictionary keyed by region_name with values containing
dictionaries keyed by interface with values being a list of
version data dictionaries. Each version data dictionary consists
of:
:version string: The normalized version of the endpoint.
:url str: The url for the endpoint.
:collection: The URL for the discovery document. May be None.
:min_microversion: The minimum microversion supported by the
endpoint. May be None.
:max_microversion: The maximum microversion supported by the
endpoint. May be None.
:status str: A canonicalized version of the status. Valid values
are CURRENT, SUPPORTED, DEPRECATED and EXPERIMENTAL
:raw_status str: The status as provided by the server
`~keystoneauth1.discover.VersionData`.
"""
auth = self._auth_required(auth, 'determine endpoint URL')
return auth.get_all_version_data(