Merge "Add user-create to OSC"
This commit is contained in:
commit
896e1cb143
@ -0,0 +1,5 @@
|
||||
---
|
||||
features:
|
||||
- |
|
||||
The command ``trove user-create`` is now available to use in
|
||||
the python-openstackclient CLI as ``openstack database user create``
|
@ -48,6 +48,7 @@ openstack.database.v1 =
|
||||
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
|
||||
database_user_create = troveclient.osc.v1.database_users:CreateDatabaseUser
|
||||
database_user_delete = troveclient.osc.v1.database_users:DeleteDatabaseUser
|
||||
database_user_list = troveclient.osc.v1.database_users:ListDatabaseUsers
|
||||
database_user_show = troveclient.osc.v1.database_users:ShowDatabaseUser
|
||||
|
@ -20,6 +20,54 @@ import six
|
||||
from troveclient.i18n import _
|
||||
|
||||
|
||||
class CreateDatabaseUser(command.Command):
|
||||
|
||||
_description = _("Creates a user on an instance.")
|
||||
|
||||
def get_parser(self, prog_name):
|
||||
parser = super(CreateDatabaseUser, 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 user.')
|
||||
)
|
||||
parser.add_argument(
|
||||
'password',
|
||||
metavar='<password>',
|
||||
help=_('Password of user.')
|
||||
)
|
||||
parser.add_argument(
|
||||
'--host',
|
||||
metavar='<host>',
|
||||
help=_('Optional host of user.')
|
||||
)
|
||||
parser.add_argument(
|
||||
'--databases',
|
||||
metavar='<databases>',
|
||||
nargs='+',
|
||||
default=[],
|
||||
help=_('Optional list of databases.')
|
||||
)
|
||||
return parser
|
||||
|
||||
def take_action(self, parsed_args):
|
||||
manager = self.app.client_manager.database
|
||||
users = manager.users
|
||||
instance = utils.find_resource(manager.instances,
|
||||
parsed_args.instance)
|
||||
databases = [{'name': value} for value in parsed_args.databases]
|
||||
user = {'name': parsed_args.name, 'password': parsed_args.password,
|
||||
'databases': databases}
|
||||
if parsed_args.host:
|
||||
user['host'] = parsed_args.host
|
||||
users.create(instance, [user])
|
||||
|
||||
|
||||
class ListDatabaseUsers(command.Lister):
|
||||
|
||||
_description = _("Lists the users for an instance.")
|
||||
|
@ -27,6 +27,37 @@ class TestUsers(fakes.TestDatabasev1):
|
||||
self.user_client = self.app.client_manager.database.users
|
||||
|
||||
|
||||
class TestDatabaseUserCreate(TestUsers):
|
||||
|
||||
def setUp(self):
|
||||
super(TestDatabaseUserCreate, self).setUp()
|
||||
self.cmd = database_users.CreateDatabaseUser(self.app, None)
|
||||
|
||||
@mock.patch.object(utils, 'find_resource')
|
||||
def test_user_create(self, mock_find):
|
||||
args = ['instance1', 'user1', 'password1']
|
||||
mock_find.return_value = args[0]
|
||||
parsed_args = self.check_parser(self.cmd, args, [])
|
||||
result = self.cmd.take_action(parsed_args)
|
||||
user = {'name': 'user1', 'password': 'password1', 'databases': []}
|
||||
self.user_client.create.assert_called_with('instance1', [user])
|
||||
self.assertIsNone(result)
|
||||
|
||||
@mock.patch.object(utils, 'find_resource')
|
||||
def test_user_create_with_optional_args(self, mock_find):
|
||||
args = ['instance2', 'user2', 'password2',
|
||||
'--host', '1.1.1.1',
|
||||
'--databases', 'db1', 'db2']
|
||||
mock_find.return_value = args[0]
|
||||
parsed_args = self.check_parser(self.cmd, args, [])
|
||||
result = self.cmd.take_action(parsed_args)
|
||||
user = {'name': 'user2', 'password': 'password2',
|
||||
'host': '1.1.1.1',
|
||||
'databases': [{'name': 'db1'}, {'name': 'db2'}]}
|
||||
self.user_client.create.assert_called_with('instance2', [user])
|
||||
self.assertIsNone(result)
|
||||
|
||||
|
||||
class TestUserList(TestUsers):
|
||||
columns = database_users.ListDatabaseUsers.columns
|
||||
values = ('harry', '%', 'db1')
|
||||
|
Loading…
Reference in New Issue
Block a user