From c888cf25560fddf236073ede42914da851833396 Mon Sep 17 00:00:00 2001 From: Stephen Finucane Date: Thu, 10 Aug 2023 19:32:07 +0100 Subject: [PATCH] network: Make better use of argparse Change-Id: I7421a0ab957412a8283eee6ae9783dac9d3f6a4a Signed-off-by: Stephen Finucane --- openstackclient/network/v2/port.py | 25 +++++++++---------- .../tests/unit/network/v2/test_port.py | 16 ++++++------ 2 files changed, 20 insertions(+), 21 deletions(-) diff --git a/openstackclient/network/v2/port.py b/openstackclient/network/v2/port.py index 9b2fc34b16..a707882385 100644 --- a/openstackclient/network/v2/port.py +++ b/openstackclient/network/v2/port.py @@ -558,7 +558,7 @@ class CreatePort(command.ShowOne, common.NeutronCommandWithExtraArgs): '--security-group', metavar='', action='append', - dest='security_group', + dest='security_groups', help=_( "Security group to associate with this port (name or ID) " "(repeat option to set multiple security groups)" @@ -566,8 +566,9 @@ class CreatePort(command.ShowOne, common.NeutronCommandWithExtraArgs): ) secgroups.add_argument( '--no-security-group', - dest='no_security_group', - action='store_true', + action='store_const', + const=[], + dest='security_groups', help=_("Associate no security groups with this port"), ) parser.add_argument( @@ -633,13 +634,11 @@ class CreatePort(command.ShowOne, common.NeutronCommandWithExtraArgs): elif parsed_args.no_fixed_ip: attrs['fixed_ips'] = [] - if parsed_args.security_group: + if parsed_args.security_groups is not None: attrs['security_group_ids'] = [ 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: attrs['allowed_address_pairs'] = _convert_address_pairs( @@ -1006,7 +1005,7 @@ class SetPort(common.NeutronCommandWithExtraArgs): '--security-group', metavar='', action='append', - dest='security_group', + dest='security_groups', help=_( "Security group to associate with this port (name or ID) " "(repeat option to set multiple security groups)" @@ -1107,7 +1106,7 @@ class SetPort(common.NeutronCommandWithExtraArgs): if parsed_args.no_security_group: attrs['security_group_ids'] = [] - if parsed_args.security_group: + if parsed_args.security_groups: if 'security_group_ids' not in attrs: # NOTE(dtroyer): Get existing security groups, iterate the # list to force a new list object to be @@ -1118,7 +1117,7 @@ class SetPort(common.NeutronCommandWithExtraArgs): ] attrs['security_group_ids'].extend( 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: @@ -1231,7 +1230,7 @@ class UnsetPort(common.NeutronUnsetCommandWithExtraArgs): '--security-group', metavar='', action='append', - dest='security_group_ids', + dest='security_groups', help=_( "Security group which should be removed this port (name " "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 raise exceptions.CommandError(msg) attrs['binding:profile'] = tmp_binding_profile - if parsed_args.security_group_ids: + if parsed_args.security_groups: try: - for sg in parsed_args.security_group_ids: + for sg in parsed_args.security_groups: sg_id = client.find_security_group( sg, ignore_missing=False ).id diff --git a/openstackclient/tests/unit/network/v2/test_port.py b/openstackclient/tests/unit/network/v2/test_port.py index bb9f3a20f8..dcfe64b3ba 100644 --- a/openstackclient/tests/unit/network/v2/test_port.py +++ b/openstackclient/tests/unit/network/v2/test_port.py @@ -332,7 +332,7 @@ class TestCreatePort(TestPort): self._port.network_id, ), ('enable', True), - ('security_group', [secgroup.id]), + ('security_groups', [secgroup.id]), ('name', 'test-port'), ] parsed_args = self.check_parser(self.cmd, arglist, verifylist) @@ -405,7 +405,7 @@ class TestCreatePort(TestPort): self._port.network_id, ), ('enable', True), - ('security_group', [sg_1.id, sg_2.id]), + ('security_groups', [sg_1.id, sg_2.id]), ('name', 'test-port'), ] parsed_args = self.check_parser(self.cmd, arglist, verifylist) @@ -434,7 +434,7 @@ class TestCreatePort(TestPort): verifylist = [ ('network', self._port.network_id), ('enable', True), - ('no_security_group', True), + ('security_groups', []), ('name', 'test-port'), ] parsed_args = self.check_parser(self.cmd, arglist, verifylist) @@ -2067,7 +2067,7 @@ class TestSetPort(TestPort): self._port.name, ] verifylist = [ - ('security_group', [sg.id]), + ('security_groups', [sg.id]), ('port', self._port.name), ] parsed_args = self.check_parser(self.cmd, arglist, verifylist) @@ -2100,7 +2100,7 @@ class TestSetPort(TestPort): _testport.name, ] verifylist = [ - ('security_group', [sg_2.id, sg_3.id]), + ('security_groups', [sg_2.id, sg_3.id]), ('port', _testport.name), ] parsed_args = self.check_parser(self.cmd, arglist, verifylist) @@ -2149,7 +2149,7 @@ class TestSetPort(TestPort): _testport.name, ] verifylist = [ - ('security_group', [sg2.id]), + ('security_groups', [sg2.id]), ('no_security_group', True), ] parsed_args = self.check_parser(self.cmd, arglist, verifylist) @@ -2770,7 +2770,7 @@ class TestUnsetPort(TestPort): _fake_port.name, ] verifylist = [ - ('security_group_ids', [_fake_sg2.id]), + ('security_groups', [_fake_sg2.id]), ] parsed_args = self.check_parser(self.cmd, arglist, verifylist) @@ -2797,7 +2797,7 @@ class TestUnsetPort(TestPort): _fake_port.name, ] verifylist = [ - ('security_group_ids', [_fake_sg2.id]), + ('security_groups', [_fake_sg2.id]), ] parsed_args = self.check_parser(self.cmd, arglist, verifylist)