Add root-enable to OSC

This change adds database support for the python-openstackclient
project for the root-enable command.

The trove command root-enable is now:
    openstack database root enable

Change-Id: I121dbe09bc10c57358c091e1eb882c816df6d4b2
Partially-Implements: blueprint trove-support-in-python-openstackclient
Signed-off-by: Zhao Chao <zhaochao1984@gmail.com>
This commit is contained in:
Zhao Chao 2018-01-12 10:25:32 +08:00
parent 8faed19f1c
commit 578cc98081
5 changed files with 177 additions and 0 deletions

View File

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

View File

@ -59,6 +59,7 @@ openstack.database.v1 =
database_instance_reset_status = troveclient.osc.v1.database_instances:ResetDatabaseInstanceStatus
database_instance_show = troveclient.osc.v1.database_instances:ShowDatabaseInstance
database_limit_list = troveclient.osc.v1.database_limits:ListDatabaseLimits
database_root_enable = troveclient.osc.v1.database_root:EnableDatabaseRoot
database_user_create = troveclient.osc.v1.database_users:CreateDatabaseUser
database_user_delete = troveclient.osc.v1.database_users:DeleteDatabaseUser
database_user_grant_access = troveclient.osc.v1.database_users:GrantDatabaseUserAccess

View File

@ -0,0 +1,83 @@
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
"""Database v1 Root action implementations"""
from osc_lib.command import command
from osc_lib import exceptions
from osc_lib import utils as osc_utils
import six
from troveclient.i18n import _
def find_instance_or_cluster(database_client_manager,
instance_or_cluster):
"""Returns an instance or cluster, found by ID or name,
along with the type of resource, instance or cluster.
Raises CommandError if none is found.
"""
db_instances = database_client_manager.instances
try:
return (osc_utils.find_resource(db_instances,
instance_or_cluster),
'instance')
except exceptions.CommandError:
db_clusters = database_client_manager.clusters
try:
return (osc_utils.find_resource(db_clusters,
instance_or_cluster),
'cluster')
except exceptions.CommandError:
raise exceptions.CommandError(
_("No instance or cluster with a name or ID of '%s' exists.")
% instance_or_cluster)
class EnableDatabaseRoot(command.ShowOne):
_description = _("Enables root for an instance and resets "
"if already exists.")
def get_parser(self, prog_name):
parser = super(EnableDatabaseRoot, self).get_parser(prog_name)
parser.add_argument(
'instance_or_cluster',
metavar='<instance_or_cluster>',
help=_('ID or name of the instance or cluster.'),
)
parser.add_argument(
'--root_password',
metavar='<root_password>',
default=None,
help=_('Root password to set.'))
return parser
def take_action(self, parsed_args):
database_client_manager = self.app.client_manager.database
instance_or_cluster, resource_type = find_instance_or_cluster(
database_client_manager,
parsed_args.instance_or_cluster)
db_root = database_client_manager.root
if resource_type == 'instance':
root = db_root.create_instance_root(instance_or_cluster,
parsed_args.root_password)
else:
root = db_root.create_cluster_root(instance_or_cluster,
parsed_args.root_password)
result = {'name': root[0],
'password': root[1]}
return zip(*sorted(six.iteritems(result)))

View File

@ -129,3 +129,13 @@ class FakeDatastores(object):
def get_datastores_d_123_versions(self):
return datastores.Datastore(None, self.fake_datastore_versions[0])
class FakeRoot(object):
def post_instance_1234_root(self):
root = fakes.FakeHTTPClient().post_instances_1234_root()[2]['user']
return root['name'], root['password']
def post_cls_1234_root(self):
root = fakes.FakeHTTPClient().post_instances_1234_root()[2]['user']
return root['name'], root['password']

View File

@ -0,0 +1,78 @@
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# 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.osc.v1 import database_root
from troveclient.tests.osc.v1 import fakes
class TestRoot(fakes.TestDatabasev1):
fake_root = fakes.FakeRoot()
def setUp(self):
super(TestRoot, self).setUp()
self.mock_client = self.app.client_manager.database
self.root_client = self.app.client_manager.database.root
class TestRootEnable(TestRoot):
def setUp(self):
super(TestRootEnable, self).setUp()
self.cmd = database_root.EnableDatabaseRoot(self.app, None)
self.data = {
'instance': self.fake_root.post_instance_1234_root(),
'cluster': self.fake_root.post_cls_1234_root()
}
self.columns = ('name', 'password',)
@mock.patch.object(utils, 'find_resource')
def test_enable_instance_1234_root(self, mock_find):
self.root_client.create_instance_root.return_value = (
self.data['instance'])
args = ['1234']
parsed_args = self.check_parser(self.cmd, args, [])
columns, data = self.cmd.take_action(parsed_args)
self.assertEqual(self.columns, columns)
self.assertEqual(('root', 'password',), data)
@mock.patch.object(utils, 'find_resource')
def test_enable_cluster_1234_root(self, mock_find):
mock_find.side_effect = [exceptions.CommandError(),
(None, 'cluster')]
self.root_client.create_cluster_root.return_value = (
self.data['cluster'])
args = ['1234']
parsed_args = self.check_parser(self.cmd, args, [])
columns, data = self.cmd.take_action(parsed_args)
self.assertEqual(self.columns, columns)
self.assertEqual(('root', 'password',), data)
@mock.patch.object(utils, 'find_resource')
def test_enable_instance_root_with_password(self, mock_find):
args = ['1234', '--root_password', 'secret']
parsed_args = self.check_parser(self.cmd, args, [])
self.cmd.take_action(parsed_args)
self.root_client.create_instance_root(None,
root_password='secret')
@mock.patch.object(utils, 'find_resource')
def test_enable_cluster_root_with_password(self, mock_find):
args = ['1234', '--root_password', 'secret']
parsed_args = self.check_parser(self.cmd, args, [])
self.cmd.take_action(parsed_args)
self.root_client.create_cluster_root(None,
root_password='secret')