Add OpenstackClient plugin for cluster policy create

This change implements the "openstack cluster policy create" command
  Based on the existing senlin command: senlin policy-create

Change-Id: I8f0e94d297f9df10e2af126610c8367cd585499e
Blueprint: senlin-support-python-openstackclient
This commit is contained in:
dixiaoli 2016-02-21 10:47:26 +08:00
parent 4643191c63
commit feca8767ec
4 changed files with 99 additions and 0 deletions

View File

@ -146,3 +146,37 @@ def _show_policy(senlin_client, policy_id):
]
return columns, utils.get_dict_properties(policy.to_dict(), columns,
formatters=formatters)
class CreatePolicy(show.ShowOne):
"""Create a policy."""
log = logging.getLogger(__name__ + ".CreatePolicy")
def get_parser(self, prog_name):
parser = super(CreatePolicy, self).get_parser(prog_name)
parser.add_argument(
'--spec-file',
metavar='<spec_file>',
required=True,
help=_('The spec file used to create the policy')
)
parser.add_argument(
'name',
metavar='<name>',
help=_('Name of the policy to create')
)
return parser
def take_action(self, parsed_args):
self.log.debug("take_action(%s)", parsed_args)
senlin_client = self.app.client_manager.clustering
spec = senlin_utils.get_spec_content(parsed_args.spec_file)
attrs = {
'name': parsed_args.name,
'spec': spec,
}
policy = senlin_client.create_policy(**attrs)
return _show_policy(senlin_client, policy.id)

View File

@ -0,0 +1,8 @@
type: senlin.policy.deletion
version: 1.0
description: A policy for choosing victim node(s) from a cluster for deletion.
properties:
criteria: OLDEST_FIRST
destroy_after_deletion: True
grace_period: 60
reduce_desired_capacity: False

View File

@ -182,3 +182,59 @@ class TestPolicyShow(TestPolicy):
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)
class TestPolicyCreate(TestPolicy):
spec_path = 'senlinclient/tests/test_specs/deletion_policy.yaml'
response = {"policy": {
"created_at": "2016-02-21T02:38:36",
"data": {},
"domain": 'null',
"id": "9f779ddf-744e-48bd-954c-acef7e11116c",
"name": "my_policy",
"project": "5f1cc92b578e4e25a3b284179cf20a9b",
"spec": {
"description": "A policy for choosing victim node(s) from a "
"cluster for deletion.",
"properties": {
"criteria": "OLDEST_FIRST",
"destroy_after_deletion": True,
"grace_period": 60,
"reduce_desired_capacity": False
},
"type": "senlin.policy.deletion",
"version": 1.0
},
"type": "senlin.policy.deletion-1.0",
"updated_at": 'null',
"user": "2d7aca950f3e465d8ef0c81720faf6ff"
}}
defaults = {
"name": "my_policy",
"spec": {
"version": 1,
"type": "senlin.policy.deletion",
"description": "A policy for choosing victim node(s) from a "
"cluster for deletion.",
"properties": {
"destroy_after_deletion": True,
"grace_period": 60,
"reduce_desired_capacity": False,
"criteria": "OLDEST_FIRST"
}
}
}
def setUp(self):
super(TestPolicyCreate, self).setUp()
self.cmd = osc_policy.CreatePolicy(self.app, None)
self.mock_client.create_policy = mock.Mock(
return_value=sdk_policy.Policy(None, self.response))
self.mock_client.get_policy = mock.Mock(
return_value=sdk_policy.Policy(None, self.response))
def test_policy_create_defaults(self):
arglist = ['my_policy', '--spec-file', self.spec_path]
parsed_args = self.check_parser(self.cmd, arglist, [])
self.cmd.take_action(parsed_args)
self.mock_client.create_policy.assert_called_with(**self.defaults)

View File

@ -35,6 +35,7 @@ openstack.clustering.v1 =
cluster_node_list = senlinclient.osc.v1.node:ListNode
cluster_node_show = senlinclient.osc.v1.node:ShowNode
cluster_node_update = senlinclient.osc.v1.node:UpdateNode
cluster_policy_create = senlinclient.osc.v1.policy:CreatePolicy
cluster_policy_list = senlinclient.osc.v1.policy:ListPolicy
cluster_policy_show = senlinclient.osc.v1.policy:ShowPolicy
cluster_profile_create = senlinclient.osc.v1.profile:CreateProfile