From 04e1a2b520b006d1ef963d6721dc92c766796884 Mon Sep 17 00:00:00 2001 From: Kien Nguyen Date: Fri, 1 Jun 2018 10:45:39 +0700 Subject: [PATCH] Add quota_destroy_all_by_project This method will be used in the follow-up patch to add api controller for the quotas. Change-Id: I0562285bfa4bab431ced967be381292ed5962756 --- zun/db/api.py | 7 +++++++ zun/db/etcd/api.py | 5 +++++ zun/db/sqlalchemy/api.py | 18 ++++++++++++++++++ zun/objects/quota.py | 12 +++++++++++- zun/tests/unit/objects/test_objects.py | 2 +- 5 files changed, 42 insertions(+), 2 deletions(-) diff --git a/zun/db/api.py b/zun/db/api.py index 72e1994c8..0f2a52dc5 100644 --- a/zun/db/api.py +++ b/zun/db/api.py @@ -940,6 +940,13 @@ def quota_destroy(context, project_id, resource): resource) +@profiler.trace("db") +def quota_destroy_all_by_project(context, project_id, resource): + """Destroy all resource associated with a given project.""" + return _get_dbdriver_instance().quota_destroy_all_by_project(context, + project_id) + + @profiler.trace("db") def quota_class_create(context, class_name, resource, limit): """Create a quota class for the given name and resource""" diff --git a/zun/db/etcd/api.py b/zun/db/etcd/api.py index b6c913ee5..2a60a87da 100644 --- a/zun/db/etcd/api.py +++ b/zun/db/etcd/api.py @@ -1234,6 +1234,11 @@ class EtcdAPI(object): def quota_destroy(self, context, project_id, resource): self.client.delete('/quotas/{}/{}' . format(project_id, resource)) + @lockutils.synchronized('etcd_quota') + def quota_destroy_all_by_project(self, context, project_id): + self.client.delete('/quotas/{}' . format(project_id)) + self.client.delete('/quota_usages/{}' . format(project_id)) + def quota_class_create(self, context, class_name, resource, limit): quota_class_data = { 'class_name': class_name, diff --git a/zun/db/sqlalchemy/api.py b/zun/db/sqlalchemy/api.py index 43fff9925..59ea54d52 100644 --- a/zun/db/sqlalchemy/api.py +++ b/zun/db/sqlalchemy/api.py @@ -1099,6 +1099,24 @@ class Connection(object): filter_by(resource=resource) query.delete() + def quota_destroy_all_by_project(self, context, project_id): + session = get_session() + with session.begin(): + quotas = model_query(context, models.Quota, session=session).\ + filter_by(project_id).\ + all() + + for quota in quotas: + quota.delete(session=session) + + quota_usages = model_query(context, models.QuotaUsage, + session=session).\ + filter_by(project_id=project_id).\ + all() + + for quota_usage in quota_usages: + quota_usage.delete(session=session) + def quota_class_create(self, context, class_name, resource, limit): quota_class_ref = models.QuotaClass() quota_class_ref.class_name = class_name diff --git a/zun/objects/quota.py b/zun/objects/quota.py index 5b6807508..eb5e77ea7 100644 --- a/zun/objects/quota.py +++ b/zun/objects/quota.py @@ -20,7 +20,8 @@ from zun.objects import base class Quota(base.ZunPersistentObject, base.ZunObject): # Version 1.0: Initial version # Version 1.1: Add uuid column - VERSION = '1.1' + # Version 1.2: Add destroy_all_by_project method + VERSION = '1.2' fields = { 'id': fields.IntegerField(), @@ -82,6 +83,15 @@ class Quota(base.ZunPersistentObject, base.ZunObject): db_quota = dbapi.quota_create(context, project_id, resource, limit) self._from_db_object(self, db_quota) + @base.remotable_classmethod + def destroy_all_by_project(cls, context, project_id): + """Destroy all quotas associated with a project. + + :param context: security context. + :param project_id: the id of the project + """ + dbapi.quota_destroy_all_by_project(context, project_id) + @base.remotable def destroy(self, context=None): """Delete the Quota from the DB. diff --git a/zun/tests/unit/objects/test_objects.py b/zun/tests/unit/objects/test_objects.py index 0adf1153c..e8558b261 100644 --- a/zun/tests/unit/objects/test_objects.py +++ b/zun/tests/unit/objects/test_objects.py @@ -358,7 +358,7 @@ object_data = { 'ComputeNode': '1.11-08be22db017745f4f0bc8f873eca7db0', 'PciDevicePool': '1.0-3f5ddc3ff7bfa14da7f6c7e9904cc000', 'PciDevicePoolList': '1.0-15ecf022a68ddbb8c2a6739cfc9f8f5e', - 'Quota': '1.1-627da82e1289d7a4df8db759e1df132b', + 'Quota': '1.2-3a7d520d119fe1e886baad968ef7990a', 'QuotaClass': '1.1-239ae335b32036b86504684d3fdbeb7f', 'ContainerPCIRequest': '1.0-b060f9f9f734bedde79a71a4d3112ee0', 'ContainerPCIRequests': '1.0-7b8f7f044661fe4e24e6949c035af2c4',