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
This commit is contained in:
zhanggang 2017-12-07 04:16:50 -05:00
parent 22ae4cf937
commit 2119af70ef
4 changed files with 69 additions and 4 deletions

View File

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

View File

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

View File

@ -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='<instance>',
help=_('ID or name of the instance.')
)
parser.add_argument(
dest='database',
metavar='<database>',
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)

View File

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