summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDr. Jens Harbott <harbott@osism.tech>2022-11-15 08:57:26 +0100
committerDr. Jens Harbott <harbott@osism.tech>2023-02-15 09:42:45 +0100
commit7506eb8e0c459494bdb4ec47de2e18a2a953d727 (patch)
tree736a65c068c7f695593a5829eb4a65719d66ccfc
parent69182a04b223824b5da1aeca253c815a9affedbc (diff)
downloadpython-openstackclient-7506eb8e0c459494bdb4ec47de2e18a2a953d727.tar.gz
Add auto-approve option to project cleanup
Sometimes it is useful to be able to use the project cleanup function in scripts and having to add "echo y | openstack" isn't really nice. Signed-off-by: Dr. Jens Harbott <harbott@osism.tech> Change-Id: I3ded9982769b4568b6dcfc49bc31ba67a6227839
-rw-r--r--openstackclient/common/project_cleanup.py29
-rw-r--r--openstackclient/tests/unit/common/test_project_cleanup.py26
-rw-r--r--releasenotes/notes/add-auto-approve-cleanup-a2d225faa42dfdcb.yaml6
3 files changed, 50 insertions, 11 deletions
diff --git a/openstackclient/common/project_cleanup.py b/openstackclient/common/project_cleanup.py
index 1479f1a4..1193051a 100644
--- a/openstackclient/common/project_cleanup.py
+++ b/openstackclient/common/project_cleanup.py
@@ -48,10 +48,16 @@ class ProjectCleanup(command.Command):
def get_parser(self, prog_name):
parser = super(ProjectCleanup, self).get_parser(prog_name)
- parser.add_argument(
+ action_group = parser.add_mutually_exclusive_group()
+ action_group.add_argument(
'--dry-run',
action='store_true',
- help=_("List a project's resources")
+ help=_("List a project's resources but do not delete them")
+ )
+ action_group.add_argument(
+ '--auto-approve',
+ action='store_true',
+ help=_("Delete resources without asking for confirmation")
)
project_group = parser.add_mutually_exclusive_group(required=True)
project_group.add_argument(
@@ -67,12 +73,12 @@ class ProjectCleanup(command.Command):
parser.add_argument(
'--created-before',
metavar='<YYYY-MM-DDTHH24:MI:SS>',
- help=_('Drop resources created before the given time')
+ help=_('Only delete resources created before the given time')
)
parser.add_argument(
'--updated-before',
metavar='<YYYY-MM-DDTHH24:MI:SS>',
- help=_('Drop resources updated before the given time')
+ help=_('Only delete resources updated before the given time')
)
identity_common.add_project_domain_option_to_parser(parser)
return parser
@@ -127,12 +133,13 @@ class ProjectCleanup(command.Command):
if parsed_args.dry_run:
return
- confirm = ask_user_yesno(
- _("These resources will be deleted. Are you sure"))
+ if not parsed_args.auto_approve:
+ if not ask_user_yesno(
+ _("These resources will be deleted. Are you sure")):
+ return
- if confirm:
- self.log.warning(_('Deleting resources'))
+ self.log.warning(_('Deleting resources'))
- project_connect.project_cleanup(dry_run=False,
- status_queue=status_queue,
- filters=filters)
+ project_connect.project_cleanup(dry_run=False,
+ status_queue=status_queue,
+ filters=filters)
diff --git a/openstackclient/tests/unit/common/test_project_cleanup.py b/openstackclient/tests/unit/common/test_project_cleanup.py
index d235aeb0..50c434b9 100644
--- a/openstackclient/tests/unit/common/test_project_cleanup.py
+++ b/openstackclient/tests/unit/common/test_project_cleanup.py
@@ -85,6 +85,32 @@ class TestProjectCleanup(TestProjectCleanupBase):
self.assertIsNone(result)
+ def test_project_cleanup_with_auto_approve(self):
+ arglist = [
+ '--project', self.project.id,
+ '--auto-approve',
+ ]
+ verifylist = [
+ ('dry_run', False),
+ ('auth_project', False),
+ ('project', self.project.id),
+ ('auto_approve', True),
+ ]
+ parsed_args = self.check_parser(self.cmd, arglist, verifylist)
+ result = None
+
+ result = self.cmd.take_action(parsed_args)
+
+ self.sdk_connect_as_project_mock.assert_called_with(
+ self.project)
+ calls = [
+ mock.call(dry_run=True, status_queue=mock.ANY, filters={}),
+ mock.call(dry_run=False, status_queue=mock.ANY, filters={})
+ ]
+ self.project_cleanup_mock.assert_has_calls(calls)
+
+ self.assertIsNone(result)
+
def test_project_cleanup_with_project(self):
arglist = [
'--project', self.project.id,
diff --git a/releasenotes/notes/add-auto-approve-cleanup-a2d225faa42dfdcb.yaml b/releasenotes/notes/add-auto-approve-cleanup-a2d225faa42dfdcb.yaml
new file mode 100644
index 00000000..945320b3
--- /dev/null
+++ b/releasenotes/notes/add-auto-approve-cleanup-a2d225faa42dfdcb.yaml
@@ -0,0 +1,6 @@
+---
+features:
+ - |
+ An ``--auto-approve`` option has been added to the
+ ``project cleanup`` command. This allows the interactive
+ confirmation of resource deletion to be skipped.