Add database-create to OSC

This change adds database support to the python-openstackclient
project for the database-create command.

The trove command database-create is now:
    openstack database db create

Change-Id: I6ef3a6f61be4d4ec94d8b3d9edd09398ac8d6939
Partially-Implements: blueprint trove-support-in-python-openstackclient
This commit is contained in:
zhanggang 2017-12-21 03:45:27 -05:00
parent f92e624653
commit dd0c859148
4 changed files with 78 additions and 0 deletions

View File

@ -0,0 +1,5 @@
---
features:
- |
The command ``trove database-create`` is now available to use in
the python-openstackclient CLI as ``openstack database db create``

View File

@ -40,6 +40,7 @@ openstack.database.v1 =
database_configuration_parameter_list = troveclient.osc.v1.database_configurations:ListDatabaseConfigurationParameters
database_configuration_parameter_show = troveclient.osc.v1.database_configurations:ShowDatabaseConfigurationParameter
database_configuration_show = troveclient.osc.v1.database_configurations:ShowDatabaseConfiguration
database_db_create = troveclient.osc.v1.databases:CreateDatabase
database_db_delete = troveclient.osc.v1.databases:DeleteDatabase
database_db_list = troveclient.osc.v1.databases:ListDatabases
database_flavor_list = troveclient.osc.v1.database_flavors:ListDatabaseFlavors

View File

@ -19,6 +19,46 @@ from osc_lib import utils
from troveclient.i18n import _
class CreateDatabase(command.Command):
_description = _("Creates a database on an instance.")
def get_parser(self, prog_name):
parser = super(CreateDatabase, self).get_parser(prog_name)
parser.add_argument(
"instance",
metavar="<instance>",
help=_("ID or name of the instance."),
)
parser.add_argument(
"name",
metavar="<name>",
help=_("Name of the database."),
)
parser.add_argument(
"--character_set",
metavar="<character_set>",
help=_("Optional character set for database."),
)
parser.add_argument(
"--collate",
metavar="<collate>",
help=_("Optional collation type for database."),
)
return parser
def take_action(self, parsed_args):
manager = self.app.client_manager.database
databases = manager.databases
instance = utils.find_resource(manager.instances, parsed_args.instance)
database_dict = {'name': parsed_args.name}
if parsed_args.collate:
database_dict['collate'] = parsed_args.collate
if parsed_args.character_set:
database_dict['character_set'] = parsed_args.character_set
databases.create(instance, [database_dict])
class ListDatabases(command.Lister):
_description = _("Get a list of all Databases from the instance.")

View File

@ -29,6 +29,38 @@ class TestDatabases(fakes.TestDatabasev1):
self.database_client = self.app.client_manager.database.databases
class TestDatabaseCreate(TestDatabases):
def setUp(self):
super(TestDatabaseCreate, self).setUp()
self.cmd = databases.CreateDatabase(self.app, None)
@mock.patch.object(utils, 'find_resource')
def test_database_create(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.create.assert_called_with('instance1',
[{'name': 'db1'}])
self.assertIsNone(result)
@mock.patch.object(utils, 'find_resource')
def test_database_create_with_optional_args(self, mock_find):
args = ['instance2', 'db2',
'--character_set', 'utf8',
'--collate', 'utf8_general_ci']
mock_find.return_value = args[0]
parsed_args = self.check_parser(self.cmd, args, [])
database_dict = {'name': 'db2',
'collate': 'utf8_general_ci',
'character_set': 'utf8'}
result = self.cmd.take_action(parsed_args)
self.database_client.create.assert_called_with('instance2',
[database_dict])
self.assertIsNone(result)
class TestDatabaseList(TestDatabases):
columns = databases.ListDatabases.columns
values = ('fakedb1',)