diff --git a/src/sunstone/public/js/plugins/secgroups-tab.js b/src/sunstone/public/js/plugins/secgroups-tab.js index 85e7f7c4f6..1a2fa59be7 100644 --- a/src/sunstone/public/js/plugins/secgroups-tab.js +++ b/src/sunstone/public/js/plugins/secgroups-tab.js @@ -465,7 +465,7 @@ function securityGroupElements(){ return getSelectedNodes(dataTable_security_groups); } -function security_groupElementArray(element_json){ +function securityGroupElementArray(element_json){ var element = element_json.SECURITY_GROUP; @@ -481,7 +481,7 @@ function security_groupElementArray(element_json){ //callback for an action affecting a security_group element function updateSecurityGroupElement(request, element_json){ var id = element_json.SECURITY_GROUP.ID; - var element = security_groupElementArray(element_json); + var element = securityGroupElementArray(element_json); updateSingleElement(element,dataTable_security_groups,'#security_group_'+id); } @@ -494,7 +494,7 @@ function deleteSecurityGroupElement(req){ //call back for actions creating a security_group element function addSecurityGroupElement(request,element_json){ var id = element_json.SECURITY_GROUP.ID; - var element = security_groupElementArray(element_json); + var element = securityGroupElementArray(element_json); addElement(element,dataTable_security_groups); } @@ -504,7 +504,7 @@ function updateSecurityGroupsView (request,list){ $.each(list,function(){ //Grab table data from the list - list_array.push(security_groupElementArray(this)); + list_array.push(securityGroupElementArray(this)); }); updateView(list_array,dataTable_security_groups); diff --git a/src/sunstone/public/js/plugins/templates-tab.js b/src/sunstone/public/js/plugins/templates-tab.js index 063412a584..4de29be25f 100644 --- a/src/sunstone/public/js/plugins/templates-tab.js +++ b/src/sunstone/public/js/plugins/templates-tab.js @@ -1786,6 +1786,12 @@ function generate_nic_tab_content(str_nic_tab_id, str_datatable_id){ ''+ ''+ ''+ + ''+ + '
'+ + ''+tr("Security Groups")+''+ + '
'+ + generateSecurityGroupTableSelect("vm_create_nic_"+str_nic_tab_id)+ + '
'+ '
'}); $("#refresh_template_nic_table_button_class"+str_nic_tab_id).die(); @@ -1815,6 +1821,13 @@ function retrieve_nic_tab_data(context){ data["ICMP"] = "drop" } + var str_nic_tab_id = $('div[name="str_nic_tab_id"]', context).attr("str_nic_tab_id"); + + var secgroups = retrieveSecurityGroupTableSelect(context, + "vm_create_nic_"+str_nic_tab_id); + + data["SECURITY_GROUPS"] = secgroups.join(","); + return data } @@ -1907,6 +1920,11 @@ function setup_nic_tab_content(nic_section, str_nic_tab_id, str_datatable_id) { return true; }); + setupSecurityGroupTableSelect(nic_section, "vm_create_nic_"+str_nic_tab_id, + {"multiple_choice": true}); + + refreshSecurityGroupTableSelect(nic_section, "vm_create_nic_"+str_nic_tab_id); + setupTips(nic_section); } diff --git a/src/sunstone/public/js/plugins/vnets-tab.js b/src/sunstone/public/js/plugins/vnets-tab.js index e1f064e662..54597cd1a4 100644 --- a/src/sunstone/public/js/plugins/vnets-tab.js +++ b/src/sunstone/public/js/plugins/vnets-tab.js @@ -37,6 +37,7 @@ var create_vn_tmpl =

'+tr("General")+'
\

'+tr("Configuration")+'
\

'+tr("Addresses")+'
\ +

'+tr("Security")+'
\

'+tr("Context")+'
\ \
\ @@ -138,6 +139,9 @@ var create_vn_tmpl =
\ \ \ +
\ + '+generateSecurityGroupTableSelect("vnet_create")+'\ +
\
\
\
\ @@ -1253,6 +1257,8 @@ function setupCreateVNetDialog() { //Initialize shown options $('#network_mode',dialog).trigger("change"); + setupSecurityGroupTableSelect(dialog, "vnet_create", {"multiple_choice": true}); + setupCustomTags($("#vnetCreateContextTab", dialog)); //Handle submission of the easy mode @@ -1266,6 +1272,9 @@ function setupCreateVNetDialog() { retrieveWizardFields($("#vnetCreateBridgeTab", dialog), network_json); retrieveWizardFields($("#vnetCreateContextTab", dialog), network_json); + var secgroups = retrieveSecurityGroupTableSelect(dialog, "vnet_create"); + network_json["SECURITY_GROUPS"] = secgroups.join(","); + retrieveCustomTags($("#vnetCreateContextTab", dialog), network_json); $('.ar_tab',dialog).each(function(){ @@ -1322,6 +1331,8 @@ function setupCreateVNetDialog() { function popUpCreateVnetDialog() { $create_vn_dialog.foundation().foundation('reveal', 'open'); + refreshSecurityGroupTableSelect($create_vn_dialog, "vnet_create"); + $("input#name",$create_vn_dialog).focus(); } @@ -1347,7 +1358,7 @@ function add_ar_tab(ar_id, dialog) { function generate_ar_tab_content(str_ar_tab_id){ var html = - '
\ + '
\
\ \ \ @@ -1390,20 +1401,35 @@ function generate_ar_tab_content(str_ar_tab_id){ \ \
\ -
\ -
\ -
\ -
\ - ' + tr("Custom attributes") + '\ -
\ -
\ +
'+ + generateAdvancedSection({ + title: tr("Advanced Options"), + html_id: 'advanced_vnet_create_ar', + content:'
\ +
\ + ' + tr("Custom attributes") + '\ +
\ +
\ +
\
\ -
\ -
\ -
'+ - customTagsHtml()+ - '
\ -
'; +
\ +
'+ + customTagsHtml()+ + '
\ +
\ +
\ +
\ +
\ +
\ +
\ + ' + tr("Security Groups") + '\ +
\ +
\ +
\ +
\ +
\ + '+generateSecurityGroupTableSelect("vnet_create_ar_"+str_ar_tab_id)+'\ +
'}); return html; } @@ -1440,6 +1466,11 @@ function setup_ar_tab_content(ar_section, str_ar_tab_id) { setupCustomTags($('#'+str_ar_tab_id+'_custom_tags',ar_section)); + setupSecurityGroupTableSelect(ar_section, "vnet_create_ar_"+str_ar_tab_id, + {"multiple_choice": true}); + + refreshSecurityGroupTableSelect(ar_section, "vnet_create_ar_"+str_ar_tab_id); + setupTips(ar_section); } @@ -1480,6 +1511,12 @@ function retrieve_ar_tab_data(ar_section){ retrieveCustomTags(ar_section, data); + var str_ar_tab_id = $('div[name="str_ar_tab_id"]', ar_section).attr("str_ar_tab_id"); + + var secgroups = retrieveSecurityGroupTableSelect(ar_section, + "vnet_create_ar_"+str_ar_tab_id); + data["SECURITY_GROUPS"] = secgroups.join(","); + return data } diff --git a/src/sunstone/public/js/sunstone.js b/src/sunstone/public/js/sunstone.js index 151db9ba78..409b7dfce1 100644 --- a/src/sunstone/public/js/sunstone.js +++ b/src/sunstone/public/js/sunstone.js @@ -5916,6 +5916,111 @@ function setupDatastoreTableSelect(section, context_id, opts){ return setupResourceTableSelect(section, context_id, options); } +function generateSecurityGroupTableSelect(context_id){ + + var columns = [ + "", + tr("ID"), + tr("Owner"), + tr("Group"), + tr("Name") + ]; + + var options = { + "id_index": 1, + "name_index": 4, + "select_resource": tr("Please select a security group from the list"), + "you_selected": tr("You selected the following security group:"), + "select_resource_multiple": tr("Please select one or more security groups from the list"), + "you_selected_multiple": tr("You selected the following security groups:") + }; + + 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 +// opts.multiple_choice: boolean true to enable multiple element selection +function setupSecurityGroupTableSelect(section, context_id, opts){ + + if(opts == undefined){ + opts = {}; + } + + if(opts.bVisible == undefined){ + // Use the settings in the conf, but removing the checkbox + var config = Config.tabTableColumns('secgroups-tab').slice(0); + var i = config.indexOf(0); + + if(i != -1){ + config.splice(i,1); + } + + opts.bVisible = config; + } + + if(opts.multiple_choice == undefined){ + opts.multiple_choice = false; + } + + var options = { + "dataTable_options": { + "bAutoWidth":false, + "iDisplayLength": 4, + "sDom" : '<"H">t<"F"p>', + "bRetrieve": true, + "bSortClasses" : false, + "bDeferRender": true, + "aoColumnDefs": [ + { "sWidth": "35px", "aTargets": [0,1] }, + { "bVisible": true, "aTargets": opts.bVisible}, + { "bVisible": false, "aTargets": ['_all']} + ] + }, + + "multiple_choice": opts.multiple_choice, + + "id_index": 1, + "name_index": 4, + + "update_fn": function(datatable){ + OpenNebula.SecurityGroup.list({ + timeout: true, + success: function (request, secgroups_list){ + var secgroup_list_array = []; + + $.each(secgroups_list,function(){ + var add = true; + + if(opts.filter_fn){ + add = opts.filter_fn(this.SECURITY_GROUP); + } + + if(add){ + secgroup_list_array.push(securityGroupElementArray(this)); + } + }); + + updateView(secgroup_list_array, datatable); + }, + error: onError + }); + } + }; + + return setupResourceTableSelect(section, context_id, options); +} + +// Clicks the refresh button +function refreshSecurityGroupTableSelect(section, context_id){ + return refreshResourceTableSelect(section, context_id); +} + +// Returns an ID, or an array of IDs for opts.multiple_choice +function retrieveSecurityGroupTableSelect(section, context_id){ + return retrieveResourceTableSelect(section, context_id); +} + function generateResourceTableSelect(context_id, columns, options){ if (!options.select_resource){ options.select_resource = tr("Please select a resource from the list"); @@ -5956,9 +6061,11 @@ function generateResourceTableSelect(context_id, columns, options){
\
\
\ -
\ +
\ '+options.select_resource+'\ \ + \ + \ \ \
\ @@ -5991,31 +6098,101 @@ function setupResourceTableSelect(section, context_id, options) { dataTable_select.fnSort( [ [options.id_index, config['user_config']['table_order']] ] ); + if(options.multiple_choice){ + $('#selected_resource_'+context_id, section).hide(); + $('#select_resource_'+context_id, section).hide(); + + $('#selected_resource_multiple_'+context_id, section).hide(); + $('#select_resource_multiple_'+context_id, section).show(); + } + $('#selected_resource_id_'+context_id, section).hide(); $('#selected_resource_name_'+context_id, section).hide(); - $('#datatable_'+context_id+' tbody', section).delegate("tr", "click", function(e){ - dataTable_select.unbind("draw"); - var aData = dataTable_select.fnGetData(this); + $('#selected_ids_row_'+context_id, section).data("options", options); - $("td.markrow", dataTable_select).removeClass('markrow'); - $('tbody input.check_item', dataTable_select).removeAttr('checked'); + if(options.multiple_choice){ + $('#selected_ids_row_'+context_id, section).data("ids", {}); - $('#selected_resource_'+context_id, section).show(); - $('#select_resource_'+context_id, section).hide(); - $('.alert-box', section).hide(); + function row_click(row){ + dataTable_select.unbind("draw"); + var aData = dataTable_select.fnGetData(row); - $("td", this).addClass('markrow'); - $('input.check_item', this).attr('checked','checked'); + var row_id = aData[options.id_index]; + var row_name = aData[options.name_index]; - $('#selected_resource_id_'+context_id, section).val(aData[options.id_index]).change(); - $('#selected_resource_id_'+context_id, section).hide(); + var ids = $('#selected_ids_row_'+context_id, section).data("ids"); - $('#selected_resource_name_'+context_id, section).text(aData[options.name_index]).change(); - $('#selected_resource_name_'+context_id, section).show(); + if( ids[row_id] ){ + delete ids[row_id]; - return true; - }); + $("td", row).removeClass('markrow'); + $('input.check_item', row).removeAttr('checked'); + + $('#selected_ids_row_'+context_id+' span[row_id="'+row_id+'"]', section).remove(); + } else { + ids[row_id] = true; + + $("td", row).addClass('markrow'); + $('input.check_item', row).attr('checked','checked'); + + $('#selected_ids_row_'+context_id, section).append(''+row_name+' '); + } + + if ($.isEmptyObject(ids)){ + $('#selected_resource_multiple_'+context_id, section).hide(); + $('#select_resource_multiple_'+context_id, section).show(); + } else { + $('#selected_resource_multiple_'+context_id, section).show(); + $('#select_resource_multiple_'+context_id, section).hide(); + } + + $('.alert-box', section).hide(); + + return true; + }; + + $('#datatable_'+context_id+' tbody', section).delegate("tr", "click", function(e){ + row_click(this); + }); + + $(section).on("click", '#selected_ids_row_'+context_id+' span.fa.fa-times', function() { + var row_id = $(this).parent("span").attr('row_id'); + + // TODO: improve preformance, linear search + $.each(dataTable_select.fnGetData(), function(index, row){ + if(row[options.id_index] == row_id){ + row_click(dataTable_select.fnGetNodes(index)); + return false; + } + }); + + }); + } + else{ + $('#datatable_'+context_id+' tbody', section).delegate("tr", "click", function(e){ + dataTable_select.unbind("draw"); + var aData = dataTable_select.fnGetData(this); + + $("td.markrow", dataTable_select).removeClass('markrow'); + $('tbody input.check_item', dataTable_select).removeAttr('checked'); + + $('#selected_resource_'+context_id, section).show(); + $('#select_resource_'+context_id, section).hide(); + $('.alert-box', section).hide(); + + $("td", this).addClass('markrow'); + $('input.check_item', this).attr('checked','checked'); + + $('#selected_resource_id_'+context_id, section).val(aData[options.id_index]).change(); + $('#selected_resource_id_'+context_id, section).hide(); + + $('#selected_resource_name_'+context_id, section).text(aData[options.name_index]).change(); + $('#selected_resource_name_'+context_id, section).show(); + + return true; + }); + } setupTips(section); } @@ -6036,6 +6213,30 @@ function resetResourceTableSelect(section, context_id, options) { $('#select_resource_'+context_id, section).show(); } +// Returns an ID, or an array of IDs for opts.multiple_choice +function retrieveResourceTableSelect(section, context_id){ + var options = $('#selected_ids_row_'+context_id, section).data("options"); + + if(options.multiple_choice){ + var ids = $('#selected_ids_row_'+context_id, section).data("ids"); + + var arr = []; + + $.each(ids, function(key, val){ + arr.push(key); + }); + + return arr; + } else { + return $('#selected_resource_id_'+context_id, section).val(); + } +} + +// Clicks the refresh button +function refreshResourceTableSelect(section, context_id){ + $('#refresh_button_'+context_id, section).click(); +} + //============================================================================== // VM & Service user inputs //==============================================================================