From 84b77958a1eff2bbcfc4840d9fed2e4aed0eeafc Mon Sep 17 00:00:00 2001 From: Jeremy Freudberg Date: Sun, 3 Dec 2017 02:51:25 +0000 Subject: [PATCH] [APIv2]Add ability to export templates to APIv2 The feature should be supported in both API versions. bp v2-api-experimental-impl bp portable-node-group-and-cluster-templates Change-Id: I102fb2ad16d0256e3a9aa364586332a13826cc90 Co-Authored-By: Iwona Kotlarska --- sahara/api/v2/cluster_templates.py | 23 +++++++++++++++++ sahara/api/v2/node_group_templates.py | 25 +++++++++++++++++++ sahara/service/api/v2/cluster_templates.py | 4 +++ sahara/service/api/v2/node_group_templates.py | 4 +++ 4 files changed, 56 insertions(+) diff --git a/sahara/api/v2/cluster_templates.py b/sahara/api/v2/cluster_templates.py index 3db2b7e3de..91347e85b7 100644 --- a/sahara/api/v2/cluster_templates.py +++ b/sahara/api/v2/cluster_templates.py @@ -72,3 +72,26 @@ def cluster_templates_update(cluster_template_id, data): def cluster_templates_delete(cluster_template_id): api.terminate_cluster_template(cluster_template_id) return u.render() + + +def _cluster_template_export_helper(template): + template.pop('id') + template.pop('updated_at') + template.pop('created_at') + template.pop('tenant_id') + template.pop('is_default') + template['default_image_id'] = '{default_image_id}' + template['node_groups'] = '{node_groups}' + + +@rest.get('/cluster-templates//export') +@acl.enforce("data-processing:cluster-templates:get") +@v.check_exists(api.get_cluster_template, 'cluster_template_id') +def cluster_template_export(cluster_template_id): + content = u.to_wrapped_dict_no_render( + api.export_cluster_template, cluster_template_id) + _cluster_template_export_helper(content['cluster_template']) + res = u.render(content) + res.headers.add('Content-Disposition', 'attachment', + filename='cluster_template.json') + return res diff --git a/sahara/api/v2/node_group_templates.py b/sahara/api/v2/node_group_templates.py index b7761061f9..7a53a858b4 100644 --- a/sahara/api/v2/node_group_templates.py +++ b/sahara/api/v2/node_group_templates.py @@ -74,3 +74,28 @@ def node_group_templates_update(node_group_template_id, data): def node_group_templates_delete(node_group_template_id): api.terminate_node_group_template(node_group_template_id) return u.render() + + +def _node_group_template_export_helper(template): + template.pop('id') + template.pop('updated_at') + template.pop('created_at') + template.pop('tenant_id') + template.pop('is_default') + template['flavor_id'] = '{flavor_id}' + template['security_groups'] = '{security_groups}' + template['image_id'] = '{image_id}' + template['floating_ip_pool'] = '{floating_ip_pool}' + + +@rest.get('/node-group-templates//export') +@acl.enforce("data-processing:node-group-templates:get") +@v.check_exists(api.get_node_group_template, 'node_group_template_id') +def node_group_template_export(node_group_template_id): + content = u.to_wrapped_dict_no_render( + api.export_node_group_template, node_group_template_id) + _node_group_template_export_helper(content['node_group_template']) + res = u.render(content) + res.headers.add('Content-Disposition', 'attachment', + filename='node_group_template.json') + return res diff --git a/sahara/service/api/v2/cluster_templates.py b/sahara/service/api/v2/cluster_templates.py index df8e647efd..f62b35b483 100644 --- a/sahara/service/api/v2/cluster_templates.py +++ b/sahara/service/api/v2/cluster_templates.py @@ -41,3 +41,7 @@ def terminate_cluster_template(id): def update_cluster_template(id, values): return conductor.cluster_template_update(context.ctx(), id, values) + + +def export_cluster_template(id): + return conductor.cluster_template_get(context.ctx(), id) diff --git a/sahara/service/api/v2/node_group_templates.py b/sahara/service/api/v2/node_group_templates.py index 1262879beb..c4a5d9dce4 100644 --- a/sahara/service/api/v2/node_group_templates.py +++ b/sahara/service/api/v2/node_group_templates.py @@ -41,3 +41,7 @@ def terminate_node_group_template(id): def update_node_group_template(id, values): return conductor.node_group_template_update(context.ctx(), id, values) + + +def export_node_group_template(id): + return conductor.node_group_template_get(context.ctx(), id)