From 261a964698bdf3f5a73808416b9968da69b9c4de Mon Sep 17 00:00:00 2001 From: Andrey Pavlov Date: Mon, 7 Dec 2015 14:26:05 +0300 Subject: [PATCH] Fixing cluster scaling via CLI If a cluster had a node group with a name different than a node group template name and you wanted to resize it via CLI, cluster wasn't able to be scaled. Change-Id: I554d9c07741dcb2077f9ecd0f6b5dbce65c6a464 Closes-bug: #1523455 --- saharaclient/osc/v1/clusters.py | 24 ++++++++------- .../tests/unit/osc/v1/test_clusters.py | 29 +++++++++++++------ 2 files changed, 33 insertions(+), 20 deletions(-) diff --git a/saharaclient/osc/v1/clusters.py b/saharaclient/osc/v1/clusters.py index 74af6ceb..2a887ce0 100644 --- a/saharaclient/osc/v1/clusters.py +++ b/saharaclient/osc/v1/clusters.py @@ -462,11 +462,11 @@ class ScaleCluster(show.ShowOne): help="Name or ID of the cluster", ) parser.add_argument( - '--node-groups', + '--instances', nargs='+', - metavar='', - help='Node groups and number of their instances to be scale to ' - '[REQUIRED if JSON is not provided]' + metavar='', + help='Node group templates and number of their instances to be ' + 'scale to [REQUIRED if JSON is not provided]' ) parser.add_argument( '--json', @@ -507,19 +507,21 @@ class ScaleCluster(show.ShowOne): "resize_node_groups": [] } scale_node_groups = dict( - map(lambda x: x.split(':', 1), parsed_args.node_groups)) - cluster_node_groups = [ng['name'] for ng in cluster.node_groups] + map(lambda x: x.split(':', 1), parsed_args.instances)) + cluster_ng_map = { + ng['node_group_template_id']: ng['name'] for ng + in cluster.node_groups} for name, count in scale_node_groups.items(): - ng = utils.get_resource(client.node_group_templates, name) - if ng.name in cluster_node_groups: + ngt = utils.get_resource(client.node_group_templates, name) + if ngt.id in cluster_ng_map: scale_object["resize_node_groups"].append({ - "name": ng.name, + "name": cluster_ng_map[ngt.id], "count": int(count) }) else: scale_object["add_node_groups"].append({ - "node_group_template_id": ng.id, - "name": ng.name, + "node_group_template_id": ngt.id, + "name": ngt.name, "count": int(count) }) if not scale_object['add_node_groups']: diff --git a/saharaclient/tests/unit/osc/v1/test_clusters.py b/saharaclient/tests/unit/osc/v1/test_clusters.py index b88738c6..b7f6a6a5 100644 --- a/saharaclient/tests/unit/osc/v1/test_clusters.py +++ b/saharaclient/tests/unit/osc/v1/test_clusters.py @@ -35,7 +35,8 @@ CLUSTER_INFO = { "id": "ng_id", "name": "fakeng", "plugin_name": 'fake', - "hadoop_version": '0.1' + "hadoop_version": '0.1', + "node_group_template_id": 'ngt_id' } ], "hadoop_version": "0.1", @@ -59,6 +60,11 @@ CT_INFO = { "id": "ct_id" } +NGT_INFO = { + 'id': 'ngt_id', + 'name': 'fakeng' +} + class TestClusters(fakes.TestDataProcessing): def setUp(self): @@ -404,10 +410,11 @@ class TestScaleCluster(TestClusters): def test_cluster_scale_resize(self): self.ngt_mock.find_unique.return_value = api_ngt.NodeGroupTemplate( - None, CLUSTER_INFO['node_groups'][0]) - arglist = ['fake', '--node-groups', 'fakeng:1'] + None, NGT_INFO) + arglist = ['fake', '--instances', 'fakeng:1'] - verifylist = [('cluster', 'fake'), ('node_groups', ['fakeng:1'])] + verifylist = [('cluster', 'fake'), + ('instances', ['fakeng:1'])] parsed_args = self.check_parser(self.cmd, arglist, verifylist) @@ -416,7 +423,10 @@ class TestScaleCluster(TestClusters): # Check that correct arguments were passed self.cl_mock.scale.assert_called_once_with( 'cluster_id', - {'resize_node_groups': [{'count': 1, 'name': 'fakeng'}]}) + {'resize_node_groups': [ + {'count': 1, + 'name': 'fakeng'}]} + ) # Check that columns are correct expected_columns = ('Anti affinity', 'Cluster template id', @@ -438,18 +448,19 @@ class TestScaleCluster(TestClusters): new_ng = {'name': 'new', 'id': 'new_id'} self.ngt_mock.find_unique.return_value = api_ngt.NodeGroupTemplate( None, new_ng) - arglist = ['fake', '--node-groups', 'fakeng:1'] + arglist = ['fake', '--instances', 'new:1'] - verifylist = [('cluster', 'fake'), ('node_groups', ['fakeng:1'])] + verifylist = [('cluster', 'fake'), ('instances', ['new:1'])] parsed_args = self.check_parser(self.cmd, arglist, verifylist) - columns, data = self.cmd.take_action(parsed_args) + self.cmd.take_action(parsed_args) # Check that correct arguments were passed self.cl_mock.scale.assert_called_once_with( 'cluster_id', {'add_node_groups': [ - {'count': 1, 'node_group_template_id': 'new_id', + {'count': 1, + 'node_group_template_id': 'new_id', 'name': 'new'} ]})