diff --git a/os_service_types/exc.py b/os_service_types/exc.py index 0eff50f..ab6abb8 100644 --- a/os_service_types/exc.py +++ b/os_service_types/exc.py @@ -31,3 +31,11 @@ class AliasUsageWarning(Warning): Requested service_type {given} is an old alias. Please update your code to reference the official service_type {official}. """ + + +class UnofficialUsageWarning(Warning): + """Use of unofficial service-types is discouraged.""" + + details = """ + Requested service_type {given} is not a known official OpenStack project. + """ diff --git a/os_service_types/service_types.py b/os_service_types/service_types.py index 3a875f3..125403e 100644 --- a/os_service_types/service_types.py +++ b/os_service_types/service_types.py @@ -208,16 +208,24 @@ class ServiceTypes(object): service_type = _normalize_type(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, permissive=False): """Given a possible service_type, return the official type. :param str service_type: A potential service-type. + :param bool permissive: + Return the original type if the given service_type is not found. :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): return service_type official = self._service_types_data['reverse'].get(service_type) + if permissive and official is None: + if self._warn: + exc.warn( + exc.UnofficialUsageWarning, + given=service_type) + return service_type if self._warn: exc.warn( exc.AliasUsageWarning, given=service_type, official=official) diff --git a/os_service_types/tests/base.py b/os_service_types/tests/base.py index 26ae512..731d152 100644 --- a/os_service_types/tests/base.py +++ b/os_service_types/tests/base.py @@ -118,6 +118,12 @@ class ServiceDataMixin(object): self.assertIsNone( self.service_types.get_service_type(self.service_type)) + def test_get_service_type_permissive(self): + self.assertEqual( + self.official or self.service_type, + self.service_types.get_service_type( + self.service_type, permissive=True)) + def test_get_aliases(self): self.assertEqual( self.aliases,