Add "server group delete" command
Support compute v2 "server group delete" command in OSC. Implements: blueprint nova-server-group-support Partial-Bug: #1542171 Related-Bug: #1563301 Change-Id: I7d792d669b147b24dc774844cfc9dbacd60d017b
This commit is contained in:
parent
a06bb28bcc
commit
7458c612ed
@ -27,3 +27,19 @@ Create a new server group
|
||||
.. describe:: <name>
|
||||
|
||||
New server group name
|
||||
|
||||
server group delete
|
||||
-------------------
|
||||
|
||||
Delete an existing server group
|
||||
|
||||
.. program:: server group delete
|
||||
.. code-block:: bash
|
||||
|
||||
os server group delete
|
||||
<server-group> [<server-group> ...]
|
||||
|
||||
.. describe:: <server-group>
|
||||
|
||||
Server group(s) to delete (name or ID)
|
||||
(repeat to delete multiple server groups)
|
||||
|
@ -16,6 +16,7 @@
|
||||
"""Compute v2 Server Group action implementations"""
|
||||
|
||||
from openstackclient.common import command
|
||||
from openstackclient.common import exceptions
|
||||
from openstackclient.common import utils
|
||||
|
||||
|
||||
@ -66,3 +67,35 @@ class CreateServerGroup(command.ShowOne):
|
||||
data = utils.get_dict_properties(info, columns,
|
||||
formatters=_formatters)
|
||||
return columns, data
|
||||
|
||||
|
||||
class DeleteServerGroup(command.Command):
|
||||
"""Delete an existing server group."""
|
||||
|
||||
def get_parser(self, prog_name):
|
||||
parser = super(DeleteServerGroup, self).get_parser(prog_name)
|
||||
parser.add_argument(
|
||||
'server_group',
|
||||
metavar='<server-group>',
|
||||
nargs='+',
|
||||
help='server group(s) to delete (name or ID)',
|
||||
)
|
||||
return parser
|
||||
|
||||
def take_action(self, parsed_args):
|
||||
compute_client = self.app.client_manager.compute
|
||||
result = 0
|
||||
for group in parsed_args.server_group:
|
||||
try:
|
||||
group_obj = utils.find_resource(compute_client.server_groups,
|
||||
group)
|
||||
compute_client.server_groups.delete(group_obj.id)
|
||||
# Catch all exceptions in order to avoid to block the next deleting
|
||||
except Exception as e:
|
||||
result += 1
|
||||
self.app.log.error(e)
|
||||
|
||||
if result > 0:
|
||||
total = len(parsed_args.server_group)
|
||||
msg = "%s of %s server groups failed to delete." % (result, total)
|
||||
raise exceptions.CommandError(msg)
|
||||
|
@ -13,6 +13,9 @@
|
||||
# under the License.
|
||||
#
|
||||
|
||||
import mock
|
||||
|
||||
from openstackclient.common import exceptions
|
||||
from openstackclient.common import utils
|
||||
from openstackclient.compute.v2 import server_group
|
||||
from openstackclient.tests.compute.v2 import fakes as compute_fakes
|
||||
@ -106,3 +109,84 @@ class TestServerGroupCreate(TestServerGroup):
|
||||
self.cmd,
|
||||
arglist,
|
||||
verifylist)
|
||||
|
||||
|
||||
class TestServerGroupDelete(TestServerGroup):
|
||||
|
||||
def setUp(self):
|
||||
super(TestServerGroupDelete, self).setUp()
|
||||
|
||||
self.server_groups_mock.get.return_value = self.fake_server_group
|
||||
self.cmd = server_group.DeleteServerGroup(self.app, None)
|
||||
|
||||
def test_server_group_delete(self):
|
||||
arglist = [
|
||||
'affinity_group',
|
||||
]
|
||||
verifylist = [
|
||||
('server_group', ['affinity_group']),
|
||||
]
|
||||
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
|
||||
result = self.cmd.take_action(parsed_args)
|
||||
self.server_groups_mock.get.assert_called_once_with('affinity_group')
|
||||
self.server_groups_mock.delete.assert_called_once_with(
|
||||
self.fake_server_group.id
|
||||
)
|
||||
self.assertIsNone(result)
|
||||
|
||||
def test_server_group_multiple_delete(self):
|
||||
arglist = [
|
||||
'affinity_group',
|
||||
'anti_affinity_group'
|
||||
]
|
||||
verifylist = [
|
||||
('server_group', ['affinity_group', 'anti_affinity_group']),
|
||||
]
|
||||
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
|
||||
result = self.cmd.take_action(parsed_args)
|
||||
self.server_groups_mock.get.assert_any_call('affinity_group')
|
||||
self.server_groups_mock.get.assert_any_call('anti_affinity_group')
|
||||
self.server_groups_mock.delete.assert_called_with(
|
||||
self.fake_server_group.id
|
||||
)
|
||||
self.assertEqual(2, self.server_groups_mock.get.call_count)
|
||||
self.assertEqual(2, self.server_groups_mock.delete.call_count)
|
||||
self.assertIsNone(result)
|
||||
|
||||
def test_server_group_delete_no_input(self):
|
||||
arglist = []
|
||||
verifylist = None
|
||||
self.assertRaises(tests_utils.ParserException,
|
||||
self.check_parser,
|
||||
self.cmd,
|
||||
arglist,
|
||||
verifylist)
|
||||
|
||||
def test_server_group_multiple_delete_with_exception(self):
|
||||
arglist = [
|
||||
'affinity_group',
|
||||
'anti_affinity_group'
|
||||
]
|
||||
verifylist = [
|
||||
('server_group', ['affinity_group', 'anti_affinity_group']),
|
||||
]
|
||||
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
|
||||
find_mock_result = [self.fake_server_group, exceptions.CommandError]
|
||||
with mock.patch.object(utils, 'find_resource',
|
||||
side_effect=find_mock_result) as find_mock:
|
||||
try:
|
||||
self.cmd.take_action(parsed_args)
|
||||
self.fail('CommandError should be raised.')
|
||||
except exceptions.CommandError as e:
|
||||
self.assertEqual('1 of 2 server groups failed to delete.',
|
||||
str(e))
|
||||
|
||||
find_mock.assert_any_call(self.server_groups_mock,
|
||||
'affinity_group')
|
||||
find_mock.assert_any_call(self.server_groups_mock,
|
||||
'anti_affinity_group')
|
||||
|
||||
self.assertEqual(2, find_mock.call_count)
|
||||
self.server_groups_mock.delete.assert_called_once_with(
|
||||
self.fake_server_group.id
|
||||
)
|
||||
|
@ -132,6 +132,7 @@ openstack.compute.v2 =
|
||||
server_unshelve = openstackclient.compute.v2.server:UnshelveServer
|
||||
|
||||
server_group_create = openstackclient.compute.v2.server_group:CreateServerGroup
|
||||
server_group_delete = openstackclient.compute.v2.server_group:DeleteServerGroup
|
||||
|
||||
usage_list = openstackclient.compute.v2.usage:ListUsage
|
||||
usage_show = openstackclient.compute.v2.usage:ShowUsage
|
||||
|
Loading…
x
Reference in New Issue
Block a user