From 4643191c63170f5006a65659cb2b3628365409f7 Mon Sep 17 00:00:00 2001 From: dixiaoli Date: Sun, 21 Feb 2016 10:22:35 +0800 Subject: [PATCH] Add OpenstackClient plugin for cluster policy show This change implements the "openstack cluster policy show" command Based on the existing senlin command: senlin policy-show Change-Id: I2f650d1c101ed7317a25fdaca8a025683e8bb4ea Blueprint: senlin-support-python-openstackclient --- senlinclient/osc/v1/policy.py | 49 +++++++++++++++++++ senlinclient/tests/unit/osc/v1/test_policy.py | 46 +++++++++++++++++ setup.cfg | 1 + 3 files changed, 96 insertions(+) diff --git a/senlinclient/osc/v1/policy.py b/senlinclient/osc/v1/policy.py index a206a510..2d01e852 100644 --- a/senlinclient/osc/v1/policy.py +++ b/senlinclient/osc/v1/policy.py @@ -15,6 +15,9 @@ import logging from cliff import lister +from cliff import show +from openstack import exceptions as sdk_exc +from openstackclient.common import exceptions as exc from openstackclient.common import utils from senlinclient.common.i18n import _ @@ -97,3 +100,49 @@ class ListPolicy(lister.Lister): (utils.get_item_properties(p, columns, formatters=formatters) for p in policies) ) + + +class ShowPolicy(show.ShowOne): + """Show the policy details.""" + + log = logging.getLogger(__name__ + ".ShowPolicy") + + def get_parser(self, prog_name): + parser = super(ShowPolicy, self).get_parser(prog_name) + parser.add_argument( + 'policy', + metavar='', + help=_('Name or Id of the policy to show') + ) + return parser + + def take_action(self, parsed_args): + self.log.debug("take_action(%s)", parsed_args) + + senlin_client = self.app.client_manager.clustering + return _show_policy(senlin_client, policy_id=parsed_args.policy) + + +def _show_policy(senlin_client, policy_id): + try: + policy = senlin_client.get_policy(policy_id) + except sdk_exc.ResourceNotFound: + raise exc.CommandError(_('Policy not found: %s') % policy_id) + + formatters = { + 'spec': senlin_utils.json_formatter + } + columns = [ + 'created_at', + 'data', + 'domain', + 'id', + 'name', + 'project', + 'spec', + 'type', + 'updated_at', + 'user' + ] + return columns, utils.get_dict_properties(policy.to_dict(), columns, + formatters=formatters) diff --git a/senlinclient/tests/unit/osc/v1/test_policy.py b/senlinclient/tests/unit/osc/v1/test_policy.py index 3d50e0f9..5c9cc822 100644 --- a/senlinclient/tests/unit/osc/v1/test_policy.py +++ b/senlinclient/tests/unit/osc/v1/test_policy.py @@ -15,6 +15,7 @@ import mock from openstack.cluster.v1 import policy as sdk_policy from openstack import exceptions as sdk_exc +from openstackclient.common import exceptions as exc from senlinclient.osc.v1 import policy as osc_policy from senlinclient.tests.unit.osc.v1 import fakes @@ -136,3 +137,48 @@ class TestPolicyList(TestPolicy): columns, data = self.cmd.take_action(parsed_args) self.mock_client.policies.assert_called_with(**kwargs) self.assertEqual(self.columns, columns) + + +class TestPolicyShow(TestPolicy): + get_response = {"policy": { + "created_at": "2015-03-02T07:40:31", + "data": {}, + "domain": 'null', + "id": "02f62195-2198-4797-b0a9-877632208527", + "name": "sp001", + "project": "42d9e9663331431f97b75e25136307ff", + "spec": { + "properties": { + "adjustment": { + "best_effort": True, + "min_step": 1, + "number": 1, + "type": "CHANGE_IN_CAPACITY" + }, + "event": "CLUSTER_SCALE_IN" + }, + "type": "senlin.policy.scaling", + "version": "1.0" + }, + "type": "senlin.policy.scaling-1.0", + "updated_at": 'null', + "user": "5e5bf8027826429c96af157f68dc9072" + }} + + def setUp(self): + super(TestPolicyShow, self).setUp() + self.cmd = osc_policy.ShowPolicy(self.app, None) + self.mock_client.get_policy = mock.Mock( + return_value=sdk_policy.Policy(None, self.get_response)) + + def test_policy_show(self): + arglist = ['sp001'] + parsed_args = self.check_parser(self.cmd, arglist, []) + self.cmd.take_action(parsed_args) + self.mock_client.get_policy.assert_called_with('sp001') + + def test_policy_show_not_found(self): + arglist = ['sp001'] + parsed_args = self.check_parser(self.cmd, arglist, []) + self.mock_client.get_policy.side_effect = sdk_exc.ResourceNotFound() + self.assertRaises(exc.CommandError, self.cmd.take_action, parsed_args) diff --git a/setup.cfg b/setup.cfg index 7b351dfb..4ba69995 100644 --- a/setup.cfg +++ b/setup.cfg @@ -36,6 +36,7 @@ openstack.clustering.v1 = cluster_node_show = senlinclient.osc.v1.node:ShowNode cluster_node_update = senlinclient.osc.v1.node:UpdateNode cluster_policy_list = senlinclient.osc.v1.policy:ListPolicy + cluster_policy_show = senlinclient.osc.v1.policy:ShowPolicy cluster_profile_create = senlinclient.osc.v1.profile:CreateProfile cluster_profile_delete = senlinclient.osc.v1.profile:DeleteProfile cluster_profile_list = senlinclient.osc.v1.profile:ListProfile