From 4fe469c28282200cd851b68dceb3dc972fdf2ccd Mon Sep 17 00:00:00 2001 From: Aaron Rosen <arosen@nicira.com> Date: Thu, 31 Jan 2013 19:54:11 -0800 Subject: [PATCH] Allow ability to remove security groups from ports This commit adds the option --no-security-groups to port-update in order to remove security groups from a port. Fixes bug 1112089 Change-Id: I43a5cc2c8b443f2d34fffe66b442925a5ae312ac --- quantumclient/quantum/v2_0/__init__.py | 22 +++++++++++++--------- quantumclient/quantum/v2_0/port.py | 12 ++++++++++++ quantumclient/tests/unit/test_cli20_port.py | 8 ++++++++ 3 files changed, 33 insertions(+), 9 deletions(-) diff --git a/quantumclient/quantum/v2_0/__init__.py b/quantumclient/quantum/v2_0/__init__.py index 530b07a..e1b07b0 100644 --- a/quantumclient/quantum/v2_0/__init__.py +++ b/quantumclient/quantum/v2_0/__init__.py @@ -248,6 +248,12 @@ class QuantumCommand(command.OpenStackCommand): elif v is None: data[self.resource][k] = '' + def add_known_arguments(self, parser): + pass + + def args2body(self, parsed_args): + return {} + class CreateCommand(QuantumCommand, show.ShowOne): """Create a resource for a given tenant @@ -269,12 +275,6 @@ class CreateCommand(QuantumCommand, show.ShowOne): self.add_known_arguments(parser) return parser - def add_known_arguments(self, parser): - pass - - def args2body(self, parsed_args): - return {} - def get_data(self, parsed_args): self.log.debug('get_data(%s)' % parsed_args) quantum_client = self.get_client() @@ -312,6 +312,7 @@ class UpdateCommand(QuantumCommand): help='ID or name of %s to update' % self.resource) add_extra_argument(parser, 'value_specs', 'new values for the %s' % self.resource) + self.add_known_arguments(parser) return parser def run(self, parsed_args): @@ -319,16 +320,19 @@ class UpdateCommand(QuantumCommand): quantum_client = self.get_client() quantum_client.format = parsed_args.request_format value_specs = parsed_args.value_specs - if not value_specs: + dict_args = self.args2body(parsed_args).get(self.resource, {}) + dict_specs = parse_args_to_dict(value_specs) + body = {self.resource: dict(dict_args.items() + + dict_specs.items())} + if not body[self.resource]: raise exceptions.CommandError( "Must specify new values to update %s" % self.resource) - data = {self.resource: parse_args_to_dict(value_specs)} _id = find_resourceid_by_name_or_id(quantum_client, self.resource, parsed_args.id) obj_updator = getattr(quantum_client, "update_%s" % self.resource) - obj_updator(_id, data) + obj_updator(_id, body) print >>self.app.stdout, ( _('Updated %(resource)s: %(id)s') % {'id': parsed_args.id, 'resource': self.resource}) diff --git a/quantumclient/quantum/v2_0/port.py b/quantumclient/quantum/v2_0/port.py index 98661b5..4487388 100644 --- a/quantumclient/quantum/v2_0/port.py +++ b/quantumclient/quantum/v2_0/port.py @@ -161,3 +161,15 @@ class UpdatePort(UpdateCommand): resource = 'port' log = logging.getLogger(__name__ + '.UpdatePort') + + def add_known_arguments(self, parser): + parser.add_argument( + '--no-security-groups', + default=False, action='store_true', + help='remove security groups from port') + + def args2body(self, parsed_args): + body = {'port': {}} + if parsed_args.no_security_groups: + body['port'].update({'security_groups': None}) + return body diff --git a/quantumclient/tests/unit/test_cli20_port.py b/quantumclient/tests/unit/test_cli20_port.py index bd776d4..7324d23 100644 --- a/quantumclient/tests/unit/test_cli20_port.py +++ b/quantumclient/tests/unit/test_cli20_port.py @@ -230,6 +230,14 @@ class CLITestV20Port(CLITestV20Base): {'name': 'myname', 'tags': ['a', 'b'], } ) + def test_update_port_security_group_off(self): + """Update port: --no-security-groups myid.""" + resource = 'port' + cmd = UpdatePort(MyApp(sys.stdout), None) + self._test_update_resource(resource, cmd, 'myid', + ['--no-security-groups', 'myid'], + {'security_groups': None}) + def test_show_port(self): """Show port: --fields id --fields name myid.""" resource = 'port'