From e7c7e3b439639ec874e827751b2b896c3c2e6ad0 Mon Sep 17 00:00:00 2001 From: dixiaoli Date: Fri, 26 Feb 2016 19:06:34 +0800 Subject: [PATCH] Add OpenstackClient plugin for cluster policy binding list This change implements the "openstack cluster policy binding list" command Based on the existing senlin command: senlin cluster-policy-list Change-Id: I4c0c66b84539bb8ecc01435e591173e8de6813ca Blueprint: senlin-support-python-openstackclient --- senlinclient/osc/v1/cluster_policy.py | 85 +++++++++++++++++++ .../tests/unit/osc/v1/test_cluster_policy.py | 74 ++++++++++++++++ setup.cfg | 1 + 3 files changed, 160 insertions(+) create mode 100644 senlinclient/osc/v1/cluster_policy.py create mode 100644 senlinclient/tests/unit/osc/v1/test_cluster_policy.py diff --git a/senlinclient/osc/v1/cluster_policy.py b/senlinclient/osc/v1/cluster_policy.py new file mode 100644 index 0000000..abdeeed --- /dev/null +++ b/senlinclient/osc/v1/cluster_policy.py @@ -0,0 +1,85 @@ +# 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. + +"""Clustering v1 cluster policy action implementations""" + +import logging + +from cliff import lister +from openstackclient.common import utils + +from senlinclient.common.i18n import _ +from senlinclient.common import utils as senlin_utils + + +class ClusterPolicyList(lister.Lister): + """List policies from cluster.""" + + log = logging.getLogger(__name__ + ".ClusterPolicyList") + + def get_parser(self, prog_name): + parser = super(ClusterPolicyList, self).get_parser(prog_name) + parser.add_argument( + '--filters', + metavar='', + help=_("Filter parameters to apply on returned results. " + "This can be specified multiple times, or once with " + "parameters separated by a semicolon. The valid filter " + "keys are: ['type', 'name']"), + action='append' + ) + parser.add_argument( + '--sort', + metavar='', + help=_("Sorting option which is a string containing a list of " + "keys separated by commas. Each key can be optionally " + "appended by a sort direction (:asc or :desc). The valid " + "sort keys are: ['type', 'name', 'created_at', " + "'updated_at']") + ) + parser.add_argument( + '--full-id', + default=False, + action="store_true", + help=_('Print full IDs in list') + ) + parser.add_argument( + 'cluster', + metavar='', + help=_('Name or ID of cluster to query on') + ) + return parser + + def take_action(self, parsed_args): + self.log.debug("take_action(%s)", parsed_args) + senlin_client = self.app.client_manager.clustering + + columns = ['policy_id', 'policy_name', 'policy_type', 'enabled'] + cluster = senlin_client.get_cluster(parsed_args.cluster) + queries = { + 'sort': parsed_args.sort, + } + + if parsed_args.filters: + queries.update(senlin_utils.format_parameters(parsed_args.filters)) + + policies = senlin_client.cluster_policies(cluster.id, **queries) + formatters = {} + if not parsed_args.full_id: + formatters = { + 'policy_id': lambda x: x[:8] + } + return ( + columns, + (utils.get_item_properties(p, columns, formatters=formatters) + for p in policies) + ) diff --git a/senlinclient/tests/unit/osc/v1/test_cluster_policy.py b/senlinclient/tests/unit/osc/v1/test_cluster_policy.py new file mode 100644 index 0000000..9109178 --- /dev/null +++ b/senlinclient/tests/unit/osc/v1/test_cluster_policy.py @@ -0,0 +1,74 @@ +# 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 senlinclient.osc.v1 import cluster_policy as osc_cluster_policy +from senlinclient.tests.unit.osc.v1 import fakes + + +class TestClusterPolicy(fakes.TestClusteringv1): + def setUp(self): + super(TestClusterPolicy, self).setUp() + self.mock_client = self.app.client_manager.clustering + + +class TestClusterPolicyList(TestClusterPolicy): + columns = ['policy_id', 'policy_name', 'policy_type', 'enabled'] + response = {"cluster_policies": [ + { + "cluster_id": "7d85f602-a948-4a30-afd4-e84f47471c15", + "cluster_name": "my_cluster", + "enabled": True, + "id": "06be3a1f-b238-4a96-a737-ceec5714087e", + "policy_id": "714fe676-a08f-4196-b7af-61d52eeded15", + "policy_name": "my_policy", + "policy_type": "senlin.policy.deletion-1.0" + }, + { + "cluster_id": "7d85f602-a948-4a30-afd4-e84f47471c15", + "cluster_name": "my_cluster", + "enabled": True, + "id": "abddc45e-ac31-4f90-93cc-db55a7d8dd6d", + "policy_id": "e026e09f-a3e9-4dad-a1b9-d7ba316026a1", + "policy_name": "my_policy", + "policy_type": "senlin.policy.scaling-1.0" + } + ]} + + args = { + 'sort': 'name:asc', + } + + def setUp(self): + super(TestClusterPolicyList, self).setUp() + self.cmd = osc_cluster_policy.ClusterPolicyList(self.app, None) + cluster = mock.Mock() + cluster.id = 'C1' + self.mock_client.get_cluster = mock.Mock( + return_value=cluster + ) + self.mock_client.cluster_policies = mock.Mock( + return_value=self.response) + + def test_cluster_policy_list(self): + arglist = ['--sort', 'name:asc', '--filter', 'name=my_policy', + 'my_cluster', '--full-id'] + parsed_args = self.check_parser(self.cmd, arglist, []) + columns, data = self.cmd.take_action(parsed_args) + self.mock_client.get_cluster.assert_called_with('my_cluster') + self.mock_client.cluster_policies.assert_called_with( + 'C1', + name='my_policy', + **self.args) + self.assertEqual(self.columns, columns) diff --git a/setup.cfg b/setup.cfg index 4c6fbc5..ec63d2d 100644 --- a/setup.cfg +++ b/setup.cfg @@ -30,6 +30,7 @@ openstack.cli.extension = clustering = senlinclient.osc.plugin openstack.clustering.v1 = + cluster_policy_binding_list = senlinclient.osc.v1.cluster_policy:ClusterPolicyList cluster_create = senlinclient.osc.v1.cluster:CreateCluster cluster_delete = senlinclient.osc.v1.cluster:DeleteCluster cluster_list = senlinclient.osc.v1.cluster:ListCluster