From 64ba86b7498c8a5bbc58a997d8159f9bbec1b0f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn?= Date: Tue, 3 Jun 2014 18:16:01 +0200 Subject: [PATCH] Feature #2858: Update AR wizard --- .../OpenNebulaJSON/VirtualNetworkJSON.rb | 5 +- src/sunstone/public/js/opennebula.js | 2 +- src/sunstone/public/js/plugins/vnets-tab.js | 286 +++++++++++++++--- src/sunstone/public/js/sunstone.js | 35 ++- 4 files changed, 284 insertions(+), 44 deletions(-) diff --git a/src/sunstone/models/OpenNebulaJSON/VirtualNetworkJSON.rb b/src/sunstone/models/OpenNebulaJSON/VirtualNetworkJSON.rb index 4d5f2f0f13..1fb96a2c8a 100644 --- a/src/sunstone/models/OpenNebulaJSON/VirtualNetworkJSON.rb +++ b/src/sunstone/models/OpenNebulaJSON/VirtualNetworkJSON.rb @@ -96,7 +96,10 @@ module OpenNebulaJSON end def update_ar(params=Hash.new) - super(params['ar_template_raw']) + template_json = params['ar_template'] + template = template_to_str(template_json) + + super(template) end end end diff --git a/src/sunstone/public/js/opennebula.js b/src/sunstone/public/js/opennebula.js index d1b40be2f9..33bbf42ccc 100644 --- a/src/sunstone/public/js/opennebula.js +++ b/src/sunstone/public/js/opennebula.js @@ -796,7 +796,7 @@ var OpenNebula = { action_obj); }, "update_ar": function(params){ - var action_obj = {"ar_template_raw" : params.data.extra_param }; + var action_obj = {"ar_template" : params.data.extra_param }; OpenNebula.Action.simple_action(params, OpenNebula.Network.resource, "update_ar", diff --git a/src/sunstone/public/js/plugins/vnets-tab.js b/src/sunstone/public/js/plugins/vnets-tab.js index ed101a47d4..e9457da542 100644 --- a/src/sunstone/public/js/plugins/vnets-tab.js +++ b/src/sunstone/public/js/plugins/vnets-tab.js @@ -332,7 +332,12 @@ var vnet_actions = { type: "single", call: OpenNebula.Network.add_ar, callback: function(req) { - Sunstone.runAction("Network.show",req.request.data[0][0]); + // Reset the wizard + $add_ar_dialog.foundation('reveal', 'close'); + $add_ar_dialog.empty(); + setupAddARDialog(); + + Sunstone.runAction("Network.show",req.request.data[0][0]); }, error: onError }, @@ -350,7 +355,12 @@ var vnet_actions = { type: "single", call: OpenNebula.Network.update_ar, callback: function(req) { - Sunstone.runAction("Network.show",req.request.data[0][0]); + // Reset the wizard + $update_ar_dialog.foundation('reveal', 'close'); + $update_ar_dialog.empty(); + setupUpdateARDialog(); + + Sunstone.runAction("Network.show",req.request.data[0][0]); }, error: onError }, @@ -681,6 +691,9 @@ function updateVNetworkInfo(request,vn){ $(this).addClass('markrowchecked'); }); + $("#update_ar_button", $("#vnet_info_panel")).attr("ar_id", id).removeAttr('disabled'); + $("#rm_ar_button", $("#vnet_info_panel")).attr("ar_id", id).removeAttr('disabled'); + $("#ar_show_info", $("#vnet_info_panel")).html(ar_show_info(vn_info, id)); return false; @@ -715,17 +728,35 @@ function ar_list_tab_content(vn_info){ var html = '
'; + html += + '
\ +
'; + if (Config.isTabActionEnabled("vnets-tab", "Network.add_ar")) { html += - '
\ -
\ - \ -
\ -
'; + ''; } + html += ''; + + if (Config.isTabActionEnabled("vnets-tab", "Network.update_ar")) { + html += + ''; + } + + if (Config.isTabActionEnabled("vnets-tab", "Network.remove_ar")) { + html += + ''; + } + + html += '
'; + html += '
\
\ @@ -820,7 +851,7 @@ function ar_show_info(vn_info, ar_id){ for (var i=0; i\ - '+txt+'\ - '+val+'\ + '+htmlDecode(txt)+'\ + '+htmlDecode(val)+'\ ' } @@ -848,31 +879,44 @@ function ar_show_info(vn_info, ar_id){
\ \ \ - \ + \ \ '; // TODO: translate ar.TYPE values? - html += ar_attr(ar.TYPE, tr("Type")); - html += ar_attr(ar.MAC, tr("MAC")); - html += ar_attr(ar.IP, tr("IP")); - html += ar_attr(ar.GLOBAL_PREFIX, tr("Global prefix")); - html += ar_attr(ar.ULA_PREFIX, tr("ULA prefix")); - html += ar_attr(ar.SIZE, tr("Size")); - html += ar_attr(ar.USED_LEASES, tr("Used leases")); + html += ar_attr(tr("Type"), ar.TYPE); + html += ar_attr(tr("MAC"), ar.MAC); + html += ar_attr(tr("IP"), ar.IP); + html += ar_attr(tr("Global prefix"),ar.GLOBAL_PREFIX); + html += ar_attr(tr("ULA prefix"), ar.ULA_PREFIX); + html += ar_attr(tr("Size"), ar.SIZE); + html += ar_attr(tr("Used leases"), ar.USED_LEASES); + + delete ar["AR_ID"]; + delete ar["TYPE"]; + delete ar["MAC"]; + delete ar["IP"]; + delete ar["GLOBAL_PREFIX"]; + delete ar["ULA_PREFIX"]; + delete ar["SIZE"]; + delete ar["USED_LEASES"]; html += '\
'+tr("Address Range")+' '+ar_id+'
'+tr("Address Range")+' '+ar_id+'
'; - if (Config.isTabActionEnabled("vnets-tab", "Network.remove_ar")) { - html += - '
\ - \ -
'; - } + html += + '
\ +
\ + \ + \ + \ + \ + '; - // TODO: extra ar config attributes + $.each(ar, function(key, value){ + html += ar_attr(key, value); + }); html += '\ @@ -1241,36 +1285,36 @@ function generate_ar_tab_content(str_ar_tab_id){ var html = '
\
\ - \ - \ - \ - \ + \ + \ + \ + \
\
\
\
\
\ \ - \ + \
\
\ \ - \ + \
\
\
\ \ - \ + \
\
\
\
\ \ - \ + \
\
\ \ - \ + \
\
\
\ @@ -1311,7 +1355,7 @@ function setup_ar_tab_content(ar_section, str_ar_tab_id) { } }); - $('input#'+str_ar_tab_id+'_ar_type_ip4',ar_section).attr('checked', true); + $('input#'+str_ar_tab_id+'_ar_type_ip4',ar_section).prop('checked', true); $('input#'+str_ar_tab_id+'_ar_type_ip4',ar_section).change(); setupCustomTags($('#'+str_ar_tab_id+'_custom_tags',ar_section)); @@ -1367,6 +1411,45 @@ function retrieve_ar_tab_data(ar_section){ return data } +function fill_ar_tab_data(ar_json, ar_section){ + var fields = $('[wizard_field]',ar_section); + + fields.each(function(){ + var field = $(this); + var field_name = field.attr('wizard_field'); + if (ar_json[field_name]){ + switch(field.attr("type")){ + case "radio": + var checked = (field.val() == ar_json[field_name]); + + field.prop("checked", checked ); + + if(checked){ + field.change(); + } + break; + default: + field.val(escapeDoubleQuotes(htmlDecode(ar_json[field_name]))); + field.change(); + } + } + }); + + fields.each(function(){ + var field = $(this); + var field_name = field.attr('wizard_field'); + + // Delete so these attributes don't end in the custom tags table also + delete ar_json[field_name]; + }); + + delete ar_json["AR_ID"]; + delete ar_json["USED_LEASES"]; + delete ar_json["LEASES"]; + + fillCustomTags(ar_section, ar_json); +}; + // Listeners to the add, hold, release, delete leases operations in the // extended information panel. function setupLeasesOps(){ @@ -1428,6 +1511,56 @@ function setupLeasesOps(){ return false; }); } + + if (Config.isTabActionEnabled("vnets-tab", "Network.update_ar")) { + $('button#update_ar_button').live("click",function(){ + + var id = $(this).parents('form').attr('vnid'); + var ar_id = $(this).attr('ar_id'); + + OpenNebula.Network.show({ + data : { + id: id + }, + timeout: true, + success: function (request, vn){ + var vn_info = vn.VNET; + + var ar_list = vn_info.AR_POOL.AR; + + if (!ar_list) //empty + { + ar_list = []; + } + else if (ar_list.constructor != Array) //>1 lease + { + ar_list = [ar_list]; + }; + + var found = false; + var ar; + + for (var i=0; i\
\
\ -

'+tr("New Address Range for Virtual Network")+'

\ +

\ + '+tr("Virtual Network")+' \ +
'+tr("New Address Range")+'\ +

\
\
'+ generate_ar_tab_content("add_ar") + '\ ×\ @@ -1465,6 +1602,13 @@ function setupAddARDialog(){ setup_ar_tab_content(dialog, "add_ar") + $('#submit_ar_reset_button').click(function(){ + $add_ar_dialog.html(""); + setupAddARDialog(); + + popUpAddAR(); + }); + $('#add_ar_form',dialog).submit(function(){ var vnet_id = $('#vnet_id', this).text(); var data = retrieve_ar_tab_data(this); @@ -1472,11 +1616,72 @@ function setupAddARDialog(){ var obj = {AR: data} Sunstone.runAction('Network.add_ar', vnet_id, obj); - $add_ar_dialog.foundation('reveal', 'close') return false; }); }; +function popUpUpdateAR(id, ar){ + $('#vnet_id',$update_ar_dialog).text(id); + $('#ar_id',$update_ar_dialog).text(ar.AR_ID); + + fill_ar_tab_data($.extend({}, ar), $update_ar_dialog); + + $update_ar_dialog.foundation().foundation('reveal', 'open'); +} + +function setupUpdateARDialog(){ + dialogs_context.append('
'); + $update_ar_dialog = $('#update_ar_dialog',dialogs_context); + var dialog = $update_ar_dialog; + + dialog.html( + '
\ + \ +
\ +
\ +

\ + '+tr("Virtual Network")+' \ +
'+tr("Edit Address Range")+' \ +

\ +
\ +
'+ + generate_ar_tab_content("update_ar") + + '\ + ×\ + \ +
'); + + // TODO: max-height? + + dialog.addClass("reveal-modal large max-height").attr("data-reveal", ""); + setupTips(dialog); + + setup_ar_tab_content(dialog, "update_ar") + + dialog.die(); + dialog.live('closed', function () { + $update_ar_dialog.html(""); + setupUpdateARDialog(); + }); + + $('#update_ar_form',dialog).submit(function(){ + var vnet_id = $('#vnet_id', this).text(); + var ar_id = $('#ar_id', this).text(); + var data = retrieve_ar_tab_data(this); + + data['AR_ID'] = ar_id; + + var obj = {AR: data} + Sunstone.runAction('Network.update_ar', vnet_id, obj); + + return false; + }); +} + //The DOM is ready and the ready() from sunstone.js //has been executed at this point. $(document).ready(function(){ @@ -1508,6 +1713,7 @@ $(document).ready(function(){ setupLeasesOps(); setupAddARDialog(); + setupUpdateARDialog(); initCheckAllBoxes(dataTable_vNetworks); tableCheckboxesListener(dataTable_vNetworks); diff --git a/src/sunstone/public/js/sunstone.js b/src/sunstone/public/js/sunstone.js index 0981e92642..f4b521615e 100644 --- a/src/sunstone/public/js/sunstone.js +++ b/src/sunstone/public/js/sunstone.js @@ -4658,10 +4658,41 @@ function setupCustomTags(div){ }); } -function retrieveCustomTags(div, hash){ +// div is the container div of customTagsHtml() +// template_json is where the key:values will be stored +// retrieveCustomTags($('#vnetCreateContextTab', $create_vn_dialog), network_json); +function retrieveCustomTags(div, template_json){ $('#custom_tags tr', div).each(function(){ if ($('#KEY', $(this)).val()) { - hash[$('#KEY', $(this)).val()] = $('#VALUE', $(this)).val(); + template_json[$('#KEY', $(this)).val()] = $('#VALUE', $(this)).val(); } }); +} + +// div is the container div of customTagsHtml() +// template_json are the key:values that will be put into the table +function fillCustomTags(div, template_json){ + $.each(template_json, function(key, value){ + var table = $('#custom_tags', div)[0]; + var rowCount = table.rows.length; + var row = table.insertRow(rowCount); + + var cell1 = row.insertCell(0); + var element1 = document.createElement("input"); + element1.id = "KEY"; + element1.type = "text"; + element1.value = htmlDecode(key); + cell1.appendChild(element1); + + var cell2 = row.insertCell(1); + var element2 = document.createElement("input"); + element2.id = "VALUE"; + element2.type = "text"; + element2.value = htmlDecode(value); + cell2.appendChild(element2); + + + var cell3 = row.insertCell(2); + cell3.innerHTML = ""; + }); } \ No newline at end of file