Skip to content

Commit d270111

Browse files
Jenkinsopenstack-gerrit
authored andcommitted
Merge "Add "consistency group delete" command"
2 parents 4debdc6 + 094e518 commit d270111

6 files changed

Lines changed: 194 additions & 0 deletions

File tree

doc/source/command-objects/consistency-group.rst

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,27 @@ Create new consistency group.
4040

4141
Name of new consistency group (default to None)
4242

43+
consistency group delete
44+
------------------------
45+
46+
Delete consistency group(s).
47+
48+
.. program:: consistency group delete
49+
.. code:: bash
50+
51+
os consistency group delete
52+
[--force]
53+
<consistency-group> [<consistency-group> ...]
54+
55+
.. option:: --force
56+
57+
Allow delete in state other than error or available
58+
59+
.. _consistency_group_delete-consistency-group:
60+
.. describe:: <consistency-group>
61+
62+
Consistency group(s) to delete (name or ID)
63+
4364
consistency group list
4465
----------------------
4566

openstackclient/tests/unit/volume/v2/fakes.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,30 @@ def create_consistency_groups(attrs=None, count=2):
546546

547547
return consistency_groups
548548

549+
@staticmethod
550+
def get_consistency_groups(consistency_groups=None, count=2):
551+
"""Note:
552+
553+
Get an iterable MagicMock object with a list of faked
554+
consistency_groups.
555+
556+
If consistency_groups list is provided, then initialize
557+
the Mock object with the list. Otherwise create one.
558+
559+
:param List consistency_groups:
560+
A list of FakeResource objects faking consistency_groups
561+
:param Integer count:
562+
The number of consistency_groups to be faked
563+
:return
564+
An iterable Mock object with side_effect set to a list of faked
565+
consistency_groups
566+
"""
567+
if consistency_groups is None:
568+
consistency_groups = (FakeConsistencyGroup.
569+
create_consistency_groups(count))
570+
571+
return mock.Mock(side_effect=consistency_groups)
572+
549573

550574
class FakeConsistencyGroupSnapshot(object):
551575
"""Fake one or more consistency group snapshot."""

openstackclient/tests/unit/volume/v2/test_consistency_group.py

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212
# under the License.
1313
#
1414

15+
import mock
16+
from mock import call
17+
18+
from osc_lib import exceptions
1519
from osc_lib import utils
1620

1721
from openstackclient.tests.unit.volume.v2 import fakes as volume_fakes
@@ -161,6 +165,103 @@ def test_consistency_group_create_from_source(self):
161165
self.assertEqual(self.data, data)
162166

163167

168+
class TestConsistencyGroupDelete(TestConsistencyGroup):
169+
170+
consistency_groups =\
171+
volume_fakes.FakeConsistencyGroup.create_consistency_groups(count=2)
172+
173+
def setUp(self):
174+
super(TestConsistencyGroupDelete, self).setUp()
175+
176+
self.consistencygroups_mock.get = volume_fakes.FakeConsistencyGroup.\
177+
get_consistency_groups(self.consistency_groups)
178+
self.consistencygroups_mock.delete.return_value = None
179+
180+
# Get the command object to mock
181+
self.cmd = consistency_group.DeleteConsistencyGroup(self.app, None)
182+
183+
def test_consistency_group_delete(self):
184+
arglist = [
185+
self.consistency_groups[0].id
186+
]
187+
verifylist = [
188+
("consistency_groups", [self.consistency_groups[0].id])
189+
]
190+
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
191+
192+
result = self.cmd.take_action(parsed_args)
193+
194+
self.consistencygroups_mock.delete.assert_called_with(
195+
self.consistency_groups[0].id, False)
196+
self.assertIsNone(result)
197+
198+
def test_consistency_group_delete_with_force(self):
199+
arglist = [
200+
'--force',
201+
self.consistency_groups[0].id,
202+
]
203+
verifylist = [
204+
('force', True),
205+
("consistency_groups", [self.consistency_groups[0].id])
206+
]
207+
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
208+
209+
result = self.cmd.take_action(parsed_args)
210+
211+
self.consistencygroups_mock.delete.assert_called_with(
212+
self.consistency_groups[0].id, True)
213+
self.assertIsNone(result)
214+
215+
def test_delete_multiple_consistency_groups(self):
216+
arglist = []
217+
for b in self.consistency_groups:
218+
arglist.append(b.id)
219+
verifylist = [
220+
('consistency_groups', arglist),
221+
]
222+
223+
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
224+
result = self.cmd.take_action(parsed_args)
225+
226+
calls = []
227+
for b in self.consistency_groups:
228+
calls.append(call(b.id, False))
229+
self.consistencygroups_mock.delete.assert_has_calls(calls)
230+
self.assertIsNone(result)
231+
232+
def test_delete_multiple_consistency_groups_with_exception(self):
233+
arglist = [
234+
self.consistency_groups[0].id,
235+
'unexist_consistency_group',
236+
]
237+
verifylist = [
238+
('consistency_groups', arglist),
239+
]
240+
241+
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
242+
243+
find_mock_result = [self.consistency_groups[0],
244+
exceptions.CommandError]
245+
with mock.patch.object(utils, 'find_resource',
246+
side_effect=find_mock_result) as find_mock:
247+
try:
248+
self.cmd.take_action(parsed_args)
249+
self.fail('CommandError should be raised.')
250+
except exceptions.CommandError as e:
251+
self.assertEqual('1 of 2 consistency groups failed to delete.',
252+
str(e))
253+
254+
find_mock.assert_any_call(self.consistencygroups_mock,
255+
self.consistency_groups[0].id)
256+
find_mock.assert_any_call(self.consistencygroups_mock,
257+
'unexist_consistency_group')
258+
259+
self.assertEqual(2, find_mock.call_count)
260+
self.consistencygroups_mock.delete.assert_called_once_with(
261+
self.consistency_groups[0].id, False
262+
)
263+
264+
164265
class TestConsistencyGroupList(TestConsistencyGroup):
165266

166267
consistency_groups = (

openstackclient/volume/v2/consistency_group.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,56 @@
1717
import logging
1818

1919
from osc_lib.command import command
20+
from osc_lib import exceptions
2021
from osc_lib import utils
2122
import six
2223

2324
from openstackclient.i18n import _
2425

26+
LOG = logging.getLogger(__name__)
27+
28+
29+
class DeleteConsistencyGroup(command.Command):
30+
_description = _("Delete consistency group(s).")
31+
32+
def get_parser(self, prog_name):
33+
parser = super(DeleteConsistencyGroup, self).get_parser(prog_name)
34+
parser.add_argument(
35+
'consistency_groups',
36+
metavar='<consistency-group>',
37+
nargs="+",
38+
help=_('Consistency group(s) to delete (name or ID)'),
39+
)
40+
parser.add_argument(
41+
'--force',
42+
action='store_true',
43+
default=False,
44+
help=_("Allow delete in state other than error or available"),
45+
)
46+
return parser
47+
48+
def take_action(self, parsed_args):
49+
volume_client = self.app.client_manager.volume
50+
result = 0
51+
52+
for i in parsed_args.consistency_groups:
53+
try:
54+
consistency_group_id = utils.find_resource(
55+
volume_client.consistencygroups, i).id
56+
volume_client.consistencygroups.delete(
57+
consistency_group_id, parsed_args.force)
58+
except Exception as e:
59+
result += 1
60+
LOG.error(_("Failed to delete consistency group with "
61+
"name or ID '%(consistency_group)s':%(e)s")
62+
% {'consistency_group': i, 'e': e})
63+
64+
if result > 0:
65+
total = len(parsed_args.consistency_groups)
66+
msg = (_("%(result)s of %(total)s consistency groups failed "
67+
"to delete.") % {'result': result, 'total': total})
68+
raise exceptions.CommandError(msg)
69+
2570

2671
LOG = logging.getLogger(__name__)
2772

releasenotes/notes/bug-1613964-837196399be16b3d.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@
22
features:
33
- Add ``consistency group create`` command in volume v2.
44
[Bug `1613964 <https://bugs.launchpad.net/python-openstackclient/+bug/1613964>`_]
5+
- Add ``consistency group delete`` command in volume v2.
6+
[Bug `1613964 <https://bugs.launchpad.net/python-openstackclient/+bug/1613964>`_]

setup.cfg

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,7 @@ openstack.volume.v2 =
509509
backup_show = openstackclient.volume.v2.backup:ShowBackup
510510

511511
consistency_group_create = openstackclient.volume.v2.consistency_group:CreateConsistencyGroup
512+
consistency_group_delete = openstackclient.volume.v2.consistency_group:DeleteConsistencyGroup
512513
consistency_group_list = openstackclient.volume.v2.consistency_group:ListConsistencyGroup
513514

514515
consistency_group_snapshot_create = openstackclient.volume.v2.consistency_group_snapshot:CreateConsistencyGroupSnapshot

0 commit comments

Comments
 (0)