Browse Source

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
Monty Taylor 1 year ago
parent
commit
a064fe49ed
No account linked to committer's email address
2 changed files with 19 additions and 0 deletions
  1. 10
    0
      os_service_types/service_types.py
  2. 9
    0
      os_service_types/tests/base.py

+ 10
- 0
os_service_types/service_types.py View File

@@ -23,6 +23,10 @@ BUILTIN_DATA = os_service_types.data.read_data('service-types.json')
23 23
 SERVICE_TYPES_URL = "https://service-types.openstack.org/service-types.json"
24 24
 
25 25
 
26
+def _normalize_type(service_type):
27
+    return service_type.replace('_', '-')
28
+
29
+
26 30
 class ServiceTypes(object):
27 31
     """Encapsulation of the OpenStack Service Types Authority data.
28 32
 
@@ -120,6 +124,7 @@ class ServiceTypes(object):
120 124
         :param str service_type: The official service-type to get data for.
121 125
         :returns dict: Service data for the service or None if not found.
122 126
         """
127
+        service_type = _normalize_type(service_type)
123 128
         for service in self._service_types_data['services']:
124 129
             if service_type == service['service_type']:
125 130
                 return service
@@ -150,6 +155,7 @@ class ServiceTypes(object):
150 155
         :param str service_type: The service-type to test.
151 156
         :returns bool: True if it's an alias type, False otherwise.
152 157
         """
158
+        service_type = _normalize_type(service_type)
153 159
         return service_type in self._service_types_data['reverse']
154 160
 
155 161
     def is_known(self, service_type):
@@ -199,6 +205,7 @@ class ServiceTypes(object):
199 205
         :param str service_type: An official service-type.
200 206
         :returns list: List of aliases, or empty list if there are none.
201 207
         """
208
+        service_type = _normalize_type(service_type)
202 209
         return self._service_types_data['forward'].get(service_type, [])
203 210
 
204 211
     def get_service_type(self, service_type):
@@ -207,6 +214,7 @@ class ServiceTypes(object):
207 214
         :param str service_type: A potential service-type.
208 215
         :returns str: The official service-type, or None if there is no match.
209 216
         """
217
+        service_type = _normalize_type(service_type)
210 218
         if self.is_official(service_type):
211 219
             return service_type
212 220
         official = self._service_types_data['reverse'].get(service_type)
@@ -221,6 +229,7 @@ class ServiceTypes(object):
221 229
         :param str service_type: The service-type or alias to get data for.
222 230
         :returns dict: Service data for the service or None if not found.
223 231
         """
232
+        service_type = _normalize_type(service_type)
224 233
         if not self.is_known(service_type):
225 234
             return [service_type]
226 235
         return self.all_types_by_service_type[
@@ -232,6 +241,7 @@ class ServiceTypes(object):
232 241
         :param str service_type: An official service-type or alias.
233 242
         :returns str: The OpenStack project name or None if there is no match.
234 243
         """
244
+        service_type = _normalize_type(service_type)
235 245
         service = self.get_service_data(service_type)
236 246
         if service:
237 247
             return service['project']

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

@@ -77,6 +77,15 @@ class ServiceDataMixin(object):
77 77
             is_known=True, is_alias=False, is_official=True,
78 78
             is_secondary=False, all_services=['block-storage'],
79 79
             project='cinder')),
80
+        ('block_storage', dict(
81
+            service_type='block_storage', official='block-storage',
82
+            all_types=['block-storage', 'volumev3', 'volumev2', 'volume',
83
+                       'block-store'],
84
+            api_reference='block-storage', api_reference_project=None,
85
+            aliases=['volumev3', 'volumev2', 'volume', 'block-store'],
86
+            is_known=True, is_alias=False, is_official=True,
87
+            is_secondary=False, all_services=['block-storage'],
88
+            project='cinder')),
80 89
         ('network', dict(
81 90
             service_type='network', official='network', aliases=[],
82 91
             all_types=['network'],

Loading…
Cancel
Save