From dadf4737cefb6b31398988943e31fc771c14cc31 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn?= Date: Thu, 11 Jun 2015 13:06:59 +0200 Subject: [PATCH] Feature #3748: Oneflow template instantiate --- .../public/app/tabs/oneflow-templates-tab.js | 3 +- .../app/tabs/oneflow-templates-tab/actions.js | 43 ++-- .../form-panels/instantiate.js | 232 ++++++++++++++++++ .../form-panels/instantiate/formPanelId.js | 3 + .../form-panels/instantiate/html.hbs | 21 ++ 5 files changed, 286 insertions(+), 16 deletions(-) create mode 100644 src/sunstone/public/app/tabs/oneflow-templates-tab/form-panels/instantiate.js create mode 100644 src/sunstone/public/app/tabs/oneflow-templates-tab/form-panels/instantiate/formPanelId.js create mode 100644 src/sunstone/public/app/tabs/oneflow-templates-tab/form-panels/instantiate/html.hbs diff --git a/src/sunstone/public/app/tabs/oneflow-templates-tab.js b/src/sunstone/public/app/tabs/oneflow-templates-tab.js index b59f418a1f..229012b76c 100644 --- a/src/sunstone/public/app/tabs/oneflow-templates-tab.js +++ b/src/sunstone/public/app/tabs/oneflow-templates-tab.js @@ -18,7 +18,8 @@ define(function(require) { ]; var _formPanels = [ - require('./oneflow-templates-tab/form-panels/create') + require('./oneflow-templates-tab/form-panels/create'), + require('./oneflow-templates-tab/form-panels/instantiate') ]; var Tab = { diff --git a/src/sunstone/public/app/tabs/oneflow-templates-tab/actions.js b/src/sunstone/public/app/tabs/oneflow-templates-tab/actions.js index b9c99b83e3..4ab46aec67 100644 --- a/src/sunstone/public/app/tabs/oneflow-templates-tab/actions.js +++ b/src/sunstone/public/app/tabs/oneflow-templates-tab/actions.js @@ -7,7 +7,7 @@ define(function(require) { var TAB_ID = require('./tabId'); var CREATE_DIALOG_ID = require('./form-panels/create/formPanelId'); - //var INSTANTIATE_DIALOG_ID = require('./dialogs/instantiate/dialogId'); + var INSTANTIATE_DIALOG_ID = require('./form-panels/instantiate/formPanelId'); var XML_ROOT = "DOCUMENT"; var RESOURCE = "ServiceTemplate"; @@ -93,26 +93,39 @@ define(function(require) { } }, - /* TODO - "ServiceTemplate.instantiate" : { - type: "multiple", - call: OpenNebula.ServiceTemplate.instantiate, - callback: function(req){ - OpenNebula.Helper.clear_cache("SERVICE"); - }, - elements: serviceTemplateElements, - error: onError, - notify: true + type: "multiple", + call: OpenNebulaResource.instantiate, + callback: function(request, response){ + Sunstone.hideFormPanel(TAB_ID); + + // TODO + //OpenNebulaHelper.clear_cache("SERVICE"); + }, + elements: function() { + return Sunstone.getDataTable(TAB_ID).elements(); + }, + error: function(request, response){ + Sunstone.hideFormPanelLoading(TAB_ID); + Notifier.onError(request, response); + }, + notify: true }, "ServiceTemplate.instantiate_dialog" : { - type: "custom", - call: function(){ - popUpInstantiateServiceTemplateDialog(); + type: "custom", + call: function() { + var selected_nodes = Sunstone.getDataTable(TAB_ID).elements(); + if (selected_nodes.length != 1) { + Notifier.notifyMessage("Please select one (and just one) template to instantiate."); + return false; } + + Sunstone.resetFormPanel(TAB_ID, INSTANTIATE_DIALOG_ID); + Sunstone.showFormPanel(TAB_ID, INSTANTIATE_DIALOG_ID, "instantiate"); + } }, - */ + }; return _actions; diff --git a/src/sunstone/public/app/tabs/oneflow-templates-tab/form-panels/instantiate.js b/src/sunstone/public/app/tabs/oneflow-templates-tab/form-panels/instantiate.js new file mode 100644 index 0000000000..e43726aba3 --- /dev/null +++ b/src/sunstone/public/app/tabs/oneflow-templates-tab/form-panels/instantiate.js @@ -0,0 +1,232 @@ +define(function(require) { + /* + DEPENDENCIES + */ + + require('foundation.tab'); + var BaseFormPanel = require('utils/form-panels/form-panel'); + var Sunstone = require('sunstone'); + var Locale = require('utils/locale'); + var Tips = require('utils/tips'); + var OpenNebulaServiceTemplate = require('opennebula/servicetemplate'); + var OpenNebulaTemplate = require('opennebula/template'); + var Notifier = require('utils/notifier'); + var WizardFields = require('utils/wizard-fields'); + var TemplateUtils = require('utils/template-utils'); + var UserInputs = require('utils/user-inputs'); + + /* + TEMPLATES + */ + + var TemplateHTML = require('hbs!./instantiate/html'); + + /* + CONSTANTS + */ + + var FORM_PANEL_ID = require('./instantiate/formPanelId'); + var TAB_ID = require('../tabId'); + + /* + CONSTRUCTOR + */ + + function FormPanel() { + this.formPanelId = FORM_PANEL_ID; + this.tabId = TAB_ID; + this.actions = { + 'instantiate': { + 'title': Locale.tr("Instantiate Service Template"), + 'buttonText': Locale.tr("Instantiate"), + 'resetButton': false + } + }; + + BaseFormPanel.call(this); + } + + FormPanel.FORM_PANEL_ID = FORM_PANEL_ID; + FormPanel.prototype = Object.create(BaseFormPanel.prototype); + FormPanel.prototype.constructor = FormPanel; + FormPanel.prototype.htmlWizard = _html; + FormPanel.prototype.submitWizard = _submitWizard; + FormPanel.prototype.onShow = _onShow; + FormPanel.prototype.fill = _fill; + FormPanel.prototype.setup = _setup; + + return FormPanel; + + /* + FUNCTION DEFINITIONS + */ + + function _html() { + return TemplateHTML({ + 'formPanelId': this.formPanelId + }); + } + + function _setup(context) { + var that = this; + + Tips.setup(context); + return false; + } + + function _onShow(context) { + var that = this; + + Sunstone.disableFormPanelSubmit(TAB_ID); + + var selected_nodes = Sunstone.getDataTable(TAB_ID).elements(); + var template_id = ""+selected_nodes[0]; + + this.service_template_json = {}; + + OpenNebulaServiceTemplate.show({ + data : { + id: template_id + }, + timeout: true, + success: function (request, template_json){ + + that.service_template_json = template_json; + + $("#instantiate_service_user_inputs", context).empty(); + + UserInputs.serviceTemplateInsert( + $("#instantiate_service_user_inputs", context), + template_json); + + n_roles = template_json.DOCUMENT.TEMPLATE.BODY.roles.length; + n_roles_done = 0; + + $.each(template_json.DOCUMENT.TEMPLATE.BODY.roles, function(index, role){ + var div_id = "user_input_role_"+index; + + $("#instantiate_service_role_user_inputs", context).append( + '
\ +
' + ); + + OpenNebulaTemplate.show({ + data : { + id: role.vm_template + }, + timeout: true, + success: function (request, vm_template_json){ + + $("#"+div_id, context).empty(); + + UserInputs.vmTemplateInsert( + $("#"+div_id, context), + vm_template_json, + { + text_header: Locale.tr("Role") + " " + role.name + } + ); + + n_roles_done += 1; + + if(n_roles_done == n_roles){ + Sunstone.enableFormPanelSubmit(TAB_ID); + } + }, + error: function(request,error_json, container){ + Notifier.onError(request,error_json, container); + $("#instantiate_vm_user_inputs", context).empty(); + } + }); + }); + }, + error: function(request,error_json, container){ + Notifier.onError(request,error_json, container); + $("#instantiate_service_user_inputs", context).empty(); + } + }); + + } + + function _submitWizard(context) { + var that = this; + + var service_name = $('#service_name',context).val(); + var n_times = $('#service_n_times',context).val(); + var n_times_int=1; + + var template_id; + if ($("#TEMPLATE_ID", context).val()) { + template_id = $("#TEMPLATE_ID", context).val(); + } else { + var selected_nodes = Sunstone.getDataTable(TAB_ID).elements(); + template_id = ""+selected_nodes[0]; + } + + if (n_times.length){ + n_times_int=parseInt(n_times,10); + } + + var extra_msg = ""; + if (n_times_int > 1) { + extra_msg = n_times_int+" times"; + } + + var extra_info = { + 'merge_template': {} + }; + + var tmp_json = WizardFields.retrieve($("#instantiate_service_user_inputs", context)); + + extra_info.merge_template.custom_attrs_values = tmp_json; + + extra_info.merge_template.roles = []; + + $.each(that.service_template_json.DOCUMENT.TEMPLATE.BODY.roles, function(index, role){ + var div_id = "user_input_role_"+index; + + tmp_json = {}; + + $.extend( tmp_json, WizardFields.retrieve($("#"+div_id, context)) ); + + $.each(role.elasticity_policies, function(i, pol){ + pol.expression = TemplateUtils.htmlDecode(pol.expression); + }); + + role.user_inputs_values = tmp_json; + + extra_info.merge_template.roles.push(role); + }); + + if (!service_name.length){ //empty name + for (var i=0; i< n_times_int; i++){ + Sunstone.runAction("ServiceTemplate.instantiate", [template_id], extra_info); + } + } else { + if (service_name.indexOf("%i") == -1){//no wildcard, all with the same name + extra_info['merge_template']['name'] = service_name; + + for (var i=0; i< n_times_int; i++){ + Sunstone.runAction( + "ServiceTemplate.instantiate", + [template_id], extra_info); + } + } else { //wildcard present: replace wildcard + for (var i=0; i< n_times_int; i++){ + extra_info['merge_template']['name'] = service_name.replace(/%i/gi,i); + + Sunstone.runAction( + "ServiceTemplate.instantiate", + [template_id], extra_info); + } + } + } + + return false; + } + + function _fill(context, element) { + var that = this; + } + +}); diff --git a/src/sunstone/public/app/tabs/oneflow-templates-tab/form-panels/instantiate/formPanelId.js b/src/sunstone/public/app/tabs/oneflow-templates-tab/form-panels/instantiate/formPanelId.js new file mode 100644 index 0000000000..7ca08a5334 --- /dev/null +++ b/src/sunstone/public/app/tabs/oneflow-templates-tab/form-panels/instantiate/formPanelId.js @@ -0,0 +1,3 @@ +define(function(require){ + return 'instantiateServiceTemplateForm'; +}); \ No newline at end of file diff --git a/src/sunstone/public/app/tabs/oneflow-templates-tab/form-panels/instantiate/html.hbs b/src/sunstone/public/app/tabs/oneflow-templates-tab/form-panels/instantiate/html.hbs new file mode 100644 index 0000000000..dc096388e4 --- /dev/null +++ b/src/sunstone/public/app/tabs/oneflow-templates-tab/form-panels/instantiate/html.hbs @@ -0,0 +1,21 @@ +
+
+
+ + +
+
+ + +
+
+
+ +
+
+
+
\ No newline at end of file