Browse Source

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
tags/1.3.0
Monty Taylor 10 months ago
parent
commit
2441006a71
No account linked to committer's email address
3 changed files with 23 additions and 1 deletions
  1. 8
    0
      os_service_types/exc.py
  2. 9
    1
      os_service_types/service_types.py
  3. 6
    0
      os_service_types/tests/base.py

+ 8
- 0
os_service_types/exc.py View File

@@ -31,3 +31,11 @@ class AliasUsageWarning(Warning):
31 31
     Requested service_type {given} is an old alias. Please update your
32 32
     code to reference the official service_type {official}.
33 33
     """
34
+
35
+
36
+class 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
+    """

+ 9
- 1
os_service_types/service_types.py View File

@@ -208,16 +208,24 @@ class ServiceTypes(object):
208 208
         service_type = _normalize_type(service_type)
209 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 212
         """Given a possible service_type, return the official type.
213 213
 
214 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 217
         :returns str: The official service-type, or None if there is no match.
216 218
         """
217 219
         service_type = _normalize_type(service_type)
218 220
         if self.is_official(service_type):
219 221
             return service_type
220 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 229
         if self._warn:
222 230
             exc.warn(
223 231
                 exc.AliasUsageWarning, given=service_type, official=official)

+ 6
- 0
os_service_types/tests/base.py View File

@@ -118,6 +118,12 @@ class ServiceDataMixin(object):
118 118
             self.assertIsNone(
119 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 127
     def test_get_aliases(self):
122 128
         self.assertEqual(
123 129
             self.aliases,

Loading…
Cancel
Save