From 22dbb0402967ea0b708e963bc03787077e9cf50d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn?= Date: Mon, 1 Jun 2015 19:01:50 +0200 Subject: [PATCH] ture #3748: Security Group create wizard --- src/sunstone/public/app/tabs/secgroups-tab.js | 5 + .../public/app/tabs/secgroups-tab/actions.js | 40 ++- .../tabs/secgroups-tab/form-panels/create.js | 296 ++++++++++++++++++ .../form-panels/create/advanced.hbs | 12 + .../form-panels/create/formPanelId.js | 3 + .../form-panels/create/wizard.hbs | 137 ++++++++ .../app/tabs/secgroups-tab/utils/common.js | 39 ++- 7 files changed, 509 insertions(+), 23 deletions(-) create mode 100644 src/sunstone/public/app/tabs/secgroups-tab/form-panels/create.js create mode 100644 src/sunstone/public/app/tabs/secgroups-tab/form-panels/create/advanced.hbs create mode 100644 src/sunstone/public/app/tabs/secgroups-tab/form-panels/create/formPanelId.js create mode 100644 src/sunstone/public/app/tabs/secgroups-tab/form-panels/create/wizard.hbs diff --git a/src/sunstone/public/app/tabs/secgroups-tab.js b/src/sunstone/public/app/tabs/secgroups-tab.js index 0c1d86e305..684bf1d658 100644 --- a/src/sunstone/public/app/tabs/secgroups-tab.js +++ b/src/sunstone/public/app/tabs/secgroups-tab.js @@ -14,6 +14,10 @@ define(function(require) { require('./secgroups-tab/panels/info') ]; + var _formPanels = [ + require('./secgroups-tab/form-panels/create') + ]; + var Tab = { tabId: TAB_ID, title: Locale.tr("Security Groups"), @@ -27,6 +31,7 @@ define(function(require) { actions: Actions, dataTable: new Table(DATATABLE_ID, {actions: true, info: true}), panels: _panels, + formPanels: _formPanels, dialogs: _dialogs }; diff --git a/src/sunstone/public/app/tabs/secgroups-tab/actions.js b/src/sunstone/public/app/tabs/secgroups-tab/actions.js index 26ed03f49a..026e905052 100644 --- a/src/sunstone/public/app/tabs/secgroups-tab/actions.js +++ b/src/sunstone/public/app/tabs/secgroups-tab/actions.js @@ -6,35 +6,31 @@ define(function(require) { var OpenNebulaSecurityGroup = require('opennebula/securitygroup'); var TAB_ID = require('./tabId'); - // TODO - //var CREATE_DIALOG_ID = require('./dialogs/create/dialogId'); + var CREATE_DIALOG_ID = require('./form-panels/create/formPanelId'); 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" : { + type: "create", + call: OpenNebulaSecurityGroup.create, + callback: function(request, response) { + Sunstone.resetFormPanel(TAB_ID, CREATE_DIALOG_ID); + Sunstone.hideFormPanel(TAB_ID); + Sunstone.getDataTable(TAB_ID).addElement(request, response); + }, + error: function(request, response) { + Sunstone.hideFormPanelLoading(TAB_ID); + Notifier.onError(request, response); + }, + notify: true }, "SecurityGroup.create_dialog" : { - type: "custom", - call: function(){ - Sunstone.popUpFormPanel("create_security_group_form", "secgroups-tab", "create", true); - } + type: "custom", + call: function() { + Sunstone.showFormPanel(TAB_ID, CREATE_DIALOG_ID, "create"); + } }, - */ "SecurityGroup.list" : { type: "list", diff --git a/src/sunstone/public/app/tabs/secgroups-tab/form-panels/create.js b/src/sunstone/public/app/tabs/secgroups-tab/form-panels/create.js new file mode 100644 index 0000000000..b8da243b8b --- /dev/null +++ b/src/sunstone/public/app/tabs/secgroups-tab/form-panels/create.js @@ -0,0 +1,296 @@ +define(function(require) { + /* + DEPENDENCIES + */ + + require('foundation.tab'); + var BaseFormPanel = require('utils/form-panels/form-panel'); + var Sunstone = require('sunstone'); + var Locale = require('utils/locale'); + var Tips = require('utils/tips'); + var TemplateUtils = require('utils/template-utils'); + var WizardFields = require('utils/wizard-fields'); + var VNetsTable = require('tabs/vnets-tab/datatable'); + var Utils = require('../utils/common'); + + /* + TEMPLATES + */ + + var TemplateWizardHTML = require('hbs!./create/wizard'); + var TemplateAdvancedHTML = require('hbs!./create/advanced'); + + /* + CONSTANTS + */ + + var FORM_PANEL_ID = require('./create/formPanelId'); + var TAB_ID = require('../tabId'); + + /* + CONSTRUCTOR + */ + + function FormPanel() { + this.formPanelId = FORM_PANEL_ID; + this.tabId = TAB_ID; + this.actions = { + 'create': { + 'title': Locale.tr("Create Security Group"), + 'buttonText': Locale.tr("Create"), + 'resetButton': true + }, + 'update': { + 'title': Locale.tr("Update Security Group"), + 'buttonText': Locale.tr("Update"), + 'resetButton': false + } + }; + + BaseFormPanel.call(this); + } + + FormPanel.FORM_PANEL_ID = FORM_PANEL_ID; + FormPanel.prototype = Object.create(BaseFormPanel.prototype); + FormPanel.prototype.constructor = FormPanel; + FormPanel.prototype.htmlWizard = _htmlWizard; + FormPanel.prototype.htmlAdvanced = _htmlAdvanced; + FormPanel.prototype.submitWizard = _submitWizard; + FormPanel.prototype.submitAdvanced = _submitAdvanced; + FormPanel.prototype.onShow = _onShow; + FormPanel.prototype.fill = _fill; + FormPanel.prototype.setup = _setup; + + return FormPanel; + + /* + FUNCTION DEFINITIONS + */ + + function _htmlWizard() { + + var opts = { + info: false, + select: true + }; + + this.vnetsTable = new VNetsTable("new_sg_rule", opts); + + return TemplateWizardHTML({ + 'formPanelId': this.formPanelId, + 'vnetsTableHTML': this.vnetsTable.dataTableHTML + }); + } + + function _htmlAdvanced() { + return TemplateAdvancedHTML({formPanelId: this.formPanelId}); + } + + function _setup(context) { + var that = this; + + context.off("change", '.security_group_rule_protocol'); + context.on("change", '.security_group_rule_protocol', function(){ + $('.range_row', context).hide(); + $('.range_row input', context).removeAttr('required'); + + $('.icmp_type_wrapper', context).hide(); + + switch ($(this).val()) { + case "TCP": + case "UDP": + $('.range_row', context).show(); + $(".range_row select", context).trigger("change"); + break; + case "ICMP": + $('.icmp_type_wrapper', context).show(); + break; + case "IPSEC": + case "ALL": + break; + } + }); + + context.off("change", '.security_group_rule_network_sel'); + context.on("change", '.security_group_rule_network_sel', function(){ + $('.security_group_rule_network',context).hide(); + $('div.security_group_rule_network input',context).removeAttr('required'); + that.vnetsTable.idInput().removeAttr("required"); + + $('.vnet_select',context).hide(); + + switch ($(this).val()) { + case "ANY": + break; + case "NETWORK": + $('.security_group_rule_network',context).show(); + $('div.security_group_rule_network input',context).attr('required', ''); + break; + case "VNET": + $('.vnet_select',context).show(); + that.vnetsTable.idInput().attr("required", ""); + + that.vnetsTable.refreshResourceTableSelect(); + + break; + } + }); + + context.off("change", '.security_group_rule_range_sel'); + context.on("change", '.security_group_rule_range_sel', function(){ + switch ($(this).val()) { + case "ALL": + $('.security_group_rule_range', context).hide(); + $(".security_group_rule_range input", context).removeAttr('required'); + break; + case "RANGE": + $('.security_group_rule_range', context).show(); + $(".security_group_rule_range input", context).attr('required', ''); + break; + } + }); + + $('#rules_form_wizard',context).off('invalid'); + $('#rules_form_wizard',context).off('valid'); + + $('#rules_form_wizard',context).on('invalid', function () { + + }).on('valid', function() { + var rule = {}; + + rule["PROTOCOL"] = $(".security_group_rule_protocol", context).val(); + rule["RULE_TYPE"] = $(".security_group_rule_type", context).val(); + + switch ($('.security_group_rule_range_sel', context).val()) { + case "ALL": + break; + case "RANGE": + rule["RANGE"] = $(".security_group_rule_range input", context).val(); + break; + } + + switch ($('.security_group_rule_network_sel', context).val()) { + case "ANY": + break; + case "NETWORK": + rule["IP"] = $('#security_group_rule_first_ip', context).val(); + rule["SIZE"] = $('#security_group_rule_size', context).val(); + break; + case "VNET": + rule["NETWORK_ID"] = that.vnetsTable.retrieveResourceTableSelect(); + break; + } + + if (rule["PROTOCOL"] == "ICMP" ){ + var icmp_type_val = $(".security_group_rule_icmp_type", context).val(); + + if (icmp_type_val != ""){ + rule["ICMP_TYPE"] = icmp_type_val; + } + } + + var text = Utils.sgRuleToSt(rule); + + $(".security_group_rules tbody", context).append( + '\ + '+text.PROTOCOL+'\ + '+text.RULE_TYPE+'\ + '+text.RANGE+'\ + '+text.NETWORK+'\ + '+text.ICMP_TYPE+'\ + \ + \ + \ + '); + + // Add data to tr element + $(".security_group_rules tbody", context).children("tr").last().data("rule", rule); + + // Reset new rule fields + $('#new_rule_wizard select option', context).prop('selected', function() { + return this.defaultSelected; + }); + + $('#new_rule_wizard select', context).trigger("change"); + + $('#new_rule_wizard input', context).val(""); + + that.vnetsTable.resetResourceTableSelect(); + }); + + context.off("click", ".security_group_rules i.remove-tab"); + context.on("click", ".security_group_rules i.remove-tab", function(){ + var tr = $(this).closest('tr'); + tr.remove(); + }); + + context.foundation(); + + this.vnetsTable.initialize(); + + Tips.setup(); + + $('#new_rule_wizard select', context).trigger("change"); + + return false; + } + + function _submitWizard(context) { + + var name = $('#security_group_name', context).val(); + var description = $('#security_group_description', context).val(); + + var rules = []; + + $(".security_group_rules tbody tr").each(function(){ + rules.push($(this).data("rule")); + }); + + var security_group_json = { + "NAME" : name, + "DESCRIPTION": description, + "RULE" : rules + }; + + if (this.action == "create") { + security_group_json = { + "security_group" : security_group_json + }; + + Sunstone.runAction("SecurityGroup.create",security_group_json); + return false; + } else if (this.action == "update") { + delete security_group_json["NAME"]; + + Sunstone.runAction( + "SecurityGroup.update", + this.resourceId, + TemplateUtils.templateToString(security_group_json)); + + return false; + } + } + + function _submitAdvanced(context) { + if (this.action == "create") { + var template = $('textarea#template', context).val(); + var security_group_json = {security_group: {security_group_raw: template}}; + Sunstone.runAction("SecurityGroup.create",security_group_json); + return false; + } else if (this.action == "update") { + var template_raw = $('textarea#template', context).val(); + Sunstone.runAction("SecurityGroup.update", this.resourceId, template_raw); + return false; + } + } + + function _onShow(context) { + this.vnetsTable.refreshResourceTableSelect(); + } + + function _fill(context, element) { + this.resourceId = element.ID; + + // TODO + } +}); diff --git a/src/sunstone/public/app/tabs/secgroups-tab/form-panels/create/advanced.hbs b/src/sunstone/public/app/tabs/secgroups-tab/form-panels/create/advanced.hbs new file mode 100644 index 0000000000..7ec4ca375b --- /dev/null +++ b/src/sunstone/public/app/tabs/secgroups-tab/form-panels/create/advanced.hbs @@ -0,0 +1,12 @@ +
+
+
+

{{tr "Write the Security Group template here"}}

+
+
+
+
+ +
+
+
\ No newline at end of file diff --git a/src/sunstone/public/app/tabs/secgroups-tab/form-panels/create/formPanelId.js b/src/sunstone/public/app/tabs/secgroups-tab/form-panels/create/formPanelId.js new file mode 100644 index 0000000000..105b7791ec --- /dev/null +++ b/src/sunstone/public/app/tabs/secgroups-tab/form-panels/create/formPanelId.js @@ -0,0 +1,3 @@ +define(function(require){ + return 'createSecurityGroupForm'; +}); \ No newline at end of file diff --git a/src/sunstone/public/app/tabs/secgroups-tab/form-panels/create/wizard.hbs b/src/sunstone/public/app/tabs/secgroups-tab/form-panels/create/wizard.hbs new file mode 100644 index 0000000000..5d79f00e48 --- /dev/null +++ b/src/sunstone/public/app/tabs/secgroups-tab/form-panels/create/wizard.hbs @@ -0,0 +1,137 @@ +
+
+
+
+ + +
+
+ +