diff --git a/src/sunstone/public/app/tabs/hosts-tab.js b/src/sunstone/public/app/tabs/hosts-tab.js index 6a99395610..252f6aefbf 100644 --- a/src/sunstone/public/app/tabs/hosts-tab.js +++ b/src/sunstone/public/app/tabs/hosts-tab.js @@ -20,6 +20,10 @@ define(function(require) { require('./hosts-tab/panels/esx') ]; + var _formPanels = [ + require('./hosts-tab/form-panels/create') + ] + var Tab = { tabId: TAB_ID, title: Locale.tr("Hosts"), @@ -37,6 +41,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/hosts-tab/actions.js b/src/sunstone/public/app/tabs/hosts-tab/actions.js index 15a388cc74..4dac23e9be 100644 --- a/src/sunstone/public/app/tabs/hosts-tab/actions.js +++ b/src/sunstone/public/app/tabs/hosts-tab/actions.js @@ -6,25 +6,29 @@ define(function(require) { var OpenNebulaHost = require('opennebula/host'); var TAB_ID = require('./tabId'); - var CREATE_DIALOG_ID = require('./dialogs/create/dialogId'); + var CREATE_DIALOG_ID = require('./form-panels/create/formPanelId'); var _actions = { "Host.create" : { type: "create", call : OpenNebulaHost.create, callback : function(request, response) { - Sunstone.getDialog(CREATE_DIALOG_ID).hide(); - Sunstone.getDialog(CREATE_DIALOG_ID).reset(); - DataTable.addElement(request, response); + $("a[href=back]", $("#"+TAB_ID)).trigger("click"); + Sunstone.hideFormPanelLoading($("#"+TAB_ID)); + Sunstone.resetFormPanel(TAB_ID, CREATE_DIALOG_ID); + Sunstone.getDataTable(TAB_ID).addElement(request, response); + }, + error: function(request, response) { + Sunstone.hideFormPanelLoading($("#" + TAB_ID)); + Notifier.onError(request, response); }, - error: Notifier.onError, notify: true }, "Host.create_dialog" : { type: "custom", call: function() { - Sunstone.getDialog(CREATE_DIALOG_ID).show(); + Sunstone.showFormPanel(TAB_ID, CREATE_DIALOG_ID, "create"); } }, diff --git a/src/sunstone/public/app/tabs/hosts-tab/form-panels/create.js b/src/sunstone/public/app/tabs/hosts-tab/form-panels/create.js new file mode 100644 index 0000000000..267c49a7b5 --- /dev/null +++ b/src/sunstone/public/app/tabs/hosts-tab/form-panels/create.js @@ -0,0 +1,543 @@ +define(function(require) { + /* + DEPENDENCIES + */ + + var BaseFormPanel = require('utils/form-panels/form-panel'); + var Sunstone = require('sunstone'); + var Locale = require('utils/locale'); + var Tips = require('utils/tips'); + var Notifier = require('utils/notifier'); + var ResourceSelect = require('utils/resource-select') + + /* + TEMPLATES + */ + + var TemplateWizardHTML = require('hbs!./create/wizard'); + + /* + CONSTANTS + */ + + var FORM_PANEL_ID = require('./create/formPanelId'); + + /* + CONSTRUCTOR + */ + + function FormPanel() { + this.formPanelId = FORM_PANEL_ID; + this.actions = { + 'create': { + 'title': Locale.tr("Create Host"), + 'buttonText': Locale.tr("Create"), + 'resetButton': true + } + } + + 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.submitWizard = _submitWizard; + FormPanel.prototype.onShow = _onShow; + FormPanel.prototype.setup = _setup; + + return FormPanel; + + /* + FUNCTION DEFINITIONS + */ + + function _htmlWizard() { + return TemplateWizardHTML({formPanelId: this.formPanelId}); + } + + function _setup(context) { + + $(".drivers", context).hide(); + + $("#host_type_mad", context).on("change", function() { + $("#vmm_mad", context).val(this.value).change(); + $("#im_mad", context).val(this.value).change(); + + if (this.value == "custom") { + $(".vcenter_credentials", context).hide(); + $("#vnm_mads", context).show(); + $("#name_container", context).show(); + $("#create_host_submit", context).show(); + $(".drivers", context).show(); + } else if (this.value == "vcenter") { + $("#vnm_mads", context).hide(); + $("#name_container", context).hide(); + $(".vcenter_credentials", context).show(); + $("#create_host_submit", context).hide(); + $(".drivers", context).hide(); + } else { + $(".vcenter_credentials", context).hide(); + $("#vnm_mads", context).show(); + $("#name_container", context).show(); + $("#create_host_submit", context).show(); + $(".drivers", context).hide(); + } + }) + + /* TODO + $("#get_vcenter_clusters", context).on("click", function() { + // TODO notify if credentials empty + var container = $(".vcenter_clusters", context); + + container.html(generateAdvancedSection({ + html_id: "/vcenter", + title: Locale.tr("Clusters"), + content: '' + + '' + + '' + + '' + })) + + $('a', container).trigger("click") + + $.ajax({ + url: 'vcenter', + type: "GET", + data: {timeout: false}, + dataType: "json", + headers: { + "X_VCENTER_USER": $("#vcenter_user", context).val(), + "X_VCENTER_PASSWORD": $("#vcenter_password", context).val(), + "X_VCENTER_HOST": $("#vcenter_host", context).val() + }, + success: function(response) { + $("#vcenter_user", context).atLocale.tr("disabled", "disabled") + $("#vcenter_password", context).atLocale.tr("disabled", "disabled") + $("#vcenter_host", context).atLocale.tr("disabled", "disabled") + $("#get_vcenter_clusters", context).hide(); + $(".import_vcenter_clusters_div", context).show(); + + $(".content", container).html(""); + + $('
' + + '
' + + '

' + Locale.tr("Please select the vCenter Clusters to be imported to OpenNebula. Each vCenter Cluster will be included as a new OpenNebula Host") + '

' + + '
' + + '
').appendTo($(".content", container)) + + $.each(response, function(datacenter_name, clusters) { + $('
' + + '
' + + '
' + + datacenter_name + ' ' + Locale.tr("Datacenter") + + '
' + + '
' + + '
').appendTo($(".content", container)) + + if (clusters.length == 0) { + $('
' + + '
' + + '' + + '
' + + '
').appendTo($(".content", container)) + } else { + $.each(clusters, function(id, cluster_name) { + var row = $('
' + + '
' + + '
' + + '' + + '
' + + '
' + + '
' + + '
' + + '
' + + '
' + + '
').appendTo($(".content", container)) + + $(".cluster_name", row).data("cluster_name", cluster_name) + $(".cluster_name", row).data("datacenter_name", datacenter_name) + }); + } + }); + + var templates_container = $(".vcenter_templates", context); + var vms_container = $(".vcenter_vms", context); + var networks_container = $(".vcenter_networks", context); + + var vcenter_user = $("#vcenter_user", context).val(); + var vcenter_password = $("#vcenter_password", context).val(); + var vcenter_host = $("#vcenter_host", context).val(); + + fillVCenterTemplates({ + container: templates_container, + vcenter_user: vcenter_user, + vcenter_password: vcenter_password, + vcenter_host: vcenter_host + }); + + fillVCenterNetworks({ + container: networks_container, + vcenter_user: vcenter_user, + vcenter_password: vcenter_password, + vcenter_host: vcenter_host + }); + }, + error: function(response) { + $(".vcenter_clusters", context).html('') + onError({}, OpenNebula.Error(response)); + } + }); + + return false; + }) + + $("#import_vcenter_clusters", context).on("click", function() { + $(this).hide(); + + var cluster_id = $('#host_cluster_id .resource_list_select', context).val(); + if (!cluster_id) cluster_id = "-1"; + + $.each($(".cluster_name:checked", context), function() { + var cluster_context = $(this).closest(".vcenter_cluster"); + $(".vcenter_host_result:not(.success)", cluster_context).html('' + + '' + + '' + + ''); + + var host_json = { + "host": { + "name": $(this).data("cluster_name"), + "vm_mad": "vcenter", + "vnm_mad": "dummy", + "im_mad": "vcenter", + "cluster_id": cluster_id + } + }; + + OpenNebula.Host.create({ + timeout: true, + data: host_json, + success: function(request, response) { + OpenNebula.Helper.clear_cache("HOST"); + + $(".vcenter_host_result", cluster_context).addClass("success").html( + '' + + '' + + '' + + ''); + + $(".vcenter_host_response", cluster_context).html('

' + + Locale.tr("Host created successfully") + ' ID:' + response.HOST.ID + + '

'); + + var template_raw = + "VCENTER_USER=\"" + $("#vcenter_user", context).val() + "\"\n" + + "VCENTER_PASSWORD=\"" + $("#vcenter_password", context).val() + "\"\n" + + "VCENTER_HOST=\"" + $("#vcenter_host", context).val() + "\"\n"; + + Sunstone.runAction("Host.update_template", response.HOST.ID, template_raw); + addHostElement(request, response); + }, + error: function (request, error_json) { + $(".vcenter_host_result", context).html('' + + '' + + '' + + ''); + + $(".vcenter_host_response", context).html('

' + + (error_json.error.message || Locale.tr("Cannot contact server: is it running and reachable?")) + + '

'); + } + }); + }) + + $.each($(".template_name:checked", context), function() { + var template_context = $(this).closest(".vcenter_template"); + + $(".vcenter_template_result:not(.success)", template_context).html( + '' + + '' + + '' + + ''); + + var template_json = { + "vmtemplate": { + "template_raw": $(this).data("one_template") + } + }; + + OpenNebula.Template.create({ + timeout: true, + data: template_json, + success: function(request, response) { + OpenNebula.Helper.clear_cache("VMTEMPLATE"); + $(".vcenter_template_result", template_context).addClass("success").html( + '' + + '' + + '' + + ''); + + $(".vcenter_template_response", template_context).html('

' + + Locale.tr("Template created successfully") + ' ID:' + response.VMTEMPLATE.ID + + '

'); + }, + error: function (request, error_json) { + $(".vcenter_template_result", template_context).html('' + + '' + + '' + + ''); + + $(".vcenter_template_response", template_context).html('

' + + (error_json.error.message || Locale.tr("Cannot contact server: is it running and reachable?")) + + '

'); + } + }); + }) + + $.each($(".vm_name:checked", context), function() { + var vm_context = $(this).closest(".vcenter_vm"); + + $(".vcenter_vm_result:not(.success)", vm_context).html( + '' + + '' + + '' + + ''); + + var vm_json = { + "vm": { + "vm_raw": $(this).data("one_vm") + } + }; + + var host_id_to_deploy = $(this).data("vm_to_host"); + + OpenNebula.VM.create({ + timeout: true, + data: vm_json, + success: function(request, response) { + OpenNebula.Helper.clear_cache("VM"); + + var extra_info = {}; + + extra_info['host_id'] = host_id_to_deploy; + extra_info['ds_id'] = -1; + extra_info['enforce'] = false; + + Sunstone.runAction("VM.deploy_action", response.VM.ID, extra_info); + + $(".vcenter_vm_result", vm_context).addClass("success").html( + '' + + '' + + '' + + ''); + + $(".vcenter_vm_response", vm_context).html('

' + + Locale.tr("VM imported successfully") + ' ID:' + response.VM.ID + + '

'); + }, + error: function (request, error_json) { + $(".vcenter_vm_response", vm_context).html('' + + '' + + '' + + ''); + + $(".vcenter_vm_response", vm_context).html('

' + + (error_json.error.message || Locale.tr("Cannot contact server: is it running and reachable?")) + + '

'); + } + }); + }) + + $.each($(".network_name:checked", context), function() { + var network_context = $(this).closest(".vcenter_network"); + + $(".vcenter_network_result:not(.success)", network_context).html( + '' + + '' + + '' + + ''); + + var network_size = $(".netsize", network_context).val(); + var network_tmpl = $(this).data("one_network"); + var netname = $(this).data("network_name"); + var type = $('.type_select', network_context).val(); + + var ar_array = []; + ar_array.push("TYPE=" + type); + ar_array.push("SIZE=" + network_size); + + switch (type) { + case 'ETHER': + var mac = $('.eth_mac_net', network_context).val(); + + if (mac) { + ar_array.push("MAC=" + mac); + } + + break; + case 'IP4': + var mac = $('.four_mac_net', network_context).val(); + var ip = $('.four_ip_net', network_context).val(); + + if (mac) { + ar_array.push("MAC=" + mac); + } + if (ip) { + ar_array.push("IP=" + ip); + } + + break; + case 'IP6': + var mac = $('.six_mac_net', network_context).val(); + var gp = $('.six_global_net', network_context).val(); + var ula = $('.six_mac_net', network_context).val(); + + if (mac) { + ar_array.push("MAC=" + mac); + } + if (gp) { + ar_array.push("GLOBAL_PREFIX=" + gp); + } + if (ula) { + ar_array.push("ULA_PREFIX=" + ula); + } + + break; + } + + network_tmpl += "\nAR=[" + network_tmpl += ar_array.join(",\n") + network_tmpl += "]" + + if ($(".vlaninfo", network_context)) { + network_tmpl += "VLAN=\"YES\"\n"; + network_tmpl += "VLAN_ID=" + $(".vlaninfo", network_context).val() + "\n"; + } + + var vnet_json = { + "vnet": { + "vnet_raw": network_tmpl + } + }; + + OpenNebula.Network.create({ + timeout: true, + data: vnet_json, + success: function(request, response) { + OpenNebula.Helper.clear_cache("VNET"); + $(".vcenter_network_result", network_context).addClass("success").html( + '' + + '' + + '' + + ''); + + $(".vcenter_network_response", network_context).html('

' + + Locale.tr("Virtual Network created successfully") + ' ID:' + response.VNET.ID + + '

'); + }, + error: function (request, error_json) { + $(".vcenter_network_result", network_context).html('' + + '' + + '' + + ''); + + $(".vcenter_network_response", network_context).html('

' + + (error_json.error.message || Locale.tr("Cannot contact server: is it running and reachable?")) + + '

'); + } + }); + }); + + return false + }); + */ + + // Show custom driver input only when custom is selected in selects + $('input[name="custom_vmm_mad"],' + + 'input[name="custom_im_mad"],' + + 'input[name="custom_vnm_mad"]', context).parent().hide(); + + $('select#vmm_mad', context).change(function() { + if ($(this).val() == "custom") + $('input[name="custom_vmm_mad"]').parent().show(); + else + $('input[name="custom_vmm_mad"]').parent().hide(); + }); + + $('select#im_mad', context).change(function() { + if ($(this).val() == "custom") + $('input[name="custom_im_mad"]').parent().show(); + else + $('input[name="custom_im_mad"]').parent().hide(); + }); + + $('select#vnm_mad', context).change(function() { + if ($(this).val() == "custom") + $('input[name="custom_vnm_mad"]').parent().show(); + else + $('input[name="custom_vnm_mad"]').parent().hide(); + }); + + $('#create_host_form').on("keyup keypress", function(e) { + var code = e.keyCode || e.which; + if (code == 13) { + e.preventDefault(); + return false; + } + }); + + Tips.setup(); + return false; + } + + function _submitWizard(context) { + var name = $('#name', this).val(); + if (!name) { + Sunstone.hideFormPanelLoading(context); + Notifier.notifyError(Locale.tr("Host name missing!")); + return false; + } + + var cluster_id = $('#host_cluster_id .resource_list_select', this).val(); + if (!cluster_id) cluster_id = "-1"; + + var vmm_mad = $('select#vmm_mad', this).val(); + vmm_mad = vmm_mad == "custom" ? $('input[name="custom_vmm_mad"]').val() : vmm_mad; + var im_mad = $('select#im_mad', this).val(); + im_mad = im_mad == "custom" ? $('input[name="custom_im_mad"]').val() : im_mad; + var vnm_mad = $('select#vnm_mad', this).val(); + vnm_mad = vnm_mad == "custom" ? $('input[name="custom_vnm_mad"]').val() : vnm_mad; + + var host_json = { + "host": { + "name": name, + "vm_mad": vmm_mad, + "vnm_mad": vnm_mad, + "im_mad": im_mad, + "cluster_id": cluster_id + } + }; + + //Create the OpenNebula.Host. + //If it is successfull we refresh the list. + Sunstone.runAction("Host.create", host_json); + return false; + } + + function _onShow(context) { + $("#name", context).focus(); + + var cluster_id = $("#host_cluster_id .resource_list_select", context).val(); + if (!cluster_id) cluster_id = "-1"; + + ResourceSelect.insert('#host_cluster_id', context, "Cluster", cluster_id, false); + return false; + } +}); diff --git a/src/sunstone/public/app/tabs/hosts-tab/form-panels/create/formPanelId.js b/src/sunstone/public/app/tabs/hosts-tab/form-panels/create/formPanelId.js new file mode 100644 index 0000000000..697ee906e8 --- /dev/null +++ b/src/sunstone/public/app/tabs/hosts-tab/form-panels/create/formPanelId.js @@ -0,0 +1,3 @@ +define(function(require){ + return 'createHostForm'; +}) \ No newline at end of file diff --git a/src/sunstone/public/app/tabs/hosts-tab/form-panels/create/wizard.hbs b/src/sunstone/public/app/tabs/hosts-tab/form-panels/create/wizard.hbs new file mode 100644 index 0000000000..f4333cb4fa --- /dev/null +++ b/src/sunstone/public/app/tabs/hosts-tab/form-panels/create/wizard.hbs @@ -0,0 +1,139 @@ +
+
+
+ + +
+
+ +
+
+
+
+
+ + +
+
+
+
+ + +
+
+ + +
+
+
+
+
+
+ {{tr "Drivers"}} +
+
+
+
+ + +
+
+ + +
+
+
+
+
+
+ + +
+
+ + +
+
+
+
+
+
+ +
\ No newline at end of file