From 09b5f9fa5e265a2521c2d97f9a47a06714224bdb Mon Sep 17 00:00:00 2001 From: Trevor McCasland Date: Fri, 3 Feb 2017 13:31:48 -0600 Subject: [PATCH] Add list to OSC This change adds database support to the python-openstackclient project for the list command. The trove command list is now: openstack database instance list Partially-Implements: blueprint trove-support-in-python-openstackclient Change-Id: Ic5328827dbf6d1f47db62f0bb2e65e164a0b2b6b --- ...instance-list-to-osc-05714dfce947a57e.yaml | 4 + setup.cfg | 2 + troveclient/osc/v1/database_instances.py | 83 +++++++++++++++++++ troveclient/tests/osc/v1/fakes.py | 8 ++ .../tests/osc/v1/test_database_instances.py | 49 +++++++++++ 5 files changed, 146 insertions(+) create mode 100644 releasenotes/notes/add-instance-list-to-osc-05714dfce947a57e.yaml create mode 100644 troveclient/osc/v1/database_instances.py create mode 100644 troveclient/tests/osc/v1/test_database_instances.py diff --git a/releasenotes/notes/add-instance-list-to-osc-05714dfce947a57e.yaml b/releasenotes/notes/add-instance-list-to-osc-05714dfce947a57e.yaml new file mode 100644 index 00000000..e8898bc4 --- /dev/null +++ b/releasenotes/notes/add-instance-list-to-osc-05714dfce947a57e.yaml @@ -0,0 +1,4 @@ +--- +features: + - The command ``trove list`` is now available to use in + the python-openstackclient CLI as ``openstack database instance list`` diff --git a/setup.cfg b/setup.cfg index fa5dfb34..956a424c 100644 --- a/setup.cfg +++ b/setup.cfg @@ -34,9 +34,11 @@ openstack.database.v1 = database_cluster_list = troveclient.osc.v1.database_clusters:ListDatabaseClusters database_configuration_list = troveclient.osc.v1.database_configurations:ListDatabaseConfigurations database_flavor_list = troveclient.osc.v1.database_flavors:ListDatabaseFlavors + database_instance_list = troveclient.osc.v1.database_instances:ListDatabaseInstances database_limit_list = troveclient.osc.v1.database_limits:ListDatabaseLimits database_user_list = troveclient.osc.v1.database_users:ListDatabaseUsers + [build_sphinx] all_files = 1 source-dir = doc/source diff --git a/troveclient/osc/v1/database_instances.py b/troveclient/osc/v1/database_instances.py new file mode 100644 index 00000000..43a83535 --- /dev/null +++ b/troveclient/osc/v1/database_instances.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 Instances action implementations""" + +from osc_lib.command import command +from osc_lib import utils as osc_utils + +from troveclient.i18n import _ + + +def set_attributes_for_print(instances): + for instance in instances: + setattr(instance, 'flavor_id', instance.flavor['id']) + if hasattr(instance, 'volume'): + setattr(instance, 'size', instance.volume['size']) + else: + setattr(instance, 'size', '-') + if hasattr(instance, 'datastore'): + if instance.datastore.get('version'): + setattr(instance, 'datastore_version', + instance.datastore['version']) + setattr(instance, 'datastore', instance.datastore['type']) + return instances + + +class ListDatabaseInstances(command.Lister): + + _description = _("List database instances") + columns = ['ID', 'Name', 'Datastore', 'Datastore Version', 'Status', + 'Flavor ID', 'Size', 'Region'] + + def get_parser(self, prog_name): + parser = super(ListDatabaseInstances, self).get_parser(prog_name) + parser.add_argument( + '--limit', + dest='limit', + metavar='', + default=None, + help=_('Limit the number of results displayed.') + ) + parser.add_argument( + '--marker', + dest='marker', + metavar='', + type=str, + default=None, + help=_('Begin displaying the results for IDs greater than the' + 'specified marker. When used with :option:`--limit,` set' + 'this to the last ID displayed in the previous run.') + ) + parser.add_argument( + '--include_clustered', '--include-clustered', + dest='include_clustered', + action="store_true", + default=False, + help=_("Include instances that are part of a cluster " + "(default %(default)s). --include-clustered may be " + "deprecated in the future, retaining just " + "--include_clustered.") + ) + return parser + + def take_action(self, parsed_args): + db_instances = self.app.client_manager.database.instances + instances = db_instances.list(limit=parsed_args.limit, + marker=parsed_args.marker, + include_clustered=(parsed_args. + include_clustered)) + if instances: + instances = set_attributes_for_print(instances) + instances = [osc_utils.get_item_properties(i, self.columns) + for i in instances] + return self.columns, instances diff --git a/troveclient/tests/osc/v1/fakes.py b/troveclient/tests/osc/v1/fakes.py index 1cdff6f0..4f43ed17 100644 --- a/troveclient/tests/osc/v1/fakes.py +++ b/troveclient/tests/osc/v1/fakes.py @@ -18,6 +18,7 @@ from troveclient.tests.osc import utils from troveclient.v1 import backups from troveclient.v1 import clusters from troveclient.v1 import flavors +from troveclient.v1 import instances from troveclient.v1 import limits from troveclient.v1 import users @@ -76,3 +77,10 @@ class FakeUsers(object): def get_instances_1234_users_harry(self): return users.User(None, self.fake_users[2]) + + +class FakeInstances(object): + fake_instances = (fakes.FakeHTTPClient().get_instances()[2]['instances']) + + def get_instances_1234(self): + return instances.Instance(None, self.fake_instances[0]) diff --git a/troveclient/tests/osc/v1/test_database_instances.py b/troveclient/tests/osc/v1/test_database_instances.py new file mode 100644 index 00000000..d05abf78 --- /dev/null +++ b/troveclient/tests/osc/v1/test_database_instances.py @@ -0,0 +1,49 @@ +# 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. + +from troveclient import common +from troveclient.osc.v1 import database_instances +from troveclient.tests.osc.v1 import fakes + + +class TestInstances(fakes.TestDatabasev1): + fake_instances = fakes.FakeInstances() + + def setUp(self): + super(TestInstances, self).setUp() + self.instance_client = self.app.client_manager.database.instances + + +class TestInstanceList(TestInstances): + + defaults = { + 'include_clustered': False, + 'limit': None, + 'marker': None + } + + columns = database_instances.ListDatabaseInstances.columns + values = ('1234', 'test-member-1', 'mysql', '5.6', 'ACTIVE', '02', 2, + 'regionOne') + + def setUp(self): + super(TestInstanceList, self).setUp() + self.cmd = database_instances.ListDatabaseInstances(self.app, None) + self.data = [self.fake_instances.get_instances_1234()] + self.instance_client.list.return_value = common.Paginated(self.data) + + def test_instance_list_defaults(self): + parsed_args = self.check_parser(self.cmd, [], []) + columns, data = self.cmd.take_action(parsed_args) + self.instance_client.list.assert_called_once_with(**self.defaults) + self.assertEqual(self.columns, columns) + self.assertEqual([self.values], data)