From 1d5172485187196e4fb5a1068047863848b1e0a6 Mon Sep 17 00:00:00 2001 From: Sergio Betanzos Date: Fri, 12 Feb 2021 13:18:01 +0100 Subject: [PATCH] F #4994: Add alias ips (#801) --- src/sunstone/models/sunstone_vm_helper.rb | 109 +++++++++------- .../public/app/templates/helpers/contains.js | 117 ++++++++++++++++++ .../public/app/utils/info-connection/info.hbs | 15 ++- .../public/app/utils/info-connection/utils.js | 2 - 4 files changed, 193 insertions(+), 50 deletions(-) create mode 100644 src/sunstone/public/app/templates/helpers/contains.js diff --git a/src/sunstone/models/sunstone_vm_helper.rb b/src/sunstone/models/sunstone_vm_helper.rb index cdb76ee382..cba5134e61 100644 --- a/src/sunstone/models/sunstone_vm_helper.rb +++ b/src/sunstone/models/sunstone_vm_helper.rb @@ -18,71 +18,90 @@ module SunstoneVMHelper class << self - def state_to_str(id, lcm_id) - id = id.to_i - state_str = VirtualMachine::VM_STATE[id] + NIC_ATTRS = %w[ + IP + EXTERNAL_IP + IP6 + IP6_GLOBAL + IP6_ULA + VROUTER_IP + VROUTER_IP6_GLOBAL + VROUTER_IP6_ULA + ] - if state_str == 'ACTIVE' - lcm_id = lcm_id.to_i - return VirtualMachine::LCM_STATE[lcm_id] + def state_to_str(id, lcm_id) + id = id.to_i + state_str = VirtualMachine::VM_STATE[id] + + if state_str == 'ACTIVE' + lcm_id = lcm_id.to_i + return VirtualMachine::LCM_STATE[lcm_id] + end + + state_str end - return state_str - end + def get_ips(vm_resource) + vm = vm_resource.to_hash['VM'] + ips = [] + vm_nics = [] - def get_ips(vm) - ips = [] + template_nic = vm['TEMPLATE']['NIC'] + template_pci = vm['TEMPLATE']['PCI'] - vm_nics = [] + vm_nics = [template_nic].flatten unless template_nic.nil? - if !vm['TEMPLATE']['NIC'].nil? - vm_nics = [vm['TEMPLATE']['NIC']].flatten - end + vm_nics = [vm_nics, template_pci].flatten unless template_pci.nil? - if !vm['TEMPLATE']['PCI'].nil? - vm_nics = [vm_nics, vm['TEMPLATE']['PCI']].flatten - end + vm_nics.each do |nic| + NIC_ATTRS.each do |attr| + next unless nic.key?(attr) - vm_nics.each do |nic| - %w[IP EXTERNAL_IP IP6_GLOBAL IP6_ULA IP6 - VROUTER_IP VROUTER_IP6_GLOBAL VROUTER_IP6_ULA].each do |attr| - if nic.key?(attr) ips.push(nic[attr]) end + + next if nic['ALIAS_IDS'].nil? + + nic['ALIAS_IDS'].split(',').each do |alias_id| + NIC_ATTRS.each do |attr| + alias_ip = vm_resource["/VM/TEMPLATE + /NIC_ALIAS[NIC_ID='#{alias_id}']/#{attr}"] + + next if alias_ip.nil? + + ips.push("* #{alias_ip}") + end + end end - end - VirtualMachine::EXTERNAL_IP_ATTRS.each do |attr| - external_ip = vm['MONITORING'][attr] + VirtualMachine::EXTERNAL_IP_ATTRS.each do |attr| + external_ip = vm['MONITORING'][attr] + + next unless external_ip.nil? && ips.include?(external_ip) - if !external_ip.nil? && !ips.include?(external_ip) ips.push(external_ip) end + + ips end - return ips - end + def get_remote_info(vm_resource) + info = { + :id => vm_resource['/VM/ID'], + :name => vm_resource['/VM/NAME'], + :state => state_to_str(vm_resource['/VM/STATE'], + vm_resource['/VM/LCM_STATE']), + :start_time => vm_resource['/VM/STIME'], + :networks => get_ips(vm_resource) + } + service_id = vm_resource['/VM/USER_TEMPLATE/SERVICE_ID'] - def get_remote_info(vm_resource) - vm = vm_resource.to_hash['VM'] + info[:service_id] = service_id if service_id - service_id = vm['USER_TEMPLATE']['SERVICE_ID'] + info + end - info = { - :id => vm['ID'], - :name => vm['NAME'], - :state => state_to_str(vm['STATE'], vm['LCM_STATE']), - :start_time => vm['STIME'], - :networks => get_ips(vm) - } - - info[:service_id] = service_id if service_id - - return info - end - - end + end end - \ No newline at end of file diff --git a/src/sunstone/public/app/templates/helpers/contains.js b/src/sunstone/public/app/templates/helpers/contains.js new file mode 100644 index 0000000000..4549ca87e9 --- /dev/null +++ b/src/sunstone/public/app/templates/helpers/contains.js @@ -0,0 +1,117 @@ +/* -------------------------------------------------------------------------- */ +/* Copyright 2002-2021, 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) { + /** + * Block helper that renders the block if `collection` has the + * given `value`, using strict equality (`===`) for comparison, + * otherwise the inverse block is rendered (if specified). If a + * `startIndex` is specified and is negative, it is used as the + * offset from the end of the collection. + * + * ```handlebars + * + * {{#contains array "d"}} + * This will not be rendered. + * {{else}} + * This will be rendered. + * {{/contains}} + * ``` + * @param {Array|Object|String} `collection` The collection to iterate over. + * @param {any} `value` The value to check for. + * @param {Number} `[startIndex=0]` Optionally define the starting index. + * @param {Object} `options` Handlebars provided options object. + * @block + * @api public + */ + + var Handlebars = require('hbs/handlebars'); + + function isFunction(val) { + return typeof val === 'function'; + }; + + function isObject(val) { + return typeof val === 'object'; + }; + + function isOptions(val) { + return isObject(val) && isObject(val.hash); + }; + + function isBlock(options) { + return isOptions(options) && isFunction(options.fn) && isFunction(options.inverse) + }; + + function getValue(val, context, options) { + if (isOptions(val)) { + return getValue(null, val, options); + } + + if (isOptions(context)) { + return getValue(val, {}, context); + } + + if (isBlock(options)) { + return !!val ? options.fn(context) : options.inverse(context); + } + + return val; + }; + + var contains = function(collection, value, startIndex, options) { + if (typeof startIndex === 'object') { + options = startIndex; + startIndex = undefined; + } + + let val = (collection === null || value === null || isNaN(collection.length)) + ? false + : collection.indexOf(value, startIndex) !== -1; + + return getValue(val, this, options); + }; + + Handlebars.registerHelper('contains', contains); + + return contains; +}); + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/sunstone/public/app/utils/info-connection/info.hbs b/src/sunstone/public/app/utils/info-connection/info.hbs index b69aa51425..e80d5555ea 100644 --- a/src/sunstone/public/app/utils/info-connection/info.hbs +++ b/src/sunstone/public/app/utils/info-connection/info.hbs @@ -39,7 +39,7 @@ {{!-- SERVICE --}} {{#if service }} - (Part of: {{ service.NAME }}) + (Part of: {{ service.NAME }}) {{/if}} @@ -66,12 +66,21 @@ diff --git a/src/sunstone/public/app/utils/info-connection/utils.js b/src/sunstone/public/app/utils/info-connection/utils.js index 48bbe5a5a0..8ce19240f0 100644 --- a/src/sunstone/public/app/utils/info-connection/utils.js +++ b/src/sunstone/public/app/utils/info-connection/utils.js @@ -35,12 +35,10 @@ define(function(require) { var json = atob(info_encode) var info = JSON.parse(json) - // state class var stateId = OpenNebulaVM.STATES[info.state] var lcmStateId = OpenNebulaVM.LCM_STATES[info.state] var stateClass = OpenNebulaVM.stateClass(stateId) || OpenNebulaVM.lcmStateClass(lcmStateId) - // get service from cache or sync var service = OpenNebulaService.getService(info.service_id) return $.extend(info, { stateClass, service })