fog/fog — pull request #3181 by endzyme (2014-09-25 23:27:24+00:00)
Title: Fixed attribute passing for add_interface

Server model has an `add_interface` method. This method allows attributes to be passed so you can configure options on the interface.

Unfortunately it doesn't pass these options to the create_interface call. This in turn leaves `attributes` empty for `create_vm.rb#create_interface` and subsequently `create_vm.rb#create_nic_backing`.

When you define a `DistributedVirtualPortgroup` as the network attribute, and `create_nic_backing` doesn't have `attributes[:datacenter]`, you will end up with a newly created StandardPortgroup instead of the correct `DistributedVirtualPortgroup` in your Backing info for the interface.

To solve this you will need to pass along the `datacenter: 'name-your-dc-here'` param when you call server.add_interface, along with `type`, and `network`.

Tested in ESX 5.5. See debug code below to replicate issue.

```ruby
v = Fog::Compute[:vsphere]

c = v.datacenters.get('data-center-name-here').virtual_machines.get('path/to/my/virtual/machine')

>> c.add_interface type: RbVmomi::VIM::VirtualE1000, network: 'network-name-here', datacenter: 'data-center-name-here'
/home/vagrant/.rvm/gems/ruby-1.9.3-p547/gems/fog-1.16.0/lib/fog/vsphere/requests/compute/modify_vm_interface.rb:10
interface=get_interface_from_options(vmid, options)

[5, 14] in /home/vagrant/.rvm/gems/ruby-1.9.3-p547/gems/fog-1.16.0/lib/fog/vsphere/requests/compute/modify_vm_interface.rb
5
6 def add_vm_interface(vmid, options = {})
7 raise ArgumentError, "instance id is a required parameter" unless vmid
8 debugger
9
=> 10 interface=get_interface_from_options(vmid, options)
11 vm_reconfig_hardware('instance_uuid' => vmid, 'hardware_spec' => {'deviceChange'=>[create_interface(interface)]})
12 end
13
14 def destroy_vm_interface(vmid, options = {})
(rdb:1) options
{:type=>VirtualE1000, :network=>"network-name-here", :datacenter=>"data-center-name-here"}
(rdb:1) n
/home/vagrant/.rvm/gems/ruby-1.9.3-p547/gems/fog-1.16.0/lib/fog/vsphere/requests/compute/modify_vm_interface.rb:11
vm_reconfig_hardware('instance_uuid' => vmid, 'hardware_spec' => {'deviceChange'=>[create_interface(interface)]})

[6, 15] in /home/vagrant/.rvm/gems/ruby-1.9.3-p547/gems/fog-1.16.0/lib/fog/vsphere/requests/compute/modify_vm_interface.rb
6 def add_vm_interface(vmid, options = {})
7 raise ArgumentError, "instance id is a required parameter" unless vmid
8 debugger
9
10 interface=get_interface_from_options(vmid, options)
=> 11 vm_reconfig_hardware('instance_uuid' => vmid, 'hardware_spec' => {'deviceChange'=>[create_interface(interface)]})
12 end
13
14 def destroy_vm_interface(vmid, options = {})
15 raise ArgumentError, "instance id is a required parameter" unless vmid
(rdb:1) s
/home/vagrant/.rvm/gems/ruby-1.9.3-p547/gems/fog-1.16.0/lib/fog/vsphere/requests/compute/create_vm.rb:79
}

[74, 83] in /home/vagrant/.rvm/gems/ruby-1.9.3-p547/gems/fog-1.16.0/lib/fog/vsphere/requests/compute/create_vm.rb
74 :label => nic.name,
75 :summary => nic.summary,
76 },
77 :backing => create_nic_backing(nic, attributes),
78 :addressType => 'generated')
=> 79 }
80 end
81
82 def create_controller operation = :add, controller_key = 1000, bus_id = 0
83 {
(rdb:1) attributes
{}
(rdb:1) s

# I hit next three times

/home/vagrant/.rvm/gems/ruby-1.9.3-p547/gems/fog-1.16.0/lib/fog/vsphere/requests/compute/create_vm.rb:53
raw_network = get_raw_network(nic.network, attributes[:datacenter], if nic.virtualswitch then nic.virtualswitch end)

[48, 57] in /home/vagrant/.rvm/gems/ruby-1.9.3-p547/gems/fog-1.16.0/lib/fog/vsphere/requests/compute/create_vm.rb
48 end
49 devices.flatten
50 end
51
52 def create_nic_backing nic, attributes
=> 53 raw_network = get_raw_network(nic.network, attributes[:datacenter], if nic.virtualswitch then nic.virtualswitch end)
54
55 if raw_network.kind_of? RbVmomi::VIM::DistributedVirtualPortgroup
56 RbVmomi::VIM.VirtualEthernetCardDistributedVirtualPortBackingInfo(
57 :port => RbVmomi::VIM.DistributedVirtualSwitchPortConnection(
(rdb:1) attributes
{}
(rdb:1) n
/home/vagrant/.rvm/gems/ruby-1.9.3-p547/gems/fog-1.16.0/lib/fog/vsphere/requests/compute/create_vm.rb:55
if raw_network.kind_of? RbVmomi::VIM::DistributedVirtualPortgroup

[50, 59] in /home/vagrant/.rvm/gems/ruby-1.9.3-p547/gems/fog-1.16.0/lib/fog/vsphere/requests/compute/create_vm.rb
50 end
51
52 def create_nic_backing nic, attributes
53 raw_network = get_raw_network(nic.network, attributes[:datacenter], if nic.virtualswitch then nic.virtualswitch end)
54
=> 55 if raw_network.kind_of? RbVmomi::VIM::DistributedVirtualPortgroup
56 RbVmomi::VIM.VirtualEthernetCardDistributedVirtualPortBackingInfo(
57 :port => RbVmomi::VIM.DistributedVirtualSwitchPortConnection(
58 :portgroupKey => raw_network.key,
59 :switchUuid => raw_network.config.distributedVirtualSwitch.uuid
(rdb:1) raw_network
nil
(rdb:1) raw_network = get_raw_network(nic.network, 'data-center-name-here', if nic.virtualswitch then nic.virtualswitch end)
DistributedVirtualPortgroup("dvportgroup-100")
(rdb:1) raw_network.kind_of? RbVmomi::VIM::DistributedVirtualPortgroup
true
(rdb:1) get_raw_network(nic.network, attributes[:datacenter], if nic.virtualswitch then nic.virtualswitch end).kind_of? RbVmomi::VIM::DistributedVirtualPortgroup
false
(rdb:1)
```