Skip to content

Commit c888cf2

Browse files
committed
network: Make better use of argparse
Change-Id: I7421a0ab957412a8283eee6ae9783dac9d3f6a4a Signed-off-by: Stephen Finucane <stephenfin@redhat.com>
1 parent 5ef5cc9 commit c888cf2

File tree

2 files changed

+20
-21
lines changed

2 files changed

+20
-21
lines changed

openstackclient/network/v2/port.py

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -558,16 +558,17 @@ def get_parser(self, prog_name):
558558
'--security-group',
559559
metavar='<security-group>',
560560
action='append',
561-
dest='security_group',
561+
dest='security_groups',
562562
help=_(
563563
"Security group to associate with this port (name or ID) "
564564
"(repeat option to set multiple security groups)"
565565
),
566566
)
567567
secgroups.add_argument(
568568
'--no-security-group',
569-
dest='no_security_group',
570-
action='store_true',
569+
action='store_const',
570+
const=[],
571+
dest='security_groups',
571572
help=_("Associate no security groups with this port"),
572573
)
573574
parser.add_argument(
@@ -633,13 +634,11 @@ def take_action(self, parsed_args):
633634
elif parsed_args.no_fixed_ip:
634635
attrs['fixed_ips'] = []
635636

636-
if parsed_args.security_group:
637+
if parsed_args.security_groups is not None:
637638
attrs['security_group_ids'] = [
638639
client.find_security_group(sg, ignore_missing=False).id
639-
for sg in parsed_args.security_group
640+
for sg in parsed_args.security_groups
640641
]
641-
elif parsed_args.no_security_group:
642-
attrs['security_group_ids'] = []
643642

644643
if parsed_args.allowed_address_pairs:
645644
attrs['allowed_address_pairs'] = _convert_address_pairs(
@@ -1006,7 +1005,7 @@ def get_parser(self, prog_name):
10061005
'--security-group',
10071006
metavar='<security-group>',
10081007
action='append',
1009-
dest='security_group',
1008+
dest='security_groups',
10101009
help=_(
10111010
"Security group to associate with this port (name or ID) "
10121011
"(repeat option to set multiple security groups)"
@@ -1107,7 +1106,7 @@ def take_action(self, parsed_args):
11071106

11081107
if parsed_args.no_security_group:
11091108
attrs['security_group_ids'] = []
1110-
if parsed_args.security_group:
1109+
if parsed_args.security_groups:
11111110
if 'security_group_ids' not in attrs:
11121111
# NOTE(dtroyer): Get existing security groups, iterate the
11131112
# list to force a new list object to be
@@ -1118,7 +1117,7 @@ def take_action(self, parsed_args):
11181117
]
11191118
attrs['security_group_ids'].extend(
11201119
client.find_security_group(sg, ignore_missing=False).id
1121-
for sg in parsed_args.security_group
1120+
for sg in parsed_args.security_groups
11221121
)
11231122

11241123
if parsed_args.no_allowed_address_pair:
@@ -1231,7 +1230,7 @@ def get_parser(self, prog_name):
12311230
'--security-group',
12321231
metavar='<security-group>',
12331232
action='append',
1234-
dest='security_group_ids',
1233+
dest='security_groups',
12351234
help=_(
12361235
"Security group which should be removed this port (name "
12371236
"or ID) (repeat option to unset multiple security groups)"
@@ -1316,9 +1315,9 @@ def take_action(self, parsed_args):
13161315
msg = _("Port does not contain binding-profile %s") % key
13171316
raise exceptions.CommandError(msg)
13181317
attrs['binding:profile'] = tmp_binding_profile
1319-
if parsed_args.security_group_ids:
1318+
if parsed_args.security_groups:
13201319
try:
1321-
for sg in parsed_args.security_group_ids:
1320+
for sg in parsed_args.security_groups:
13221321
sg_id = client.find_security_group(
13231322
sg, ignore_missing=False
13241323
).id

openstackclient/tests/unit/network/v2/test_port.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ def test_create_with_security_group(self):
332332
self._port.network_id,
333333
),
334334
('enable', True),
335-
('security_group', [secgroup.id]),
335+
('security_groups', [secgroup.id]),
336336
('name', 'test-port'),
337337
]
338338
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
@@ -405,7 +405,7 @@ def test_create_with_security_groups(self):
405405
self._port.network_id,
406406
),
407407
('enable', True),
408-
('security_group', [sg_1.id, sg_2.id]),
408+
('security_groups', [sg_1.id, sg_2.id]),
409409
('name', 'test-port'),
410410
]
411411
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
@@ -434,7 +434,7 @@ def test_create_with_no_security_groups(self):
434434
verifylist = [
435435
('network', self._port.network_id),
436436
('enable', True),
437-
('no_security_group', True),
437+
('security_groups', []),
438438
('name', 'test-port'),
439439
]
440440
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
@@ -2067,7 +2067,7 @@ def test_set_port_security_group(self):
20672067
self._port.name,
20682068
]
20692069
verifylist = [
2070-
('security_group', [sg.id]),
2070+
('security_groups', [sg.id]),
20712071
('port', self._port.name),
20722072
]
20732073
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
@@ -2100,7 +2100,7 @@ def test_set_port_security_group_append(self):
21002100
_testport.name,
21012101
]
21022102
verifylist = [
2103-
('security_group', [sg_2.id, sg_3.id]),
2103+
('security_groups', [sg_2.id, sg_3.id]),
21042104
('port', _testport.name),
21052105
]
21062106
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
@@ -2149,7 +2149,7 @@ def test_set_port_security_group_replace(self):
21492149
_testport.name,
21502150
]
21512151
verifylist = [
2152-
('security_group', [sg2.id]),
2152+
('security_groups', [sg2.id]),
21532153
('no_security_group', True),
21542154
]
21552155
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
@@ -2770,7 +2770,7 @@ def test_unset_security_group(self):
27702770
_fake_port.name,
27712771
]
27722772
verifylist = [
2773-
('security_group_ids', [_fake_sg2.id]),
2773+
('security_groups', [_fake_sg2.id]),
27742774
]
27752775

27762776
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
@@ -2797,7 +2797,7 @@ def test_unset_port_security_group_not_existent(self):
27972797
_fake_port.name,
27982798
]
27992799
verifylist = [
2800-
('security_group_ids', [_fake_sg2.id]),
2800+
('security_groups', [_fake_sg2.id]),
28012801
]
28022802

28032803
parsed_args = self.check_parser(self.cmd, arglist, verifylist)

0 commit comments

Comments
 (0)