Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

F #3112: Manage IPs for vCenter imported VMs #3301

Merged
merged 3 commits into from
May 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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