From 2119af70ef4cfa8ea031c25f9e0d3abcf102fdcc Mon Sep 17 00:00:00 2001 From: zhanggang Date: Thu, 7 Dec 2017 04:16:50 -0500 Subject: [PATCH] Add database-delete to OSC This change adds database support to the python-openstackclient project for the database-delete command. The trove command database-delete is now: openstack database db delete Change-Id: I7e56326b8f60624c2f408046055a10a42ea4d183 Partially-Implements: trove-support-in-python-openstackclient --- ...tabase-delete-to-osc-e6703e2d438824d1.yaml | 5 +++ setup.cfg | 1 + troveclient/osc/v1/databases.py | 37 +++++++++++++++++-- troveclient/tests/osc/v1/test_databases.py | 30 ++++++++++++++- 4 files changed, 69 insertions(+), 4 deletions(-) create mode 100644 releasenotes/notes/add-database-delete-to-osc-e6703e2d438824d1.yaml diff --git a/releasenotes/notes/add-database-delete-to-osc-e6703e2d438824d1.yaml b/releasenotes/notes/add-database-delete-to-osc-e6703e2d438824d1.yaml new file mode 100644 index 00000000..a6ee6e63 --- /dev/null +++ b/releasenotes/notes/add-database-delete-to-osc-e6703e2d438824d1.yaml @@ -0,0 +1,5 @@ +--- +features: + - | + The command ``trove database-delete`` is now available to use in + the python-openstackclient CLI as ``openstack database db delete`` diff --git a/setup.cfg b/setup.cfg index d8aba3e8..e8f6663f 100644 --- a/setup.cfg +++ b/setup.cfg @@ -36,6 +36,7 @@ openstack.database.v1 = database_cluster_show = troveclient.osc.v1.database_clusters:ShowDatabaseCluster database_configuration_list = troveclient.osc.v1.database_configurations:ListDatabaseConfigurations database_configuration_show = troveclient.osc.v1.database_configurations:ShowDatabaseConfiguration + database_db_delete = troveclient.osc.v1.databases:DeleteDatabase database_flavor_list = troveclient.osc.v1.database_flavors:ListDatabaseFlavors database_flavor_show = troveclient.osc.v1.database_flavors:ShowDatabaseFlavor database_instance_list = troveclient.osc.v1.database_instances:ListDatabaseInstances diff --git a/troveclient/osc/v1/databases.py b/troveclient/osc/v1/databases.py index 54a9746a..ea68697e 100644 --- a/troveclient/osc/v1/databases.py +++ b/troveclient/osc/v1/databases.py @@ -13,10 +13,10 @@ """Database v1 Databases action implementations""" from osc_lib.command import command -from osc_lib import utils as osc_utils +from osc_lib import exceptions +from osc_lib import utils from troveclient.i18n import _ -from troveclient import utils class ListDatabases(command.Lister): @@ -42,5 +42,36 @@ class ListDatabases(command.Lister): while items.next: items = databases.list(instance, marker=items.next) dbs += items - dbs = [osc_utils.get_item_properties(db, self.columns) for db in dbs] + dbs = [utils.get_item_properties(db, self.columns) for db in dbs] return self.columns, dbs + + +class DeleteDatabase(command.Command): + + _description = _("Deletes a database from an instance.") + + def get_parser(self, prog_name): + parser = super(DeleteDatabase, self).get_parser(prog_name) + parser.add_argument( + dest='instance', + metavar='', + help=_('ID or name of the instance.') + ) + parser.add_argument( + dest='database', + metavar='', + help=_('Name of the database.') + ) + return parser + + def take_action(self, parsed_args): + manager = self.app.client_manager.database + databases = manager.databases + try: + instance = utils.find_resource(manager.instances, + parsed_args.instance) + databases.delete(instance, parsed_args.database) + except Exception as e: + msg = (_("Failed to delete database %(database)s: %(e)s") + % {'database': parsed_args.database, 'e': e}) + raise exceptions.CommandError(msg) diff --git a/troveclient/tests/osc/v1/test_databases.py b/troveclient/tests/osc/v1/test_databases.py index 2593aa35..f55adf45 100644 --- a/troveclient/tests/osc/v1/test_databases.py +++ b/troveclient/tests/osc/v1/test_databases.py @@ -12,10 +12,12 @@ import mock +from osc_lib import exceptions +from osc_lib import utils + from troveclient import common from troveclient.osc.v1 import databases from troveclient.tests.osc.v1 import fakes -from troveclient import utils class TestDatabases(fakes.TestDatabasev1): @@ -46,3 +48,29 @@ class TestDatabaseList(TestDatabases): self.database_client.list.assert_called_once_with(args[0]) self.assertEqual(self.columns, columns) self.assertEqual([tuple(self.values)], data) + + +class TestDatabaseDelete(TestDatabases): + + def setUp(self): + super(TestDatabaseDelete, self).setUp() + self.cmd = databases.DeleteDatabase(self.app, None) + + @mock.patch.object(utils, 'find_resource') + def test_database_delete(self, mock_find): + args = ['instance1', 'db1'] + mock_find.return_value = args[0] + parsed_args = self.check_parser(self.cmd, args, []) + result = self.cmd.take_action(parsed_args) + self.database_client.delete.assert_called_with('instance1', 'db1') + self.assertIsNone(result) + + @mock.patch.object(utils, 'find_resource') + def test_database_delete_with_exception(self, mock_find): + args = ['fakeinstance', 'db1'] + parsed_args = self.check_parser(self.cmd, args, []) + + mock_find.side_effect = exceptions.CommandError + self.assertRaises(exceptions.CommandError, + self.cmd.take_action, + parsed_args)