Browse Source

Rename deprecated context params

The user, tenant and parameters without _id are deprecated as properties
on the object however have never been deprecated as the argument that is
passed.

This should be a fairly easy transition from a context perspective so
simply use the debtcollector function to rename them in place.

Change-Id: Id6ae5a1c869b8e4279d0100bdb6dbf6790dd9b83
Jamie Lennox 1 year ago
parent
commit
d78cf592e1
2 changed files with 67 additions and 44 deletions
  1. 46
    23
      oslo_context/context.py
  2. 21
    21
      oslo_context/tests/test_context.py

+ 46
- 23
oslo_context/context.py View File

@@ -27,12 +27,14 @@ or logging context.
27 27
 """
28 28
 
29 29
 import collections
30
+import functools
30 31
 import itertools
31 32
 import threading
32 33
 import uuid
33 34
 import warnings
34 35
 
35 36
 import debtcollector
37
+from debtcollector import renames
36 38
 
37 39
 
38 40
 _request_store = threading.local()
@@ -42,13 +44,13 @@ _request_store = threading.local()
42 44
 _ENVIRON_HEADERS = {
43 45
     'auth_token': ['HTTP_X_AUTH_TOKEN',
44 46
                    'HTTP_X_STORAGE_TOKEN'],
45
-    'user': ['HTTP_X_USER_ID',
46
-             'HTTP_X_USER'],
47
-    'tenant': ['HTTP_X_PROJECT_ID',
48
-               'HTTP_X_TENANT_ID',
49
-               'HTTP_X_TENANT'],
50
-    'user_domain': ['HTTP_X_USER_DOMAIN_ID'],
51
-    'project_domain': ['HTTP_X_PROJECT_DOMAIN_ID'],
47
+    'user_id': ['HTTP_X_USER_ID',
48
+                'HTTP_X_USER'],
49
+    'project_id': ['HTTP_X_PROJECT_ID',
50
+                   'HTTP_X_TENANT_ID',
51
+                   'HTTP_X_TENANT'],
52
+    'user_domain_id': ['HTTP_X_USER_DOMAIN_ID'],
53
+    'project_domain_id': ['HTTP_X_PROJECT_DOMAIN_ID'],
52 54
     'user_name': ['HTTP_X_USER_NAME'],
53 55
     'project_name': ['HTTP_X_PROJECT_NAME',
54 56
                      'HTTP_X_TENANT_NAME'],
@@ -163,6 +165,12 @@ def _moved_property(new_name, old_name=None, target=None):
163 165
     return property(getter, setter, deleter)
164 166
 
165 167
 
168
+_renamed_kwarg = functools.partial(renames.renamed_kwarg,
169
+                                   version='2.18',
170
+                                   removal_version='3.0',
171
+                                   replace=True)
172
+
173
+
166 174
 class RequestContext(object):
167 175
 
168 176
     """Helper class to represent useful information about a request context.
@@ -173,13 +181,18 @@ class RequestContext(object):
173 181
 
174 182
     user_idt_format = u'{user} {tenant} {domain} {user_domain} {p_domain}'
175 183
 
184
+    @_renamed_kwarg('user', 'user_id')
185
+    @_renamed_kwarg('tenant', 'project_id')
186
+    @_renamed_kwarg('domain', 'domain_id')
187
+    @_renamed_kwarg('user_domain', 'user_domain_id')
188
+    @_renamed_kwarg('project_domain', 'project_domain_id')
176 189
     def __init__(self,
177 190
                  auth_token=None,
178
-                 user=None,
179
-                 tenant=None,
180
-                 domain=None,
181
-                 user_domain=None,
182
-                 project_domain=None,
191
+                 user_id=None,
192
+                 project_id=None,
193
+                 domain_id=None,
194
+                 user_domain_id=None,
195
+                 project_domain_id=None,
183 196
                  is_admin=False,
184 197
                  read_only=False,
185 198
                  show_deleted=False,
@@ -214,11 +227,11 @@ class RequestContext(object):
214 227
         :type is_admin_project: bool
215 228
         """
216 229
         # setting to private variables to avoid triggering subclass properties
217
-        self._user_id = user
218
-        self._project_id = tenant
219
-        self._domain_id = domain
220
-        self._user_domain_id = user_domain
221
-        self._project_domain_id = project_domain
230
+        self._user_id = user_id
231
+        self._project_id = project_id
232
+        self._domain_id = domain_id
233
+        self._user_domain_id = user_domain_id
234
+        self._project_domain_id = project_domain_id
222 235
 
223 236
         self.auth_token = auth_token
224 237
         self.user_name = user_name
@@ -349,14 +362,19 @@ class RequestContext(object):
349 362
         return self.global_request_id or self.request_id
350 363
 
351 364
     @classmethod
365
+    @_renamed_kwarg('user', 'user_id')
366
+    @_renamed_kwarg('tenant', 'project_id')
367
+    @_renamed_kwarg('domain', 'domain_id')
368
+    @_renamed_kwarg('user_domain', 'user_domain_id')
369
+    @_renamed_kwarg('project_domain', 'project_domain_id')
352 370
     def from_dict(cls, values, **kwargs):
353 371
         """Construct a context object from a provided dictionary."""
354 372
         kwargs.setdefault('auth_token', values.get('auth_token'))
355
-        kwargs.setdefault('user', values.get('user'))
356
-        kwargs.setdefault('tenant', values.get('tenant'))
357
-        kwargs.setdefault('domain', values.get('domain'))
358
-        kwargs.setdefault('user_domain', values.get('user_domain'))
359
-        kwargs.setdefault('project_domain', values.get('project_domain'))
373
+        kwargs.setdefault('user_id', values.get('user'))
374
+        kwargs.setdefault('project_id', values.get('tenant'))
375
+        kwargs.setdefault('domain_id', values.get('domain'))
376
+        kwargs.setdefault('user_domain_id', values.get('user_domain'))
377
+        kwargs.setdefault('project_domain_id', values.get('project_domain'))
360 378
         kwargs.setdefault('is_admin', values.get('is_admin', False))
361 379
         kwargs.setdefault('read_only', values.get('read_only', False))
362 380
         kwargs.setdefault('show_deleted', values.get('show_deleted', False))
@@ -375,6 +393,11 @@ class RequestContext(object):
375 393
         return cls(**kwargs)
376 394
 
377 395
     @classmethod
396
+    @_renamed_kwarg('user', 'user_id')
397
+    @_renamed_kwarg('tenant', 'project_id')
398
+    @_renamed_kwarg('domain', 'domain_id')
399
+    @_renamed_kwarg('user_domain', 'user_domain_id')
400
+    @_renamed_kwarg('project_domain', 'project_domain_id')
378 401
     def from_environ(cls, environ, **kwargs):
379 402
         """Load a context object from a request environment.
380 403
 
@@ -421,7 +444,7 @@ class RequestContext(object):
421 444
 def get_admin_context(show_deleted=False):
422 445
     """Create an administrator context."""
423 446
     context = RequestContext(None,
424
-                             tenant=None,
447
+                             project_id=None,
425 448
                              is_admin=True,
426 449
                              show_deleted=show_deleted,
427 450
                              overwrite=False)

+ 21
- 21
oslo_context/tests/test_context.py View File

@@ -592,33 +592,33 @@ class ContextTest(test_base.BaseTestCase):
592 592
             self.assertIn(key, str(w[0].message))
593 593
 
594 594
     def test_deprecated_args(self):
595
-        user = uuid.uuid4().hex
596
-        tenant = uuid.uuid4().hex
597
-        domain = uuid.uuid4().hex
598
-        user_domain = uuid.uuid4().hex
599
-        project_domain = uuid.uuid4().hex
600
-
601
-        ctx = context.RequestContext(user=user,
602
-                                     tenant=tenant,
603
-                                     domain=domain,
604
-                                     user_domain=user_domain,
605
-                                     project_domain=project_domain)
595
+        user_id = uuid.uuid4().hex
596
+        project_id = uuid.uuid4().hex
597
+        domain_id = uuid.uuid4().hex
598
+        user_domain_id = uuid.uuid4().hex
599
+        project_domain_id = uuid.uuid4().hex
600
+
601
+        ctx = context.RequestContext(user_id=user_id,
602
+                                     project_id=project_id,
603
+                                     domain_id=domain_id,
604
+                                     user_domain_id=user_domain_id,
605
+                                     project_domain_id=project_domain_id)
606 606
 
607 607
         self.assertEqual(0, len(self.warnings))
608
-        self.assertEqual(user, ctx.user_id)
609
-        self.assertEqual(tenant, ctx.project_id)
610
-        self.assertEqual(domain, ctx.domain_id)
611
-        self.assertEqual(user_domain, ctx.user_domain_id)
612
-        self.assertEqual(project_domain, ctx.project_domain_id)
608
+        self.assertEqual(user_id, ctx.user_id)
609
+        self.assertEqual(project_id, ctx.project_id)
610
+        self.assertEqual(domain_id, ctx.domain_id)
611
+        self.assertEqual(user_domain_id, ctx.user_domain_id)
612
+        self.assertEqual(project_domain_id, ctx.project_domain_id)
613 613
 
614 614
         self.assertEqual(0, len(self.warnings))
615
-        self.assertEqual(user, ctx.user)
615
+        self.assertEqual(user_id, ctx.user)
616 616
         self.assertEqual(1, len(self.warnings))
617
-        self.assertEqual(tenant, ctx.tenant)
617
+        self.assertEqual(project_id, ctx.tenant)
618 618
         self.assertEqual(2, len(self.warnings))
619
-        self.assertEqual(domain, ctx.domain)
619
+        self.assertEqual(domain_id, ctx.domain)
620 620
         self.assertEqual(3, len(self.warnings))
621
-        self.assertEqual(user_domain, ctx.user_domain)
621
+        self.assertEqual(user_domain_id, ctx.user_domain)
622 622
         self.assertEqual(4, len(self.warnings))
623
-        self.assertEqual(project_domain, ctx.project_domain)
623
+        self.assertEqual(project_domain_id, ctx.project_domain)
624 624
         self.assertEqual(5, len(self.warnings))

Loading…
Cancel
Save