diff --git a/src/sunstone/models/OpenNebulaJSON/TemplateJSON.rb b/src/sunstone/models/OpenNebulaJSON/TemplateJSON.rb index 006641a1fc..ce5e1c0f68 100644 --- a/src/sunstone/models/OpenNebulaJSON/TemplateJSON.rb +++ b/src/sunstone/models/OpenNebulaJSON/TemplateJSON.rb @@ -48,6 +48,7 @@ module OpenNebulaJSON when "chown" then self.chown(action_hash['params']) when "chmod" then self.chmod_octet(action_hash['params']) when "instantiate" then self.instantiate(action_hash['params']) + when "clone" then self.clone(action_hash['params']) else error_msg = "#{action_hash['perform']} action not " << " available for this resource" @@ -71,5 +72,9 @@ module OpenNebulaJSON super(params['vm_name']) end + def clone(params=Hash.new) + super(params['name']) + end + end end diff --git a/src/sunstone/public/js/opennebula.js b/src/sunstone/public/js/opennebula.js index ae4ec5b5ff..2e4cd6a96b 100644 --- a/src/sunstone/public/js/opennebula.js +++ b/src/sunstone/public/js/opennebula.js @@ -849,12 +849,16 @@ var OpenNebula = { "unpublish" : function(params){ OpenNebula.Action.simple_action(params,OpenNebula.Template.resource,"unpublish"); }, - "instantiate" : function(params) { var vm_name = params.data.extra_param ? params.data.extra_param : ""; var action_obj = { "vm_name" : vm_name }; OpenNebula.Action.simple_action(params,OpenNebula.Template.resource, "instantiate",action_obj); + }, + "clone" : function(params) { + var name = params.data.extra_param ? params.data.extra_param : ""; + var action_obj = { "name" : name }; + OpenNebula.Action.simple_action(params,OpenNebula.Template.resource, "clone", action_obj); } }, diff --git a/src/sunstone/public/js/plugins/templates-tab.js b/src/sunstone/public/js/plugins/templates-tab.js index 7b7dda5855..88543322a1 100644 --- a/src/sunstone/public/js/plugins/templates-tab.js +++ b/src/sunstone/public/js/plugins/templates-tab.js @@ -781,7 +781,16 @@ var template_actions = { error: onError, notify: true }, - + "Template.clone_dialog" : { + type: "custom", + call: popUpTemplateCloneDialog + }, + "Template.clone" : { + type: "single", + call: OpenNebula.Template.clone, + error: onError, + notify: true + }, "Template.help" : { type: "custom", call: function() { @@ -824,7 +833,10 @@ var template_buttons = { tip: tr("Select the new group")+":", condition: mustBeAdmin }, - + "Template.clone_dialog" : { + type: "action", + text: tr("Clone"), + }, "Template.delete" : { type: "confirm", text: tr("Delete") @@ -2137,6 +2149,78 @@ function popUpTemplateTemplateUpdateDialog(){ }; +function setupTemplateCloneDialog(){ + //Append to DOM + dialogs_context.append('
'); + var dialog = $('#template_clone_dialog',dialogs_context); + + //Put HTML in place + + var html = '\ +'; + + dialog.html(html); + + //Convert into jQuery + dialog.dialog({ + autoOpen:false, + width:375, + modal:true, + resizable:false, + }); + + $('button',dialog).button(); + + $('form',dialog).submit(function(){ + var name = $('input', this).val(); + var sel_elems = templateElements(); + if (!name || !sel_elems.length) + notifyError('A name or prefix is needed!'); + if (sel_elems.length > 1){ + for (var i=0; i< sel_elems.length; i++) + Sunstone.runAction('Template.clone', + sel_elems[i], + name+getTemplateName(sel_elems[i])); + } else { + Sunstone.runAction('Template.clone',sel_elems[0],name) + }; + $(this).parents('#template_clone_dialog').dialog('close'); + setTimeout(function(){ + Sunstone.runAction('Template.refresh'); + }, 1500); + return false; + }); +} + +function popUpTemplateCloneDialog(){ + var dialog = $('#template_clone_dialog'); + var sel_elems = templateElements(); + //show different text depending on how many elements are selected + if (sel_elems.length > 1){ + $('.clone_one',dialog).hide(); + $('.clone_several',dialog).show(); + $('input',dialog).val('Copy of '); + } + else { + $('.clone_one',dialog).show(); + $('.clone_several',dialog).hide(); + $('input',dialog).val('Copy of '+getTemplateName(sel_elems[0])); + }; + + $(dialog).dialog('open'); +} + // Set the autorefresh interval for the datatable function setTemplateAutorefresh() { setInterval(function(){ @@ -2182,6 +2266,7 @@ $(document).ready(function(){ setupCreateTemplateDialog(); setupTemplateTemplateUpdateDialog(); + setupTemplateCloneDialog(); setTemplateAutorefresh(); initCheckAllBoxes(dataTable_templates); diff --git a/src/sunstone/public/js/sunstone-util.js b/src/sunstone/public/js/sunstone-util.js index 16484f15ae..f887985a43 100644 --- a/src/sunstone/public/js/sunstone-util.js +++ b/src/sunstone/public/js/sunstone-util.js @@ -464,6 +464,13 @@ function getHostName(id){ return id; }; +function getTemplateName(id){ + if (typeof(dataTable_templates) != "undefined"){ + return getName(id,dataTable_templates,4); + } + return id; +}; + function getName(id,dataTable,name_col){ var name = id; if (typeof(dataTable) == "undefined") {