summaryrefslogtreecommitdiff
path: root/troveclient/osc
diff options
context:
space:
mode:
authorLingxian Kong <anlin.kong@gmail.com>2019-06-06 13:11:35 +1200
committerLingxian Kong <anlin.kong@gmail.com>2019-06-06 13:38:35 +1200
commit16d1dedb54dc70a6f0ae7e5068fd5582f0539a5a (patch)
tree28af16d970a0a45769d65cbadc58176562f7c330 /troveclient/osc
parent7a791177e86b8ce84267fb860ada36d6daf0417b (diff)
downloadpython-troveclient-16d1dedb54dc70a6f0ae7e5068fd5582f0539a5a.tar.gz
Support to batch delete database instances
Allow users to delete database instances in batch, usage: usage: openstack database instance delete [-h] instance [instance ...] Change-Id: I88a15ba1910f7b3294067b54ae4a7a9b4c4a17a5
Diffstat (limited to 'troveclient/osc')
-rw-r--r--troveclient/osc/v1/base.py34
-rw-r--r--troveclient/osc/v1/database_instances.py42
2 files changed, 63 insertions, 13 deletions
diff --git a/troveclient/osc/v1/base.py b/troveclient/osc/v1/base.py
new file mode 100644
index 0000000..337b6b8
--- /dev/null
+++ b/troveclient/osc/v1/base.py
@@ -0,0 +1,34 @@
+# Copyright 2019 Catalyst Cloud Ltd.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+from osc_lib.command import command
+from osc_lib import exceptions
+
+
+class TroveDeleter(command.Command):
+ def delete_resources(self, ids):
+ """Delete one or more resources."""
+ failure_flag = False
+ success_msg = "Request to delete %s %s has been accepted."
+ error_msg = "Unable to delete the specified %s(s)."
+
+ for id in ids:
+ try:
+ self.delete_func(id)
+ print(success_msg % (self.resource, id))
+ except Exception as e:
+ failure_flag = True
+ print(e)
+
+ if failure_flag:
+ raise exceptions.CommandError(error_msg % self.resource)
diff --git a/troveclient/osc/v1/database_instances.py b/troveclient/osc/v1/database_instances.py
index d71434b..0ecf4ac 100644
--- a/troveclient/osc/v1/database_instances.py
+++ b/troveclient/osc/v1/database_instances.py
@@ -13,12 +13,16 @@
"""Database v1 Instances action implementations"""
import argparse
+import six
+
from osc_lib.command import command
from osc_lib import exceptions
from osc_lib import utils as osc_utils
-import six
+from oslo_utils import uuidutils
from troveclient.i18n import _
+from troveclient.osc.v1 import base
+from troveclient import utils as trove_utils
def set_attributes_for_print(instances):
@@ -155,29 +159,41 @@ class ShowDatabaseInstance(command.ShowOne):
return zip(*sorted(six.iteritems(instance)))
-class DeleteDatabaseInstance(command.Command):
-
+class DeleteDatabaseInstance(base.TroveDeleter):
_description = _("Deletes an instance.")
def get_parser(self, prog_name):
parser = super(DeleteDatabaseInstance, self).get_parser(prog_name)
parser.add_argument(
'instance',
- metavar='<instance>',
- help=_('ID or name of the Instance'),
+ nargs='+',
+ metavar='instance',
+ help='Id or name of instance(s).'
)
return parser
def take_action(self, parsed_args):
db_instances = self.app.client_manager.database.instances
- try:
- instance = osc_utils.find_resource(db_instances,
- parsed_args.instance)
- db_instances.delete(instance)
- except Exception as e:
- msg = (_("Failed to delete instance %(instance)s: %(e)s")
- % {'instance': parsed_args.instance, 'e': e})
- raise exceptions.CommandError(msg)
+
+ # Used for batch deletion
+ self.delete_func = db_instances.delete
+ self.resource = 'database instance'
+
+ ids = []
+ for instance_id in parsed_args.instance:
+ if not uuidutils.is_uuid_like(instance_id):
+ try:
+ instance_id = trove_utils.get_resource_id_by_name(
+ db_instances, instance_id
+ )
+ except Exception as e:
+ msg = ("Failed to get database instance %s, error: %s" %
+ (instance_id, str(e)))
+ raise exceptions.CommandError(msg)
+
+ ids.append(instance_id)
+
+ self.delete_resources(ids)
class CreateDatabaseInstance(command.ShowOne):