From 7ddc0983bbb0efb353178bad6a63fff0e1847252 Mon Sep 17 00:00:00 2001 From: Daniel Clavijo Coca Date: Fri, 22 Mar 2019 00:36:11 -0600 Subject: [PATCH] #3029 Avoid running nic_query in network pre script --- src/vnm_mad/remotes/lib/vm.rb | 134 +++++++++++++++++----------------- 1 file changed, 69 insertions(+), 65 deletions(-) diff --git a/src/vnm_mad/remotes/lib/vm.rb b/src/vnm_mad/remotes/lib/vm.rb index 4a32ea5978b..b24254f3de6 100644 --- a/src/vnm_mad/remotes/lib/vm.rb +++ b/src/vnm_mad/remotes/lib/vm.rb @@ -16,100 +16,104 @@ module VNMMAD -module VNMNetwork + module VNMNetwork - ############################################################################ - # This class represents the VM abstraction. It provides basic methods - # to interact with its network interfaces. - ############################################################################ - class VM - attr_accessor :nics, :vm_info, :deploy_id, :vm_root + ######################################################################## + # This class represents the VM abstraction. It provides basic methods + # to interact with its network interfaces. + ######################################################################## + class VM + attr_accessor :nics, :vm_info, :deploy_id, :vm_root - # Creates a new VM object, and bootstrap the NICs array - # @param vm_root [REXML] XML document representing the VM - # @param xpath_filer [String] to get the VM NICs - # @param deploy_id [String] refers to the VM in the hypervisor - def initialize(vm_root, xpath_filter, deploy_id) - @vm_root = vm_root - @deploy_id = deploy_id + # Creates a new VM object, and bootstrap the NICs array + # @param vm_root [REXML] XML document representing the VM + # @param xpath_filer [String] to get the VM NICs + # @param deploy_id [String] refers to the VM in the hypervisor + def initialize(vm_root, xpath_filter, deploy_id) + @vm_root = vm_root + @deploy_id = deploy_id - @vm_info = Hash.new + @vm_info = {} - @deploy_id = nil if deploy_id == "-" + @deploy_id = nil if deploy_id == '-' - nics = VNMNetwork::Nics.new(hypervisor) + nics = VNMNetwork::Nics.new(hypervisor) - @vm_root.elements.each(xpath_filter) do |nic_element| - nic = nics.new_nic + @vm_root.elements.each(xpath_filter) do |nic_element| + nic = nics.new_nic - nic_build_hash(nic_element,nic) + nic_build_hash(nic_element, nic) - nic.get_info(self) - nic.get_tap(self) + if File.basename($PROGRAM_NAME) != 'pre' + nic.get_info(self) + nic.get_tap(self) + end + + nics << nic + end - nics << nic + @nics = nics end - @nics = nics - end + # Iterator on each NIC of the VM + def each_nic(block) + return if @nics.nil? - # Iterator on each NIC of the VM - def each_nic(block) - if @nics != nil @nics.each do |the_nic| block.call(the_nic) end end - end - # Access an XML Element of the VM - # @param element [String] element name - # @return [String] value of the element or nil if not found - def [](element) - if @vm_root - val = @vm_root.elements[element] - return val.text if !val.nil? && val.text - end + # Access an XML Element of the VM + # @param element [String] element name + # @return [String] value of the element or nil if not found + def [](element) + if @vm_root + val = @vm_root.elements[element] + return val.text if !val.nil? && val.text + end - nil - end + nil + end - # Gets the Hypervisor VM_MAD from the Template - # @return [String] name of the hypervisor driver - def hypervisor - xpath = 'HISTORY_RECORDS/HISTORY/VM_MAD' - @vm_root.root.elements[xpath].text - end + # Gets the Hypervisor VM_MAD from the Template + # @return [String] name of the hypervisor driver + def hypervisor + xpath = 'HISTORY_RECORDS/HISTORY/VM_MAD' + @vm_root.root.elements[xpath].text + end - private + private - # Method to build the associated Hash from a NIC - # @param nic_element [REXML] for the NIC - # @param nic [Nic] class representation - def nic_build_hash(nic_element,nic) - nic_element.elements.each('*') do |nic_attribute| - key = nic_attribute.name.downcase.to_sym + # Method to build the associated Hash from a NIC + # @param nic_element [REXML] for the NIC + # @param nic [Nic] class representation + def nic_build_hash(nic_element, nic) + nic_element.elements.each('*') do |nic_attribute| + key = nic_attribute.name.downcase.to_sym - if nic_attribute.has_elements? - data = {} - nic_build_hash(nic_attribute,data) - else - data = nic_attribute.text - end + if nic_attribute.has_elements? + data = {} + nic_build_hash(nic_attribute, data) + else + data = nic_attribute.text + end - if nic[key] - if nic[key].instance_of?(Array) - nic[key] << data + if nic[key] + if nic[key].instance_of?(Array) + nic[key] << data + else + nic[key] = [nic[key], data] + end else - nic[key] = [nic[key], data] + nic[key] = data end - else - nic[key] = data end end + end + end -end end