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 index 1a2e771fd7..ad7cc9a4fe 100644 --- a/src/sunstone/public/app/tabs/hosts-tab/form-panels/create.js +++ b/src/sunstone/public/app/tabs/hosts-tab/form-panels/create.js @@ -8,7 +8,12 @@ define(function(require) { var Locale = require('utils/locale'); var Tips = require('utils/tips'); var Notifier = require('utils/notifier'); - var ResourceSelect = require('utils/resource-select') + var ResourceSelect = require('utils/resource-select'); + var OpenNebulaError = require('opennebula/error'); + var OpenNebulaHost = require('opennebula/host'); + var OpenNebulaTemplate = require('opennebula/template'); + var OpenNebulaVM = require('opennebula/vm'); + var OpenNebulaNetwork = require('opennebula/network'); /* TEMPLATES @@ -71,396 +76,381 @@ define(function(require) { $(".vcenter_credentials", context).hide(); $("#vnm_mads", context).show(); $("#name_container", context).show(); - $("#create_host_submit", context).show(); + $("#"+TAB_ID+"submit_button", "#"+TAB_ID).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(); + $("#"+TAB_ID+"submit_button", "#"+TAB_ID).hide(); $(".drivers", context).hide(); } else { $(".vcenter_credentials", context).hide(); $("#vnm_mads", context).show(); $("#name_container", context).show(); - $("#create_host_submit", context).show(); + $("#"+TAB_ID+"submit_button", "#"+TAB_ID).show(); $(".drivers", context).hide(); } - }) + }); - /* TODO $("#get_vcenter_clusters", context).on("click", function() { - // TODO notify if credentials empty - var container = $(".vcenter_clusters", context); + // TODO notify if credentials empty + var container = $(".vcenter_clusters", context); - container.html(generateAdvancedSection({ - html_id: "/vcenter", - title: Locale.tr("Clusters"), - content: '' + - '' + - '' + - '' - })) + container.show(); - $('a', container).trigger("click") + $(".accordion_advanced_toggle", 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(); + $.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).attr("disabled", "disabled"); + $("#vcenter_password", context).attr("disabled", "disabled"); + $("#vcenter_host", context).attr("disabled", "disabled"); + $("#get_vcenter_clusters", context).hide(); + $(".import_vcenter_clusters_div", context).show(); - $(".content", container).html(""); + $(".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") + '
' + + '' + Locale.tr("Please select the vCenter Clusters to be imported to OpenNebula. Each vCenter Cluster will be included as a new OpenNebula Host") + '
' + + '' + '' + - 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( + OpenNebulaHost.create({ + timeout: true, + data: host_json, + success: function(request, response) { + $(".vcenter_host_result", cluster_context).addClass("success").html( '' + '' + - '' + + '' + ''); - var template_json = { - "vmtemplate": { - "template_raw": $(this).data("one_template") - } - }; + $(".vcenter_host_response", cluster_context).html('' + + Locale.tr("Host created successfully") + ' ID:' + response.HOST.ID + + '
'); - 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( - '' + - '' + - '' + - ''); + var template_raw = + "VCENTER_USER=\"" + $("#vcenter_user", context).val() + "\"\n" + + "VCENTER_PASSWORD=\"" + $("#vcenter_password", context).val() + "\"\n" + + "VCENTER_HOST=\"" + $("#vcenter_host", context).val() + "\"\n"; - $(".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( - '' + + Sunstone.runAction("Host.update_template", response.HOST.ID, template_raw); + Sunstone.getDataTable(TAB_ID).addElement(request, response); + }, + error: function (request, error_json) { + $(".vcenter_host_result", 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 + $(".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") + } + }; + + OpenNebulaTemplate.create({ + timeout: true, + data: template_json, + success: function(request, response) { + $(".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"); + + OpenNebulaVM.create({ + timeout: true, + data: vm_json, + success: function(request, response) { + 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 + } + }; + + OpenNebulaNetwork.create({ + timeout: true, + data: vnet_json, + success: function(request, response) { + $(".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"],' + @@ -542,4 +532,268 @@ define(function(require) { ResourceSelect.insert('#host_cluster_id', context, "Cluster", cluster_id, false); return false; } + + /* + Retrieve the list of templates from vCenter and fill the container with them + + opts = { + datacenter: "Datacenter Name", + cluster: "Cluster Name", + container: Jquery div to inject the html, + vcenter_user: vCenter Username, + vcenter_password: vCenter Password, + vcenter_host: vCenter Host + } + */ + function fillVCenterTemplates(opts) { + var path = '/vcenter/templates'; + + opts.container.show(); + + $(".accordion_advanced_toggle", opts.container).trigger("click"); + + $.ajax({ + url: path, + type: "GET", + data: {timeout: false}, + dataType: "json", + headers: { + "X_VCENTER_USER": opts.vcenter_user, + "X_VCENTER_PASSWORD": opts.vcenter_password, + "X_VCENTER_HOST": opts.vcenter_host + }, + success: function(response){ + $(".content", opts.container).html(""); + + $('' + Locale.tr("Please select the vCenter Templates to be imported to OpenNebula.") + '
' + + '' + Locale.tr("Please select the vCenter Networks to be imported to OpenNebula.") + '
' + + '