nsidc / vagrant-vsphere

VMware vSphere provider for Vagrant
Other
607 stars 166 forks source link

Custom VLANs do not connect to switch correctly #187

Closed adampointer closed 8 years ago

adampointer commented 8 years ago

My VM is trying to join a VLAN on a distributed switch, but when launching it was creating it's own 'special' vlan not connected to the switch. I tracked down bug to lib/vSphere/action/clone.rb in the add_custom_vlan method.

        def add_custom_vlan(template, dc, spec, vlan)
          network = get_network_by_name(dc, vlan)

          modify_network_card(template, spec) do |card|
            begin
              switch_port = RbVmomi::VIM.DistributedVirtualSwitchPortConnection(switchUuid: network.config.distributedVirtualSwitch.uuid, portgroupKey: network.key)
              card.backing = RbVmomi::VIM::VirtualEthernetCardNetworkBackingInfo(port: switch_port)
            rescue Exception => e
              puts e
              # not connected to a distibuted switch?
              card.backing = RbVmomi::VIM::VirtualEthernetCardNetworkBackingInfo(network: network, deviceName: network.name)
            end
          end
        end

This solved it for me but I am not sure if this is the correct approach. Basically an exception was been thrown when setting:

card.backing.port = switch_port

That is because VirtualEthernetCardNetworkBackingInfo does not have a port method. Instead we need to use a VirtualEthernetCardNetworkBackingInfo class and assign that to card.backing.