Skip to content

Commit 6dcef7c

Browse files
committed
volume: Make better use of argparse
Change-Id: Ifeab60aaf18a9163465d4968c53e3ed66dad769b Signed-off-by: Stephen Finucane <stephenfin@redhat.com>
1 parent 83f5c80 commit 6dcef7c

2 files changed

Lines changed: 31 additions & 28 deletions

File tree

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

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,7 @@ def test_type_create_public(self):
8080
]
8181
verifylist = [
8282
("description", self.new_volume_type.description),
83-
("public", True),
84-
("private", False),
83+
("is_public", True),
8584
("name", self.new_volume_type.name),
8685
]
8786
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
@@ -107,8 +106,7 @@ def test_type_create_private(self):
107106
]
108107
verifylist = [
109108
("description", self.new_volume_type.description),
110-
("public", False),
111-
("private", True),
109+
("is_public", False),
112110
("project", self.project.id),
113111
("name", self.new_volume_type.name),
114112
]
@@ -325,8 +323,7 @@ def test_type_list_without_options(self):
325323
arglist = []
326324
verifylist = [
327325
("long", False),
328-
("private", False),
329-
("public", False),
326+
("is_public", None),
330327
("default", False),
331328
]
332329
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
@@ -343,8 +340,7 @@ def test_type_list_with_options(self):
343340
]
344341
verifylist = [
345342
("long", True),
346-
("private", False),
347-
("public", True),
343+
("is_public", True),
348344
("default", False),
349345
]
350346
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
@@ -360,8 +356,7 @@ def test_type_list_with_private_option(self):
360356
]
361357
verifylist = [
362358
("long", False),
363-
("private", True),
364-
("public", False),
359+
("is_public", False),
365360
("default", False),
366361
]
367362
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
@@ -378,8 +373,7 @@ def test_type_list_with_default_option(self):
378373
verifylist = [
379374
("encryption_type", False),
380375
("long", False),
381-
("private", False),
382-
("public", False),
376+
("is_public", None),
383377
("default", True),
384378
]
385379
parsed_args = self.check_parser(self.cmd, arglist, verifylist)

openstackclient/volume/v2/volume_type.py

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -124,13 +124,15 @@ def get_parser(self, prog_name):
124124
public_group.add_argument(
125125
"--public",
126126
action="store_true",
127-
default=False,
127+
dest="is_public",
128+
default=None,
128129
help=_("Volume type is accessible to the public"),
129130
)
130131
public_group.add_argument(
131132
"--private",
132-
action="store_true",
133-
default=False,
133+
action="store_false",
134+
dest="is_public",
135+
default=None,
134136
help=_("Volume type is not accessible to the public"),
135137
)
136138
parser.add_argument(
@@ -201,15 +203,13 @@ def take_action(self, parsed_args):
201203
identity_client = self.app.client_manager.identity
202204
volume_client = self.app.client_manager.volume
203205

204-
if parsed_args.project and not parsed_args.private:
206+
if parsed_args.project and parsed_args.is_public is not False:
205207
msg = _("--project is only allowed with --private")
206208
raise exceptions.CommandError(msg)
207209

208210
kwargs = {}
209-
if parsed_args.public:
210-
kwargs['is_public'] = True
211-
if parsed_args.private:
212-
kwargs['is_public'] = False
211+
if parsed_args.is_public is not None:
212+
kwargs['is_public'] = parsed_args.is_public
213213

214214
volume_type = volume_client.volume_types.create(
215215
parsed_args.name, description=parsed_args.description, **kwargs
@@ -329,11 +329,15 @@ def get_parser(self, prog_name):
329329
public_group.add_argument(
330330
"--public",
331331
action="store_true",
332+
dest="is_public",
333+
default=None,
332334
help=_("List only public types"),
333335
)
334336
public_group.add_argument(
335337
"--private",
336-
action="store_true",
338+
action="store_false",
339+
dest="is_public",
340+
default=None,
337341
help=_("List only private types (admin only)"),
338342
)
339343
parser.add_argument(
@@ -348,8 +352,15 @@ def get_parser(self, prog_name):
348352

349353
def take_action(self, parsed_args):
350354
volume_client = self.app.client_manager.volume
355+
351356
if parsed_args.long:
352-
columns = ['ID', 'Name', 'Is Public', 'Description', 'Extra Specs']
357+
columns = [
358+
'ID',
359+
'Name',
360+
'Is Public',
361+
'Description',
362+
'Extra Specs',
363+
]
353364
column_headers = [
354365
'ID',
355366
'Name',
@@ -360,15 +371,13 @@ def take_action(self, parsed_args):
360371
else:
361372
columns = ['ID', 'Name', 'Is Public']
362373
column_headers = ['ID', 'Name', 'Is Public']
374+
363375
if parsed_args.default:
364376
data = [volume_client.volume_types.default()]
365377
else:
366-
is_public = None
367-
if parsed_args.public:
368-
is_public = True
369-
if parsed_args.private:
370-
is_public = False
371-
data = volume_client.volume_types.list(is_public=is_public)
378+
data = volume_client.volume_types.list(
379+
is_public=parsed_args.is_public
380+
)
372381

373382
formatters = {'Extra Specs': format_columns.DictColumn}
374383

0 commit comments

Comments
 (0)