From f96709a59eae976b4f78ec82c287d0eb25f537b4 Mon Sep 17 00:00:00 2001 From: Jeremy Freudberg Date: Mon, 4 Jun 2018 15:26:18 -0400 Subject: [PATCH] [APIv2]Consolidate cluster creation endpoints Creation of a single cluster and creation of multiple clusters will now share an API endpoint (in the APIv2 case). More specifically, the original single-cluster endpoint will accept a `count` parameter in the request and the multiple-cluster endpoint has been removed. We can make this kind of change because APIv2 is still experimental. Also, when creating multiple clusters, the response will now contain all details about the clusters; previously, the response simply gave cluster IDs. Change-Id: I90faf4956a8ea4b4ae31a29382732771fdfddecb Story: 2002099 Task: 19777 --- ...luster-creation-apiv2-5d5aceeb2e97c702.yaml | 5 +++++ sahara/api/v2/clusters.py | 18 ++++++------------ sahara/service/api/v2/clusters.py | 4 ++-- sahara/service/validations/clusters.py | 7 +++++++ sahara/service/validations/clusters_schema.py | 13 ++++++------- .../tests/unit/service/api/v2/test_clusters.py | 6 ++++-- 6 files changed, 30 insertions(+), 23 deletions(-) create mode 100644 releasenotes/notes/consolidate-cluster-creation-apiv2-5d5aceeb2e97c702.yaml diff --git a/releasenotes/notes/consolidate-cluster-creation-apiv2-5d5aceeb2e97c702.yaml b/releasenotes/notes/consolidate-cluster-creation-apiv2-5d5aceeb2e97c702.yaml new file mode 100644 index 0000000000..ef24d083e0 --- /dev/null +++ b/releasenotes/notes/consolidate-cluster-creation-apiv2-5d5aceeb2e97c702.yaml @@ -0,0 +1,5 @@ +--- +features: + - The experimental APIv2 supports simultaneous creation of multiple clusters + only through POST /v2/clusters (using the `count` parameter). The POST + /v2/clusters/multiple endpoint has been removed. diff --git a/sahara/api/v2/clusters.py b/sahara/api/v2/clusters.py index 1d10647c91..7dce8b6e60 100644 --- a/sahara/api/v2/clusters.py +++ b/sahara/api/v2/clusters.py @@ -38,23 +38,17 @@ def clusters_list(): @rest.post('/clusters') @acl.enforce("data-processing:clusters:create") -@v.validate(v_c_schema.CLUSTER_SCHEMA_V2, v_c.check_cluster_create) +@v.validate(v_c_schema.CLUSTER_SCHEMA_V2, + v_c.check_one_or_multiple_clusters_create) def clusters_create(data): # renaming hadoop_version -> plugin_version # this can be removed once APIv1 is deprecated data['hadoop_version'] = data['plugin_version'] del data['plugin_version'] - return u.render(api.create_cluster(data).to_wrapped_dict()) - - -@rest.post('/clusters/multiple') -@acl.enforce("data-processing:clusters:create") -@v.validate( - v_c_schema.MULTIPLE_CLUSTER_SCHEMA_V2, v_c.check_multiple_clusters_create) -def clusters_create_multiple(data): - data['hadoop_version'] = data['plugin_version'] - del data['plugin_version'] - return u.render(api.create_multiple_clusters(data)) + if data.get('count', None) is not None: + return u.render(api.create_multiple_clusters(data)) + else: + return u.render(api.create_cluster(data).to_wrapped_dict()) @rest.put('/clusters/') diff --git a/sahara/service/api/v2/clusters.py b/sahara/service/api/v2/clusters.py index ac04f54602..632f50e27e 100644 --- a/sahara/service/api/v2/clusters.py +++ b/sahara/service/api/v2/clusters.py @@ -105,9 +105,9 @@ def create_multiple_clusters(values): cluster_dict['name'] = get_multiple_cluster_name(num_of_clusters, cluster_name, counter + 1) - cluster = _cluster_create(cluster_dict, plugin) + cluster = _cluster_create(cluster_dict, plugin).to_wrapped_dict() - clusters.append(cluster.id) + clusters.append(cluster) clusters_dict = {'clusters': clusters} return clusters_dict diff --git a/sahara/service/validations/clusters.py b/sahara/service/validations/clusters.py index d0eba0dcc8..c6c5523c40 100644 --- a/sahara/service/validations/clusters.py +++ b/sahara/service/validations/clusters.py @@ -40,6 +40,13 @@ def check_multiple_clusters_create(data, **kwargs): b.check_cluster_unique_name(cluster_name) +def check_one_or_multiple_clusters_create(data, **kwargs): + if data.get('count', None) is not None: + check_multiple_clusters_create(data, **kwargs) + else: + check_cluster_create(data, **kwargs) + + def _check_cluster_create(data): plugin_version = 'hadoop_version' if data.get('plugin_version'): diff --git a/sahara/service/validations/clusters_schema.py b/sahara/service/validations/clusters_schema.py index 110f64ad1c..9a83f00b9d 100644 --- a/sahara/service/validations/clusters_schema.py +++ b/sahara/service/validations/clusters_schema.py @@ -46,6 +46,12 @@ def _build_cluster_schema(api_version='v1'): "type": "string", "format": "uuid", }}) + + if api_version == 'v2': + cluster_schema['properties'].update({ + "count": { + "type": "integer" + }}) return cluster_schema @@ -59,13 +65,6 @@ MULTIPLE_CLUSTER_SCHEMA['properties'].update({ }}) MULTIPLE_CLUSTER_SCHEMA['required'].append('count') -MULTIPLE_CLUSTER_SCHEMA_V2 = copy.deepcopy(CLUSTER_SCHEMA_V2) -MULTIPLE_CLUSTER_SCHEMA_V2['properties'].update({ - "count": { - "type": "integer" - }}) -MULTIPLE_CLUSTER_SCHEMA_V2['required'].append('count') - CLUSTER_UPDATE_SCHEMA = { "type": "object", "properties": { diff --git a/sahara/tests/unit/service/api/v2/test_clusters.py b/sahara/tests/unit/service/api/v2/test_clusters.py index f4b9d4a855..7496d6ab21 100644 --- a/sahara/tests/unit/service/api/v2/test_clusters.py +++ b/sahara/tests/unit/service/api/v2/test_clusters.py @@ -123,8 +123,10 @@ class TestClusterApi(base.SaharaWithDbTestCase): MULTIPLE_CLUSTERS['count'] = 2 clusters = api.create_multiple_clusters(MULTIPLE_CLUSTERS) self.assertEqual(2, check_cluster.call_count) - result_cluster1 = api.get_cluster(clusters['clusters'][0]) - result_cluster2 = api.get_cluster(clusters['clusters'][1]) + result_cluster1 = api.get_cluster( + clusters['clusters'][0]['cluster']['id']) + result_cluster2 = api.get_cluster( + clusters['clusters'][1]['cluster']['id']) self.assertEqual(c_u.CLUSTER_STATUS_ACTIVE, result_cluster1.status) self.assertEqual(c_u.CLUSTER_STATUS_ACTIVE, result_cluster2.status) expected_count = {