diff --git a/src/sunstone/public/js/plugins/oneflow-templates.js b/src/sunstone/public/js/plugins/oneflow-templates.js index 8cfa53d03e..01c2cbfe2d 100644 --- a/src/sunstone/public/js/plugins/oneflow-templates.js +++ b/src/sunstone/public/js/plugins/oneflow-templates.js @@ -399,6 +399,40 @@ function generate_elasticity_accordion(role_id, context) { $(document).foundation(); } +var instantiate_service_template_tmpl ='\ +
\ +

'+tr("Instantiate Service Template")+'

\ +
\ +
\ +
\ +
\ + \ + \ +
\ +
\ +
\ +
\ + \ + \ +
\ +
\ +
\ + \ +
\ +
\ +
\ +
\ + \ +
\ + ×\ +
'; + + + var dataTable_service_templates; var $create_service_template_dialog; @@ -494,6 +528,13 @@ var service_template_actions = { notify: true }, + "ServiceTemplate.instantiate_dialog" : { + type: "custom", + call: function(){ + popUpInstantiateServiceTemplateDialog(); + } + }, + "ServiceTemplate.refresh" : { type: "custom", call: function () { @@ -570,7 +611,7 @@ var service_template_buttons = { type: "create_dialog", layout: "create" }, - "ServiceTemplate.instantiate" : { + "ServiceTemplate.instantiate_dialog" : { type: "action", layout: "main", text: tr("Instantiate") @@ -1668,6 +1709,183 @@ function fillUpUpdateServiceTemplateDialog(request, response){ dialog.foundation('reveal', 'open'); } +function popUpInstantiateServiceTemplateDialog(){ + var selected_nodes = getSelectedNodes(dataTable_service_templates); + + if ( selected_nodes.length != 1 ) + { + notifyMessage("Please select one (and just one) template to instantiate."); + return false; + } + + setupInstantiateServiceTemplateDialog(); + $instantiate_service_template_dialog.foundation().foundation('reveal', 'open'); + $("input#service_name",$instantiate_service_template_dialog).focus(); +} + +// Instantiate dialog +// Sets up the instiantiate template dialog and all the processing associated to it +function setupInstantiateServiceTemplateDialog(){ + + dialogs_context.append('
'); + //Insert HTML in place + $instantiate_service_template_dialog = $('#instantiate_service_template_dialog') + var dialog = $instantiate_service_template_dialog; + + dialog.html(instantiate_service_template_tmpl); + dialog.addClass("reveal-modal medium").attr("data-reveal", ""); + dialog.removeClass("max-height"); + + $("#instantiate_service_tenplate_proceed", dialog).attr("disabled", "disabled"); + + var selected_nodes = getSelectedNodes(dataTable_service_templates); + var template_id = ""+selected_nodes[0]; + + var service_template_json; + + OpenNebula.ServiceTemplate.show({ + data : { + id: template_id + }, + timeout: true, + success: function (request, template_json){ + + service_template_json = template_json; + + $("#instantiate_service_user_inputs", dialog).empty(); + + generateServiceTemplateUserInputs( + $("#instantiate_service_user_inputs", dialog), + 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", dialog).append( + '
\ +
\ +
\ +
' + ); + + OpenNebula.Template.show({ + data : { + id: role.vm_template + }, + timeout: true, + success: function (request, vm_template_json){ + + $("#"+div_id, dialog).empty(); + + generateVMTemplateUserInputs( + $("#"+div_id, dialog), + vm_template_json, + { + text_header: tr("Custom Attributes for Role") + " " + role.name + } + ); + + n_roles_done += 1; + + if(n_roles_done == n_roles){ + $("#instantiate_service_tenplate_proceed", dialog).removeAttr("disabled"); + } + }, + error: function(request,error_json, container){ + onError(request,error_json, container); + $("#instantiate_vm_user_inputs", dialog).empty(); + } + }); + }); + }, + error: function(request,error_json, container){ + onError(request,error_json, container); + $("#instantiate_service_user_inputs", dialog).empty(); + } + }); + + setupTips(dialog); + + $('#instantiate_service_template_form',dialog).submit(function(){ + var service_name = $('#service_name',this).val(); + var n_times = $('#service_n_times',this).val(); + var n_times_int=1; + + var template_id + if ($("#TEMPLATE_ID", this).val()) { + template_id = $("#TEMPLATE_ID", this).val(); + } else { + var selected_nodes = getSelectedNodes(dataTable_service_templates); + 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 = {}; + retrieveWizardFields($("#instantiate_service_user_inputs", dialog), tmp_json); + + extra_info.merge_template.custom_attrs_values = tmp_json; + + extra_info.merge_template.roles = []; + + $.each(service_template_json.DOCUMENT.TEMPLATE.BODY.roles, function(index, role){ + var div_id = "user_input_role_"+index; + + tmp_json = {}; + + retrieveWizardFields($("#"+div_id, dialog), tmp_json); + + 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); + } + } + } + + $instantiate_service_template_dialog.foundation('reveal', 'close') + return false; + }); +} + //The DOM is ready at this point $(document).ready(function(){ var tab_name = "oneflow-templates"; diff --git a/src/sunstone/public/js/plugins/templates-tab.js b/src/sunstone/public/js/plugins/templates-tab.js index 51acbb8da6..e2d875457b 100644 --- a/src/sunstone/public/js/plugins/templates-tab.js +++ b/src/sunstone/public/js/plugins/templates-tab.js @@ -93,7 +93,7 @@ var instantiate_vm_template_tmpl ='\ \ \
\ - \ + \
\ ×\ '; @@ -213,7 +213,7 @@ var easy_provision_vm_template_tmpl ='\ \ \ \ ×\ \ @@ -4839,10 +4839,10 @@ function setupInstantiateTemplateDialog(easy_provision){ dialog.addClass("reveal-modal").attr("data-reveal", ""); dialog.removeClass("max-height"); - $("#instantiate_vm_tenplate_proceed", dialog).attr("disabled", "disabled"); + $("#instantiate_vm_template_proceed", dialog).attr("disabled", "disabled"); var selected_nodes = getSelectedNodes(dataTable_templates); - template_id = ""+selected_nodes[0]; + var template_id = ""+selected_nodes[0]; OpenNebula.Template.show({ data : { @@ -4850,15 +4850,14 @@ function setupInstantiateTemplateDialog(easy_provision){ }, timeout: true, success: function (request, template_json){ - if (template_json.VMTEMPLATE.TEMPLATE.USER_INPUTS) { - generateVMTemplateUserInputs( - $("#instantiate_vm_user_inputs", dialog), - template_json); - } else { - $("#instantiate_vm_user_inputs", dialog).empty(); - } - $("#instantiate_vm_tenplate_proceed", dialog).removeAttr("disabled"); + $("#instantiate_vm_user_inputs", dialog).empty(); + + generateVMTemplateUserInputs( + $("#instantiate_vm_user_inputs", dialog), + template_json); + + $("#instantiate_vm_template_proceed", dialog).removeAttr("disabled"); }, error: function(request,error_json, container){ onError(request,error_json, container); diff --git a/src/sunstone/public/js/sunstone.js b/src/sunstone/public/js/sunstone.js index cd7dc9be89..7be05cfaa7 100644 --- a/src/sunstone/public/js/sunstone.js +++ b/src/sunstone/public/js/sunstone.js @@ -4756,10 +4756,10 @@ function generateVNetTableSelect(context_id){ return generateResourceTableSelect(context_id, columns, options); } +// opts.bVisible: dataTable bVisible option. If not set, the .yaml visibility will be used +// opts.filter_fn: boolean function to filter which vnets to show function setupVNetTableSelect(section, context_id, opts){ - var bVisible = opts.bVisible; - if(opts == undefined){ opts = {}; } @@ -4927,53 +4927,133 @@ function setupResourceTableSelect(section, context_id, options) { } // It will replace the div's html with a row for each USER_INPUTS -function generateVMTemplateUserInputs(div, template_json) { +// opts.text_header: header text for the text & password inputs +// opts.network_header: header text for the network inputs +function generateVMTemplateUserInputs(div, template_json, opts) { + generateInstantiateUserInputs( + div, template_json.VMTEMPLATE.TEMPLATE.USER_INPUTS, opts); +} + +// It will replace the div's html with a row for each USER_INPUTS +// opts.text_header: header text for the text & password inputs +// opts.network_header: header text for the network inputs +function generateServiceTemplateUserInputs(div, template_json, opts) { + generateInstantiateUserInputs( + div, template_json.DOCUMENT.TEMPLATE.BODY.custom_attrs, opts); +} + +// It will replace the div's html with a row for each USER_INPUTS +// opts.text_header: header text for the text & password inputs +// opts.network_header: header text for the network inputs +function generateInstantiateUserInputs(div, user_inputs, opts) { + + div.empty(); + + if(user_inputs == undefined){ + return; + } + + if(opts == undefined){ + opts = {}; + } + + if(!opts.text_header){ + opts.text_header = tr("Custom Attributes"); + } + + if(!opts.network_header){ + opts.network_header = tr("Network"); + } + + var network_attrs = []; var text_attrs = []; - $.each(template_json.VMTEMPLATE.TEMPLATE.USER_INPUTS, function(key, value){ + $.each(user_inputs, function(key, value){ var parts = value.split("|"); // 0 mandatory; 1 type; 2 desc; var attrs = { - "name": key, - "mandatory": parts[0], - "type": parts[1], - "description": parts[2], + "name": key, + "mandatory": parts[0], + "type": parts[1], + "description": parts[2], } switch (parts[1]) { - case "text": - text_attrs.push(attrs) - break; - case "password": - text_attrs.push(attrs) - break; + case "vnet_id": + network_attrs.push(attrs) + break; + case "text": + text_attrs.push(attrs) + break; + case "password": + text_attrs.push(attrs) + break; } }); - if (text_attrs.length > 0) { - div.html( + if (network_attrs.length > 0) { + div.append( '
'+ '
'+ '
'+ '

'+ - tr("Custom Attributes")+ + opts.network_header+ '

'+ '
'+ '
'+ '
'+ '
'); + var separator = ""; - $.each(text_attrs, function(index, custom_attr){ - $(".instantiate_user_inputs", div).append( + $.each(network_attrs, function(index, vnet_attr){ + var unique_id = "user_input_"+vnet_attr.name; + + $(".instantiate_user_inputs", div).append( + '
'+ + '
'+ + separator+ + ''+ + generateVNetTableSelect(unique_id)+ + '
'+ + '
'); + + separator = "
"; + + setupVNetTableSelect(div, unique_id); + + $('#refresh_button_'+unique_id).click(); + + $("input#selected_resource_id_"+unique_id, div).attr( + "wizard_field", vnet_attr.name); + }); + } + + if (text_attrs.length > 0) { + div.append( + '
'+ '
'+ '
'+ - ''+ + '

'+ + opts.text_header+ + '

'+ '
'+ + '
'+ + '
'+ '
'); - }) - } -} + + $.each(text_attrs, function(index, custom_attr){ + $(".instantiate_user_inputs", div).append( + '
'+ + '
'+ + ''+ + '
'+ + '
'); + }); + } +} \ No newline at end of file