From f88012ebecd80fdd1d26e4c4d2c645fe56f92e1c Mon Sep 17 00:00:00 2001 From: zhanggang Date: Fri, 8 Dec 2017 01:39:01 -0500 Subject: [PATCH] Add instance-delete to OSC This change adds database support to the python-openstackclient project for the instance-delete command. The trove command delete is now: openstack database instance delete Change-Id: Id052d14190e9fbdfe10db4e043fa78d6aef83ba9 Partially-Implements: trove-support-in-python-openstackclient --- ...stance-delete-to-osc-bf8de501c8945d58.yaml | 5 +++ setup.cfg | 1 + troveclient/osc/v1/database_instances.py | 26 ++++++++++++++++ .../tests/osc/v1/test_database_instances.py | 31 +++++++++++++++++++ 4 files changed, 63 insertions(+) create mode 100644 releasenotes/notes/add-instance-delete-to-osc-bf8de501c8945d58.yaml diff --git a/releasenotes/notes/add-instance-delete-to-osc-bf8de501c8945d58.yaml b/releasenotes/notes/add-instance-delete-to-osc-bf8de501c8945d58.yaml new file mode 100644 index 00000000..005d019b --- /dev/null +++ b/releasenotes/notes/add-instance-delete-to-osc-bf8de501c8945d58.yaml @@ -0,0 +1,5 @@ +--- +features: + - | + The command ``trove delete`` is now available to use in + the python-openstackclient CLI as ``openstack database instance delete`` diff --git a/setup.cfg b/setup.cfg index 032800a8..aa4740c3 100644 --- a/setup.cfg +++ b/setup.cfg @@ -38,6 +38,7 @@ openstack.database.v1 = database_configuration_show = troveclient.osc.v1.database_configurations:ShowDatabaseConfiguration database_flavor_list = troveclient.osc.v1.database_flavors:ListDatabaseFlavors database_flavor_show = troveclient.osc.v1.database_flavors:ShowDatabaseFlavor + database_instance_delete = troveclient.osc.v1.database_instances:DeleteDatabaseInstance database_instance_list = troveclient.osc.v1.database_instances:ListDatabaseInstances database_instance_show = troveclient.osc.v1.database_instances:ShowDatabaseInstance database_limit_list = troveclient.osc.v1.database_limits:ListDatabaseLimits diff --git a/troveclient/osc/v1/database_instances.py b/troveclient/osc/v1/database_instances.py index e5fef9da..45c8c5c1 100644 --- a/troveclient/osc/v1/database_instances.py +++ b/troveclient/osc/v1/database_instances.py @@ -13,6 +13,7 @@ """Database v1 Instances action implementations""" from osc_lib.command import command +from osc_lib import exceptions from osc_lib import utils as osc_utils import six @@ -133,3 +134,28 @@ class ShowDatabaseInstance(command.ShowOne): instance = osc_utils.find_resource(db_instances, parsed_args.instance) instance = set_attributes_for_print_detail(instance) return zip(*sorted(six.iteritems(instance))) + + +class DeleteDatabaseInstance(command.Command): + + _description = _("Deletes an instance.") + + def get_parser(self, prog_name): + parser = super(DeleteDatabaseInstance, self).get_parser(prog_name) + parser.add_argument( + 'instance', + metavar='', + help=_('ID or name of the Instance'), + ) + 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) diff --git a/troveclient/tests/osc/v1/test_database_instances.py b/troveclient/tests/osc/v1/test_database_instances.py index 7cabf630..7d49f000 100644 --- a/troveclient/tests/osc/v1/test_database_instances.py +++ b/troveclient/tests/osc/v1/test_database_instances.py @@ -10,6 +10,11 @@ # License for the specific language governing permissions and limitations # under the License. +import mock + +from osc_lib import exceptions +from osc_lib import utils + from troveclient import common from troveclient.osc.v1 import database_instances from troveclient.tests.osc.v1 import fakes @@ -77,3 +82,29 @@ class TestInstanceShow(TestInstances): columns, data = self.cmd.take_action(parsed_args) self.assertEqual(self.columns, columns) self.assertEqual(self.values, data) + + +class TestDatabaseInstanceDelete(TestInstances): + + def setUp(self): + 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.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)