summaryrefslogtreecommitdiff
path: root/troveclient
diff options
context:
space:
mode:
authorZhao Chao <zhaochao1984@gmail.com>2018-01-12 10:25:32 +0800
committerZhao Chao <zhaochao1984@gmail.com>2018-01-12 11:35:26 +0800
commit578cc980816176fd9a4da5b6342792e16f9b3f88 (patch)
tree49eb6e319a4df3b2cee521595c1aa67668740d29 /troveclient
parent8faed19f1ca7172a03ac2d79bb12fae86c204eef (diff)
downloadpython-troveclient-578cc980816176fd9a4da5b6342792e16f9b3f88.tar.gz
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>
Diffstat (limited to 'troveclient')
-rw-r--r--troveclient/osc/v1/database_root.py83
-rw-r--r--troveclient/tests/osc/v1/fakes.py10
-rw-r--r--troveclient/tests/osc/v1/test_database_root.py78
3 files changed, 171 insertions, 0 deletions
diff --git a/troveclient/osc/v1/database_root.py b/troveclient/osc/v1/database_root.py
new file mode 100644
index 0000000..d65a1ab
--- /dev/null
+++ b/troveclient/osc/v1/database_root.py
@@ -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)))
diff --git a/troveclient/tests/osc/v1/fakes.py b/troveclient/tests/osc/v1/fakes.py
index 277274d..90205a6 100644
--- a/troveclient/tests/osc/v1/fakes.py
+++ b/troveclient/tests/osc/v1/fakes.py
@@ -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']
diff --git a/troveclient/tests/osc/v1/test_database_root.py b/troveclient/tests/osc/v1/test_database_root.py
new file mode 100644
index 0000000..3e3d9f1
--- /dev/null
+++ b/troveclient/tests/osc/v1/test_database_root.py
@@ -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')