diff options
author | Zuul <zuul@review.opendev.org> | 2019-06-06 02:14:28 +0000 |
---|---|---|
committer | Gerrit Code Review <review@openstack.org> | 2019-06-06 02:14:29 +0000 |
commit | 8d0bd0b6d181f5d0b9f20d62488cfb3033e7bc95 (patch) | |
tree | eab47cf8b0869068fa783ad26917d3c189e2d5e9 | |
parent | be04075d2efac7403afe8d131f520e44c094461c (diff) | |
parent | 16d1dedb54dc70a6f0ae7e5068fd5582f0539a5a (diff) | |
download | python-troveclient-8d0bd0b6d181f5d0b9f20d62488cfb3033e7bc95.tar.gz |
Merge "Support to batch delete database instances"
-rw-r--r-- | releasenotes/notes/bulk-delete-database-instances-7938121487fb11e9.yaml | 2 | ||||
-rw-r--r-- | troveclient/osc/v1/base.py | 34 | ||||
-rw-r--r-- | troveclient/osc/v1/database_instances.py | 42 | ||||
-rw-r--r-- | troveclient/tests/osc/v1/test_database_instances.py | 38 | ||||
-rw-r--r-- | troveclient/utils.py | 5 |
5 files changed, 99 insertions, 22 deletions
diff --git a/releasenotes/notes/bulk-delete-database-instances-7938121487fb11e9.yaml b/releasenotes/notes/bulk-delete-database-instances-7938121487fb11e9.yaml new file mode 100644 index 0000000..88512ba --- /dev/null +++ b/releasenotes/notes/bulk-delete-database-instances-7938121487fb11e9.yaml @@ -0,0 +1,2 @@ +features: + - Allow users to delete database instances in batch. 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): diff --git a/troveclient/tests/osc/v1/test_database_instances.py b/troveclient/tests/osc/v1/test_database_instances.py index 9adeae8..e1ea493 100644 --- a/troveclient/tests/osc/v1/test_database_instances.py +++ b/troveclient/tests/osc/v1/test_database_instances.py @@ -14,6 +14,7 @@ import mock from osc_lib import exceptions from osc_lib import utils +from oslo_utils import uuidutils from troveclient import common from troveclient.osc.v1 import database_instances @@ -119,25 +120,44 @@ class TestDatabaseInstanceDelete(TestInstances): super(TestDatabaseInstanceDelete, self).setUp() self.cmd = database_instances.DeleteDatabaseInstance(self.app, None) - @mock.patch.object(utils, 'find_resource') - def test_instance_delete(self, mock_find): + @mock.patch("troveclient.utils.get_resource_id_by_name") + def test_instance_delete(self, mock_getid): + mock_getid.return_value = "fake_uuid" + args = ['instance1'] - mock_find.return_value = args[0] parsed_args = self.check_parser(self.cmd, args, []) - result = self.cmd.take_action(parsed_args) - self.instance_client.delete.assert_called_with('instance1') - self.assertIsNone(result) + self.cmd.take_action(parsed_args) + + mock_getid.assert_called_once_with(self.instance_client, "instance1") + self.instance_client.delete.assert_called_with('fake_uuid') + + @mock.patch("troveclient.utils.get_resource_id_by_name") + def test_instance_delete_with_exception(self, mock_getid): + mock_getid.side_effect = exceptions.CommandError - @mock.patch.object(utils, 'find_resource') - def test_instance_delete_with_exception(self, mock_find): args = ['fakeinstance'] parsed_args = self.check_parser(self.cmd, args, []) - mock_find.side_effect = exceptions.CommandError self.assertRaises(exceptions.CommandError, self.cmd.take_action, parsed_args) + @mock.patch("troveclient.utils.get_resource_id_by_name") + def test_instance_bulk_delete(self, mock_getid): + instance_1 = uuidutils.generate_uuid() + instance_2 = uuidutils.generate_uuid() + mock_getid.return_value = instance_1 + + args = ["fake_instance", instance_2] + parsed_args = self.check_parser(self.cmd, args, []) + self.cmd.take_action(parsed_args) + + mock_getid.assert_called_once_with(self.instance_client, + "fake_instance") + + calls = [mock.call(instance_1), mock.call(instance_2)] + self.instance_client.delete.assert_has_calls(calls) + class TestDatabaseInstanceCreate(TestInstances): diff --git a/troveclient/utils.py b/troveclient/utils.py index 28096a7..7486007 100644 --- a/troveclient/utils.py +++ b/troveclient/utils.py @@ -210,6 +210,11 @@ def print_dict(d, property="Property"): _print(pt, property) +def get_resource_id_by_name(manager, name): + resource = manager.find(name=name) + return resource.id + + def find_resource(manager, name_or_id): """Helper for the _find_* methods.""" # first try to get entity as integer id |