From ce545997f3925270d74d85522969134a378f7587 Mon Sep 17 00:00:00 2001 From: dixiaoli Date: Thu, 25 Feb 2016 00:06:10 +0800 Subject: [PATCH] Add OSC plugin for openstack cluster update This change implements the "openstack cluster update" command Based on the existing senlin command: senlin cluster-update Change-Id: Ie3ff230919372680bd796a1da57a4a8fb841cdf7 Blueprint: senlin-support-python-openstackclient --- senlinclient/osc/v1/cluster.py | 52 ++++++++++++++++ .../tests/unit/osc/v1/test_cluster.py | 61 +++++++++++++++++++ setup.cfg | 1 + 3 files changed, 114 insertions(+) diff --git a/senlinclient/osc/v1/cluster.py b/senlinclient/osc/v1/cluster.py index 409540d..a904edd 100644 --- a/senlinclient/osc/v1/cluster.py +++ b/senlinclient/osc/v1/cluster.py @@ -210,3 +210,55 @@ class CreateCluster(show.ShowOne): cluster = senlin_client.create_cluster(**attrs) return _show_cluster(senlin_client, cluster.id) + + +class UpdateCluster(show.ShowOne): + """Update the cluster.""" + + log = logging.getLogger(__name__ + ".UpdateCluster") + + def get_parser(self, prog_name): + parser = super(UpdateCluster, self).get_parser(prog_name) + parser.add_argument( + '--profile', + metavar='', + help=_('ID or name of new profile to use') + ) + parser.add_argument( + '--timeout', + metavar='', + help=_('New timeout (in seconds) value for the cluster') + ) + parser.add_argument( + '--metadata', + metavar='', + help=_('Metadata values to be attached to the cluster. ' + 'This can be specified multiple times, or once with ' + 'key-value pairs separated by a semicolon'), + action='append' + ) + parser.add_argument( + '--name', + metavar='', + help=_('New name for the cluster to update') + ) + parser.add_argument( + 'cluster', + metavar='', + help=_('Name or ID of cluster to be updated') + ) + return parser + + def take_action(self, parsed_args): + self.log.debug("take_action(%s)", parsed_args) + senlin_client = self.app.client_manager.clustering + cluster = senlin_client.get_cluster(parsed_args.cluster) + attrs = { + 'name': parsed_args.name, + 'profile_id': parsed_args.profile, + 'metadata': senlin_utils.format_parameters(parsed_args.metadata), + 'timeout': parsed_args.timeout, + } + + senlin_client.update_cluster(cluster.id, **attrs) + return _show_cluster(senlin_client, cluster.id) diff --git a/senlinclient/tests/unit/osc/v1/test_cluster.py b/senlinclient/tests/unit/osc/v1/test_cluster.py index 9b349e6..2a22158 100644 --- a/senlinclient/tests/unit/osc/v1/test_cluster.py +++ b/senlinclient/tests/unit/osc/v1/test_cluster.py @@ -259,3 +259,64 @@ class TestClusterCreate(TestCluster): parsed_args = self.check_parser(self.cmd, arglist, []) self.cmd.take_action(parsed_args) self.mock_client.create_cluster.assert_called_with(**kwargs) + + +class TestClusterUpdate(TestCluster): + response = {"cluster": { + "created_at": "2015-02-11T15:13:20", + "data": {}, + "desired_capacity": 0, + "domain": 'null', + "id": "45edadcb-c73b-4920-87e1-518b2f29f54b", + "init_time": "2015-02-10T14:26:10", + "max_size": -1, + "metadata": {}, + "min_size": 0, + "name": "test_cluster", + "nodes": [], + "policies": [], + "profile_id": "edc63d0a-2ca4-48fa-9854-27926da76a4a", + "profile_name": "mystack", + "project": "6e18cc2bdbeb48a5b3cad2dc499f6804", + "status": "INIT", + "status_reason": "Initializing", + "timeout": 3600, + "updated_at": 'null', + "user": "5e5bf8027826429c96af157f68dc9072" + }} + + defaults = { + "metadata": { + "nk1": "nv1", + "nk2": "nv2", + }, + "name": 'new_cluster', + "profile_id": 'new_profile', + "timeout": "30" + } + + def setUp(self): + super(TestClusterUpdate, self).setUp() + self.cmd = osc_cluster.UpdateCluster(self.app, None) + self.mock_client.update_cluster = mock.Mock( + return_value=sdk_cluster.Cluster(None, self.response)) + self.mock_client.get_cluster = mock.Mock( + return_value=sdk_cluster.Cluster(None, self.response)) + + def test_cluster_update_defaults(self): + arglist = ['--name', 'new_cluster', '--metadata', 'nk1=nv1;nk2=nv2', + '--profile', 'new_profile', '--timeout', '30', 'c6b8b252'] + parsed_args = self.check_parser(self.cmd, arglist, []) + self.cmd.take_action(parsed_args) + self.mock_client.update_cluster.assert_called_with(None, + **self.defaults) + + def test_cluster_update_not_found(self): + arglist = ['--name', 'new_cluster', '--metadata', 'nk1=nv1;nk2=nv2', + '--profile', 'new_profile', 'c6b8b252'] + parsed_args = self.check_parser(self.cmd, arglist, []) + self.mock_client.get_cluster.side_effect = sdk_exc.ResourceNotFound() + error = self.assertRaises(sdk_exc.ResourceNotFound, + self.cmd.take_action, + parsed_args) + self.assertIn('ResourceNotFound: ResourceNotFound', str(error)) diff --git a/setup.cfg b/setup.cfg index 107c591..5298076 100644 --- a/setup.cfg +++ b/setup.cfg @@ -33,6 +33,7 @@ openstack.clustering.v1 = cluster_create = senlinclient.osc.v1.cluster:CreateCluster cluster_list = senlinclient.osc.v1.cluster:ListCluster cluster_show = senlinclient.osc.v1.cluster:ShowCluster + cluster_update = senlinclient.osc.v1.cluster:UpdateCluster cluster_node_create = senlinclient.osc.v1.node:CreateNode cluster_node_delete = senlinclient.osc.v1.node:DeleteNode cluster_node_list = senlinclient.osc.v1.node:ListNode