Skip to content

Commit 98b9bc1

Browse files
author
Huanxuan Ao
committed
Add "consistency group create" command
Add "consistency group create" command in volume v2 (v2 only). Change-Id: I2e9affe390b1012aa18459e64d04afcdfc15e27d Implements: bp cinder-command-support Partial-Bug: #1613964
1 parent 0ef8535 commit 98b9bc1

5 files changed

Lines changed: 250 additions & 0 deletions

File tree

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,42 @@ consistency group
44

55
Block Storage v2
66

7+
consistency group create
8+
------------------------
9+
10+
Create new consistency group.
11+
12+
.. program:: consistency group create
13+
.. code:: bash
14+
15+
os consistency group create
16+
--volume-type <volume-type> | --consistency-group-source <consistency-group>
17+
[--description <description>]
18+
[--availability-zone <availability-zone>]
19+
[<name>]
20+
21+
.. option:: --volume-type <volume-type>
22+
23+
Volume type of this consistency group (name or ID)
24+
25+
.. option:: --consistency-group-source <consistency-group>
26+
27+
Existing consistency group (name or ID)
28+
29+
.. option:: --description <description>
30+
31+
Description of this consistency group
32+
33+
.. option:: --availability-zone <availability-zone>
34+
35+
Availability zone for this consistency group
36+
(not available if creating consistency group from source)
37+
38+
.. _consistency_group_create-name:
39+
.. option:: <name>
40+
41+
Name of new consistency group (default to None)
42+
743
consistency group list
844
----------------------
945

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

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,138 @@ def setUp(self):
2828
self.app.client_manager.volume.consistencygroups)
2929
self.consistencygroups_mock.reset_mock()
3030

31+
self.types_mock = self.app.client_manager.volume.volume_types
32+
self.types_mock.reset_mock()
33+
34+
35+
class TestConsistencyGroupCreate(TestConsistencyGroup):
36+
37+
volume_type = volume_fakes.FakeType.create_one_type()
38+
new_consistency_group = (
39+
volume_fakes.FakeConsistencyGroup.create_one_consistency_group())
40+
41+
columns = (
42+
'availability_zone',
43+
'created_at',
44+
'description',
45+
'id',
46+
'name',
47+
'status',
48+
'volume_types',
49+
)
50+
data = (
51+
new_consistency_group.availability_zone,
52+
new_consistency_group.created_at,
53+
new_consistency_group.description,
54+
new_consistency_group.id,
55+
new_consistency_group.name,
56+
new_consistency_group.status,
57+
new_consistency_group.volume_types,
58+
)
59+
60+
def setUp(self):
61+
super(TestConsistencyGroupCreate, self).setUp()
62+
self.consistencygroups_mock.create.return_value = (
63+
self.new_consistency_group)
64+
self.consistencygroups_mock.create_from_src.return_value = (
65+
self.new_consistency_group)
66+
self.consistencygroups_mock.get.return_value = (
67+
self.new_consistency_group)
68+
self.types_mock.get.return_value = self.volume_type
69+
70+
# Get the command object to test
71+
self.cmd = consistency_group.CreateConsistencyGroup(self.app, None)
72+
73+
def test_consistency_group_create(self):
74+
arglist = [
75+
'--volume-type', self.volume_type.id,
76+
'--description', self.new_consistency_group.description,
77+
'--availability-zone',
78+
self.new_consistency_group.availability_zone,
79+
self.new_consistency_group.name,
80+
]
81+
verifylist = [
82+
('volume_type', self.volume_type.id),
83+
('description', self.new_consistency_group.description),
84+
('availability_zone',
85+
self.new_consistency_group.availability_zone),
86+
('name', self.new_consistency_group.name),
87+
]
88+
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
89+
90+
columns, data = self.cmd.take_action(parsed_args)
91+
92+
self.types_mock.get.assert_called_once_with(
93+
self.volume_type.id)
94+
self.consistencygroups_mock.get.assert_not_called()
95+
self.consistencygroups_mock.create.assert_called_once_with(
96+
self.volume_type.id,
97+
name=self.new_consistency_group.name,
98+
description=self.new_consistency_group.description,
99+
availability_zone=self.new_consistency_group.availability_zone,
100+
)
101+
102+
self.assertEqual(self.columns, columns)
103+
self.assertEqual(self.data, data)
104+
105+
def test_consistency_group_create_without_name(self):
106+
arglist = [
107+
'--volume-type', self.volume_type.id,
108+
'--description', self.new_consistency_group.description,
109+
'--availability-zone',
110+
self.new_consistency_group.availability_zone,
111+
]
112+
verifylist = [
113+
('volume_type', self.volume_type.id),
114+
('description', self.new_consistency_group.description),
115+
('availability_zone',
116+
self.new_consistency_group.availability_zone),
117+
]
118+
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
119+
120+
columns, data = self.cmd.take_action(parsed_args)
121+
122+
self.types_mock.get.assert_called_once_with(
123+
self.volume_type.id)
124+
self.consistencygroups_mock.get.assert_not_called()
125+
self.consistencygroups_mock.create.assert_called_once_with(
126+
self.volume_type.id,
127+
name=None,
128+
description=self.new_consistency_group.description,
129+
availability_zone=self.new_consistency_group.availability_zone,
130+
)
131+
132+
self.assertEqual(self.columns, columns)
133+
self.assertEqual(self.data, data)
134+
135+
def test_consistency_group_create_from_source(self):
136+
arglist = [
137+
'--consistency-group-source', self.new_consistency_group.id,
138+
'--description', self.new_consistency_group.description,
139+
self.new_consistency_group.name,
140+
]
141+
verifylist = [
142+
('consistency_group_source', self.new_consistency_group.id),
143+
('description', self.new_consistency_group.description),
144+
('name', self.new_consistency_group.name),
145+
]
146+
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
147+
148+
columns, data = self.cmd.take_action(parsed_args)
149+
150+
self.types_mock.get.assert_not_called()
151+
self.consistencygroups_mock.get.assert_called_once_with(
152+
self.new_consistency_group.id)
153+
self.consistencygroups_mock.create_from_src.assert_called_with(
154+
None,
155+
self.new_consistency_group.id,
156+
name=self.new_consistency_group.name,
157+
description=self.new_consistency_group.description,
158+
)
159+
160+
self.assertEqual(self.columns, columns)
161+
self.assertEqual(self.data, data)
162+
31163

32164
class TestConsistencyGroupList(TestConsistencyGroup):
33165

openstackclient/volume/v2/consistency_group.py

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,89 @@
1414

1515
"""Volume v2 consistency group action implementations"""
1616

17+
import logging
18+
1719
from osc_lib.command import command
1820
from osc_lib import utils
21+
import six
1922

2023
from openstackclient.i18n import _
2124

2225

26+
LOG = logging.getLogger(__name__)
27+
28+
29+
class CreateConsistencyGroup(command.ShowOne):
30+
_description = _("Create new consistency group.")
31+
32+
def get_parser(self, prog_name):
33+
parser = super(CreateConsistencyGroup, self).get_parser(prog_name)
34+
parser.add_argument(
35+
"name",
36+
metavar="<name>",
37+
nargs="?",
38+
help=_("Name of new consistency group (default to None)")
39+
)
40+
exclusive_group = parser.add_mutually_exclusive_group(required=True)
41+
exclusive_group.add_argument(
42+
"--volume-type",
43+
metavar="<volume-type>",
44+
help=_("Volume type of this consistency group (name or ID)")
45+
)
46+
exclusive_group.add_argument(
47+
"--consistency-group-source",
48+
metavar="<consistency-group>",
49+
help=_("Existing consistency group (name or ID)")
50+
)
51+
parser.add_argument(
52+
"--description",
53+
metavar="<description>",
54+
help=_("Description of this consistency group")
55+
)
56+
parser.add_argument(
57+
"--availability-zone",
58+
metavar="<availability-zone>",
59+
help=_("Availability zone for this consistency group "
60+
"(not available if creating consistency group "
61+
"from source)"),
62+
)
63+
return parser
64+
65+
def take_action(self, parsed_args):
66+
volume_client = self.app.client_manager.volume
67+
if parsed_args.volume_type:
68+
volume_type_id = utils.find_resource(
69+
volume_client.volume_types,
70+
parsed_args.volume_type).id
71+
consistency_group = volume_client.consistencygroups.create(
72+
volume_type_id,
73+
name=parsed_args.name,
74+
description=parsed_args.description,
75+
availability_zone=parsed_args.availability_zone
76+
)
77+
elif parsed_args.consistency_group_source:
78+
if parsed_args.availability_zone:
79+
msg = _("'--availability-zone' option will not work "
80+
"if creating consistency group from source")
81+
LOG.warning(msg)
82+
consistency_group_id = utils.find_resource(
83+
volume_client.consistencygroups,
84+
parsed_args.consistency_group_source).id
85+
consistency_group_snapshot = None
86+
# TODO(Huanxuan Ao): Support for creating from consistency group
87+
# snapshot after adding "consistency_group_snapshot" resource
88+
consistency_group = (
89+
volume_client.consistencygroups.create_from_src(
90+
consistency_group_snapshot,
91+
consistency_group_id,
92+
name=parsed_args.name,
93+
description=parsed_args.description
94+
)
95+
)
96+
97+
return zip(*sorted(six.iteritems(consistency_group._info)))
98+
99+
23100
class ListConsistencyGroup(command.Lister):
24101
_description = _("List consistency groups.")
25102

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
features:
3+
- Add ``consistency group create`` command in volume v2.
4+
[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
@@ -506,6 +506,7 @@ openstack.volume.v2 =
506506
backup_restore = openstackclient.volume.v2.backup:RestoreBackup
507507
backup_show = openstackclient.volume.v2.backup:ShowBackup
508508

509+
consistency_group_create = openstackclient.volume.v2.consistency_group:CreateConsistencyGroup
509510
consistency_group_list = openstackclient.volume.v2.consistency_group:ListConsistencyGroup
510511

511512
snapshot_create = openstackclient.volume.v2.snapshot:CreateSnapshot

0 commit comments

Comments
 (0)