From 827d5c84ae5898c68fcd7ecb63a0b59aba5b6244 Mon Sep 17 00:00:00 2001 From: dixiaoli Date: Fri, 19 Feb 2016 09:59:24 +0800 Subject: [PATCH] Add OpenstackClient plugin for cluster profile type show This change implements the "openstack cluster profile type show" command Based on the existing senlin command: senlin profile-type-show Change-Id: Iab715ce2f2f13432e60187f42ca34d07c1f29fd0 Blueprint: senlin-support-python-openstackclient --- senlinclient/osc/v1/profile_type.py | 33 +++++++++++++++++++ .../tests/unit/osc/v1/test_profile_type.py | 33 +++++++++++++++++++ setup.cfg | 1 + 3 files changed, 67 insertions(+) diff --git a/senlinclient/osc/v1/profile_type.py b/senlinclient/osc/v1/profile_type.py index edf2bc88..993edd0e 100644 --- a/senlinclient/osc/v1/profile_type.py +++ b/senlinclient/osc/v1/profile_type.py @@ -13,8 +13,13 @@ """Clustering v1 profile type action implementations""" import logging +import six from cliff import lister +from openstack import exceptions as sdk_exc +from openstackclient.common import exceptions as exc +from senlinclient.common import format_utils +from senlinclient.common.i18n import _ class ProfileTypeList(lister.Lister): @@ -33,3 +38,31 @@ class ProfileTypeList(lister.Lister): columns = ['name'] rows = sorted([t.name] for t in types) return columns, rows + + +class ProfileTypeShow(format_utils.YamlFormat): + """Show the details about a profile type.""" + + log = logging.getLogger(__name__ + ".ProfileTypeShow") + + def get_parser(self, prog_name): + parser = super(ProfileTypeShow, self).get_parser(prog_name) + parser.add_argument( + 'type_name', + metavar='', + help=_('Profile type to retrieve') + ) + return parser + + def take_action(self, parsed_args): + self.log.debug("take_action(%s)", parsed_args) + senlin_client = self.app.client_manager.clustering + + try: + res = senlin_client.get_profile_type(parsed_args.type_name) + except sdk_exc.ResourceNotFound: + raise exc.CommandError(_('Profile Type not found: %s') + % parsed_args.type_name) + rows = list(six.itervalues(res)) + columns = list(six.iterkeys(res)) + return columns, rows diff --git a/senlinclient/tests/unit/osc/v1/test_profile_type.py b/senlinclient/tests/unit/osc/v1/test_profile_type.py index f843ba1d..9826d161 100644 --- a/senlinclient/tests/unit/osc/v1/test_profile_type.py +++ b/senlinclient/tests/unit/osc/v1/test_profile_type.py @@ -13,6 +13,8 @@ import mock from openstack.cluster.v1 import profile_type as sdk_profile_type +from openstack import exceptions as sdk_exc +from openstackclient.common import exceptions as exc from senlinclient.osc.v1 import profile_type as osc_profile_type from senlinclient.tests.unit.osc.v1 import fakes @@ -60,3 +62,34 @@ class TestProfileTypeList(TestProfileType): self.mock_client.profile_types.assert_called_with() self.assertEqual(self.expected_columns, columns) self.assertEqual(self.expected_rows, rows) + + +class TestProfileTypeShow(TestProfileType): + + response = ({'name': 'os.heat.stack-1.0', + 'schema': { + 'foo': 'bar'}}) + + def setUp(self): + super(TestProfileTypeShow, self).setUp() + self.cmd = osc_profile_type.ProfileTypeShow(self.app, None) + self.mock_client.get_profile_type = mock.Mock( + return_value=sdk_profile_type.ProfileType(self.response) + ) + + def test_profile_type_show(self): + arglist = ['os.heat.stack-1.0'] + parsed_args = self.check_parser(self.cmd, arglist, []) + self.cmd.take_action(parsed_args) + self.mock_client.get_profile_type.assert_called_once_with( + 'os.heat.stack-1.0') + + def test_profile_type_show_not_found(self): + arglist = ['os.heat.stack-1.1'] + parsed_args = self.check_parser(self.cmd, arglist, []) + self.mock_client.get_profile_type.side_effect = ( + sdk_exc.ResourceNotFound()) + error = self.assertRaises(exc.CommandError, self.cmd.take_action, + parsed_args) + self.assertEqual('Profile Type not found: os.heat.stack-1.1', + str(error)) diff --git a/setup.cfg b/setup.cfg index daf224ad..bb54d705 100644 --- a/setup.cfg +++ b/setup.cfg @@ -33,6 +33,7 @@ openstack.clustering.v1 = cluster_profile_list = senlinclient.osc.v1.profile:ListProfile cluster_profile_show = senlinclient.osc.v1.profile:ShowProfile cluster_profile_type_list = senlinclient.osc.v1.profile_type:ProfileTypeList + cluster_profile_type_show = senlinclient.osc.v1.profile_type:ProfileTypeShow [global] setup-hooks =