Add OSC plugin for openstack cluster delete
This change implements the "openstack cluster delete" command Based on the existing senlin command: senlin cluster-delete Change-Id: Ib94d33384cbaab817a17d8581a5390a4e11457da Blueprint: senlin-support-python-openstackclient
This commit is contained in:
parent
2b9251da64
commit
590c96dbb8
@ -14,7 +14,9 @@
|
||||
|
||||
import logging
|
||||
import six
|
||||
import sys
|
||||
|
||||
from cliff import command
|
||||
from cliff import lister
|
||||
from cliff import show
|
||||
from openstack import exceptions as sdk_exc
|
||||
@ -22,6 +24,7 @@ from openstackclient.common import exceptions as exc
|
||||
from openstackclient.common import utils
|
||||
|
||||
from senlinclient.common.i18n import _
|
||||
from senlinclient.common.i18n import _LI
|
||||
from senlinclient.common import utils as senlin_utils
|
||||
|
||||
|
||||
@ -262,3 +265,58 @@ class UpdateCluster(show.ShowOne):
|
||||
|
||||
senlin_client.update_cluster(cluster.id, **attrs)
|
||||
return _show_cluster(senlin_client, cluster.id)
|
||||
|
||||
|
||||
class DeleteCluster(command.Command):
|
||||
"""Delete the cluster(s)."""
|
||||
|
||||
log = logging.getLogger(__name__ + ".DeleteCluster")
|
||||
|
||||
def get_parser(self, prog_name):
|
||||
parser = super(DeleteCluster, self).get_parser(prog_name)
|
||||
parser.add_argument(
|
||||
'cluster',
|
||||
metavar='<cluster>',
|
||||
nargs='+',
|
||||
help=_('Name or ID of cluster(s) to delete')
|
||||
)
|
||||
parser.add_argument(
|
||||
'--force',
|
||||
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)
|
||||
senlin_client = self.app.client_manager.clustering
|
||||
|
||||
try:
|
||||
if not parsed_args.force and sys.stdin.isatty():
|
||||
sys.stdout.write(
|
||||
_("Are you sure you want to delete this cluster(s)"
|
||||
" [y/N]?"))
|
||||
prompt_response = sys.stdin.readline().lower()
|
||||
if not prompt_response.startswith('y'):
|
||||
return
|
||||
except KeyboardInterrupt: # Ctrl-c
|
||||
self.log.info(_LI('Ctrl-c detected.'))
|
||||
return
|
||||
except EOFError: # Ctrl-d
|
||||
self.log.info(_LI('Ctrl-d detected'))
|
||||
return
|
||||
|
||||
failure_count = 0
|
||||
|
||||
for cid in parsed_args.cluster:
|
||||
try:
|
||||
senlin_client.delete_cluster(cid, False)
|
||||
except Exception as ex:
|
||||
failure_count += 1
|
||||
print(ex)
|
||||
if failure_count:
|
||||
raise exc.CommandError(_('Failed to delete %(count)s of the '
|
||||
'%(total)s specified cluster(s).') %
|
||||
{'count': failure_count,
|
||||
'total': len(parsed_args.cluster)})
|
||||
print('Request accepted')
|
||||
|
@ -12,6 +12,7 @@
|
||||
|
||||
import copy
|
||||
import mock
|
||||
import six
|
||||
|
||||
from openstack.cluster.v1 import cluster as sdk_cluster
|
||||
from openstack import exceptions as sdk_exc
|
||||
@ -320,3 +321,76 @@ class TestClusterUpdate(TestCluster):
|
||||
self.cmd.take_action,
|
||||
parsed_args)
|
||||
self.assertIn('ResourceNotFound: ResourceNotFound', str(error))
|
||||
|
||||
|
||||
class TestClusterDelete(TestCluster):
|
||||
def setUp(self):
|
||||
super(TestClusterDelete, self).setUp()
|
||||
self.cmd = osc_cluster.DeleteCluster(self.app, None)
|
||||
self.mock_client.delete_cluster = mock.Mock()
|
||||
|
||||
def test_cluster_delete(self):
|
||||
arglist = ['cluster1', 'cluster2', 'cluster3']
|
||||
parsed_args = self.check_parser(self.cmd, arglist, [])
|
||||
self.cmd.take_action(parsed_args)
|
||||
self.mock_client.delete_cluster.assert_has_calls(
|
||||
[mock.call('cluster1', False), mock.call('cluster2', False),
|
||||
mock.call('cluster3', False)]
|
||||
)
|
||||
|
||||
def test_cluster_delete_force(self):
|
||||
arglist = ['cluster1', 'cluster2', 'cluster3', '--force']
|
||||
parsed_args = self.check_parser(self.cmd, arglist, [])
|
||||
self.cmd.take_action(parsed_args)
|
||||
self.mock_client.delete_cluster.assert_has_calls(
|
||||
[mock.call('cluster1', False), mock.call('cluster2', False),
|
||||
mock.call('cluster3', False)]
|
||||
)
|
||||
|
||||
def test_cluster_delete_not_found(self):
|
||||
arglist = ['my_cluster']
|
||||
self.mock_client.delete_cluster.side_effect = sdk_exc.ResourceNotFound
|
||||
parsed_args = self.check_parser(self.cmd, arglist, [])
|
||||
error = self.assertRaises(exc.CommandError, self.cmd.take_action,
|
||||
parsed_args)
|
||||
self.assertIn('Failed to delete 1 of the 1 specified cluster(s).',
|
||||
str(error))
|
||||
|
||||
def test_cluster_delete_one_found_one_not_found(self):
|
||||
arglist = ['cluster1', 'cluster2']
|
||||
self.mock_client.delete_cluster.side_effect = (
|
||||
[None, sdk_exc.ResourceNotFound]
|
||||
)
|
||||
parsed_args = self.check_parser(self.cmd, arglist, [])
|
||||
error = self.assertRaises(exc.CommandError,
|
||||
self.cmd.take_action, parsed_args)
|
||||
self.mock_client.delete_cluster.assert_has_calls(
|
||||
[mock.call('cluster1', False), mock.call('cluster2', False)]
|
||||
)
|
||||
self.assertEqual('Failed to delete 1 of the 2 specified cluster(s).',
|
||||
str(error))
|
||||
|
||||
@mock.patch('sys.stdin', spec=six.StringIO)
|
||||
def test_cluster_delete_prompt_yes(self, mock_stdin):
|
||||
arglist = ['my_cluster']
|
||||
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.mock_client.delete_cluster.assert_called_with('my_cluster',
|
||||
False)
|
||||
|
||||
@mock.patch('sys.stdin', spec=six.StringIO)
|
||||
def test_cluster_delete_prompt_no(self, mock_stdin):
|
||||
arglist = ['my_cluster']
|
||||
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.mock_client.delete_cluster.assert_not_called()
|
||||
|
@ -31,6 +31,7 @@ openstack.cli.extension =
|
||||
|
||||
openstack.clustering.v1 =
|
||||
cluster_create = senlinclient.osc.v1.cluster:CreateCluster
|
||||
cluster_delete = senlinclient.osc.v1.cluster:DeleteCluster
|
||||
cluster_list = senlinclient.osc.v1.cluster:ListCluster
|
||||
cluster_show = senlinclient.osc.v1.cluster:ShowCluster
|
||||
cluster_update = senlinclient.osc.v1.cluster:UpdateCluster
|
||||
|
Loading…
x
Reference in New Issue
Block a user