diff --git a/src/sunstone/public/app/app.js b/src/sunstone/public/app/app.js index eb458a03eb..1b306ba8d9 100644 --- a/src/sunstone/public/app/app.js +++ b/src/sunstone/public/app/app.js @@ -8,9 +8,10 @@ define(function(require) { require('tabs/zones-tab'), require('tabs/datastores-tab'), require('tabs/hosts-tab'), + require('tabs/vnets-tab'), + require('tabs/secgroups-tab'), require('tabs/vresources-tab'), - require('tabs/images-tab'), - require('tabs/vnets-tab') + require('tabs/images-tab') ]; var _commonDialogs = [ diff --git a/src/sunstone/public/app/tabs/secgroups-tab.js b/src/sunstone/public/app/tabs/secgroups-tab.js new file mode 100644 index 0000000000..0c1d86e305 --- /dev/null +++ b/src/sunstone/public/app/tabs/secgroups-tab.js @@ -0,0 +1,34 @@ +define(function(require) { + var Locale = require('utils/locale'); + var Buttons = require('./secgroups-tab/buttons'); + var Actions = require('./secgroups-tab/actions'); + var Table = require('./secgroups-tab/datatable'); + + var TAB_ID = require('./secgroups-tab/tabId'); + var DATATABLE_ID = "dataTableSecurityGroups"; + + var _dialogs = [ + ]; + + var _panels = [ + require('./secgroups-tab/panels/info') + ]; + + var Tab = { + tabId: TAB_ID, + title: Locale.tr("Security Groups"), + tabClass: "subTab", + parentTab: "infra-tab", + listHeader: ' '+Locale.tr("Security Groups"), + infoHeader: ' '+Locale.tr("Security Group"), + subheader: '  ', + resource: 'SecurityGroup', + buttons: Buttons, + actions: Actions, + dataTable: new Table(DATATABLE_ID, {actions: true, info: true}), + panels: _panels, + dialogs: _dialogs + }; + + return Tab; +}); diff --git a/src/sunstone/public/app/tabs/secgroups-tab/actions.js b/src/sunstone/public/app/tabs/secgroups-tab/actions.js new file mode 100644 index 0000000000..26ed03f49a --- /dev/null +++ b/src/sunstone/public/app/tabs/secgroups-tab/actions.js @@ -0,0 +1,210 @@ +define(function(require) { + var Sunstone = require('sunstone'); + var Notifier = require('utils/notifier'); + var Locale = require('utils/locale'); + var DataTable = require('./datatable'); + var OpenNebulaSecurityGroup = require('opennebula/securitygroup'); + + var TAB_ID = require('./tabId'); + // TODO + //var CREATE_DIALOG_ID = require('./dialogs/create/dialogId'); + + var _actions = { + /* + TODO + "SecurityGroup.create" : { + type: "create", + call: OpenNebulaSecurityGroup.create, + callback: function(request, response){ + $("a[href=back]", $("#secgroups-tab")).trigger("click"); + popFormDialog("create_security_group_form", $("#secgroups-tab")); + + addSecurityGroupElement(request, response); + notifyCustom(tr("Security Group created"), " ID: " + response.SECURITY_GROUP.ID, false); + }, + error: function(request, response){ + popFormDialog("create_security_group_form", $("#secgroups-tab")); + onError(request, response); + } + }, + + "SecurityGroup.create_dialog" : { + type: "custom", + call: function(){ + Sunstone.popUpFormPanel("create_security_group_form", "secgroups-tab", "create", true); + } + }, + */ + + "SecurityGroup.list" : { + type: "list", + call: OpenNebulaSecurityGroup.list, + callback: function(request, response) { + Sunstone.getDataTable(TAB_ID).updateView(request, response); + }, + error: Notifier.onError + }, + + "SecurityGroup.show" : { + type: "single", + call: OpenNebulaSecurityGroup.show, + callback: function(request, response) { + Sunstone.getDataTable(TAB_ID).updateElement(request, response); + if (Sunstone.rightInfoVisible($('#'+TAB_ID))) { + Sunstone.insertPanels(TAB_ID, response); + } + }, + error: Notifier.onError + }, + + "SecurityGroup.refresh" : { + type: "custom", + call: function() { + var tab = $('#' + TAB_ID); + if (Sunstone.rightInfoVisible(tab)) { + Sunstone.runAction("SecurityGroup.show", Sunstone.rightInfoResourceId(tab)); + } else { + Sunstone.getDataTable(TAB_ID).waitingNodes(); + Sunstone.runAction("SecurityGroup.list", {force: true}); + } + }, + error: Notifier.onError + }, + + "SecurityGroup.delete" : { + type: "multiple", + call : OpenNebulaSecurityGroup.del, + callback : function(request, response) { + Sunstone.getDataTable(TAB_ID).deleteElement(request, response); + }, + elements: function() { + return Sunstone.getDataTable(TAB_ID).elements(); + }, + error: Notifier.onError, + notify: true + }, + /* + TODO + "SecurityGroup.update_dialog" : { + type: "custom", + call: function(){ + var selected_nodes = securityGroupElements(); + if ( selected_nodes.length != 1 ) { + notifyMessage("Please select one (and just one) Security Group to update."); + return false; + } + + var resource_id = ""+selected_nodes[0]; + Sunstone.runAction("SecurityGroup.show_to_update", resource_id); + } + }, + + "SecurityGroup.show_to_update" : { + type: "single", + call: OpenNebulaSecurityGroup.show, + callback: function(request, response) { + // TODO: global var, better use jquery .data + sg_to_update_id = response.SECURITY_GROUP.ID; + + Sunstone.popUpFormPanel("create_security_group_form", "secgroups-tab", "create", true); + + Sunstone.popUpFormPanel("create_security_group_form", "secgroups-tab", "update", true, function(context){ + fillSecurityGroupUpdateFormPanel(response.SECURITY_GROUP, context); + }); + }, + error: Notifier.onError + }, + + "SecurityGroup.update" : { + type: "single", + call: OpenNebulaSecurityGroup.update, + callback: function(request, response){ + $("a[href=back]", $("#secgroups-tab")).trigger("click"); + popFormDialog("create_security_group_form", $("#secgroups-tab")); + + notifyMessage(tr("Security Group updated correctly")); + }, + error: function(request, response){ + popFormDialog("create_security_group_form", $("#secgroups-tab")); + + onError(request, response); + } + }, + */ + + "SecurityGroup.update_template" : { + type: "single", + call: OpenNebulaSecurityGroup.update, + callback: function(request,response){ + Sunstone.runAction('SecurityGroup.show',request.request.data[0][0]); + }, + error: Notifier.onError + }, + + "SecurityGroup.chown" : { + type: "multiple", + call: OpenNebulaSecurityGroup.chown, + callback: function (req) { + Sunstone.runAction("SecurityGroup.show", req.request.data[0]); + }, + elements: function() { + return Sunstone.getDataTable(TAB_ID).elements(); + }, + error: Notifier.onError, + notify: true + }, + + "SecurityGroup.chgrp" : { + type: "multiple", + call: OpenNebulaSecurityGroup.chgrp, + callback: function (req) { + Sunstone.runAction("SecurityGroup.show", req.request.data[0]); + }, + elements: function() { + return Sunstone.getDataTable(TAB_ID).elements(); + }, + error: Notifier.onError, + notify: true + }, + + "SecurityGroup.chmod" : { + type: "single", + call: OpenNebulaSecurityGroup.chmod, + callback: function (req) { + Sunstone.runAction("SecurityGroup.show", req.request.data[0]); + }, + elements: function() { + return Sunstone.getDataTable(TAB_ID).elements(); + }, + error: Notifier.onError, + notify: true + }, + + /* + TODO + "SecurityGroup.clone_dialog" : { + type: "custom", + call: popUpSecurityGroupCloneDialog + }, + */ + + "SecurityGroup.clone" : { + type: "single", + call: OpenNebulaSecurityGroup.clone, + error: Notifier.onError, + notify: true + }, + + "SecurityGroup.rename" : { + type: "single", + call: OpenNebulaSecurityGroup.rename, + callback: function(request) { + Sunstone.runAction('SecurityGroup.show',request.request.data[0][0]); + }, + error: Notifier.onError, + notify: true + } + }; + + return _actions; +}) diff --git a/src/sunstone/public/app/tabs/secgroups-tab/buttons.js b/src/sunstone/public/app/tabs/secgroups-tab/buttons.js new file mode 100644 index 0000000000..3fa0db45ce --- /dev/null +++ b/src/sunstone/public/app/tabs/secgroups-tab/buttons.js @@ -0,0 +1,46 @@ +define(function(require) { + var Locale = require('utils/locale'); + + var Buttons = { + "SecurityGroup.refresh" : { + type: "action", + layout: "refresh", + alwaysActive: true + }, + "SecurityGroup.create_dialog" : { + type: "create_dialog", + layout: "create" + }, + "SecurityGroup.update_dialog" : { + type: "action", + layout: "main", + text: Locale.tr("Update") + }, + "SecurityGroup.chown" : { + type: "confirm_with_select", + text: Locale.tr("Change owner"), + layout: "user_select", + select: "User", + tip: Locale.tr("Select the new owner")+":" + }, + "SecurityGroup.chgrp" : { + type: "confirm_with_select", + text: Locale.tr("Change group"), + layout: "user_select", + select: "Group", + tip: Locale.tr("Select the new group")+":" + }, + "SecurityGroup.clone_dialog" : { + type: "action", + layout: "main", + text: Locale.tr("Clone") + }, + "SecurityGroup.delete" : { + type: "confirm", + layout: "del", + text: Locale.tr("Delete") + } + }; + + return Buttons; +}) diff --git a/src/sunstone/public/app/tabs/secgroups-tab/panels/info.js b/src/sunstone/public/app/tabs/secgroups-tab/panels/info.js new file mode 100644 index 0000000000..89c175ceb9 --- /dev/null +++ b/src/sunstone/public/app/tabs/secgroups-tab/panels/info.js @@ -0,0 +1,98 @@ +define(function(require) { + /* + DEPENDENCIES + */ + + var TemplateInfo = require('hbs!./info/html'); + var Locale = require('utils/locale'); + var PermissionsTable = require('utils/panel/permissions-table'); + var RenameTr = require('utils/panel/rename-tr'); + var OpenNebulaSecurityGroup = require('opennebula/securitygroup'); + var Utils = require('../utils/common'); + + /* + TEMPLATES + */ + + var TemplateTable = require('utils/panel/template-table'); + + /* + CONSTANTS + */ + + var TAB_ID = require('../tabId'); + var PANEL_ID = require('./info/panelId'); + var RESOURCE = "SecurityGroup"; + var XML_ROOT = "SECURITY_GROUP"; + + /* + CONSTRUCTOR + */ + + function Panel(info) { + this.title = Locale.tr("Info"); + this.icon = "fa-info-circle"; + + this.element = info[XML_ROOT]; + + return this; + } + + Panel.PANEL_ID = PANEL_ID; + Panel.prototype.html = _html; + Panel.prototype.setup = _setup; + + return Panel; + + /* + FUNCTION DEFINITIONS + */ + + function _html() { + var renameTrHTML = RenameTr.html(RESOURCE, this.element.NAME); + var permissionsTableHTML = PermissionsTable.html(TAB_ID, RESOURCE, this.element); + + var rules = Utils.getRules(this.element); + + var ruleTextList = []; + + $.each(rules, function(){ + ruleTextList.push(Utils.sgRuleToSt(this)); + }); + + // TODO + //$(".resource-info-header", $("#secgroups-tab")).html(this.element.NAME); + + // TODO: simplify interface? + var strippedTemplate = $.extend({}, this.element.TEMPLATE); + delete strippedTemplate["RULE"]; + + var templateTableHTML = TemplateTable.html(strippedTemplate, RESOURCE, + Locale.tr("Attributes")); + //==== + + return TemplateInfo({ + 'element': this.element, + 'renameTrHTML': renameTrHTML, + 'permissionsTableHTML': permissionsTableHTML, + 'ruleList': ruleTextList, + 'templateTableHTML': templateTableHTML + }); + } + + function _setup(context) { + RenameTr.setup(RESOURCE, this.element.ID, context); + PermissionsTable.setup(TAB_ID, RESOURCE, this.element, context); + + // TODO: simplify interface? + var strippedTemplate = $.extend({}, this.element.TEMPLATE); + delete strippedTemplate["RULE"]; + + var hiddenValues = {RULE: this.element.TEMPLATE.RULE}; + + TemplateTable.setup(strippedTemplate, RESOURCE, this.element.ID, context, hiddenValues); + //=== + + return false; + } +}); diff --git a/src/sunstone/public/app/tabs/secgroups-tab/panels/info/html.hbs b/src/sunstone/public/app/tabs/secgroups-tab/panels/info/html.hbs new file mode 100644 index 0000000000..7b77470039 --- /dev/null +++ b/src/sunstone/public/app/tabs/secgroups-tab/panels/info/html.hbs @@ -0,0 +1,59 @@ +
+
+ + + + + + + + + + + + {{{renameTrHTML}}} + +
{{tr "Information"}}
{{tr "ID"}}{{element.ID}}
+
+
+ {{{permissionsTableHTML}}} +
+
+
+
+ + + + + + +
{{tr "Rules"}}
+ + + + + + + + + + + + {{#each ruleList}} + + + + + + + + {{/each}} + +
{{tr "Protocol"}}{{tr "Type"}}{{tr "Port Range"}}{{tr "Network"}}{{tr "ICMP Type"}}
{{PROTOCOL}}{{RULE_TYPE}}{{RANGE}}{{NETWORK}}{{ICMP_TYPE}}
+
+
+
+
+ {{{templateTableHTML}}} +
+
\ No newline at end of file diff --git a/src/sunstone/public/app/tabs/secgroups-tab/panels/info/panelId.js b/src/sunstone/public/app/tabs/secgroups-tab/panels/info/panelId.js new file mode 100644 index 0000000000..449a2d4cf7 --- /dev/null +++ b/src/sunstone/public/app/tabs/secgroups-tab/panels/info/panelId.js @@ -0,0 +1,3 @@ +define(function(require){ + return 'security_group_info_tab'; +}) \ No newline at end of file diff --git a/src/sunstone/public/app/tabs/secgroups-tab/utils/common.js b/src/sunstone/public/app/tabs/secgroups-tab/utils/common.js new file mode 100644 index 0000000000..bb35369dfa --- /dev/null +++ b/src/sunstone/public/app/tabs/secgroups-tab/utils/common.js @@ -0,0 +1,123 @@ +define(function(require) { + /* + Common functions for Security Groups + */ + + /* + DEPENDENCIES + */ + + var Locale = require('utils/locale'); + + /* + @param {Object} rule Object representing the rule as returned by OpenNebula + Returns an object with the human readable attributes of the rule. List of attributes: + PROTOCOL + RULE_TYPE + ICMP_TYPE + RANGE + NETWORK + */ + function _sgRuleToSt(rule){ + var text = {}; + + if(rule.PROTOCOL != undefined){ + switch(rule.PROTOCOL.toUpperCase()){ + case "TCP": + text["PROTOCOL"] = Locale.tr("TCP"); + break; + case "UDP": + text["PROTOCOL"] = Locale.tr("UDP"); + break; + case "ICMP": + text["PROTOCOL"] = Locale.tr("ICMP"); + break; + case "IPSEC": + text["PROTOCOL"] = Locale.tr("IPsec"); + break; + case "ALL": + text["PROTOCOL"] = Locale.tr("All"); + break; + default: + text["PROTOCOL"] = ""; + } + } else { + text["PROTOCOL"] = ""; + } + + if(rule.RULE_TYPE != undefined){ + switch(rule.RULE_TYPE.toUpperCase()){ + case "OUTBOUND": + text["RULE_TYPE"] = Locale.tr("Outbound"); + break; + case "INBOUND": + text["RULE_TYPE"] = Locale.tr("Inbound"); + break; + default: + text["RULE_TYPE"] = ""; + } + } else { + text["RULE_TYPE"] = ""; + } + + if(rule.ICMP_TYPE != undefined){ + text["ICMP_TYPE"] = icmp_to_st(rule.ICMP_TYPE); + } else { + text["ICMP_TYPE"] = ""; + } + + if(rule.RANGE != undefined && rule.RANGE != ""){ + text["RANGE"] = rule.RANGE; + } else { + text["RANGE"] = Locale.tr("All"); + } + + var network = ""; + + if(rule.NETWORK_ID != undefined && rule.NETWORK_ID != ""){ + network += (Locale.tr("Virtual Network") + " " + rule.NETWORK_ID); + } + + if(rule.SIZE != undefined && rule.SIZE != ""){ + if(network != ""){ + network += ":
"; + } + + if(rule.IP != undefined && rule.IP != ""){ + network += Locale.tr("Start") + ": " + rule.IP + ", "; + } else if(rule.MAC != undefined && rule.MAC != ""){ + network += Locale.tr("Start") + ": " + rule.MAC + ", "; + } + + network += Locale.tr("Size") + ": " + rule.SIZE; + } + + if(network == ""){ + network = Locale.tr("Any"); + } + + text["NETWORK"] = network; + + return text; + } + + /* + @param {Object} info Object representing the Security Group as returned by OpenNebula + */ + function _getRules(info){ + var rules = info.TEMPLATE.RULE; + + if (!rules){ //empty + rules = []; + } else if (rules.constructor != Array) { //>1 rule + rules = [rules]; + } + + return rules; + } + + return { + 'sgRuleToSt': _sgRuleToSt, + 'getRules': _getRules + }; +});