Skip to content

Commit

Permalink
F #3112: Manage IPs for vCenter imported VMs (#3301)
Browse files Browse the repository at this point in the history
* F #3112: Manage IPs for vCenter imported VMs

* F #3112: Bug fix

* F #3112: Code refracting

(cherry picked from commit 46057cd)
  • Loading branch information
sergiojvg authored and tinova committed May 6, 2019
1 parent c5d5fc0 commit 8e474c8
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 18 deletions.
3 changes: 2 additions & 1 deletion src/vmm_mad/remotes/lib/vcenter_driver/virtual_machine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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?

Expand Down
78 changes: 61 additions & 17 deletions src/vmm_mad/remotes/lib/vcenter_driver/vm_template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# See the License for the specific language governing permissions and #
# limitations under the License. #
#--------------------------------------------------------------------------- #
require 'ipaddr'

module VCenterDriver

Expand Down Expand Up @@ -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 = {}
Expand Down Expand Up @@ -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"

Expand Down Expand Up @@ -1187,6 +1230,7 @@ def import(selected)
hpool,
vcenter,
template_moref,
nil,
id,
dc)

Expand Down

0 comments on commit 8e474c8

Please sign in to comment.