From 2f12f61965ff91b9e72e7722affd7fd0ac2b975c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn?= Date: Thu, 1 Oct 2015 16:49:03 +0200 Subject: [PATCH] Feature #4012: Add list of vcenter customization specs to sunstone --- src/sunstone/public/app/opennebula/host.js | 189 ++++++++++++------ .../tabs/templates-tab/form-panels/create.js | 14 ++ .../form-panels/create/wizard-tabs/context.js | 81 ++++++++ .../create/wizard-tabs/context/html.hbs | 10 + .../form-panels/create/wizard-tabs/general.js | 21 +- .../form-panels/create/wizard-tabs/hybrid.js | 30 +-- src/sunstone/sunstone-server.rb | 14 +- 7 files changed, 270 insertions(+), 89 deletions(-) diff --git a/src/sunstone/public/app/opennebula/host.js b/src/sunstone/public/app/opennebula/host.js index b2a4ecb8ae..1eb7ec04be 100644 --- a/src/sunstone/public/app/opennebula/host.js +++ b/src/sunstone/public/app/opennebula/host.js @@ -21,14 +21,15 @@ define(function(require) { var OpenNebulaHelper = require('./helper'); - var pcisCache; - var pcisWaiting = false; + var infrastructureCache; + var infrastructureWaiting = false; var pcisCallbacks = []; + var customizationsCallbacks = []; var CACHE_EXPIRE = 300000; //ms var _clearCache = function() { - pcisCache = null; + infrastructureCache = null; //console.log("Host.pciDevices. Cache cleaned"); }; @@ -124,13 +125,13 @@ define(function(require) { var callbackError = params.error; var request = OpenNebulaHelper.request(RESOURCE, "infrastructure"); - if (pcisCache && - pcisCache["timestamp"] + CACHE_EXPIRE > new Date().getTime()) { + if (infrastructureCache && + infrastructureCache["timestamp"] + CACHE_EXPIRE > new Date().getTime()) { //console.log("Host.pciDevices. Cache used"); return callback ? - callback(request, pcisCache["data"]) : null; + callback(request, infrastructureCache["pcis"]) : null; } pcisCallbacks.push({ @@ -140,67 +141,129 @@ define(function(require) { //console.log("Host.pciDevices. Callback queued"); - if (pcisWaiting) { - return; + _infrastructure(); + }, + "vcenterCustomizations": function(params){ + var callback = params.success; + var callbackError = params.error; + var request = OpenNebulaHelper.request(RESOURCE, "infrastructure"); + + if (infrastructureCache && + infrastructureCache["timestamp"] + CACHE_EXPIRE > new Date().getTime()) { + + //console.log("Host.vcenterCustomizations. Cache used"); + + return callback ? + callback(request, infrastructureCache["customizations"]) : null; } - pcisWaiting = true; - - //console.log("Host.pciDevices. NO cache, calling ajax"); - - $.ajax({ - url: "infrastructure", - type: "GET", - dataType: "json", - success: function(response) { - var pcis = response.pci_devices; - - if (pcis == undefined){ - pcis = []; - } - - if (!$.isArray(pcis)){ // If only 1 convert to array - pcis = [pcis]; - } - - pcisCache = { - timestamp : new Date().getTime(), - data : pcis - }; - - pcisWaiting = false; - - for (var i = 0; i < pcisCallbacks.length; i++) { - var callback = pcisCallbacks[i].success; - - if (callback) { - //console.log("Host.pciDevices. Callback called"); - callback(request, pcis); - } - } - - pcisCallbacks = []; - - return; - }, - error: function(response) { - pcisWaiting = false; - - for (var i = 0; i < pcisCallbacks.length; i++) { - var callback = pcisCallbacks[i].error; - - if (callback) { - //console.log("Host.pciDevices. ERROR Callback called"); - callback(request, OpenNebulaError(response)); - } - } - - pcisCallbacks = []; - - return; - } + customizationsCallbacks.push({ + success : callback, + error : callbackError }); + + //console.log("Host.vcenterCustomizations. Callback queued"); + + _infrastructure(); } + }; + + function _infrastructure(){ + if (infrastructureWaiting) { + return; + } + + var request = OpenNebulaHelper.request(RESOURCE, "infrastructure"); + + infrastructureWaiting = true; + + //console.log("Host.infrastructure. NO cache, calling ajax"); + + $.ajax({ + url: "infrastructure", + type: "GET", + dataType: "json", + success: function(response) { + var pcis = response.pci_devices; + + if (pcis == undefined){ + pcis = []; + } + + if (!$.isArray(pcis)){ // If only 1 convert to array + pcis = [pcis]; + } + + var customizations = response.vcenter_customizations; + + if (customizations == undefined){ + customizations = []; + } + + if (!$.isArray(customizations)){ // If only 1 convert to array + customizations = [customizations]; + } + + infrastructureCache = { + timestamp : new Date().getTime(), + pcis : pcis, + customizations : customizations + }; + + infrastructureWaiting = false; + + for (var i = 0; i < pcisCallbacks.length; i++) { + var callback = pcisCallbacks[i].success; + + if (callback) { + //console.log("Host.pciDevices. Callback called"); + callback(request, pcis); + } + } + + pcisCallbacks = []; + + for (var i = 0; i < customizationsCallbacks.length; i++) { + var callback = customizationsCallbacks[i].success; + + if (callback) { + //console.log("Host.vcenterCustomizations. Callback called"); + callback(request, customizations); + } + } + + customizationsCallbacks = []; + + return; + }, + error: function(response) { + infrastructureWaiting = false; + + for (var i = 0; i < pcisCallbacks.length; i++) { + var callback = pcisCallbacks[i].error; + + if (callback) { + //console.log("Host.pciDevices. ERROR Callback called"); + callback(request, OpenNebulaError(response)); + } + } + + pcisCallbacks = []; + + for (var i = 0; i < customizationsCallbacks.length; i++) { + var callback = customizationsCallbacks[i].error; + + if (callback) { + //console.log("Host.vcenterCustomizations. ERROR Callback called"); + callback(request, OpenNebulaError(response)); + } + } + + customizationsCallbacks = []; + + return; + } + }); } return Host; diff --git a/src/sunstone/public/app/tabs/templates-tab/form-panels/create.js b/src/sunstone/public/app/tabs/templates-tab/form-panels/create.js index 8eb5d96ea9..ce17a753b2 100644 --- a/src/sunstone/public/app/tabs/templates-tab/form-panels/create.js +++ b/src/sunstone/public/app/tabs/templates-tab/form-panels/create.js @@ -139,6 +139,20 @@ define(function(require) { $.extend(true, templateJSON, wizardTab.retrieve($('#' + wizardTab.wizardTabId, context))); }); + // vCenter PUBLIC_CLOUD is not defined in the hybrid tab. Because it is + // part of an array, and it is filled in different tabs, the $.extend deep + // merge can't work. We define an auxiliary attribute for it. + + if (templateJSON["VCENTER_PUBLIC_CLOUD"]) { + if (templateJSON['PUBLIC_CLOUD'] == undefined) { + templateJSON['PUBLIC_CLOUD'] = []; + } + + templateJSON['PUBLIC_CLOUD'].push(templateJSON["VCENTER_PUBLIC_CLOUD"]); + + delete templateJSON["VCENTER_PUBLIC_CLOUD"]; + } + if (this.action == "create") { Sunstone.runAction("Template.create", {'vmtemplate': templateJSON}); diff --git a/src/sunstone/public/app/tabs/templates-tab/form-panels/create/wizard-tabs/context.js b/src/sunstone/public/app/tabs/templates-tab/form-panels/create/wizard-tabs/context.js index fb8c00b99f..651f5cd8cf 100644 --- a/src/sunstone/public/app/tabs/templates-tab/form-panels/create/wizard-tabs/context.js +++ b/src/sunstone/public/app/tabs/templates-tab/form-panels/create/wizard-tabs/context.js @@ -26,6 +26,7 @@ define(function(require) { var TemplateUtils = require('utils/template-utils'); var CustomTagsTable = require('utils/custom-tags-table'); var FilesTable = require('tabs/files-tab/datatable') + var OpenNebulaHost = require('opennebula/host'); /* TEMPLATES @@ -88,6 +89,47 @@ define(function(require) { function _setup(context) { var that = this; + context.on("change", "select#vcenter_customizations", function(){ + var option = $("option:selected", this); + + if (option.attr("custom") == "true"){ + $('input#vcenter_customizations_value', context).show(); + } else { + $('input#vcenter_customizations_value', context).hide(); + } + + $('input#vcenter_customizations_value', context).val( $(this).val() ); + }); + + $('input#vcenter_customizations_value', context).hide(); + + OpenNebulaHost.vcenterCustomizations({ + data : {}, + timeout: true, + success: function (request, customizations){ + _fillCustomizations(context, customizations); + }, + error: function(request, error_json){ + console.error("There was an error requesting the vCenter customizations: "+ + error_json.error.message); + + _fillCustomizations(context, []); + } + }); + + context.on("change", "input#vcenter_customizations_value", function(){ + var opt = + $('option'+ + '[value="'+$('input#vcenter_customizations_value', context).val()+'"]', context); + + if (opt.size() == 0){ + opt = $('option[custom="true"]', context); + $('input#vcenter_customizations_value', context).show(); + } + + opt.attr('selected', 'selected'); + }); + CustomTagsTable.setup(context); var selectOptions = { @@ -132,11 +174,35 @@ define(function(require) { }); } + function _fillCustomizations(context, customizations) { + var html = "'; + + $("#vcenter_customizations", context).html(html); + } + function _retrieve(context) { var templateJSON = {}; var contextJSON = WizardFields.retrieve(context); $.extend(contextJSON, CustomTagsTable.retrieve(context)); + var customization = $('input#vcenter_customizations_value', context).val(); + + if (customization) { + templateJSON["VCENTER_PUBLIC_CLOUD"] = { + CUSTOMIZATION_SPEC : customization + }; + } + if ($("#ssh_context", context).is(":checked")) { var public_key = $("#ssh_public_key", context).val(); if (public_key) { @@ -205,6 +271,21 @@ define(function(require) { delete templateJSON['USER_INPUTS']; } + var publicClouds = templateJSON["PUBLIC_CLOUD"]; + + if (publicClouds != undefined) { + if (!$.isArray(publicClouds)){ + publicClouds = [publicClouds]; + } + + $.each(publicClouds, function(){ + if(this["TYPE"] == "vcenter"){ + $('input#vcenter_customizations_value', context).val(this["CUSTOMIZATION_SPEC"]).change(); + return false; + } + }); + } + if (contextJSON) { var file_ds_regexp = /\$FILE\[IMAGE_ID=([0-9]+)+/g; var net_regexp = /^NETWORK$/;; diff --git a/src/sunstone/public/app/tabs/templates-tab/form-panels/create/wizard-tabs/context/html.hbs b/src/sunstone/public/app/tabs/templates-tab/form-panels/create/wizard-tabs/context/html.hbs index f26135db49..e84bec5e1b 100644 --- a/src/sunstone/public/app/tabs/templates-tab/form-panels/create/wizard-tabs/context/html.hbs +++ b/src/sunstone/public/app/tabs/templates-tab/form-panels/create/wizard-tabs/context/html.hbs @@ -27,6 +27,16 @@
+
diff --git a/src/sunstone/public/app/tabs/templates-tab/form-panels/create/wizard-tabs/general.js b/src/sunstone/public/app/tabs/templates-tab/form-panels/create/wizard-tabs/general.js index 36879e75b8..027a69705e 100644 --- a/src/sunstone/public/app/tabs/templates-tab/form-panels/create/wizard-tabs/general.js +++ b/src/sunstone/public/app/tabs/templates-tab/form-panels/create/wizard-tabs/general.js @@ -118,10 +118,10 @@ define(function(require) { var templateJSON = WizardFields.retrieve(context); if (templateJSON["HYPERVISOR"] == 'vcenter') { - templateJSON["PUBLIC_CLOUD"] = { + templateJSON["VCENTER_PUBLIC_CLOUD"] = { 'TYPE': 'vcenter', 'VM_TEMPLATE': $("#vcenter_template_uuid", context).val() - } + }; templateJSON["KEEP_DISKS_ON_DONE"] = $("#KEEP_DISKS", context).is(':checked')?"YES":"NO" } @@ -157,6 +157,23 @@ define(function(require) { delete templateJSON["KEEP_DISKS_ON_DONE"] } + if (templateJSON["HYPERVISOR"] == 'vcenter') { + var publicClouds = templateJSON["PUBLIC_CLOUD"]; + + if (publicClouds != undefined) { + if (!$.isArray(publicClouds)){ + publicClouds = [publicClouds]; + } + + $.each(publicClouds, function(){ + if(this["TYPE"] == "vcenter"){ + $("#vcenter_template_uuid", context).val(this["VM_TEMPLATE"]); + return false; + } + }); + } + } + if (templateJSON["HYPERVISOR"]) { $("input[name='hypervisor'][value='"+templateJSON["HYPERVISOR"]+"']", context).trigger("click") delete templateJSON["HYPERVISOR"]; diff --git a/src/sunstone/public/app/tabs/templates-tab/form-panels/create/wizard-tabs/hybrid.js b/src/sunstone/public/app/tabs/templates-tab/form-panels/create/wizard-tabs/hybrid.js index a912701752..81cc3db7ea 100644 --- a/src/sunstone/public/app/tabs/templates-tab/form-panels/create/wizard-tabs/hybrid.js +++ b/src/sunstone/public/app/tabs/templates-tab/form-panels/create/wizard-tabs/hybrid.js @@ -397,18 +397,6 @@ define(function(require) { var publicCloudJSON = []; var ec2JSON = []; - /* TODO Check if vCenter is defined - if ($.isEmptyObject(vm_json["PUBLIC_CLOUD"])) { - vm_json["PUBLIC_CLOUD"] = []; - }*/ - - if ($("[wizard_field='HYPERVISOR']:checked").val() == 'vcenter') { - publicCloudJSON.push({ - 'TYPE': 'vcenter', - 'VM_TEMPLATE': $("#vcenter_template_uuid").val() - }); - } - $('.provider', context).each(function() { var hash = WizardFields.retrieve(this); if (!$.isEmptyObject(hash)) { @@ -536,15 +524,15 @@ define(function(require) { function _fillProviderTab(context, provider, providerType, clickButton) { var that = this; if (providerType == "vcenter") { - $("#vcenter_template_uuid").val(provider["VM_TEMPLATE"]) - } else { - if (clickButton) { - $("#tf_btn_hybrid", context).trigger("click"); - } - - var providerContext = $(".provider", context).last(); - $("input.hybridRadio[value='" + providerType + "']", providerContext).trigger("click"); - WizardFields.fill(providerContext, provider); + return false; } + + if (clickButton) { + $("#tf_btn_hybrid", context).trigger("click"); + } + + var providerContext = $(".provider", context).last(); + $("input.hybridRadio[value='" + providerType + "']", providerContext).trigger("click"); + WizardFields.fill(providerContext, provider); } }); diff --git a/src/sunstone/sunstone-server.rb b/src/sunstone/sunstone-server.rb index 927a807bff..964d5fd8d7 100755 --- a/src/sunstone/sunstone-server.rb +++ b/src/sunstone/sunstone-server.rb @@ -470,6 +470,8 @@ get '/infrastructure' do error 500, "" end + infrastructure = {} + set = Set.new xml = XMLElement.new @@ -483,9 +485,15 @@ get '/infrastructure' do }) end - infrastructure = { - :pci_devices => set.to_a - } + infrastructure[:pci_devices] = set.to_a + + set = Set.new + + xml.each('HOST/TEMPLATE/CUSTOMIZATION') do |customization| + set.add(customization['NAME']) + end + + infrastructure[:vcenter_customizations] = set.to_a [200, infrastructure.to_json] end