Merge "Add import of cluster templates to GUI."

This commit is contained in:
Zuul 2018-01-04 14:13:05 +00:00 committed by Gerrit Code Review
commit 4be68b6d44
9 changed files with 358 additions and 0 deletions

View File

@ -0,0 +1,219 @@
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from django.utils.translation import ugettext_lazy as _
from oslo_serialization import jsonutils as json
from horizon import exceptions
from horizon import forms
from saharaclient.api import base as api_base
from sahara_dashboard.api import sahara as saharaclient
import sahara_dashboard.content.data_processing. \
utils.workflow_helpers as whelpers
from sahara_dashboard import utils
BASE_IMAGE_URL = "horizon:project:data_processing.clusters:register"
class ImportClusterTemplateFileForm(forms.SelfHandlingForm):
class Meta(object):
name = _("Import Cluster Template")
def __init__(self, *args, **kwargs):
self.next_view = kwargs.pop("next_view")
super(ImportClusterTemplateFileForm, self).__init__(
*args, **kwargs)
template_upload = forms.FileField(
label=_("Template File"),
required=True)
def handle(self, request, data):
kwargs = {"template_upload": data["template_upload"]}
request.method = "GET"
return self.next_view.as_view()(request, **kwargs)
class ImportClusterTemplateNameForm(forms.SelfHandlingForm):
class Meta(object):
name = _("Import Cluster Template")
template_upload = forms.CharField(
widget=forms.widgets.HiddenInput)
name = forms.CharField(label=_("Name"),
required=False,
help_text=_("Name must be provided "
"either here or in the template. If "
"provided in both places, this one "
"will be used."))
def __init__(self, *args, **kwargs):
try:
request = args[0]
template_string = ""
self.next_view = kwargs.pop("next_view")
if "template_upload" in kwargs:
template_upload = kwargs.pop("template_upload")
super(ImportClusterTemplateNameForm, self).__init__(
*args, **kwargs)
template_string = template_upload.read()
self.fields["template_upload"].initial = template_string
else:
super(ImportClusterTemplateNameForm, self).__init__(
*args, **kwargs)
except (ValueError, KeyError):
raise exceptions.BadRequest(_("Could not parse template"))
except Exception:
exceptions.handle(request)
def handle(self, request, data):
template = data["template_upload"]
if data["name"]:
template = json.loads(template)
template["cluster_template"]["name"] = data["name"]
template = json.dumps(template)
kwargs = {"template_upload": template}
request.method = "GET"
return self.next_view.as_view()(request, **kwargs)
class ImportClusterTemplateNodegroupsForm(forms.SelfHandlingForm):
class Meta(object):
name = _("Import Cluster Template")
template_upload = forms.CharField(
widget=forms.widgets.HiddenInput)
hidden_nodegroups_field = forms.CharField(
required=False,
widget=forms.HiddenInput(attrs={"class": "hidden_nodegroups_field"}))
forms_ids = forms.CharField(
required=False,
widget=forms.HiddenInput())
def __init__(self, *args, **kwargs):
try:
request = args[0]
template_string = ""
if "template_upload" in kwargs:
template_string = kwargs.pop("template_upload")
super(ImportClusterTemplateNodegroupsForm, self).__init__(
*args, **kwargs)
self.fields["template_upload"].initial = template_string
else:
super(ImportClusterTemplateNodegroupsForm, self).__init__(
*args, **kwargs)
template_string = self.data["template_upload"]
template_json = json.loads(template_string)
template_json = template_json["cluster_template"]
req = request.GET.copy()
req.update(request.POST)
plugin = template_json["plugin_name"]
version = template_json["hadoop_version"]
if not plugin or not version:
self.templates = saharaclient.nodegroup_template_find(request)
else:
self.templates = saharaclient.nodegroup_template_find(
request, plugin_name=plugin, hadoop_version=version)
deletable = req.get("deletable", dict())
if "forms_ids" in req:
self.groups = []
for id in json.loads(req["forms_ids"]):
group_name = "group_name_" + str(id)
template_id = "template_id_" + str(id)
count = "count_" + str(id)
serialized = "serialized_" + str(id)
self.groups.append({"name": req[group_name],
"template_id": req[template_id],
"count": req[count],
"id": id,
"deletable": deletable.get(
req[group_name], "true"),
"serialized": req[serialized]})
whelpers.build_node_group_fields(self,
group_name,
template_id,
count,
serialized)
except (ValueError, KeyError):
raise exceptions.BadRequest(_("Could not parse template"))
except Exception:
exceptions.handle(request)
def handle(self, request, data):
try:
template = data["template_upload"]
template = json.loads(template)
template = template["cluster_template"]
if "name" not in template.keys():
return False
if "neutron_management_network" in template:
template["net_id"] = (
template.pop("neutron_management_network"))
# default_image_id is not supported by the client now
if "default_image_id" in template:
template.pop("default_image_id")
node_groups = []
ids = json.loads(data['forms_ids'])
for id in ids:
name = data['group_name_' + str(id)]
template_id = data['template_id_' + str(id)]
count = data['count_' + str(id)]
raw_ng = data.get("serialized_" + str(id))
if raw_ng and raw_ng != 'null':
ng = json.loads(utils.deserialize(str(raw_ng)))
else:
ng = dict()
ng["name"] = name
ng["count"] = count
if template_id and template_id != u'None':
ng["node_group_template_id"] = template_id
node_groups.append(ng)
template["node_groups"] = node_groups
saharaclient.cluster_template_create(request, **template)
return True
except api_base.APIException as e:
self.error_description = str(e)
return False
except Exception as e:
if isinstance(e, TypeError):
raise exceptions.BadRequest(
_("Template JSON contained invalid key"))
else:
raise exceptions.BadRequest(_("Could not parse template"))

View File

@ -114,6 +114,15 @@ class CreateClusterTemplate(tables.LinkAction):
icon = "plus"
class ImportClusterTemplate(tables.LinkAction):
name = "import"
verbose_name = _("Import Template")
url = ("horizon:project:data_processing.clusters:"
"import-cluster-template-file")
classes = ("ajax-modal",)
icon = "plus"
class ConfigureClusterTemplate(tables.LinkAction):
name = "configure"
verbose_name = _("Configure Cluster Template")
@ -176,6 +185,7 @@ class ClusterTemplatesTable(sahara_table.SaharaPaginateTabbedTable):
name = "cluster_templates"
verbose_name = _("Cluster Templates")
table_actions = (CreateClusterTemplate,
ImportClusterTemplate,
ConfigureClusterTemplate,
DeleteTemplate,
ClusterTemplatesFilterAction,)

View File

@ -12,9 +12,11 @@
# limitations under the License.
from django.core.urlresolvers import reverse
from django.core.urlresolvers import reverse_lazy
from django.utils.translation import ugettext_lazy as _
from horizon import exceptions
from horizon import forms
from horizon import tabs
from horizon.utils import memoized
from horizon import workflows
@ -30,6 +32,8 @@ import sahara_dashboard.content.data_processing.clusters. \
cluster_templates.workflows.create as create_flow
import sahara_dashboard.content.data_processing.clusters. \
cluster_templates.workflows.edit as edit_flow
import sahara_dashboard.content.data_processing.clusters. \
cluster_templates.forms.import_forms as import_forms
class ClusterTemplateDetailsView(tabs.TabView):
@ -122,3 +126,56 @@ class EditClusterTemplateView(CopyClusterTemplateView):
success_url = ("horizon:project:data_processing.clusters"
":index")
template_name = "cluster_templates/configure.html"
class ImportClusterTemplateFileView(forms.ModalFormView):
template_name = "cluster_templates/import.html"
form_class = import_forms.ImportClusterTemplateFileForm
submit_label = _("Next")
submit_url = reverse_lazy("horizon:project:data_processing."
"clusters:import-cluster-template-file")
success_url = reverse_lazy("horizon:project:data_processing."
"clusters:import-cluster-template-name")
page_title = _("Import Cluster Template")
def get_form_kwargs(self):
kwargs = super(ImportClusterTemplateFileView, self).get_form_kwargs()
kwargs['next_view'] = ImportClusterTemplateNameView
return kwargs
class ImportClusterTemplateNameView(forms.ModalFormView):
template_name = "cluster_templates/import.html"
form_class = import_forms.ImportClusterTemplateNameForm
submit_label = _("Next")
submit_url = reverse_lazy("horizon:project:data_processing."
"clusters:import-cluster-template-name")
success_url = reverse_lazy("horizon:project:data_processing."
"clusters:import-cluster-template-nodegroups")
page_title = _("Import Cluster Template")
def get_form_kwargs(self):
kwargs = super(ImportClusterTemplateNameView, self).get_form_kwargs()
kwargs['next_view'] = ImportClusterTemplateNodegroupsView
if 'template_upload' in self.kwargs:
kwargs['template_upload'] = self.kwargs['template_upload']
return kwargs
class ImportClusterTemplateNodegroupsView(forms.ModalFormView):
template_name = "cluster_templates/import_nodegroups.html"
# template_name = "some_random_stuff.html"
form_class = import_forms.ImportClusterTemplateNodegroupsForm
submit_label = _("Import")
submit_url = reverse_lazy("horizon:project:data_processing."
"clusters:import-cluster-template-nodegroups")
success_url = reverse_lazy("horizon:project:data_processing."
"clusters:index")
page_title = _("Import Cluster Template")
def get_form_kwargs(self):
kwargs = super(ImportClusterTemplateNodegroupsView,
self).get_form_kwargs()
if 'template_upload' in self.kwargs:
kwargs['template_upload'] = self.kwargs['template_upload']
return kwargs

View File

@ -0,0 +1,24 @@
{% extends "horizon/common/_modal_form.html" %}
{% load i18n %}
{% block form_id %}import-cluster-template{% endblock %}
{% block form_action %}{{ submit_url }}{% endblock %}
{% block form_attrs %}enctype="multipart/form-data"{% endblock %}
{% block modal-header %}{{ page_title }}{% endblock %}
{% block modal-body %}
<div class="left">
<fieldset>
{% include "horizon/common/_form_fields.html" %}
</fieldset>
</div>
{% endblock %}
{% block modal-footer %}
<input class="btn btn-primary pull-right" id="upload_file_btn" type="submit" value="{{ submit_label }}"/>
<a href="{% url 'horizon:project:data_processing.clusters:index' %}"
class="btn btn-default cancel">{% trans "Cancel" %}</a>
{% endblock %}

View File

@ -0,0 +1,25 @@
{% extends "horizon/common/_modal_form.html" %}
{% load i18n %}
{% block form_id %}import-cluster-template{% endblock %}
{% block form_action %}{{ submit_url }}{% endblock %}
{% block form_attrs %}enctype="multipart/form-data"{% endblock %}
{% block modal-header %}{{ page_title }}{% endblock %}
{% block modal-body %}
<div>
<fieldset>
{% include "horizon/common/_form_fields.html" %}
{% include "cluster_templates/cluster_node_groups_template.html" %}
</fieldset>
</div>
{% endblock %}
{% block modal-footer %}
<input class="btn btn-primary pull-right" id="upload_file_btn" type="submit" value="{{ submit_label }}"/>
<a href="{% url 'horizon:project:data_processing.clusters:index' %}"
class="btn btn-default cancel">{% trans "Cancel" %}</a>
{% endblock %}

View File

@ -0,0 +1,7 @@
{% extends 'base.html' %}
{% load i18n %}
{% block title %}{% trans "Import Cluster Template" %}{% endblock %}
{% block main %}
{% include 'cluster_templates/_import.html' %}
{% endblock %}

View File

@ -0,0 +1,7 @@
{% extends 'base.html' %}
{% load i18n %}
{% block title %}{% trans "Import Cluster Template" %}{% endblock %}
{% block main %}
{% include 'cluster_templates/_import.html' %}
{% endblock %}

View File

@ -45,6 +45,15 @@ urlpatterns = [url(r'^$', views.IndexView.as_view(), name='index'),
url(r'^configure-cluster-template$',
ct_views.ConfigureClusterTemplateView.as_view(),
name='configure-cluster-template'),
url(r'^import-cluster-template-file$',
ct_views.ImportClusterTemplateFileView.as_view(),
name='import-cluster-template-file'),
url(r'^import-cluster-template-name$',
ct_views.ImportClusterTemplateNameView.as_view(),
name='import-cluster-template-name'),
url(r'^import-cluster-template-nodegroups$',
ct_views.ImportClusterTemplateNodegroupsView.as_view(),
name='import-cluster-template-nodegroups'),
url(r'^configure-nodegroup-template$',
ngt_views.ConfigureNodegroupTemplateView.as_view(),
name='configure-nodegroup-template'),