summaryrefslogtreecommitdiff
path: root/openstackclient
diff options
context:
space:
mode:
Diffstat (limited to 'openstackclient')
-rw-r--r--openstackclient/common/quota.py79
-rw-r--r--openstackclient/tests/unit/common/test_quota.py107
2 files changed, 186 insertions, 0 deletions
diff --git a/openstackclient/common/quota.py b/openstackclient/common/quota.py
index 44482367..0110feb6 100644
--- a/openstackclient/common/quota.py
+++ b/openstackclient/common/quota.py
@@ -697,3 +697,82 @@ class ShowQuota(command.ShowOne, BaseQuota):
info['project_name'] = project_name
return zip(*sorted(info.items()))
+
+
+class DeleteQuota(command.Command):
+ _description = _(
+ "Delete configured quota for a project and revert to defaults."
+ )
+
+ def get_parser(self, prog_name):
+ parser = super().get_parser(prog_name)
+ parser.add_argument(
+ 'project',
+ metavar='<project>',
+ help=_('Delete quotas for this project (name or ID)'),
+ )
+ option = parser.add_mutually_exclusive_group()
+ option.add_argument(
+ '--all',
+ action='store_const',
+ const='all',
+ dest='service',
+ default='all',
+ help=_('Delete project quotas for all services (default)'),
+ )
+ option.add_argument(
+ '--compute',
+ action='store_const',
+ const='compute',
+ dest='service',
+ default='all',
+ help=_(
+ 'Delete compute quotas for the project '
+ '(including network quotas when using nova-network)'
+ ),
+ )
+ option.add_argument(
+ '--volume',
+ action='store_const',
+ const='volume',
+ dest='service',
+ default='all',
+ help=_('Delete volume quotas for the project'),
+ )
+ option.add_argument(
+ '--network',
+ action='store_const',
+ const='network',
+ dest='service',
+ default='all',
+ help=_('Delete network quotas for the project'),
+ )
+ return parser
+
+ def take_action(self, parsed_args):
+ identity_client = self.app.client_manager.identity
+ project = utils.find_resource(
+ identity_client.projects,
+ parsed_args.project,
+ )
+
+ # compute quotas
+ if parsed_args.service in {'all', 'compute'}:
+ compute_client = self.app.client_manager.compute
+ compute_client.quotas.delete(project)
+
+ # volume quotas
+ if parsed_args.service in {'all', 'volume'}:
+ volume_client = self.app.client_manager.volume
+ volume_client.quotas.delete(project)
+
+ # network quotas (but only if we're not using nova-network, otherwise
+ # we already deleted the quotas in the compute step)
+ if (
+ parsed_args.service in {'all', 'network'}
+ and self.app.client_manager.is_network_endpoint_enabled()
+ ):
+ network_client = self.app.client_manager.network
+ network_client.quotas.delete(project)
+
+ return None
diff --git a/openstackclient/tests/unit/common/test_quota.py b/openstackclient/tests/unit/common/test_quota.py
index 70fd1436..087443c1 100644
--- a/openstackclient/tests/unit/common/test_quota.py
+++ b/openstackclient/tests/unit/common/test_quota.py
@@ -62,6 +62,9 @@ class TestQuota(compute_fakes.TestComputev2):
self.app.client_manager.volume.quota_classes
self.volume_quotas_class_mock.reset_mock()
+ self.app.client_manager.network.quotas = mock.Mock()
+ self.network_quotas_mock = self.app.client_manager.network.quotas
+
self.app.client_manager.auth_ref = mock.Mock()
self.app.client_manager.auth_ref.service_catalog = mock.Mock()
self.service_catalog_mock = \
@@ -1154,3 +1157,107 @@ class TestQuotaShow(TestQuota):
self.assertEqual(len(network_fakes.QUOTA) - 1, len(result))
# Go back to default mock
self.network.get_quota = orig_get_quota
+
+
+class TestQuotaDelete(TestQuota):
+ """Test cases for quota delete command"""
+
+ def setUp(self):
+ super().setUp()
+
+ self.cmd = quota.DeleteQuota(self.app, None)
+
+ def test_delete(self):
+ """Delete all quotas"""
+ arglist = [
+ self.projects[0].id,
+ ]
+ verifylist = [
+ ('service', 'all'),
+ ('project', self.projects[0].id),
+ ]
+
+ parsed_args = self.check_parser(self.cmd, arglist, verifylist)
+
+ result = self.cmd.take_action(parsed_args)
+
+ self.assertIsNone(result)
+ self.projects_mock.get.assert_called_once_with(self.projects[0].id)
+ self.compute_quotas_mock.delete.assert_called_once_with(
+ self.projects[0],
+ )
+ self.volume_quotas_mock.delete.assert_called_once_with(
+ self.projects[0],
+ )
+ self.network_quotas_mock.delete.assert_called_once_with(
+ self.projects[0],
+ )
+
+ def test_delete__compute(self):
+ """Delete compute quotas only"""
+ arglist = [
+ '--compute',
+ self.projects[0].id,
+ ]
+ verifylist = [
+ ('service', 'compute'),
+ ('project', self.projects[0].id),
+ ]
+
+ parsed_args = self.check_parser(self.cmd, arglist, verifylist)
+
+ result = self.cmd.take_action(parsed_args)
+
+ self.assertIsNone(result)
+ self.projects_mock.get.assert_called_once_with(self.projects[0].id)
+ self.compute_quotas_mock.delete.assert_called_once_with(
+ self.projects[0],
+ )
+ self.volume_quotas_mock.delete.assert_not_called()
+ self.network_quotas_mock.delete.assert_not_called()
+
+ def test_delete__volume(self):
+ """Delete volume quotas only"""
+ arglist = [
+ '--volume',
+ self.projects[0].id,
+ ]
+ verifylist = [
+ ('service', 'volume'),
+ ('project', self.projects[0].id),
+ ]
+
+ parsed_args = self.check_parser(self.cmd, arglist, verifylist)
+
+ result = self.cmd.take_action(parsed_args)
+
+ self.assertIsNone(result)
+ self.projects_mock.get.assert_called_once_with(self.projects[0].id)
+ self.compute_quotas_mock.delete.assert_not_called()
+ self.volume_quotas_mock.delete.assert_called_once_with(
+ self.projects[0],
+ )
+ self.network_quotas_mock.delete.assert_not_called()
+
+ def test_delete__network(self):
+ """Delete network quotas only"""
+ arglist = [
+ '--network',
+ self.projects[0].id,
+ ]
+ verifylist = [
+ ('service', 'network'),
+ ('project', self.projects[0].id),
+ ]
+
+ parsed_args = self.check_parser(self.cmd, arglist, verifylist)
+
+ result = self.cmd.take_action(parsed_args)
+
+ self.assertIsNone(result)
+ self.projects_mock.get.assert_called_once_with(self.projects[0].id)
+ self.compute_quotas_mock.delete.assert_not_called()
+ self.volume_quotas_mock.delete.assert_not_called()
+ self.network_quotas_mock.delete.assert_called_once_with(
+ self.projects[0],
+ )