diff --git a/src/sunstone/models/OpenNebulaJSON/VirtualNetworkJSON.rb b/src/sunstone/models/OpenNebulaJSON/VirtualNetworkJSON.rb index 7a4669a5d5..b18230f2ac 100644 --- a/src/sunstone/models/OpenNebulaJSON/VirtualNetworkJSON.rb +++ b/src/sunstone/models/OpenNebulaJSON/VirtualNetworkJSON.rb @@ -31,7 +31,7 @@ module OpenNebulaJSON else template = template_to_str(vnet_hash) end - + self.allocate(template) end @@ -46,6 +46,7 @@ module OpenNebulaJSON when "rmleases" then self.rmleases(action_hash['params']) when "publish" then self.publish when "unpublish" then self.unpublish + when "update" then self.update(action_hash['params']) when "chown" then self.chown(action_hash['params']) else error_msg = "#{action_hash['perform']} action not " << @@ -62,6 +63,10 @@ module OpenNebulaJSON super(params['ip']) end + def update(params=Hash.new) + super(params['template_raw']) + end + def chown(params=Hash.new) super(params['owner_id'].to_i,params['group_id'].to_i) end diff --git a/src/sunstone/public/js/opennebula.js b/src/sunstone/public/js/opennebula.js index 12ea698981..8a7224d161 100644 --- a/src/sunstone/public/js/opennebula.js +++ b/src/sunstone/public/js/opennebula.js @@ -510,7 +510,17 @@ var OpenNebula = { OpenNebula.Network.resource, "rmleases", action_obj); - } + }, + "update": function(params){ + var action_obj = {"template_raw" : params.data.extra_param }; + OpenNebula.Action.simple_action(params, + OpenNebula.Network.resource, + "update", + action_obj); + }, + "fetch_template" : function(params){ + OpenNebula.Action.show(params,OpenNebula.Network.resource,"template"); + }, }, "VM": { diff --git a/src/sunstone/public/js/plugins/images-tab.js b/src/sunstone/public/js/plugins/images-tab.js index 8a70352d59..b2bb396b1a 100644 --- a/src/sunstone/public/js/plugins/images-tab.js +++ b/src/sunstone/public/js/plugins/images-tab.js @@ -169,6 +169,34 @@ var create_image_tmpl = \ '; +var update_image_tmpl = + '
'; + var images_select = ""; var dataTable_images; var $create_image_dialog; @@ -228,7 +256,7 @@ var image_actions = { type: "single", call: OpenNebula.Image.fetch_template, callback: function (request,response) { - $('#template_update_dialog #template_update_textarea').val(response.template); + $('#image_template_update_dialog #image_template_update_textarea').val(response.template); }, error: onError }, @@ -236,14 +264,7 @@ var image_actions = { "Image.update_dialog" : { type: "custom", call: function() { - popUpTemplateUpdateDialog("Image", - makeSelectOptions(dataTable_images, - 1,//id_col - 4,//name_col - [], - [] - ), - getSelectedNodes(dataTable_images)); + popUpImageTemplateUpdateDialog(); } }, @@ -450,6 +471,8 @@ function imageElements() { // Returns an array containing the values of the image_json and ready // to be inserted in the dataTable function imageElementArray(image_json){ + //Changing this? It may affect to the is_public and is_persistent + //variables in setupImageTemplateUpdateDialog(); var image = image_json.IMAGE; return [ '', @@ -775,6 +798,127 @@ function popUpCreateImageDialog(){ $create_image_dialog.dialog('open'); } + + +function setupImageTemplateUpdateDialog(){ + + //Append to DOM + dialogs_context.append(''); + var dialog = $('#image_template_update_dialog',dialogs_context); + + //Put HTML in place + dialog.html(update_image_tmpl); + + //Convert into jQuery + dialog.dialog({ + autoOpen:false, + width:700, + modal:true, + height:480, + resizable:false, + }); + + $('button',dialog).button(); + + $('#image_template_update_select',dialog).change(function(){ + var id = $(this).val(); + if (id && id.length){ + var dialog = $('#image_template_update_dialog'); + $('#image_template_update_textarea',dialog).val("Loading..."); + + var data = getElementData(id,"#image",dataTable_images); + var is_public = data[7] == "yes"; + var is_persistent = data[8] == "yes"; + + if (is_public){ + $('#image_template_update_public',dialog).attr("checked","checked") + } else { + $('#image_template_update_public',dialog).removeAttr("checked") + } + + if (is_persistent){ + $('#image_template_update_persistent',dialog).attr("checked","checked") + } else { + $('#image_template_update_persistent',dialog).removeAttr("checked") + } + + Sunstone.runAction("Image.fetch_template",id); + } else { + $('#image_template_update_textarea',dialog).val(""); + }; + }); + + $('#image_template_update_button',dialog).click(function(){ + var dialog = $('#image_template_update_dialog'); + var new_template = $('#image_template_update_textarea',dialog).val(); + var id = $('#image_template_update_select',dialog).val(); + if (!id || !id.length) { + dialog.dialog('close'); + return false; + }; + + var data = getElementData(id,"#image",dataTable_images); + var is_public = data[7] == "yes"; + var is_persistent = data[8] == "yes"; + + var new_public = $('#image_template_update_public:checked',dialog).length; + var new_persistent = $('#image_template_update_persistent:checked',dialog).length; + + if (is_public != new_public){ + if (new_public) Sunstone.runAction("Image.publish",id); + else Sunstone.runAction("Image.unpublish",id); + }; + + if (is_persistent != new_persistent){ + if (new_persistent) Sunstone.runAction("Image.persistent",id); + else Sunstone.runAction("Image.nonpersistent",id); + }; + + Sunstone.runAction("Image.update",id,new_template); + dialog.dialog('close'); + return false; + }); +} + + +function popUpImageTemplateUpdateDialog(){ + var select = makeSelectOptions(dataTable_images, + 1,//id_col + 4,//name_col + [], + [] + ); + var sel_elems = getSelectedNodes(dataTable_images); + + + var dialog = $('#image_template_update_dialog'); + $('#image_template_update_select',dialog).html(select); + $('#image_template_update_textarea',dialog).val(""); + $('#image_template_update_public',dialog).removeAttr("checked") + $('#image_template_update_persistent',dialog).removeAttr("checked") + + if (sel_elems.length >= 1){ //several items in the list are selected + //grep them + var new_select= sel_elems.length > 1? '' : ""; + $('option','').each(function(){ + var val = $(this).val(); + if ($.inArray(val,sel_elems) >= 0){ + new_select+=''; + }; + }); + $('#image_template_update_select',dialog).html(new_select); + if (sel_elems.length == 1) { + $('#image_template_update_select option',dialog).attr("selected","selected"); + $('#image_template_update_select',dialog).trigger("change"); + } + + }; + + dialog.dialog('open'); + return false; + +} + // Set the autorefresh interval for the datatable function setImageAutorefresh() { setInterval(function(){ @@ -811,6 +955,7 @@ $(document).ready(function(){ Sunstone.runAction("Image.list"); setupCreateImageDialog(); + setupImageTemplateUpdateDialog(); setupTips($create_image_dialog); setImageAutorefresh(); diff --git a/src/sunstone/public/js/plugins/vnets-tab.js b/src/sunstone/public/js/plugins/vnets-tab.js index 2151858d4c..84c7735406 100644 --- a/src/sunstone/public/js/plugins/vnets-tab.js +++ b/src/sunstone/public/js/plugins/vnets-tab.js @@ -135,6 +135,30 @@ var create_vn_tmpl = \ '; +var update_vnet_tmpl = + ''; + var vnetworks_select=""; var dataTable_vNetworks; var $create_vn_dialog; @@ -272,8 +296,34 @@ var vnet_actions = { elements: vnElements, error:onError, notify: true - } -} + }, + + "Network.fetch_template" : { + type: "single", + call: OpenNebula.Network.fetch_template, + callback: function (request,response) { + $('#vnet_template_update_dialog #vnet_template_update_textarea').val(response.template); + }, + error: onError + }, + + "Network.update_dialog" : { + type: "custom", + call: function() { + popUpVNetTemplateUpdateDialog(); + } + }, + + "Network.update" : { + type: "single", + call: OpenNebula.Network.update, + callback: function() { + notifyMessage("Template updated correctly"); + }, + error: onError + }, + +}; var vnet_buttons = { @@ -288,6 +338,12 @@ var vnet_buttons = { text: "+ New" }, + "Network.update_dialog" : { + type: "action", + text: "Update a template", + alwaysActive: true + }, + "Network.publish" : { type: "action", text: "Publish" @@ -684,6 +740,109 @@ function popUpCreateVnetDialog() { $create_vn_dialog.dialog('open'); } + +function setupVNetTemplateUpdateDialog(){ + //Append to DOM + dialogs_context.append(''); + var dialog = $('#vnet_template_update_dialog',dialogs_context); + + //Put HTML in place + dialog.html(update_vnet_tmpl); + + //Convert into jQuery + dialog.dialog({ + autoOpen:false, + width:700, + modal:true, + height:480, + resizable:false, + }); + + $('button',dialog).button(); + + $('#vnet_template_update_select',dialog).change(function(){ + var id = $(this).val(); + if (id && id.length){ + var dialog = $('#vnet_template_update_dialog'); + $('#vnet_template_update_textarea',dialog).val("Loading..."); + + var data = getElementData(id,"#vnetwork",dataTable_vNetworks); + var is_public = data[7] == "yes"; + + if (is_public){ + $('#vnet_template_update_public',dialog).attr("checked","checked") + } else { + $('#vnet_template_update_public',dialog).removeAttr("checked") + } + + Sunstone.runAction("Network.fetch_template",id); + } else { + $('#vnet_template_update_textarea',dialog).val(""); + }; + }); + + $('#vnet_template_update_button',dialog).click(function(){ + var dialog = $('#vnet_template_update_dialog'); + var new_template = $('#vnet_template_update_textarea',dialog).val(); + var id = $('#vnet_template_update_select',dialog).val(); + if (!id || !id.length) { + dialog.dialog('close'); + return false; + }; + + var data = getElementData(id,"#vnetwork",dataTable_vNetworks); + var is_public = data[7] == "yes"; + + var new_public = $('#vnet_template_update_public:checked',dialog).length; + + if (is_public != new_public){ + if (new_public) Sunstone.runAction("Network.publish",id); + else Sunstone.runAction("Network.unpublish",id); + }; + + Sunstone.runAction("Network.update",id,new_template); + dialog.dialog('close'); + return false; + }); +}; + +function popUpVNetTemplateUpdateDialog(){ + var select = makeSelectOptions(dataTable_vNetworks, + 1,//id_col + 4,//name_col + [], + [] + ); + var sel_elems = getSelectedNodes(dataTable_vNetworks); + + + var dialog = $('#vnet_template_update_dialog'); + $('#vnet_template_update_select',dialog).html(select); + $('#vnet_template_update_textarea',dialog).val(""); + $('#vnet_template_update_public',dialog).removeAttr("checked") + + if (sel_elems.length >= 1){ //several items in the list are selected + //grep them + var new_select= sel_elems.length > 1? '' : ""; + $('option','').each(function(){ + var val = $(this).val(); + if ($.inArray(val,sel_elems) >= 0){ + new_select+=''; + }; + }); + $('#vnet_template_update_select',dialog).html(new_select); + if (sel_elems.length == 1) { + $('#vnet_template_update_select option',dialog).attr("selected","selected"); + $('#vnet_template_update_select',dialog).trigger("change"); + } + + }; + + dialog.dialog('open'); + return false; + +} + function setupAddRemoveLeaseDialog() { dialogs_context.append(''); $lease_vn_dialog = $('#lease_vn_dialog',dialogs_context) @@ -786,6 +945,7 @@ $(document).ready(function(){ Sunstone.runAction("Network.list"); setupCreateVNetDialog(); + setupVNetTemplateUpdateDialog(); setupAddRemoveLeaseDialog(); setVNetAutorefresh(); diff --git a/src/sunstone/public/js/sunstone-util.js b/src/sunstone/public/js/sunstone-util.js index 25d1b6af3e..4caa9babfe 100644 --- a/src/sunstone/public/js/sunstone-util.js +++ b/src/sunstone/public/js/sunstone-util.js @@ -140,7 +140,7 @@ function tableCheckboxesListener(dataTable){ //listen to changes in the visible inputs $('tbody input',dataTable).live("change",function(){ var datatable = $(this).parents('table'); - recountCheckboxes(dataTable); + recountCheckboxes(datatable); }); } @@ -163,6 +163,12 @@ function updateSingleElement(element,dataTable,tag){ recountCheckboxes(dataTable); } +function getElementData(id, resource_tag, dataTable){ + var nodes = dataTable.fnGetNodes(); + var tr = $(resource_tag+'_'+id,nodes).parents('tr')[0]; + return dataTable.fnGetData(tr); +} + // Returns an string in the form key=value key=value ... // Does not explore objects in depth. function stringJSON(json){ @@ -657,6 +663,12 @@ function setupTemplateUpdateDialog(){ var dialog = $('#template_update_dialog'); var new_template = $('#template_update_textarea',dialog).val(); var id = $('#template_update_select',dialog).val(); + + if (!id || !id.length) { + dialog.dialog('close'); + return false; + }; + var resource = $(this).val(); Sunstone.runAction(resource+".update",id,new_template); dialog.dialog('close');