Browse Source

Add all tenants support for checkpoints listing

Change-Id: Ic02a60a0eec1e7d3b0ca7b1a355a5d62d5c0046c
Implements: bp checkpoint-all-tenants
tags/1.2.0
jiaopengju 6 months ago
parent
commit
abab99c0eb

+ 7
- 2
karbor/api/v1/providers.py View File

@@ -311,7 +311,8 @@ class ProvidersController(wsgi.Controller):
311 311
 
312 312
         if filters is None:
313 313
             filters = {}
314
-
314
+        all_tenants = utils.get_bool_param(
315
+            'all_tenants', filters) and context.is_admin
315 316
         try:
316 317
             if limit is not None:
317 318
                 limit = int(limit)
@@ -325,12 +326,16 @@ class ProvidersController(wsgi.Controller):
325 326
         if filters:
326 327
             LOG.debug("Searching by: %s.", six.text_type(filters))
327 328
 
329
+        if all_tenants:
330
+            del filters['all_tenants']
328 331
         checkpoints = self.protection_api.list_checkpoints(
329 332
             context, provider_id, marker, limit,
330 333
             sort_keys=sort_keys,
331 334
             sort_dirs=sort_dirs,
332 335
             filters=filters,
333
-            offset=offset)
336
+            offset=offset,
337
+            all_tenants=all_tenants
338
+        )
334 339
 
335 340
         LOG.info("Get all checkpoints completed successfully.")
336 341
         return checkpoints

+ 3
- 2
karbor/services/protection/api.py View File

@@ -52,7 +52,7 @@ class API(base.Base):
52 52
         )
53 53
 
54 54
     def list_checkpoints(self, context, provider_id, marker, limit,
55
-                         sort_keys, sort_dirs, filters, offset):
55
+                         sort_keys, sort_dirs, filters, offset, all_tenants):
56 56
         return self.protection_rpcapi.list_checkpoints(
57 57
             context,
58 58
             provider_id,
@@ -60,7 +60,8 @@ class API(base.Base):
60 60
             limit,
61 61
             sort_keys,
62 62
             sort_dirs,
63
-            filters
63
+            filters,
64
+            all_tenants
64 65
         )
65 66
 
66 67
     def list_protectable_types(self, context):

+ 53
- 15
karbor/services/protection/checkpoint.py View File

@@ -303,9 +303,49 @@ class CheckpointCollection(object):
303 303
         self._checkpoints_section = bank.get_sub_section("/checkpoints")
304 304
         self._indices_section = bank.get_sub_section("/indices")
305 305
 
306
+    @staticmethod
307
+    def _get_prefix_and_marker_by_provider(provider_id, project_id, marker,
308
+                                           marker_checkpoint, all_tenants):
309
+        if all_tenants:
310
+            prefix = "/by-provider/%s/" % provider_id
311
+            marker = "/%s/%s" % (
312
+                marker_checkpoint["project_id"], marker) if marker else marker
313
+        else:
314
+            prefix = "/by-provider/%s/%s/" % (provider_id, project_id)
315
+            marker = "/%s" % marker if marker else marker
316
+        return prefix, marker
317
+
318
+    @staticmethod
319
+    def _get_prefix_and_marker_by_plan(plan_id, project_id, marker,
320
+                                       marker_checkpoint, all_tenants):
321
+        if all_tenants:
322
+            prefix = "/by-plan/%s/" % plan_id
323
+            marker = "/%s/%s/%s" % (
324
+                marker_checkpoint["project_id"],
325
+                marker_checkpoint["created_at"], marker) if marker else marker
326
+        else:
327
+            prefix = "/by-plan/%s/%s/" % (plan_id, project_id)
328
+            marker = "/%s/%s" % (
329
+                marker_checkpoint["created_at"], marker) if marker else marker
330
+        return prefix, marker
331
+
332
+    @staticmethod
333
+    def _get_prefix_and_marker_by_date(project_id, marker, marker_checkpoint,
334
+                                       all_tenants):
335
+        prefix = "/by-date/"
336
+        if all_tenants:
337
+            marker = "/%s/%s/%s" % (
338
+                marker_checkpoint["created_at"],
339
+                marker_checkpoint["project_id"], marker) if marker else marker
340
+        else:
341
+            marker = "/%s/%s/%s" % (
342
+                marker_checkpoint["created_at"], project_id,
343
+                marker) if marker else marker
344
+        return prefix, marker
345
+
306 346
     def list_ids(self, project_id, provider_id, limit=None, marker=None,
307 347
                  plan_id=None, start_date=None, end_date=None, sort_dir=None,
308
-                 context=None):
348
+                 context=None, all_tenants=False):
309 349
         marker_checkpoint = None
310 350
         if marker is not None:
311 351
             checkpoint_section = self._checkpoints_section.get_sub_section(
@@ -319,25 +359,22 @@ class CheckpointCollection(object):
319 359
                 end_date = timeutils.utcnow()
320 360
 
321 361
         if plan_id is None and start_date is None:
322
-            prefix = "/by-provider/%s/%s/" % (provider_id, project_id)
323
-            if marker is not None:
324
-                marker = "/%s" % marker
362
+            prefix, marker = self._get_prefix_and_marker_by_provider(
363
+                provider_id, project_id, marker,
364
+                marker_checkpoint, all_tenants)
325 365
         elif plan_id is not None:
326
-            prefix = "/by-plan/%s/%s/" % (plan_id, project_id)
327
-            if marker is not None:
328
-                date = marker_checkpoint["created_at"]
329
-                marker = "/%s/%s" % (date, marker)
366
+            prefix, marker = self._get_prefix_and_marker_by_plan(
367
+                plan_id, project_id, marker, marker_checkpoint, all_tenants)
330 368
         else:
331
-            prefix = "/by-date/"
332
-            if marker is not None:
333
-                date = marker_checkpoint["created_at"]
334
-                marker = "/%s/%s/%s" % (date, project_id, marker)
369
+            prefix, marker = self._get_prefix_and_marker_by_date(
370
+                project_id, marker, marker_checkpoint, all_tenants)
335 371
 
336 372
         return self._list_ids(project_id, prefix, limit, marker, start_date,
337
-                              end_date, sort_dir, context=context)
373
+                              end_date, sort_dir, context=context,
374
+                              all_tenants=all_tenants)
338 375
 
339 376
     def _list_ids(self, project_id, prefix, limit, marker, start_date,
340
-                  end_date, sort_dir, context=None):
377
+                  end_date, sort_dir, context=None, all_tenants=False):
341 378
         if start_date is None:
342 379
             return [key[key.find("@") + 1:]
343 380
                     for key in self._indices_section.list_objects(
@@ -359,7 +396,8 @@ class CheckpointCollection(object):
359 396
                     key.split("/")[date_cursor], "%Y-%m-%d")
360 397
                 checkpoint_project_id = key.split("/")[project_id_cursor]
361 398
                 if start_date <= date <= end_date and (
362
-                        checkpoint_project_id == project_id):
399
+                        all_tenants or (
400
+                            checkpoint_project_id == project_id)):
363 401
                     ids.append(key[key.find("@") + 1:])
364 402
                 if limit is not None and len(ids) == limit:
365 403
                     return ids

+ 4
- 2
karbor/services/protection/manager.py View File

@@ -369,7 +369,8 @@ class ProtectionManager(manager.Manager):
369 369
                                    exception.CheckpointNotFound,
370 370
                                    exception.BankListObjectsFailed)
371 371
     def list_checkpoints(self, context, provider_id, marker=None, limit=None,
372
-                         sort_keys=None, sort_dirs=None, filters=None):
372
+                         sort_keys=None, sort_dirs=None, filters=None,
373
+                         all_tenants=False):
373 374
         LOG.info("Starting list checkpoints. provider_id:%s", provider_id)
374 375
         plan_id = filters.get("plan_id", None)
375 376
         start_date = None
@@ -380,13 +381,14 @@ class ProtectionManager(manager.Manager):
380 381
         if filters.get("end_date", None):
381 382
             end_date = datetime.strptime(
382 383
                 filters.get("end_date"), "%Y-%m-%d")
384
+
383 385
         sort_dir = None if sort_dirs is None else sort_dirs[0]
384 386
         provider = self.provider_registry.show_provider(provider_id)
385 387
         project_id = context.project_id
386 388
         checkpoint_ids = provider.list_checkpoints(
387 389
             project_id, provider_id, limit=limit, marker=marker,
388 390
             plan_id=plan_id, start_date=start_date, end_date=end_date,
389
-            sort_dir=sort_dir, context=context)
391
+            sort_dir=sort_dir, context=context, all_tenants=all_tenants)
390 392
         checkpoints = []
391 393
         for checkpoint_id in checkpoint_ids:
392 394
             checkpoint = provider.get_checkpoint(checkpoint_id,

+ 4
- 2
karbor/services/protection/provider.py View File

@@ -150,12 +150,14 @@ class PluggableProtectionProvider(object):
150 150
 
151 151
     def list_checkpoints(self, project_id, provider_id, limit=None,
152 152
                          marker=None, plan_id=None, start_date=None,
153
-                         end_date=None, sort_dir=None, context=None):
153
+                         end_date=None, sort_dir=None, context=None,
154
+                         all_tenants=False):
154 155
         checkpoint_collection = self.get_checkpoint_collection()
155 156
         return checkpoint_collection.list_ids(
156 157
             project_id=project_id, provider_id=provider_id, limit=limit,
157 158
             marker=marker, plan_id=plan_id, start_date=start_date,
158
-            end_date=end_date, sort_dir=sort_dir, context=context)
159
+            end_date=end_date, sort_dir=sort_dir, context=context,
160
+            all_tenants=all_tenants)
159 161
 
160 162
 
161 163
 class ProviderRegistry(object):

+ 4
- 2
karbor/services/protection/rpcapi.py View File

@@ -92,7 +92,7 @@ class ProtectionAPI(object):
92 92
 
93 93
     def list_checkpoints(self, ctxt, provider_id, marker=None,
94 94
                          limit=None, sort_keys=None,
95
-                         sort_dirs=None, filters=None):
95
+                         sort_dirs=None, filters=None, all_tenants=False):
96 96
         cctxt = self.client.prepare(version='1.0')
97 97
         return cctxt.call(
98 98
             ctxt,
@@ -102,7 +102,9 @@ class ProtectionAPI(object):
102 102
             limit=limit,
103 103
             sort_keys=sort_keys,
104 104
             sort_dirs=sort_dirs,
105
-            filters=filters)
105
+            filters=filters,
106
+            all_tenants=all_tenants
107
+        )
106 108
 
107 109
     def list_protectable_types(self, ctxt):
108 110
         cctxt = self.client.prepare(version='1.0')

+ 55
- 0
karbor/tests/unit/protection/test_checkpoint_collection.py View File

@@ -47,6 +47,23 @@ class CheckpointCollectionTest(base.TestCase):
47 47
         self.assertEqual(set(collection.list_ids(
48 48
             project_id=project_id, provider_id=provider_id)), result)
49 49
 
50
+    def test_list_checkpoints_with_all_tenants(self):
51
+        collection = self._create_test_collection()
52
+        plan_1 = fake_protection_plan()
53
+        plan_1["id"] = "fake_plan_id_1"
54
+        plan_1["project_id"] = "fake_project_id_1"
55
+        provider_id_1 = plan_1['provider_id']
56
+        checkpoints_plan_1 = {collection.create(plan_1).id for i in range(10)}
57
+
58
+        plan_2 = fake_protection_plan()
59
+        plan_2["id"] = "fake_plan_id_2"
60
+        plan_2["project_id"] = "fake_project_id_2"
61
+        checkpoints_plan_2 = {collection.create(plan_2).id for i in range(10)}
62
+        checkpoints_plan_1.update(checkpoints_plan_2)
63
+        self.assertEqual(set(collection.list_ids(
64
+            project_id="fake_project_id_1", provider_id=provider_id_1,
65
+            all_tenants=True)), checkpoints_plan_1)
66
+
50 67
     def test_list_checkpoints_by_plan_id(self):
51 68
         collection = self._create_test_collection()
52 69
         plan_1 = fake_protection_plan()
@@ -69,6 +86,21 @@ class CheckpointCollectionTest(base.TestCase):
69 86
             project_id="fake_project_id_2", provider_id=provider_id_2,
70 87
             plan_id="fake_plan_id_2")), checkpoints_plan_2)
71 88
 
89
+    def test_list_checkpoints_by_plan_with_all_tenants(self):
90
+        collection = self._create_test_collection()
91
+        plan_1 = fake_protection_plan()
92
+        plan_1["id"] = "fake_plan_id_1"
93
+        plan_1["project_id"] = "fake_project_id_1"
94
+        provider_id_1 = plan_1['provider_id']
95
+        checkpoints_plan_1 = {collection.create(plan_1).id for i in range(10)}
96
+        plan_1["project_id"] = "fake_project_id_2"
97
+        checkpoints_plan_2 = {collection.create(plan_1).id for i in range(10)}
98
+        checkpoints_plan_2.update(checkpoints_plan_1)
99
+        self.assertEqual(set(collection.list_ids(
100
+            project_id="fake_project_id_1", provider_id=provider_id_1,
101
+            plan_id='fake_plan_id_1',
102
+            all_tenants=True)), checkpoints_plan_2)
103
+
72 104
     def test_list_checkpoints_by_plan_id_and_filter_by_start_date(self):
73 105
         collection = self._create_test_collection()
74 106
         date1 = datetime.strptime("2018-11-12", "%Y-%m-%d")
@@ -135,6 +167,29 @@ class CheckpointCollectionTest(base.TestCase):
135 167
             end_date=date2)),
136 168
             checkpoints_date_2)
137 169
 
170
+    def test_list_checkpoints_by_date_with_all_tenants(self):
171
+        collection = self._create_test_collection()
172
+        date1 = datetime.strptime("2018-11-15", "%Y-%m-%d")
173
+        timeutils.utcnow = mock.MagicMock()
174
+        timeutils.utcnow.return_value = date1
175
+        plan_1 = fake_protection_plan()
176
+        plan_1["id"] = "fake_plan_id_1"
177
+        plan_1["project_id"] = "fake_project_id_1"
178
+        provider_id_1 = plan_1['provider_id']
179
+        checkpoints_1 = {collection.create(plan_1).id for i in range(10)}
180
+
181
+        date2 = datetime.strptime("2018-11-17", "%Y-%m-%d")
182
+        timeutils.utcnow = mock.MagicMock()
183
+        timeutils.utcnow.return_value = date2
184
+        plan_1["id"] = "fake_plan_id_2"
185
+        plan_1["project_id"] = "fake_project_id_2"
186
+        checkpoints_2 = {collection.create(plan_1).id for i in range(10)}
187
+        checkpoints_2.update(checkpoints_1)
188
+        self.assertEqual(set(collection.list_ids(
189
+            project_id="fake_project_id_1", provider_id=provider_id_1,
190
+            start_date=date1,
191
+            all_tenants=True)), checkpoints_2)
192
+
138 193
     def test_list_checkpoints_by_date_with_marker(self):
139 194
         collection = self._create_test_collection()
140 195
         date = datetime.strptime("2018-11-12", "%Y-%m-%d")

+ 4
- 0
releasenotes/notes/checkpoints-listing-with-all-tenants-d7b0d1a149cb690d.yaml View File

@@ -0,0 +1,4 @@
1
+---
2
+features:
3
+  - |
4
+    Add support for listing checkpoints by admin with all_tenants.

Loading…
Cancel
Save