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

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

merged 3 commits into from
May 6, 2019

Conversation

sergiojvg
Copy link
Contributor

No description provided.

Copy link
Member

@tinova tinova left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please consider a refactor in the vm_template.rb code, a proposal follows:

#Network is already in OpenNebula
if network_found
    nic_tmp = "NIC=[\n"
    nic_tmp << "NETWORK_ID=\"#{network_found["ID"]}\",\n"

    ipv4, ipv6 = find_ips_in_network(network_found, vm_object) if vm?
end


def find_ips_in_network(network, vm_object)
        ipv4 = ipv6 = ""
        return if !vm_object.is_a?(VCenterDriver::VirtualMachine)

        # Refresh OpenNebula network
        network.info
            
        # Iterate over Retrieve vCenter VM NICs
        vm_object.item.guest.net.each do |net|
            if nic[:mac] == net.macAddress
                net.ipConfig.ipAddress.each do |ip_config|
                    ip = IPAddr.new(ip_config.ipAddress)
                    ar_array = network.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

        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

@tinova tinova merged commit 46057cd into master May 6, 2019
@tinova
Copy link
Member

tinova commented May 6, 2019

Also, to one-5.8

tinova pushed a commit that referenced this pull request May 6, 2019
* F #3112: Manage IPs for vCenter imported VMs

* F #3112: Bug fix

* F #3112: Code refracting

(cherry picked from commit 46057cd)
@tinova tinova deleted the feature-3112 branch October 14, 2020 11:15
rsmontero pushed a commit that referenced this pull request Nov 20, 2024
Signed-off-by: Jaime <jconchello@opennebula.io>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants