From 041ea4978b94149d5037b5afc7743db939b75331 Mon Sep 17 00:00:00 2001 From: Huanxuan Ao Date: Thu, 16 Jun 2016 13:09:27 +0800 Subject: Support bulk deletion for delete commands in networkv2 This patch support bulk deletion for delete commands below: 1.subnet delete 2.subnet pool delete Up to now, all the delete commands in networkv2 support bulk deletion. Change-Id: I63f6d1d02bad1fcc26e72b7028b53958a68ce2dc Partially-Implements: blueprint multi-argument-network Partial-Bug: #1592906 --- openstackclient/network/v2/subnet.py | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) (limited to 'openstackclient/network/v2/subnet.py') diff --git a/openstackclient/network/v2/subnet.py b/openstackclient/network/v2/subnet.py index ceb1cb14..a2e32622 100644 --- a/openstackclient/network/v2/subnet.py +++ b/openstackclient/network/v2/subnet.py @@ -14,6 +14,7 @@ """Subnet action implementations""" import copy +import logging from osc_lib.cli import parseractions from osc_lib.command import command @@ -24,6 +25,9 @@ from openstackclient.i18n import _ from openstackclient.identity import common as identity_common +LOG = logging.getLogger(__name__) + + def _format_allocation_pools(data): pool_formatted = ['%s-%s' % (pool.get('start', ''), pool.get('end', '')) for pool in data] @@ -270,21 +274,37 @@ class CreateSubnet(command.ShowOne): class DeleteSubnet(command.Command): - """Delete subnet""" + """Delete subnet(s)""" def get_parser(self, prog_name): parser = super(DeleteSubnet, self).get_parser(prog_name) parser.add_argument( 'subnet', metavar="", - help=_("Subnet to delete (name or ID)") + nargs='+', + help=_("Subnet(s) to delete (name or ID)") ) return parser def take_action(self, parsed_args): client = self.app.client_manager.network - client.delete_subnet( - client.find_subnet(parsed_args.subnet)) + result = 0 + + for subnet in parsed_args.subnet: + try: + obj = client.find_subnet(subnet, ignore_missing=False) + client.delete_subnet(obj) + except Exception as e: + result += 1 + LOG.error(_("Failed to delete subnet with " + "name or ID '%(subnet)s': %(e)s") + % {'subnet': subnet, 'e': e}) + + if result > 0: + total = len(parsed_args.subnet) + msg = (_("%(result)s of %(total)s subnets failed " + "to delete.") % {'result': result, 'total': total}) + raise exceptions.CommandError(msg) class ListSubnet(command.Lister): -- cgit v1.2.1