diff --git a/releasenotes/notes/add-cluster-show-to-osc-5925431f5e94a746.yaml b/releasenotes/notes/add-cluster-show-to-osc-5925431f5e94a746.yaml new file mode 100644 index 00000000..e13e798b --- /dev/null +++ b/releasenotes/notes/add-cluster-show-to-osc-5925431f5e94a746.yaml @@ -0,0 +1,5 @@ +--- +features: + - The command ``trove cluster-show`` is now available to use in + the python-openstackclient CLI as ``openstack database cluster + show`` diff --git a/setup.cfg b/setup.cfg index cb809033..d8aba3e8 100644 --- a/setup.cfg +++ b/setup.cfg @@ -33,6 +33,7 @@ openstack.database.v1 = database_backup_list = troveclient.osc.v1.database_backups:ListDatabaseBackups database_backup_show = troveclient.osc.v1.database_backups:ShowDatabaseBackup database_cluster_list = troveclient.osc.v1.database_clusters:ListDatabaseClusters + 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_flavor_list = troveclient.osc.v1.database_flavors:ListDatabaseFlavors diff --git a/troveclient/osc/v1/database_clusters.py b/troveclient/osc/v1/database_clusters.py index 13bad6cc..7cd034b1 100644 --- a/troveclient/osc/v1/database_clusters.py +++ b/troveclient/osc/v1/database_clusters.py @@ -14,10 +14,29 @@ from osc_lib.command import command from osc_lib import utils +import six from troveclient.i18n import _ +def set_attributes_for_print_detail(cluster): + info = cluster._info.copy() + if hasattr(cluster, 'datastore'): + info['datastore'] = cluster.datastore['type'] + info['datastore_version'] = cluster.datastore['version'] + if hasattr(cluster, 'task'): + info['task_description'] = cluster.task['description'] + info['task_name'] = cluster.task['name'] + info.pop('task', None) + if hasattr(cluster, 'ip'): + info['ip'] = ', '.join(cluster.ip) + instances = info.pop('instances', None) + if instances: + info['instance_count'] = len(instances) + info.pop('links', None) + return info + + class ListDatabaseClusters(command.Lister): _description = _("List database clusters") @@ -59,3 +78,22 @@ class ListDatabaseClusters(command.Lister): clusters = [utils.get_item_properties(c, self.columns) for c in clusters] return self.columns, clusters + + +class ShowDatabaseCluster(command.ShowOne): + _description = _("Shows details of a database cluster") + + def get_parser(self, prog_name): + parser = super(ShowDatabaseCluster, self).get_parser(prog_name) + parser.add_argument( + 'cluster', + metavar='', + help=_('ID or name of the cluster'), + ) + return parser + + def take_action(self, parsed_args): + database_clusters = self.app.client_manager.database.clusters + cluster = utils.find_resource(database_clusters, parsed_args.cluster) + cluster = set_attributes_for_print_detail(cluster) + return zip(*sorted(six.iteritems(cluster))) diff --git a/troveclient/tests/osc/v1/test_database_clusters.py b/troveclient/tests/osc/v1/test_database_clusters.py index abd4bcfc..ca13de9e 100644 --- a/troveclient/tests/osc/v1/test_database_clusters.py +++ b/troveclient/tests/osc/v1/test_database_clusters.py @@ -46,3 +46,34 @@ class TestClusterList(TestClusters): self.cluster_client.list.assert_called_once_with(**self.defaults) self.assertEqual(self.columns, columns) self.assertEqual([self.values], data) + + +class TestClusterShow(TestClusters): + + values = ('2015-05-02T10:37:04', 'vertica', '7.1', 'cls-1234', 2, + 'test-clstr', 'No tasks for the cluster.', 'NONE', + '2015-05-02T11:06:19') + + def setUp(self): + super(TestClusterShow, self).setUp() + self.cmd = database_clusters.ShowDatabaseCluster(self.app, None) + self.data = self.fake_clusters.get_clusters_cls_1234() + self.cluster_client.get.return_value = self.data + self.columns = ( + 'created', + 'datastore', + 'datastore_version', + 'id', + 'instance_count', + 'name', + 'task_description', + 'task_name', + 'updated', + ) + + def test_show(self): + args = ['cls-1234'] + parsed_args = self.check_parser(self.cmd, args, []) + columns, data = self.cmd.take_action(parsed_args) + self.assertEqual(self.columns, columns) + self.assertEqual(self.values, data)