Allow passing in service types with _ in them

-'s vs. _'s is a thing people can get wrong from time to time. If
someone says "I want block_storage" - we know what they mean - or at
least we can.

Change-Id: I97c65c25ae8e787f4bfedab54db619d98a16f037
This commit is contained in:
Monty Taylor 2018-03-22 10:12:58 -05:00
parent 7a2ab03029
commit a064fe49ed
No known key found for this signature in database
GPG Key ID: 7BAE94BC7141A594
2 changed files with 19 additions and 0 deletions

View File

@ -23,6 +23,10 @@ BUILTIN_DATA = os_service_types.data.read_data('service-types.json')
SERVICE_TYPES_URL = "https://service-types.openstack.org/service-types.json" SERVICE_TYPES_URL = "https://service-types.openstack.org/service-types.json"
def _normalize_type(service_type):
return service_type.replace('_', '-')
class ServiceTypes(object): class ServiceTypes(object):
"""Encapsulation of the OpenStack Service Types Authority data. """Encapsulation of the OpenStack Service Types Authority data.
@ -120,6 +124,7 @@ class ServiceTypes(object):
:param str service_type: The official service-type to get data for. :param str service_type: The official service-type to get data for.
:returns dict: Service data for the service or None if not found. :returns dict: Service data for the service or None if not found.
""" """
service_type = _normalize_type(service_type)
for service in self._service_types_data['services']: for service in self._service_types_data['services']:
if service_type == service['service_type']: if service_type == service['service_type']:
return service return service
@ -150,6 +155,7 @@ class ServiceTypes(object):
:param str service_type: The service-type to test. :param str service_type: The service-type to test.
:returns bool: True if it's an alias type, False otherwise. :returns bool: True if it's an alias type, False otherwise.
""" """
service_type = _normalize_type(service_type)
return service_type in self._service_types_data['reverse'] return service_type in self._service_types_data['reverse']
def is_known(self, service_type): def is_known(self, service_type):
@ -199,6 +205,7 @@ class ServiceTypes(object):
:param str service_type: An official service-type. :param str service_type: An official service-type.
:returns list: List of aliases, or empty list if there are none. :returns list: List of aliases, or empty list if there are none.
""" """
service_type = _normalize_type(service_type)
return self._service_types_data['forward'].get(service_type, []) return self._service_types_data['forward'].get(service_type, [])
def get_service_type(self, service_type): def get_service_type(self, service_type):
@ -207,6 +214,7 @@ class ServiceTypes(object):
:param str service_type: A potential service-type. :param str service_type: A potential service-type.
:returns str: The official service-type, or None if there is no match. :returns str: The official service-type, or None if there is no match.
""" """
service_type = _normalize_type(service_type)
if self.is_official(service_type): if self.is_official(service_type):
return service_type return service_type
official = self._service_types_data['reverse'].get(service_type) official = self._service_types_data['reverse'].get(service_type)
@ -221,6 +229,7 @@ class ServiceTypes(object):
:param str service_type: The service-type or alias to get data for. :param str service_type: The service-type or alias to get data for.
:returns dict: Service data for the service or None if not found. :returns dict: Service data for the service or None if not found.
""" """
service_type = _normalize_type(service_type)
if not self.is_known(service_type): if not self.is_known(service_type):
return [service_type] return [service_type]
return self.all_types_by_service_type[ return self.all_types_by_service_type[
@ -232,6 +241,7 @@ class ServiceTypes(object):
:param str service_type: An official service-type or alias. :param str service_type: An official service-type or alias.
:returns str: The OpenStack project name or None if there is no match. :returns str: The OpenStack project name or None if there is no match.
""" """
service_type = _normalize_type(service_type)
service = self.get_service_data(service_type) service = self.get_service_data(service_type)
if service: if service:
return service['project'] return service['project']

View File

@ -77,6 +77,15 @@ class ServiceDataMixin(object):
is_known=True, is_alias=False, is_official=True, is_known=True, is_alias=False, is_official=True,
is_secondary=False, all_services=['block-storage'], is_secondary=False, all_services=['block-storage'],
project='cinder')), project='cinder')),
('block_storage', dict(
service_type='block_storage', official='block-storage',
all_types=['block-storage', 'volumev3', 'volumev2', 'volume',
'block-store'],
api_reference='block-storage', api_reference_project=None,
aliases=['volumev3', 'volumev2', 'volume', 'block-store'],
is_known=True, is_alias=False, is_official=True,
is_secondary=False, all_services=['block-storage'],
project='cinder')),
('network', dict( ('network', dict(
service_type='network', official='network', aliases=[], service_type='network', official='network', aliases=[],
all_types=['network'], all_types=['network'],