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
This commit is contained in:
Lingxian Kong 2019-06-06 13:11:35 +12:00
parent 7a791177e8
commit 16d1dedb54
5 changed files with 101 additions and 24 deletions

View File

@ -0,0 +1,2 @@
features:
- Allow users to delete database instances in batch.

View File

@ -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)

View File

@ -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):

View File

@ -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):
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)
@mock.patch("troveclient.utils.get_resource_id_by_name")
def test_instance_delete(self, mock_getid):
mock_getid.return_value = "fake_uuid"
args = ['instance1']
parsed_args = self.check_parser(self.cmd, args, [])
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):

View File

@ -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