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
This commit is contained in:
dixiaoli
2016-02-26 19:06:34 +08:00
parent 3bc2a22a52
commit e7c7e3b439
3 changed files with 160 additions and 0 deletions

View File

@@ -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='<key1=value1;key2=value2...>',
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='<sort_string>',
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='<cluster>',
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)
)

View File

@@ -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)

View File

@@ -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