network: Make better use of argparse

Change-Id: I7421a0ab957412a8283eee6ae9783dac9d3f6a4a
Signed-off-by: Stephen Finucane <stephenfin@redhat.com>
This commit is contained in:
Stephen Finucane 2023-08-10 19:32:07 +01:00
parent 5ef5cc9c82
commit c888cf2556
2 changed files with 20 additions and 21 deletions

View File

@ -558,7 +558,7 @@ class CreatePort(command.ShowOne, common.NeutronCommandWithExtraArgs):
'--security-group', '--security-group',
metavar='<security-group>', metavar='<security-group>',
action='append', action='append',
dest='security_group', dest='security_groups',
help=_( help=_(
"Security group to associate with this port (name or ID) " "Security group to associate with this port (name or ID) "
"(repeat option to set multiple security groups)" "(repeat option to set multiple security groups)"
@ -566,8 +566,9 @@ class CreatePort(command.ShowOne, common.NeutronCommandWithExtraArgs):
) )
secgroups.add_argument( secgroups.add_argument(
'--no-security-group', '--no-security-group',
dest='no_security_group', action='store_const',
action='store_true', const=[],
dest='security_groups',
help=_("Associate no security groups with this port"), help=_("Associate no security groups with this port"),
) )
parser.add_argument( parser.add_argument(
@ -633,13 +634,11 @@ class CreatePort(command.ShowOne, common.NeutronCommandWithExtraArgs):
elif parsed_args.no_fixed_ip: elif parsed_args.no_fixed_ip:
attrs['fixed_ips'] = [] attrs['fixed_ips'] = []
if parsed_args.security_group: if parsed_args.security_groups is not None:
attrs['security_group_ids'] = [ attrs['security_group_ids'] = [
client.find_security_group(sg, ignore_missing=False).id client.find_security_group(sg, ignore_missing=False).id
for sg in parsed_args.security_group for sg in parsed_args.security_groups
] ]
elif parsed_args.no_security_group:
attrs['security_group_ids'] = []
if parsed_args.allowed_address_pairs: if parsed_args.allowed_address_pairs:
attrs['allowed_address_pairs'] = _convert_address_pairs( attrs['allowed_address_pairs'] = _convert_address_pairs(
@ -1006,7 +1005,7 @@ class SetPort(common.NeutronCommandWithExtraArgs):
'--security-group', '--security-group',
metavar='<security-group>', metavar='<security-group>',
action='append', action='append',
dest='security_group', dest='security_groups',
help=_( help=_(
"Security group to associate with this port (name or ID) " "Security group to associate with this port (name or ID) "
"(repeat option to set multiple security groups)" "(repeat option to set multiple security groups)"
@ -1107,7 +1106,7 @@ class SetPort(common.NeutronCommandWithExtraArgs):
if parsed_args.no_security_group: if parsed_args.no_security_group:
attrs['security_group_ids'] = [] attrs['security_group_ids'] = []
if parsed_args.security_group: if parsed_args.security_groups:
if 'security_group_ids' not in attrs: if 'security_group_ids' not in attrs:
# NOTE(dtroyer): Get existing security groups, iterate the # NOTE(dtroyer): Get existing security groups, iterate the
# list to force a new list object to be # list to force a new list object to be
@ -1118,7 +1117,7 @@ class SetPort(common.NeutronCommandWithExtraArgs):
] ]
attrs['security_group_ids'].extend( attrs['security_group_ids'].extend(
client.find_security_group(sg, ignore_missing=False).id client.find_security_group(sg, ignore_missing=False).id
for sg in parsed_args.security_group for sg in parsed_args.security_groups
) )
if parsed_args.no_allowed_address_pair: if parsed_args.no_allowed_address_pair:
@ -1231,7 +1230,7 @@ class UnsetPort(common.NeutronUnsetCommandWithExtraArgs):
'--security-group', '--security-group',
metavar='<security-group>', metavar='<security-group>',
action='append', action='append',
dest='security_group_ids', dest='security_groups',
help=_( help=_(
"Security group which should be removed this port (name " "Security group which should be removed this port (name "
"or ID) (repeat option to unset multiple security groups)" "or ID) (repeat option to unset multiple security groups)"
@ -1316,9 +1315,9 @@ class UnsetPort(common.NeutronUnsetCommandWithExtraArgs):
msg = _("Port does not contain binding-profile %s") % key msg = _("Port does not contain binding-profile %s") % key
raise exceptions.CommandError(msg) raise exceptions.CommandError(msg)
attrs['binding:profile'] = tmp_binding_profile attrs['binding:profile'] = tmp_binding_profile
if parsed_args.security_group_ids: if parsed_args.security_groups:
try: try:
for sg in parsed_args.security_group_ids: for sg in parsed_args.security_groups:
sg_id = client.find_security_group( sg_id = client.find_security_group(
sg, ignore_missing=False sg, ignore_missing=False
).id ).id

View File

@ -332,7 +332,7 @@ class TestCreatePort(TestPort):
self._port.network_id, self._port.network_id,
), ),
('enable', True), ('enable', True),
('security_group', [secgroup.id]), ('security_groups', [secgroup.id]),
('name', 'test-port'), ('name', 'test-port'),
] ]
parsed_args = self.check_parser(self.cmd, arglist, verifylist) parsed_args = self.check_parser(self.cmd, arglist, verifylist)
@ -405,7 +405,7 @@ class TestCreatePort(TestPort):
self._port.network_id, self._port.network_id,
), ),
('enable', True), ('enable', True),
('security_group', [sg_1.id, sg_2.id]), ('security_groups', [sg_1.id, sg_2.id]),
('name', 'test-port'), ('name', 'test-port'),
] ]
parsed_args = self.check_parser(self.cmd, arglist, verifylist) parsed_args = self.check_parser(self.cmd, arglist, verifylist)
@ -434,7 +434,7 @@ class TestCreatePort(TestPort):
verifylist = [ verifylist = [
('network', self._port.network_id), ('network', self._port.network_id),
('enable', True), ('enable', True),
('no_security_group', True), ('security_groups', []),
('name', 'test-port'), ('name', 'test-port'),
] ]
parsed_args = self.check_parser(self.cmd, arglist, verifylist) parsed_args = self.check_parser(self.cmd, arglist, verifylist)
@ -2067,7 +2067,7 @@ class TestSetPort(TestPort):
self._port.name, self._port.name,
] ]
verifylist = [ verifylist = [
('security_group', [sg.id]), ('security_groups', [sg.id]),
('port', self._port.name), ('port', self._port.name),
] ]
parsed_args = self.check_parser(self.cmd, arglist, verifylist) parsed_args = self.check_parser(self.cmd, arglist, verifylist)
@ -2100,7 +2100,7 @@ class TestSetPort(TestPort):
_testport.name, _testport.name,
] ]
verifylist = [ verifylist = [
('security_group', [sg_2.id, sg_3.id]), ('security_groups', [sg_2.id, sg_3.id]),
('port', _testport.name), ('port', _testport.name),
] ]
parsed_args = self.check_parser(self.cmd, arglist, verifylist) parsed_args = self.check_parser(self.cmd, arglist, verifylist)
@ -2149,7 +2149,7 @@ class TestSetPort(TestPort):
_testport.name, _testport.name,
] ]
verifylist = [ verifylist = [
('security_group', [sg2.id]), ('security_groups', [sg2.id]),
('no_security_group', True), ('no_security_group', True),
] ]
parsed_args = self.check_parser(self.cmd, arglist, verifylist) parsed_args = self.check_parser(self.cmd, arglist, verifylist)
@ -2770,7 +2770,7 @@ class TestUnsetPort(TestPort):
_fake_port.name, _fake_port.name,
] ]
verifylist = [ verifylist = [
('security_group_ids', [_fake_sg2.id]), ('security_groups', [_fake_sg2.id]),
] ]
parsed_args = self.check_parser(self.cmd, arglist, verifylist) parsed_args = self.check_parser(self.cmd, arglist, verifylist)
@ -2797,7 +2797,7 @@ class TestUnsetPort(TestPort):
_fake_port.name, _fake_port.name,
] ]
verifylist = [ verifylist = [
('security_group_ids', [_fake_sg2.id]), ('security_groups', [_fake_sg2.id]),
] ]
parsed_args = self.check_parser(self.cmd, arglist, verifylist) parsed_args = self.check_parser(self.cmd, arglist, verifylist)