From 8e474c85ed8c72ad02585a7674d1239ec29c48aa Mon Sep 17 00:00:00 2001 From: sergiojvg Date: Mon, 6 May 2019 10:18:46 -0500 Subject: [PATCH] F #3112: Manage IPs for vCenter imported VMs (#3301) * F #3112: Manage IPs for vCenter imported VMs * F #3112: Bug fix * F #3112: Code refracting (cherry picked from commit 46057cd97e7eec5119b439fe6f2ac109a5cf213d) --- .../lib/vcenter_driver/virtual_machine.rb | 3 +- .../remotes/lib/vcenter_driver/vm_template.rb | 78 +++++++++++++++---- 2 files changed, 63 insertions(+), 18 deletions(-) diff --git a/src/vmm_mad/remotes/lib/vcenter_driver/virtual_machine.rb b/src/vmm_mad/remotes/lib/vcenter_driver/virtual_machine.rb index 9ced06d6df2..6d32c7430b6 100644 --- a/src/vmm_mad/remotes/lib/vcenter_driver/virtual_machine.rb +++ b/src/vmm_mad/remotes/lib/vcenter_driver/virtual_machine.rb @@ -3168,7 +3168,8 @@ def import(selected) npool, hpool, vc_name, - vm_ref) + vm_ref, + vc_vm) opts = {uuid: vc_uuid, npool: npool, error: error } Raction.delete_ars(ar_ids, opts) if !error.empty? diff --git a/src/vmm_mad/remotes/lib/vcenter_driver/vm_template.rb b/src/vmm_mad/remotes/lib/vcenter_driver/vm_template.rb index 8c8d8035dfa..441137150d1 100644 --- a/src/vmm_mad/remotes/lib/vcenter_driver/vm_template.rb +++ b/src/vmm_mad/remotes/lib/vcenter_driver/vm_template.rb @@ -13,6 +13,7 @@ # See the License for the specific language governing permissions and # # limitations under the License. # #--------------------------------------------------------------------------- # +require 'ipaddr' module VCenterDriver @@ -387,8 +388,49 @@ def save_ar_ids(network_found, nic, ar_ids, start_ids = false) last_id end + def find_ips_in_network(network, vm_object) + if vm? + ipv4 = ipv6 = String.new + return if !vm_object.is_a?(VCenterDriver::VirtualMachine) + network.info + + # Iterate over Retrieve vCenter VM NICs + vm_object.item.guest.net.each do |net| + mac = net.macAddress + if nic[:mac] == mac + net.ipConfig.ipAddress.each do |ip_config| + ip = IPAddr.new(ip_config.ipAddress) + ar_array = network_found.to_hash['VNET']['AR_POOL']['AR'] + ar_array = [ar_array] if ar_array.is_a?(Hash) + ipv4, ipv6 = find_ip_in_ar(ip, ar_array) if ar_array + end + break + end + end + end + return ipv4, ipv6 + end + + def find_ip_in_ar(ip, ar_array) + ipv4 = ipv6 = "" + ar_array.each do |ar| + if ar.key?('IP') && ar.key?('IP_END') + start_ip = IPAddr.new(ar['IP']) + end_ip = IPAddr.new(ar['IP_END']) + if ip.family == start_ip.family && + ip.family == end_ip.family + if ip > start_ip && ip < end_ip + ipv4 = ip.to_s if ip.ipv4? + ipv6 = ip.to_s if ip.ipv6? + end + end + end + end + return ipv4, ipv6 + end + def import_vcenter_nics(vc_uuid, npool, hpool, vcenter_instance_name, - template_ref, vm_id=nil, dc_name=nil) + template_ref, vm_object, vm_id=nil, dc_name=nil) nic_info = "" error = "" ar_ids = {} @@ -425,22 +467,23 @@ def import_vcenter_nics(vc_uuid, npool, hpool, vcenter_instance_name, nic_tmp = "NIC=[\n" nic_tmp << "NETWORK_ID=\"#{network_found["ID"]}\",\n" - if vm? - ar_tmp = create_ar(nic) - network_found.add_ar(ar_tmp) - network_found.info - last_id = save_ar_ids(network_found, nic, ar_ids) - - # This is the existing nic info - nic_tmp << "AR_ID=\"#{last_id}\",\n" - nic_tmp << "MAC=\"#{nic[:mac]}\",\n" if nic[:mac] - nic_tmp << "VCENTER_ADDITIONALS_IP4=\"#{nic[:ipv4_additionals]}\",\n" if nic[:ipv4_additionals] - nic_tmp << "VCENTER_IP6=\"#{nic[:ipv6]}\",\n" if nic[:ipv6] - nic_tmp << "IP6_GLOBAL=\"#{nic[:ipv6_global]}\",\n" if nic[:ipv6_global] - nic_tmp << "IP6_ULA=\"#{nic[:ipv6_ula]}\",\n" if nic[:ipv6_ula] - nic_tmp << "VCENTER_ADDITIONALS_IP6=\"#{nic[:ipv6_additionals]}\",\n" if nic[:ipv6_additionals] - end - + ipv4, ipv6 = find_ips_in_network(network_found, vm_object) if vm? + + ar_tmp = create_ar(nic) + network_found.add_ar(ar_tmp) + network_found.info + last_id = save_ar_ids(network_found, nic, ar_ids) + + # This is the existing nic info + nic_tmp << "AR_ID=\"#{last_id}\",\n" + nic_tmp << "MAC=\"#{nic[:mac]}\",\n" if nic[:mac] and ipv4.empty? and ipv6.empty? + nic_tmp << "IP=\"#{ipv4}\"," if !ipv4.empty? + nic_tmp << "IP=\"#{ipv6}\"," if !ipv6.empty? + nic_tmp << "VCENTER_ADDITIONALS_IP4=\"#{nic[:ipv4_additionals]}\",\n" if nic[:ipv4_additionals] + nic_tmp << "VCENTER_IP6=\"#{nic[:ipv6]}\",\n" if nic[:ipv6] + nic_tmp << "IP6_GLOBAL=\"#{nic[:ipv6_global]}\",\n" if nic[:ipv6_global] + nic_tmp << "IP6_ULA=\"#{nic[:ipv6_ula]}\",\n" if nic[:ipv6_ula] + nic_tmp << "VCENTER_ADDITIONALS_IP6=\"#{nic[:ipv6_additionals]}\",\n" if nic[:ipv6_additionals] nic_tmp << "OPENNEBULA_MANAGED=\"NO\"\n" nic_tmp << "]\n" @@ -1187,6 +1230,7 @@ def import(selected) hpool, vcenter, template_moref, + nil, id, dc)