summaryrefslogtreecommitdiff
path: root/openstackclient/network
diff options
context:
space:
mode:
Diffstat (limited to 'openstackclient/network')
-rw-r--r--openstackclient/network/common.py37
-rw-r--r--openstackclient/network/v2/network.py20
2 files changed, 47 insertions, 10 deletions
diff --git a/openstackclient/network/common.py b/openstackclient/network/common.py
index 1e2c4cce..a3047d84 100644
--- a/openstackclient/network/common.py
+++ b/openstackclient/network/common.py
@@ -15,6 +15,7 @@ import abc
import six
from openstackclient.common import command
+from openstackclient.common import exceptions
@six.add_metaclass(abc.ABCMeta)
@@ -69,6 +70,42 @@ class NetworkAndComputeCommand(command.Command):
@six.add_metaclass(abc.ABCMeta)
+class NetworkAndComputeDelete(NetworkAndComputeCommand):
+ """Network and Compute Delete
+
+ Delete class for commands that support implementation via
+ the network or compute endpoint. Such commands have different
+ implementations for take_action() and may even have different
+ arguments. This class supports bulk deletion, and error handling
+ following the rules in doc/source/command-errors.rst.
+ """
+
+ def take_action(self, parsed_args):
+ ret = 0
+ resources = getattr(parsed_args, self.resource, [])
+
+ for r in resources:
+ self.r = r
+ try:
+ if self.app.client_manager.is_network_endpoint_enabled():
+ self.take_action_network(self.app.client_manager.network,
+ parsed_args)
+ else:
+ self.take_action_compute(self.app.client_manager.compute,
+ parsed_args)
+ except Exception as e:
+ self.app.log.error("Failed to delete %s with name or ID "
+ "'%s': %s" % (self.resource, r, e))
+ ret += 1
+
+ if ret:
+ total = len(resources)
+ msg = "%s of %s %ss failed to delete." % (ret, total,
+ self.resource)
+ raise exceptions.CommandError(msg)
+
+
+@six.add_metaclass(abc.ABCMeta)
class NetworkAndComputeLister(command.Lister):
"""Network and Compute Lister
diff --git a/openstackclient/network/v2/network.py b/openstackclient/network/v2/network.py
index bdde9173..bf01e2ec 100644
--- a/openstackclient/network/v2/network.py
+++ b/openstackclient/network/v2/network.py
@@ -248,9 +248,13 @@ class CreateNetwork(common.NetworkAndComputeShowOne):
return (columns, data)
-class DeleteNetwork(common.NetworkAndComputeCommand):
+class DeleteNetwork(common.NetworkAndComputeDelete):
"""Delete network(s)"""
+ # Used by base class to find resources in parsed_args.
+ resource = 'network'
+ r = None
+
def update_parser_common(self, parser):
parser.add_argument(
'network',
@@ -258,20 +262,16 @@ class DeleteNetwork(common.NetworkAndComputeCommand):
nargs="+",
help=_("Network(s) to delete (name or ID)")
)
+
return parser
def take_action_network(self, client, parsed_args):
- for network in parsed_args.network:
- obj = client.find_network(network)
- client.delete_network(obj)
+ obj = client.find_network(self.r, ignore_missing=False)
+ client.delete_network(obj)
def take_action_compute(self, client, parsed_args):
- for network in parsed_args.network:
- network = utils.find_resource(
- client.networks,
- network,
- )
- client.networks.delete(network.id)
+ network = utils.find_resource(client.networks, self.r)
+ client.networks.delete(network.id)
class ListNetwork(common.NetworkAndComputeLister):