Add '--yes' for openstack stack snapshot delete

Add optional arguments '--yes' or '-y' in osc cli `openstack stack
snapshot delete`. There is no judgement before use that cli
So propose to add it in case that we delete the snapshot unintentionally.
Closes-Bug: #1642874

Change-Id: If7b515dff64a18f56046b890279c2c59b0ab9dc7
This commit is contained in:
ricolin 2016-11-16 11:48:54 +08:00
parent 71b4fc35d0
commit e2d92f7930
2 changed files with 51 additions and 1 deletions

View File

@ -14,6 +14,7 @@
"""Orchestration v1 Stack Snapshot implementations."""
import logging
import sys
from osc_lib.command import command
from osc_lib import exceptions as exc
@ -192,11 +193,34 @@ class DeleteSnapshot(command.Command):
metavar='<snapshot>',
help=_('ID of stack snapshot')
)
parser.add_argument(
'-y', '--yes',
action='store_true',
help=_('Skip yes/no prompt (assume yes)')
)
return parser
def take_action(self, parsed_args):
self.log.debug('take_action(%s)' % parsed_args)
heat_client = self.app.client_manager.orchestration
msg = ('User did not confirm snapshot delete '
'%sso taking no action.')
try:
if not parsed_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'):
self.log.info(msg, '')
return
except KeyboardInterrupt: # ctrl-c
self.log.info(msg, '(ctrl-c) ')
return
except EOFError: # ctrl-d
self.log.info(msg, '(ctrl-d) ')
return
try:
heat_client.stacks.snapshot_delete(parsed_args.stack,
parsed_args.snapshot)

View File

@ -10,8 +10,9 @@
# License for the specific language governing permissions and limitations
# under the License.
#
import mock
from osc_lib import exceptions as exc
import six
from heatclient import exc as heat_exc
from heatclient.osc.v1 import snapshot
@ -157,3 +158,28 @@ class TestSnapshotDelete(TestStack):
exc.CommandError,
self.cmd.take_action,
parsed_args)
@mock.patch('sys.stdin', spec=six.StringIO)
def test_snapshot_delete_prompt(self, mock_stdin):
arglist = ['my_stack', 'snapshot_id']
mock_stdin.isatty.return_value = True
mock_stdin.readline.return_value = 'y'
parsed_args = self.check_parser(self.cmd, arglist, [])
self.cmd.take_action(parsed_args)
mock_stdin.readline.assert_called_with()
self.stack_client.snapshot_delete.assert_called_with('my_stack',
'snapshot_id')
@mock.patch('sys.stdin', spec=six.StringIO)
def test_snapshot_delete_prompt_no(self, mock_stdin):
arglist = ['my_stack', 'snapshot_id']
mock_stdin.isatty.return_value = True
mock_stdin.readline.return_value = 'n'
parsed_args = self.check_parser(self.cmd, arglist, [])
self.cmd.take_action(parsed_args)
mock_stdin.readline.assert_called_with()
self.stack_client.snapshot_delete.assert_not_called()