From ea688c3022a38720d03be900b032e6ec06de710b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn?= Date: Wed, 11 Nov 2015 10:59:09 +0100 Subject: [PATCH] Bug #4121: Use default cost values from oned.conf in sunstone (cherry picked from commit 21a15a7875eb9386ec9620c88cc24a5a7a80a032) --- src/sunstone/public/app/app.js | 2 + src/sunstone/public/app/opennebula/system.js | 114 ++++++++++++++++++ src/sunstone/public/app/sunstone-config.js | 33 ++++- src/sunstone/public/app/tabs/provision-tab.js | 65 +++++++--- .../templates-tab/form-panels/instantiate.js | 10 +- .../app/tabs/vms-tab/panels/capacity.js | 15 ++- .../app/tabs/vms-tab/panels/capacity/html.hbs | 4 +- .../public/app/tabs/vms-tab/panels/storage.js | 11 +- .../app/tabs/vms-tab/panels/storage/html.hbs | 2 +- src/sunstone/public/app/utils/disks-resize.js | 8 +- src/sunstone/sunstone-server.rb | 17 +++ 11 files changed, 254 insertions(+), 27 deletions(-) create mode 100644 src/sunstone/public/app/opennebula/system.js diff --git a/src/sunstone/public/app/app.js b/src/sunstone/public/app/app.js index 2c5da866d0..8cb9ecae3f 100644 --- a/src/sunstone/public/app/app.js +++ b/src/sunstone/public/app/app.js @@ -53,6 +53,8 @@ define(function(require) { _setupCloseDropdownsOnClick(); _insertUserAndZoneSelector(); + Config.initDefaultCost(); + if (Config.isTabEnabled(PROVISION_TAB_ID)) { Sunstone.showTab(PROVISION_TAB_ID); $('#loading').hide(); diff --git a/src/sunstone/public/app/opennebula/system.js b/src/sunstone/public/app/opennebula/system.js new file mode 100644 index 0000000000..ee1a4eb89a --- /dev/null +++ b/src/sunstone/public/app/opennebula/system.js @@ -0,0 +1,114 @@ +/* -------------------------------------------------------------------------- */ +/* Copyright 2002-2015, OpenNebula Project, OpenNebula Systems */ +/* */ +/* Licensed under the Apache License, Version 2.0 (the "License"); you may */ +/* not use this file except in compliance with the License. You may obtain */ +/* a copy of the License at */ +/* */ +/* http://www.apache.org/licenses/LICENSE-2.0 */ +/* */ +/* Unless required by applicable law or agreed to in writing, software */ +/* distributed under the License is distributed on an "AS IS" BASIS, */ +/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */ +/* See the License for the specific language governing permissions and */ +/* limitations under the License. */ +/* -------------------------------------------------------------------------- */ + +define(function(require) { + var OpenNebulaAction = require('./action'); + var Locale = require('utils/locale'); + var OpenNebulaError = require('./error'); + var OpenNebulaHelper = require('./helper'); + + + var onedconfCache; + var onedconfWaiting = false; + var onedconfCallbacks = []; + + var CACHE_EXPIRE = 36000000; //ms + + var _clearCache = function() { + onedconfCache = null; + //console.log("onedconf. Cache cleaned"); + }; + + var RESOURCE = "SYSTEM"; + + var System = { + "resource": RESOURCE, + "onedconf": function(params){ + var callback = params.success; + var callbackError = params.error; + var request = OpenNebulaHelper.request(RESOURCE, "onedconf"); + + if (onedconfCache && + onedconfCache["timestamp"] + CACHE_EXPIRE > new Date().getTime()) { + + //console.log("onedconf. Cache used"); + + return callback ? + callback(request, onedconfCache["pcis"]) : null; + } + + onedconfCallbacks.push({ + success : callback, + error : callbackError + }); + + //console.log("onedconf. Callback queued"); + + if (onedconfWaiting) { + return; + } + + onedconfWaiting = true; + + //console.log("onedconf. NO cache, calling ajax"); + + $.ajax({ + url: "onedconf", + type: "GET", + dataType: "json", + success: function(response) { + onedconfCache = { + timestamp : new Date().getTime(), + onedConf : response + }; + + onedconfWaiting = false; + + for (var i = 0; i < onedconfCallbacks.length; i++) { + var callback = onedconfCallbacks[i].success; + + if (callback) { + //console.log("onedconf. Callback called"); + callback(request, response); + } + } + + onedconfCallbacks = []; + + return; + }, + error: function(response) { + onedconfWaiting = false; + + for (var i = 0; i < onedconfCallbacks.length; i++) { + var callback = onedconfCallbacks[i].error; + + if (callback) { + //console.log("onedconf. ERROR Callback called"); + callback(request, OpenNebulaError(response)); + } + } + + onedconfCallbacks = []; + + return; + } + }); + } + }; + + return System; +}) diff --git a/src/sunstone/public/app/sunstone-config.js b/src/sunstone/public/app/sunstone-config.js index 5ec2d5c7b9..b558b378a4 100644 --- a/src/sunstone/public/app/sunstone-config.js +++ b/src/sunstone/public/app/sunstone-config.js @@ -16,10 +16,17 @@ define(function(require) { require('jquery'); + var OpenNebulaSystem = require('opennebula/system'); // Clone the local config object in a private var var _config = $.extend(true, {}, config); + var _defaultCost = { + cpuCost : 0, + memoryCost : 0, + diskCost : 0 + }; + var Config = { 'isTabEnabled': function(tabName) { var enabled = _config['view']['enabled_tabs'][tabName]; @@ -136,7 +143,31 @@ define(function(require) { 'vncProxyPort': _config['system_config']['vnc_proxy_port'], 'vncWSS': _config['user_config']['vnc_wss'], 'logo': (_config['view']["small_logo"] || "images/one_small_logo.png"), - 'enabledTabs': _config['view']['enabled_tabs'] + 'enabledTabs': _config['view']['enabled_tabs'], + "defaultCost" : _defaultCost, + "initDefaultCost" : function() { + OpenNebulaSystem.onedconf({ + data : {}, + timeout: true, + success: function (request, onedconf){ + if (onedconf.DEFAULT_COST != undefined){ + if (onedconf.DEFAULT_COST.CPU_COST != undefined){ + _defaultCost.cpuCost = parseInt(onedconf.DEFAULT_COST.CPU_COST); + } + if (onedconf.DEFAULT_COST.MEMORY_COST != undefined){ + _defaultCost.memoryCost = parseInt(onedconf.DEFAULT_COST.MEMORY_COST); + } + if (onedconf.DEFAULT_COST.DISK_COST != undefined){ + _defaultCost.diskCost = parseInt(onedconf.DEFAULT_COST.DISK_COST); + } + } + }, + error: function(request, error_json){ + console.error("There was an error requesting oned.conf: "+ + error_json.error.message); + } + }); + } } return Config; diff --git a/src/sunstone/public/app/tabs/provision-tab.js b/src/sunstone/public/app/tabs/provision-tab.js index 86201d541a..36c14bed76 100644 --- a/src/sunstone/public/app/tabs/provision-tab.js +++ b/src/sunstone/public/app/tabs/provision-tab.js @@ -246,20 +246,37 @@ define(function(require) { var capacity = template_json.VMTEMPLATE.TEMPLATE; var cost = 0; - if ((capacity.CPU_COST || capacity.MEMORY_COST || capacity.DISK_COST) && Config.isFeatureEnabled("showback")) { + + var cpuCost = capacity.CPU_COST; + var memoryCost = capacity.MEMORY_COST; + var diskCost = capacity.DISK_COST; + + if (cpuCost == undefined){ + cpuCost = Config.defaultCost.cpuCost; + } + + if (memoryCost == undefined){ + memoryCost = Config.defaultCost.memoryCost; + } + + if (diskCost == undefined){ + diskCost = Config.defaultCost.diskCost; + } + + if ((cpuCost != 0 || memoryCost != 0 || diskCost != 0) && Config.isFeatureEnabled("showback")) { $(".provision_create_service_cost_div", context).show(); - if (capacity.CPU && capacity.CPU_COST) { - cost += capacity.CPU * capacity.CPU_COST - $(".cost_value", context).data("CPU_COST", capacity.CPU_COST); + if (capacity.CPU) { + cost += capacity.CPU * cpuCost; + $(".cost_value", context).data("CPU_COST", cpuCost); } - if (capacity.MEMORY && capacity.MEMORY_COST) { - cost += capacity.MEMORY * capacity.MEMORY_COST - $(".cost_value", context).data("MEMORY_COST", capacity.MEMORY_COST); + if (capacity.MEMORY) { + cost += capacity.MEMORY * memoryCost; + $(".cost_value", context).data("MEMORY_COST", memoryCost); } - if (capacity.DISK_COST) { + if (diskCost != 0) { var template_disk = capacity.DISK; var disks = []; if ($.isArray(template_disk)) { @@ -268,15 +285,15 @@ define(function(require) { disks = [template_disk]; } - $(".cost_value", context).data("DISK_COST", capacity.DISK_COST); + $(".cost_value", context).data("DISK_COST", diskCost); $.each(disks, function(i,disk){ if (disk.SIZE) { - cost += capacity.DISK_COST * disk.SIZE; + cost += diskCost * disk.SIZE; } if (disk.DISK_SNAPSHOT_TOTAL_SIZE) { - cost += capacity.DISK_COST * disk.DISK_SNAPSHOT_TOTAL_SIZE; + cost += diskCost * disk.DISK_SNAPSHOT_TOTAL_SIZE; } }); } @@ -386,17 +403,29 @@ define(function(require) { '
'); var cost = 0; - if ((capacity.CPU_COST || capacity.MEMORY_COST) && Config.isFeatureEnabled("showback")) { + + var cpuCost = capacity.CPU_COST; + var memoryCost = capacity.MEMORY_COST; + + if (cpuCost == undefined){ + cpuCost = Config.defaultCost.cpuCost; + } + + if (memoryCost == undefined){ + memoryCost = Config.defaultCost.memoryCost; + } + + if ((cpuCost != 0 || memoryCost != 0) && Config.isFeatureEnabled("showback")) { $(".provision_create_template_cost_div").show(); - if (capacity.CPU && capacity.CPU_COST) { - cost += capacity.CPU * capacity.CPU_COST - $(".cost_value").data("CPU_COST", capacity.CPU_COST); + if (capacity.CPU) { + cost += capacity.CPU * cpuCost; + $(".cost_value").data("CPU_COST", cpuCost); } - if (capacity.MEMORY && capacity.MEMORY_COST) { - cost += capacity.MEMORY * capacity.MEMORY_COST - $(".cost_value").data("MEMORY_COST", capacity.MEMORY_COST); + if (capacity.MEMORY) { + cost += capacity.MEMORY * memoryCost; + $(".cost_value").data("MEMORY_COST", memoryCost); } $(".cost_value").html(cost.toFixed(2)); diff --git a/src/sunstone/public/app/tabs/templates-tab/form-panels/instantiate.js b/src/sunstone/public/app/tabs/templates-tab/form-panels/instantiate.js index e8593c6b4b..4970c4c187 100644 --- a/src/sunstone/public/app/tabs/templates-tab/form-panels/instantiate.js +++ b/src/sunstone/public/app/tabs/templates-tab/form-panels/instantiate.js @@ -200,7 +200,15 @@ define(function(require) { var cpuCost = template_json.VMTEMPLATE.TEMPLATE.CPU_COST; var memoryCost = template_json.VMTEMPLATE.TEMPLATE.MEMORY_COST; - if ((cpuCost != undefined || memoryCost != undefined) && Config.isFeatureEnabled("showback")) { + if (cpuCost == undefined){ + cpuCost = Config.defaultCost.cpuCost; + } + + if (memoryCost == undefined){ + memoryCost = Config.defaultCost.memoryCost; + } + + if ((cpuCost != 0 || memoryCost != 0) && Config.isFeatureEnabled("showback")) { var cost = 0; var cpu = template_json.VMTEMPLATE.TEMPLATE.CPU; diff --git a/src/sunstone/public/app/tabs/vms-tab/panels/capacity.js b/src/sunstone/public/app/tabs/vms-tab/panels/capacity.js index 989aa3d96c..b456d04c6f 100644 --- a/src/sunstone/public/app/tabs/vms-tab/panels/capacity.js +++ b/src/sunstone/public/app/tabs/vms-tab/panels/capacity.js @@ -78,9 +78,22 @@ define(function(require) { var resizeStateEnabled = (RESIZE_STATES.indexOf(parseInt(this.element.STATE)) > -1); + var cpuCost = this.element.TEMPLATE.CPU_COST; + var memoryCost = this.element.TEMPLATE.MEMORY_COST; + + if (cpuCost == undefined){ + cpuCost = Config.defaultCost.cpuCost; + } + + if (memoryCost == undefined){ + memoryCost = Config.defaultCost.memoryCost; + } + return TemplateInfo({ 'element': this.element, - 'resizeStateEnabled': resizeStateEnabled + 'resizeStateEnabled': resizeStateEnabled, + 'cpuCost': cpuCost, + 'memoryCost': memoryCost }); } diff --git a/src/sunstone/public/app/tabs/vms-tab/panels/capacity/html.hbs b/src/sunstone/public/app/tabs/vms-tab/panels/capacity/html.hbs index 2ce49fa7a1..d91d01a3fb 100644 --- a/src/sunstone/public/app/tabs/vms-tab/panels/capacity/html.hbs +++ b/src/sunstone/public/app/tabs/vms-tab/panels/capacity/html.hbs @@ -35,8 +35,8 @@ {{valOrDefault element.TEMPLATE.VCPU '-'}} {{humanizeSize "MB" element.TEMPLATE.MEMORY}} {{#isFeatureEnabled "showback"}} - {{valOrDefault element.TEMPLATE.CPU_COST '-'}} - {{valOrDefault element.TEMPLATE.MEMORY_COST '-'}} + {{cpuCost}} + {{memoryCost}} {{/isFeatureEnabled}} {{#isTabActionEnabled "vms-tab" "VM.resize"}} diff --git a/src/sunstone/public/app/tabs/vms-tab/panels/storage.js b/src/sunstone/public/app/tabs/vms-tab/panels/storage.js index db8a58b7b5..c37713455c 100644 --- a/src/sunstone/public/app/tabs/vms-tab/panels/storage.js +++ b/src/sunstone/public/app/tabs/vms-tab/panels/storage.js @@ -69,7 +69,16 @@ define(function(require) { */ function _html() { - return TemplateHtml({element: this.element}); + var diskCost = this.element.TEMPLATE.DISK_COST; + + if (diskCost == undefined){ + diskCost = Config.defaultCost.diskCost; + } + + return TemplateHtml({ + element: this.element, + diskCost: diskCost + }); } function _setup(context) { diff --git a/src/sunstone/public/app/tabs/vms-tab/panels/storage/html.hbs b/src/sunstone/public/app/tabs/vms-tab/panels/storage/html.hbs index f6fc209e1f..01ab58791f 100644 --- a/src/sunstone/public/app/tabs/vms-tab/panels/storage/html.hbs +++ b/src/sunstone/public/app/tabs/vms-tab/panels/storage/html.hbs @@ -49,7 +49,7 @@ {{tr "Cost / MByte"}} - {{valOrDefault element.TEMPLATE.DISK_COST '-'}} + {{diskCost}} diff --git a/src/sunstone/public/app/utils/disks-resize.js b/src/sunstone/public/app/utils/disks-resize.js index dbfd0c5ed3..0f28713e28 100644 --- a/src/sunstone/public/app/utils/disks-resize.js +++ b/src/sunstone/public/app/utils/disks-resize.js @@ -74,9 +74,13 @@ define(function(require){ extendedDisks = [extendedTemplateDisk] } - var disk_cost = template_json.VMTEMPLATE.TEMPLATE.DISK_COST; - if (disk_cost && Config.isFeatureEnabled("showback")) { + + if (disk_cost == undefined) { + disk_cost = Config.defaultCost.diskCost; + } + + if (disk_cost != 0 && Config.isFeatureEnabled("showback")) { $(".provision_create_template_disk_cost_div", disksContext).show(); disksContext.on("change.fndtn.slider", '.range-slider', function(){ diff --git a/src/sunstone/sunstone-server.rb b/src/sunstone/sunstone-server.rb index 964d5fd8d7..be5da00e34 100755 --- a/src/sunstone/sunstone-server.rb +++ b/src/sunstone/sunstone-server.rb @@ -498,6 +498,23 @@ get '/infrastructure' do [200, infrastructure.to_json] end +get '/onedconf' do + serveradmin_client = $cloud_auth.client(nil, session[:active_zone_endpoint]) + + rc = OpenNebula::System.new(serveradmin_client).get_configuration + + if OpenNebula.is_error?(rc) + logger.error { rc.message } + error 500, "" + end + + onedconf = { + :DEFAULT_COST => rc.to_hash()['TEMPLATE']['DEFAULT_COST'] + } + + [200, onedconf.to_json] +end + get '/vm/:id/log' do @SunstoneServer.get_vm_log(params[:id]) end