summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMonty Taylor <mordred@inaugust.com>2018-07-24 10:38:15 -0500
committerMonty Taylor <mordred@inaugust.com>2018-07-26 08:36:03 -0400
commit2441006a7160738faa1342beea1a9b78d3ddb41c (patch)
treeafb95d9e07507545d7fbd38c4e0abb55a6a98074
parenta064fe49edb7b82ffade11096081e8ccbb45c612 (diff)
Add flag for returning unofficial types
In consuming code in openstacksdk and keystoneauth, the pattern: if stm.is_official(service_type): return stm.get_service_type(service_type) return service_type Gets used in several places. Let's provide a way to say that with a flag: return std.get_service_type(service_type, permissive=True) which says "get me the official service_type for this, but if it's not something I know about, give me the thing the user asked for. Change-Id: Ib8fb14a88ecc690da67967b7e906deb7d923f869
Notes
Notes (review): Code-Review+2: Eric Fried <efried@us.ibm.com> Code-Review+2: Clark Boylan <cboylan@sapwetik.org> Workflow+1: Clark Boylan <cboylan@sapwetik.org> Verified+2: Zuul Submitted-by: Zuul Submitted-at: Thu, 26 Jul 2018 22:10:35 +0000 Reviewed-on: https://review.openstack.org/585411 Project: openstack/os-service-types Branch: refs/heads/master
-rw-r--r--os_service_types/exc.py8
-rw-r--r--os_service_types/service_types.py10
-rw-r--r--os_service_types/tests/base.py6
3 files changed, 23 insertions, 1 deletions
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):
31 Requested service_type {given} is an old alias. Please update your 31 Requested service_type {given} is an old alias. Please update your
32 code to reference the official service_type {official}. 32 code to reference the official service_type {official}.
33 """ 33 """
34
35
36class UnofficialUsageWarning(Warning):
37 """Use of unofficial service-types is discouraged."""
38
39 details = """
40 Requested service_type {given} is not a known official OpenStack project.
41 """
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):
208 service_type = _normalize_type(service_type) 208 service_type = _normalize_type(service_type)
209 return self._service_types_data['forward'].get(service_type, []) 209 return self._service_types_data['forward'].get(service_type, [])
210 210
211 def get_service_type(self, service_type): 211 def get_service_type(self, service_type, permissive=False):
212 """Given a possible service_type, return the official type. 212 """Given a possible service_type, return the official type.
213 213
214 :param str service_type: A potential service-type. 214 :param str service_type: A potential service-type.
215 :param bool permissive:
216 Return the original type if the given service_type is not found.
215 :returns str: The official service-type, or None if there is no match. 217 :returns str: The official service-type, or None if there is no match.
216 """ 218 """
217 service_type = _normalize_type(service_type) 219 service_type = _normalize_type(service_type)
218 if self.is_official(service_type): 220 if self.is_official(service_type):
219 return service_type 221 return service_type
220 official = self._service_types_data['reverse'].get(service_type) 222 official = self._service_types_data['reverse'].get(service_type)
223 if permissive and official is None:
224 if self._warn:
225 exc.warn(
226 exc.UnofficialUsageWarning,
227 given=service_type)
228 return service_type
221 if self._warn: 229 if self._warn:
222 exc.warn( 230 exc.warn(
223 exc.AliasUsageWarning, given=service_type, official=official) 231 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):
118 self.assertIsNone( 118 self.assertIsNone(
119 self.service_types.get_service_type(self.service_type)) 119 self.service_types.get_service_type(self.service_type))
120 120
121 def test_get_service_type_permissive(self):
122 self.assertEqual(
123 self.official or self.service_type,
124 self.service_types.get_service_type(
125 self.service_type, permissive=True))
126
121 def test_get_aliases(self): 127 def test_get_aliases(self):
122 self.assertEqual( 128 self.assertEqual(
123 self.aliases, 129 self.aliases,