From 4e62e1e2e18cb93ba0f88bff8727182b1002de4b Mon Sep 17 00:00:00 2001 From: Steve Martinelli Date: Thu, 16 Jun 2016 14:25:33 -0400 Subject: support multi-delete for volume-type Added the ability to delete multiple volume types at once. Note there are no unit tests exist for v1 volume-types, so instead a functional test was created. Partial-Bug: #1592906 Change-Id: I99f3f22901ab35252b91a3072b14de7d19cb17ca --- openstackclient/volume/v1/volume_type.py | 35 ++++++++++++++++++++++++++------ openstackclient/volume/v2/volume_type.py | 29 ++++++++++++++++++++------ 2 files changed, 52 insertions(+), 12 deletions(-) (limited to 'openstackclient/volume') diff --git a/openstackclient/volume/v1/volume_type.py b/openstackclient/volume/v1/volume_type.py index 289966e5..3fe4fa05 100644 --- a/openstackclient/volume/v1/volume_type.py +++ b/openstackclient/volume/v1/volume_type.py @@ -15,14 +15,20 @@ """Volume v1 Type action implementations""" +import logging + from osc_lib.cli import parseractions from osc_lib.command import command +from osc_lib import exceptions from osc_lib import utils import six from openstackclient.i18n import _ +LOG = logging.getLogger(__name__) + + class CreateVolumeType(command.ShowOne): """Create new volume type""" @@ -56,22 +62,39 @@ class CreateVolumeType(command.ShowOne): class DeleteVolumeType(command.Command): - """Delete volume type""" + """Delete volume type(s)""" def get_parser(self, prog_name): parser = super(DeleteVolumeType, self).get_parser(prog_name) parser.add_argument( - 'volume_type', + 'volume_types', metavar='', - help=_('Volume type to delete (name or ID)'), + nargs='+', + help=_('Volume type(s) to delete (name or ID)'), ) return parser def take_action(self, parsed_args): volume_client = self.app.client_manager.volume - volume_type_id = utils.find_resource( - volume_client.volume_types, parsed_args.volume_type).id - volume_client.volume_types.delete(volume_type_id) + result = 0 + + for volume_type in parsed_args.volume_types: + try: + vol_type = utils.find_resource(volume_client.volume_types, + volume_type) + + volume_client.volume_types.delete(vol_type) + except Exception as e: + result += 1 + LOG.error(_("Failed to delete volume type with " + "name or ID '%(volume_type)s': %(e)s") + % {'volume_type': volume_type, 'e': e}) + + if result > 0: + total = len(parsed_args.volume_types) + msg = (_("%(result)s of %(total)s volume types failed " + "to delete.") % {'result': result, 'total': total}) + raise exceptions.CommandError(msg) class ListVolumeType(command.Lister): diff --git a/openstackclient/volume/v2/volume_type.py b/openstackclient/volume/v2/volume_type.py index 5e9faa1d..87f4c547 100644 --- a/openstackclient/volume/v2/volume_type.py +++ b/openstackclient/volume/v2/volume_type.py @@ -92,22 +92,39 @@ class CreateVolumeType(command.ShowOne): class DeleteVolumeType(command.Command): - """Delete volume type""" + """Delete volume type(s)""" def get_parser(self, prog_name): parser = super(DeleteVolumeType, self).get_parser(prog_name) parser.add_argument( - "volume_type", + "volume_types", metavar="", - help=_("Volume type to delete (name or ID)") + nargs="+", + help=_("Volume type(s) to delete (name or ID)") ) return parser def take_action(self, parsed_args): volume_client = self.app.client_manager.volume - volume_type = utils.find_resource( - volume_client.volume_types, parsed_args.volume_type) - volume_client.volume_types.delete(volume_type.id) + result = 0 + + for volume_type in parsed_args.volume_types: + try: + vol_type = utils.find_resource(volume_client.volume_types, + volume_type) + + volume_client.volume_types.delete(vol_type) + except Exception as e: + result += 1 + LOG.error(_("Failed to delete volume type with " + "name or ID '%(volume_type)s': %(e)s") + % {'volume_type': volume_type, 'e': e}) + + if result > 0: + total = len(parsed_args.volume_types) + msg = (_("%(result)s of %(total)s volume types failed " + "to delete.") % {'result': result, 'total': total}) + raise exceptions.CommandError(msg) class ListVolumeType(command.Lister): -- cgit v1.2.1