From c9ff30dfdb5f520013692d2e3caddcd20428d8b9 Mon Sep 17 00:00:00 2001 From: Telles Nobrega Date: Mon, 29 Jun 2015 13:45:14 -0300 Subject: [PATCH] Allow multiple clusters creation This patch allows the user to use the client to create multiple clusters Change-Id: I3b48d6ede758e86e8cbc5bbccc069c4c24b53a44 Partially-implements: bp simultaneously-creating-multiple-clusters --- saharaclient/api/clusters.py | 14 ++++++++++++-- saharaclient/api/shell.py | 5 +++++ saharaclient/tests/unit/test_clusters.py | 21 +++++++++++++++++++++ 3 files changed, 38 insertions(+), 2 deletions(-) diff --git a/saharaclient/api/clusters.py b/saharaclient/api/clusters.py index 20af3e42..130143db 100644 --- a/saharaclient/api/clusters.py +++ b/saharaclient/api/clusters.py @@ -29,7 +29,7 @@ class ClusterManager(base.ResourceManager): cluster_template_id=None, default_image_id=None, is_transient=None, description=None, cluster_configs=None, node_groups=None, user_keypair_id=None, - anti_affinity=None, net_id=None): + anti_affinity=None, net_id=None, count=None): data = { 'name': name, @@ -37,6 +37,12 @@ class ClusterManager(base.ResourceManager): 'hadoop_version': hadoop_version, } + # Checking if count is greater than 1, otherwise we set it to None + # so the created dict in the _copy_if_defined method does not contain + # the count parameter. + if count and count <= 1: + count = None + self._copy_if_defined(data, cluster_template_id=cluster_template_id, is_transient=is_transient, @@ -46,7 +52,11 @@ class ClusterManager(base.ResourceManager): node_groups=node_groups, user_keypair_id=user_keypair_id, anti_affinity=anti_affinity, - neutron_management_network=net_id) + neutron_management_network=net_id, + count=count) + + if count: + return self._create('/clusters/multiple', data) return self._create('/clusters', data, 'cluster') diff --git a/saharaclient/api/shell.py b/saharaclient/api/shell.py index 3b46057b..5a3812cb 100644 --- a/saharaclient/api/shell.py +++ b/saharaclient/api/shell.py @@ -317,6 +317,10 @@ def do_cluster_show(cs, args): default=sys.stdin, type=argparse.FileType('r'), help='JSON representation of cluster.') +@utils.arg('--count', + default=1, + type=int, + help='Number of clusters to be created.') def do_cluster_create(cs, args): """Create a cluster.""" # TODO(mattf): improve template validation, e.g. template w/o name key @@ -326,6 +330,7 @@ def do_cluster_create(cs, args): # create w/ **template. It may be desirable to simple change # clusters.create in the future. remap = {'neutron_management_network': 'net_id'} + template['count'] = args.count _filter_call_args(template, cs.clusters.create, remap) _show_cluster(cs.clusters.create(**template)) diff --git a/saharaclient/tests/unit/test_clusters.py b/saharaclient/tests/unit/test_clusters.py index 37c49ebe..1ee1b7ad 100644 --- a/saharaclient/tests/unit/test_clusters.py +++ b/saharaclient/tests/unit/test_clusters.py @@ -26,6 +26,14 @@ class ClusterTest(base.BaseTestCase): 'cluster_template_id': 'id', } + body_with_count = { + 'name': 'name', + 'plugin_name': 'fake', + 'hadoop_version': '0.1', + 'cluster_template_id': 'id', + 'count': 2 + } + body_with_progress = { 'name': 'name', 'plugin_name': 'fake', @@ -62,6 +70,19 @@ class ClusterTest(base.BaseTestCase): self.assertIsInstance(resp, cl.Cluster) self.assertFields(body, resp) + def test_create_multiple_clusters(self): + url = self.URL + '/clusters/multiple' + self.responses.post(url, status_code=202, + json={'clusters': ['id1', 'id2']}) + + resp = self.client.clusters.create(**self.body_with_count) + + self.assertEqual(url, self.responses.last_request.url) + self.assertEqual(self.body_with_count, + json.loads(self.responses.last_request.body)) + self.assertIsInstance(resp, cl.Cluster) + self.assertFields({'clusters': ['id1', 'id2']}, resp) + def test_clusters_list(self): url = self.URL + '/clusters' self.responses.get(url, json={'clusters': [self.body]})