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

fixes #24571 - vsphere nic selection works with fog-vsphere updates #5973

Merged
merged 1 commit into from Aug 20, 2018
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions app/models/concerns/fog_extensions/vsphere/server.rb
Expand Up @@ -40,15 +40,15 @@ def scsi_controller_type
def select_nic(fog_nics, nic)
nic_attrs = nic.compute_attributes
all_networks = service.list_networks(datacenter: datacenter)
vm_network = all_networks.detect { |network| nic_attrs['network'] && [network[:name], network[:key]].compact.include?(nic_attrs['network']) }
vm_network = all_networks.detect { |network| nic_attrs['network'] && [network[:name], network[:id], network[:_ref]].compact.include?(nic_attrs['network']) }
vm_network ||= all_networks.detect { |network| network[:_ref] == nic_attrs['network'] }
unless vm_network
Rails.logger.info "Could not find Vsphere network for #{nic_attrs.inspect}"
return
end
selected_nic = fog_nics.detect { |fn| fn.network == vm_network[:name] } # grab any nic on the same network
if selected_nic.nil? && vm_network.respond_to?(:key)
selected_nic = fog_nics.detect { |fn| fn.network == vm_network[:key] } # try to match on portgroup
if selected_nic.nil? && vm_network[:id].present?
selected_nic = fog_nics.detect { |fn| fn.network == vm_network[:id] } # try to match on portgroup
end
selected_nic
end
Expand Down
83 changes: 83 additions & 0 deletions test/unit/fog_extensions/vsphere/server_test.rb
@@ -0,0 +1,83 @@
require 'test_helper'

module FogExtensions
module Vsphere
class ServerTest < ActiveSupport::TestCase
setup { Fog.mock! }
teardown { Fog.unmock! }

describe '#select_nic' do
let(:uuid) { '5032c8a5-9c5e-ba7a-3804-832a03e16381' }
let(:client) do
::Fog::Compute.new(
:provider => 'vsphere',
:vsphere_username => 'someuser',
:vsphere_password => 'password',
:vsphere_server => 'vsphere.example.com',
:vsphere_expected_pubkey_hash => 'somehash'
)
end
let(:vm) { client.servers.get(uuid) }
let(:fog_nics) { vm.interfaces.dup }
let(:host) { FactoryBot.build(:host, :managed) }

setup do
vm.service.stubs(:list_networks).returns(
[
{
:name => 'exampledvnet',
:accessible => true,
:id => 'dvportgroup-123456',
:vlanid => 123,
:virtualswitch => 'VDS-example',
:datacenter => 'Solutions',
:_ref => 'dvportgroup-123456'
},
{
:name => 'examplenet',
:accessible => true,
:id => 'network-12345',
:datacenter => 'Solutions',
:_ref => 'network-12345'
}
]
)
end

context 'on a standard switch' do
let(:nic) do
host.primary_interface.tap do |nic|
nic.compute_attributes = {
'type' => 'VirtualVmxnet3',
'network' => 'examplenet'
}
end
end

test 'finds the vm nic for a host nic' do
fog_nic = vm.interfaces.first
fog_nic.network = 'examplenet'
assert_equal vm.interfaces.first, vm.select_nic(fog_nics, nic)
end
end

context 'on a distributed switch' do
let(:nic) do
host.primary_interface.tap do |nic|
nic.compute_attributes = {
'type' => 'VirtualVmxnet3',
'network' => 'dvportgroup-123456'
}
end
end

test 'finds the vm nic for a host nic' do
fog_nic = vm.interfaces.first
fog_nic.network = 'dvportgroup-123456'
assert_equal vm.interfaces.first, vm.select_nic(fog_nics, nic)
end
end
end
end
end
end