summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorjiaopengju <jiaopengju@cmss.chinamobile.com>2018-11-18 19:46:47 +0800
committerjiaopengju <jiaopengju@cmss.chinamobile.com>2018-11-19 09:35:33 +0800
commitabab99c0eb57ceef1c9701000c16c0616fba3e2b (patch)
tree0766651dc13e429899b05646f8756461716e4b97
parent0a756778ed2e14ec054911b61b00cace65f47620 (diff)
Add all tenants support for checkpoints listing
Notes
Notes (review): Code-Review+2: Jiao Pengju <jiaopengju@cmss.chinamobile.com> Workflow+1: Jiao Pengju <jiaopengju@cmss.chinamobile.com> Verified+2: Zuul Submitted-by: Zuul Submitted-at: Mon, 19 Nov 2018 03:11:31 +0000 Reviewed-on: https://review.openstack.org/618639 Project: openstack/karbor Branch: refs/heads/master
-rw-r--r--karbor/api/v1/providers.py9
-rw-r--r--karbor/services/protection/api.py5
-rw-r--r--karbor/services/protection/checkpoint.py68
-rw-r--r--karbor/services/protection/manager.py6
-rwxr-xr-xkarbor/services/protection/provider.py6
-rw-r--r--karbor/services/protection/rpcapi.py6
-rw-r--r--karbor/tests/unit/protection/test_checkpoint_collection.py55
-rw-r--r--releasenotes/notes/checkpoints-listing-with-all-tenants-d7b0d1a149cb690d.yaml4
8 files changed, 134 insertions, 25 deletions
diff --git a/karbor/api/v1/providers.py b/karbor/api/v1/providers.py
index 679e468..095ffa5 100644
--- a/karbor/api/v1/providers.py
+++ b/karbor/api/v1/providers.py
@@ -311,7 +311,8 @@ class ProvidersController(wsgi.Controller):
311 311
312 if filters is None: 312 if filters is None:
313 filters = {} 313 filters = {}
314 314 all_tenants = utils.get_bool_param(
315 'all_tenants', filters) and context.is_admin
315 try: 316 try:
316 if limit is not None: 317 if limit is not None:
317 limit = int(limit) 318 limit = int(limit)
@@ -325,12 +326,16 @@ class ProvidersController(wsgi.Controller):
325 if filters: 326 if filters:
326 LOG.debug("Searching by: %s.", six.text_type(filters)) 327 LOG.debug("Searching by: %s.", six.text_type(filters))
327 328
329 if all_tenants:
330 del filters['all_tenants']
328 checkpoints = self.protection_api.list_checkpoints( 331 checkpoints = self.protection_api.list_checkpoints(
329 context, provider_id, marker, limit, 332 context, provider_id, marker, limit,
330 sort_keys=sort_keys, 333 sort_keys=sort_keys,
331 sort_dirs=sort_dirs, 334 sort_dirs=sort_dirs,
332 filters=filters, 335 filters=filters,
333 offset=offset) 336 offset=offset,
337 all_tenants=all_tenants
338 )
334 339
335 LOG.info("Get all checkpoints completed successfully.") 340 LOG.info("Get all checkpoints completed successfully.")
336 return checkpoints 341 return checkpoints
diff --git a/karbor/services/protection/api.py b/karbor/services/protection/api.py
index 369d067..d8c01ef 100644
--- a/karbor/services/protection/api.py
+++ b/karbor/services/protection/api.py
@@ -52,7 +52,7 @@ class API(base.Base):
52 ) 52 )
53 53
54 def list_checkpoints(self, context, provider_id, marker, limit, 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 return self.protection_rpcapi.list_checkpoints( 56 return self.protection_rpcapi.list_checkpoints(
57 context, 57 context,
58 provider_id, 58 provider_id,
@@ -60,7 +60,8 @@ class API(base.Base):
60 limit, 60 limit,
61 sort_keys, 61 sort_keys,
62 sort_dirs, 62 sort_dirs,
63 filters 63 filters,
64 all_tenants
64 ) 65 )
65 66
66 def list_protectable_types(self, context): 67 def list_protectable_types(self, context):
diff --git a/karbor/services/protection/checkpoint.py b/karbor/services/protection/checkpoint.py
index 72cbf24..003f9af 100644
--- a/karbor/services/protection/checkpoint.py
+++ b/karbor/services/protection/checkpoint.py
@@ -303,9 +303,49 @@ class CheckpointCollection(object):
303 self._checkpoints_section = bank.get_sub_section("/checkpoints") 303 self._checkpoints_section = bank.get_sub_section("/checkpoints")
304 self._indices_section = bank.get_sub_section("/indices") 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 def list_ids(self, project_id, provider_id, limit=None, marker=None, 346 def list_ids(self, project_id, provider_id, limit=None, marker=None,
307 plan_id=None, start_date=None, end_date=None, sort_dir=None, 347 plan_id=None, start_date=None, end_date=None, sort_dir=None,
308 context=None): 348 context=None, all_tenants=False):
309 marker_checkpoint = None 349 marker_checkpoint = None
310 if marker is not None: 350 if marker is not None:
311 checkpoint_section = self._checkpoints_section.get_sub_section( 351 checkpoint_section = self._checkpoints_section.get_sub_section(
@@ -319,25 +359,22 @@ class CheckpointCollection(object):
319 end_date = timeutils.utcnow() 359 end_date = timeutils.utcnow()
320 360
321 if plan_id is None and start_date is None: 361 if plan_id is None and start_date is None:
322 prefix = "/by-provider/%s/%s/" % (provider_id, project_id) 362 prefix, marker = self._get_prefix_and_marker_by_provider(
323 if marker is not None: 363 provider_id, project_id, marker,
324 marker = "/%s" % marker 364 marker_checkpoint, all_tenants)
325 elif plan_id is not None: 365 elif plan_id is not None:
326 prefix = "/by-plan/%s/%s/" % (plan_id, project_id) 366 prefix, marker = self._get_prefix_and_marker_by_plan(
327 if marker is not None: 367 plan_id, project_id, marker, marker_checkpoint, all_tenants)
328 date = marker_checkpoint["created_at"]
329 marker = "/%s/%s" % (date, marker)
330 else: 368 else:
331 prefix = "/by-date/" 369 prefix, marker = self._get_prefix_and_marker_by_date(
332 if marker is not None: 370 project_id, marker, marker_checkpoint, all_tenants)
333 date = marker_checkpoint["created_at"]
334 marker = "/%s/%s/%s" % (date, project_id, marker)
335 371
336 return self._list_ids(project_id, prefix, limit, marker, start_date, 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 def _list_ids(self, project_id, prefix, limit, marker, start_date, 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 if start_date is None: 378 if start_date is None:
342 return [key[key.find("@") + 1:] 379 return [key[key.find("@") + 1:]
343 for key in self._indices_section.list_objects( 380 for key in self._indices_section.list_objects(
@@ -359,7 +396,8 @@ class CheckpointCollection(object):
359 key.split("/")[date_cursor], "%Y-%m-%d") 396 key.split("/")[date_cursor], "%Y-%m-%d")
360 checkpoint_project_id = key.split("/")[project_id_cursor] 397 checkpoint_project_id = key.split("/")[project_id_cursor]
361 if start_date <= date <= end_date and ( 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 ids.append(key[key.find("@") + 1:]) 401 ids.append(key[key.find("@") + 1:])
364 if limit is not None and len(ids) == limit: 402 if limit is not None and len(ids) == limit:
365 return ids 403 return ids
diff --git a/karbor/services/protection/manager.py b/karbor/services/protection/manager.py
index 4ffcad1..59aafcf 100644
--- a/karbor/services/protection/manager.py
+++ b/karbor/services/protection/manager.py
@@ -369,7 +369,8 @@ class ProtectionManager(manager.Manager):
369 exception.CheckpointNotFound, 369 exception.CheckpointNotFound,
370 exception.BankListObjectsFailed) 370 exception.BankListObjectsFailed)
371 def list_checkpoints(self, context, provider_id, marker=None, limit=None, 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 LOG.info("Starting list checkpoints. provider_id:%s", provider_id) 374 LOG.info("Starting list checkpoints. provider_id:%s", provider_id)
374 plan_id = filters.get("plan_id", None) 375 plan_id = filters.get("plan_id", None)
375 start_date = None 376 start_date = None
@@ -380,13 +381,14 @@ class ProtectionManager(manager.Manager):
380 if filters.get("end_date", None): 381 if filters.get("end_date", None):
381 end_date = datetime.strptime( 382 end_date = datetime.strptime(
382 filters.get("end_date"), "%Y-%m-%d") 383 filters.get("end_date"), "%Y-%m-%d")
384
383 sort_dir = None if sort_dirs is None else sort_dirs[0] 385 sort_dir = None if sort_dirs is None else sort_dirs[0]
384 provider = self.provider_registry.show_provider(provider_id) 386 provider = self.provider_registry.show_provider(provider_id)
385 project_id = context.project_id 387 project_id = context.project_id
386 checkpoint_ids = provider.list_checkpoints( 388 checkpoint_ids = provider.list_checkpoints(
387 project_id, provider_id, limit=limit, marker=marker, 389 project_id, provider_id, limit=limit, marker=marker,
388 plan_id=plan_id, start_date=start_date, end_date=end_date, 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 checkpoints = [] 392 checkpoints = []
391 for checkpoint_id in checkpoint_ids: 393 for checkpoint_id in checkpoint_ids:
392 checkpoint = provider.get_checkpoint(checkpoint_id, 394 checkpoint = provider.get_checkpoint(checkpoint_id,
diff --git a/karbor/services/protection/provider.py b/karbor/services/protection/provider.py
index 359f95f..0ed0109 100755
--- a/karbor/services/protection/provider.py
+++ b/karbor/services/protection/provider.py
@@ -150,12 +150,14 @@ class PluggableProtectionProvider(object):
150 150
151 def list_checkpoints(self, project_id, provider_id, limit=None, 151 def list_checkpoints(self, project_id, provider_id, limit=None,
152 marker=None, plan_id=None, start_date=None, 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 checkpoint_collection = self.get_checkpoint_collection() 155 checkpoint_collection = self.get_checkpoint_collection()
155 return checkpoint_collection.list_ids( 156 return checkpoint_collection.list_ids(
156 project_id=project_id, provider_id=provider_id, limit=limit, 157 project_id=project_id, provider_id=provider_id, limit=limit,
157 marker=marker, plan_id=plan_id, start_date=start_date, 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
161class ProviderRegistry(object): 163class ProviderRegistry(object):
diff --git a/karbor/services/protection/rpcapi.py b/karbor/services/protection/rpcapi.py
index 8e3de81..7fd2f81 100644
--- a/karbor/services/protection/rpcapi.py
+++ b/karbor/services/protection/rpcapi.py
@@ -92,7 +92,7 @@ class ProtectionAPI(object):
92 92
93 def list_checkpoints(self, ctxt, provider_id, marker=None, 93 def list_checkpoints(self, ctxt, provider_id, marker=None,
94 limit=None, sort_keys=None, 94 limit=None, sort_keys=None,
95 sort_dirs=None, filters=None): 95 sort_dirs=None, filters=None, all_tenants=False):
96 cctxt = self.client.prepare(version='1.0') 96 cctxt = self.client.prepare(version='1.0')
97 return cctxt.call( 97 return cctxt.call(
98 ctxt, 98 ctxt,
@@ -102,7 +102,9 @@ class ProtectionAPI(object):
102 limit=limit, 102 limit=limit,
103 sort_keys=sort_keys, 103 sort_keys=sort_keys,
104 sort_dirs=sort_dirs, 104 sort_dirs=sort_dirs,
105 filters=filters) 105 filters=filters,
106 all_tenants=all_tenants
107 )
106 108
107 def list_protectable_types(self, ctxt): 109 def list_protectable_types(self, ctxt):
108 cctxt = self.client.prepare(version='1.0') 110 cctxt = self.client.prepare(version='1.0')
diff --git a/karbor/tests/unit/protection/test_checkpoint_collection.py b/karbor/tests/unit/protection/test_checkpoint_collection.py
index 5f10d4d..4232eb0 100644
--- a/karbor/tests/unit/protection/test_checkpoint_collection.py
+++ b/karbor/tests/unit/protection/test_checkpoint_collection.py
@@ -47,6 +47,23 @@ class CheckpointCollectionTest(base.TestCase):
47 self.assertEqual(set(collection.list_ids( 47 self.assertEqual(set(collection.list_ids(
48 project_id=project_id, provider_id=provider_id)), result) 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 def test_list_checkpoints_by_plan_id(self): 67 def test_list_checkpoints_by_plan_id(self):
51 collection = self._create_test_collection() 68 collection = self._create_test_collection()
52 plan_1 = fake_protection_plan() 69 plan_1 = fake_protection_plan()
@@ -69,6 +86,21 @@ class CheckpointCollectionTest(base.TestCase):
69 project_id="fake_project_id_2", provider_id=provider_id_2, 86 project_id="fake_project_id_2", provider_id=provider_id_2,
70 plan_id="fake_plan_id_2")), checkpoints_plan_2) 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 def test_list_checkpoints_by_plan_id_and_filter_by_start_date(self): 104 def test_list_checkpoints_by_plan_id_and_filter_by_start_date(self):
73 collection = self._create_test_collection() 105 collection = self._create_test_collection()
74 date1 = datetime.strptime("2018-11-12", "%Y-%m-%d") 106 date1 = datetime.strptime("2018-11-12", "%Y-%m-%d")
@@ -135,6 +167,29 @@ class CheckpointCollectionTest(base.TestCase):
135 end_date=date2)), 167 end_date=date2)),
136 checkpoints_date_2) 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 def test_list_checkpoints_by_date_with_marker(self): 193 def test_list_checkpoints_by_date_with_marker(self):
139 collection = self._create_test_collection() 194 collection = self._create_test_collection()
140 date = datetime.strptime("2018-11-12", "%Y-%m-%d") 195 date = datetime.strptime("2018-11-12", "%Y-%m-%d")
diff --git a/releasenotes/notes/checkpoints-listing-with-all-tenants-d7b0d1a149cb690d.yaml b/releasenotes/notes/checkpoints-listing-with-all-tenants-d7b0d1a149cb690d.yaml
new file mode 100644
index 0000000..fe08e3b
--- /dev/null
+++ b/releasenotes/notes/checkpoints-listing-with-all-tenants-d7b0d1a149cb690d.yaml
@@ -0,0 +1,4 @@
1---
2features:
3 - |
4 Add support for listing checkpoints by admin with all_tenants.