From 7998d7bb3fdce093df2c88e5b82b1bfa9db5560b Mon Sep 17 00:00:00 2001 From: ricolin Date: Tue, 15 Nov 2016 08:39:07 +0800 Subject: =?UTF-8?q?Add=20optional=20arguments=20'-y'=20in=20CLI=EF=BC=9Asn?= =?UTF-8?q?apshot-delete?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit There is no judgement before use the cli: heat snapshot-delete. So I add it. Partial-Bug: #1642490 Depends-On: If7b515dff64a18f56046b890279c2c59b0ab9dc7 Change-Id: I6e82630816e54aa2d98c3653ab43b865f445e881 --- heatclient/tests/unit/test_shell.py | 64 ++++++++++++++++++++++++++++++++++++- heatclient/v1/shell.py | 23 ++++++++++++- 2 files changed, 85 insertions(+), 2 deletions(-) diff --git a/heatclient/tests/unit/test_shell.py b/heatclient/tests/unit/test_shell.py index abb5845..8ac91d3 100644 --- a/heatclient/tests/unit/test_shell.py +++ b/heatclient/tests/unit/test_shell.py @@ -32,6 +32,7 @@ import testscenarios import testtools import yaml +from heatclient._i18n import _ from heatclient.common import http from heatclient.common import utils from heatclient import exc @@ -2225,6 +2226,65 @@ class ShellTestUserPass(ShellBase): resp = self.shell('snapshot-show teststack/1 2') self.assertEqual(resp_dict, jsonutils.loads(resp)) + # the main thing this @mock.patch is doing here is keeping + # sys.stdin untouched for later tests + @mock.patch('sys.stdin', new_callable=six.StringIO) + def test_snapshot_delete_prompt_with_tty(self, ms): + self.register_keystone_auth_fixture() + resp_dict = {"snapshot": { + "id": "2", + "creation_time": "2012-10-25T01:58:47Z" + }} + + mock_stdin = mock.Mock() + mock_stdin.isatty = mock.Mock() + mock_stdin.isatty.return_value = True + mock_stdin.readline = mock.Mock() + mock_stdin.readline.return_value = 'n' + sys.stdin = mock_stdin + + self.mock_request_delete('/stacks/teststack/1/snapshots/2', resp_dict) + + self.m.ReplayAll() + + resp = self.shell('snapshot-delete teststack/1 2') + resp_text = ('Are you sure you want to delete the snapshot of ' + 'this stack [Y/N]?') + self.assertEqual(resp_text, resp) + self.m.ReplayAll() + + mock_stdin.readline.return_value = 'Y' + resp = self.shell('snapshot-delete teststack/1 2') + msg = _("Request to delete the snapshot 2 of the stack " + "teststack/1 has been accepted.") + self.assertRegex(resp, msg) + + # the main thing this @mock.patch is doing here is keeping + # sys.stdin untouched for later tests + @mock.patch('sys.stdin', new_callable=six.StringIO) + def test_snapshot_delete_prompt_with_tty_y(self, ms): + self.register_keystone_auth_fixture() + resp_dict = {"snapshot": { + "id": "2", + "creation_time": "2012-10-25T01:58:47Z" + }} + + mock_stdin = mock.Mock() + mock_stdin.isatty = mock.Mock() + mock_stdin.isatty.return_value = True + mock_stdin.readline = mock.Mock() + mock_stdin.readline.return_value = '' + sys.stdin = mock_stdin + + self.mock_request_delete('/stacks/teststack/1/snapshots/2', resp_dict) + + self.m.ReplayAll() + # -y from the shell should skip the n/y prompt + resp = self.shell('snapshot-delete -y teststack/1 2') + msg = _("Request to delete the snapshot 2 of the stack " + "teststack/1 has been accepted.") + self.assertRegex(resp, msg) + def test_snapshot_delete(self): self.register_keystone_auth_fixture() @@ -2236,7 +2296,9 @@ class ShellTestUserPass(ShellBase): self.m.ReplayAll() resp = self.shell('snapshot-delete teststack/1 2') - self.assertEqual("", resp) + msg = _("Request to delete the snapshot 2 of the stack " + "teststack/1 has been accepted.") + self.assertRegex(resp, msg) def test_stack_restore(self): self.register_keystone_auth_fixture() diff --git a/heatclient/v1/shell.py b/heatclient/v1/shell.py index 70ffc9f..97484ca 100644 --- a/heatclient/v1/shell.py +++ b/heatclient/v1/shell.py @@ -1634,13 +1634,34 @@ def do_snapshot_show(hc, args): help=_('Name or ID of the stack containing the snapshot.')) @utils.arg('snapshot', metavar='', help=_('The ID of the snapshot to delete.')) +@utils.arg('-y', '--yes', default=False, action="store_true", + help=_('Skip yes/no prompt (assume yes).')) def do_snapshot_delete(hc, args): '''Delete a snapshot of a stack.''' show_deprecated('heat snapshot-delete', 'openstack stack snapshot delete') - + msg = "User did not confirm snapshot delete %sso taking no action." + try: + if not args.yes and sys.stdin.isatty(): + sys.stdout.write( + _('Are you sure you want to delete the snapshot of this ' + 'stack [Y/N]?')) + prompt_response = sys.stdin.readline().lower() + if not prompt_response.startswith('y'): + logger.info(msg, '') + return + except KeyboardInterrupt: # ctrl-c + logger.info(msg, '(ctrl-c) ') + return + except EOFError: # ctrl-d + logger.info(msg, '(ctrl-d) ') + return fields = {'stack_id': args.id, 'snapshot_id': args.snapshot} try: hc.stacks.snapshot_delete(**fields) + success_msg = _("Request to delete the snapshot %(snapshot_id)s of " + "the stack %(stack_id)s has been accepted.") + print(success_msg % {'stack_id': args.id, + 'snapshot_id': args.snapshot}) except exc.HTTPNotFound: raise exc.CommandError(_('Stack or snapshot not found')) -- cgit v1.2.1