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

Add vlan configuration #91

Merged
merged 2 commits into from
Sep 19, 2014
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
19 changes: 18 additions & 1 deletion lib/vSphere/action/clone.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ def call(env)
customization_info = get_customization_spec_info_by_name connection, machine

spec[:customization] = get_customization_spec(machine, customization_info) unless customization_info.nil?
add_custom_vlan(template, dc, spec, config.vlan) unless config.vlan.nil?

env[:ui].info I18n.t('vsphere.creating_cloned_vm')
env[:ui].info " -- #{config.clone_from_vm ? "Source" : "Template"} VM: #{template.pretty_path}"
Expand Down Expand Up @@ -131,9 +132,25 @@ def get_vm_base_folder(dc, template, config)
if config.vm_base_path.nil?
template.parent
else
dc.vmFolder.traverse(config.vm_base_path, RbVmomi::VIM::Folder, create=true)
dc.vmFolder.traverse(config.vm_base_path, RbVmomi::VIM::Folder, create = true)
end
end

def add_custom_vlan(template, dc, spec, vlan)
spec[:config] = RbVmomi::VIM.VirtualMachineConfigSpec(:deviceChange => Array.new)
network = get_network_by_name(dc, vlan)
config = template.config
card = config.hardware.device.grep(RbVmomi::VIM::VirtualEthernetCard).first or fail Errors::VSphereError, :missing_network_card
begin
switch_port = RbVmomi::VIM.DistributedVirtualSwitchPortConnection(:switchUuid => network.config.distributedVirtualSwitch.uuid, :portgroupKey => network.key)
card.backing.port = switch_port
rescue
# not connected to a distibuted switch?
card.backing.deviceName = network.name
end
dev_spec = RbVmomi::VIM.VirtualDeviceConfigSpec(:device => card, :operation => "edit")
spec[:config][:deviceChange].push dev_spec
end
end
end
end
Expand Down
1 change: 1 addition & 0 deletions lib/vSphere/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class Config < Vagrant.plugin('2', :config)
attr_accessor :linked_clone
attr_accessor :proxy_host
attr_accessor :proxy_port
attr_accessor :vlan

def validate(machine)
errors = _detected_errors
Expand Down
4 changes: 4 additions & 0 deletions lib/vSphere/util/vim_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ def get_datastore(connection, machine)

get_datacenter(connection, machine).find_datastore name or fail Errors::VSphereError, :missing_datastore
end

def get_network_by_name(dc, name)
dc.network.find { |f| f.name == name } or fail Errors::VSphereError, :missing_vlan
end
end
end
end
Expand Down
4 changes: 4 additions & 0 deletions locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ en:
Configured data store not found
too_many_private_networks: |-
There a more private networks configured than can be assigned to the customization spec
missing_vlan: |-
Configured vlan not found
missing_network_card: |-
Cannot find network card to customize
config:
host: |-
Configuration must specify a vSphere host
Expand Down
21 changes: 21 additions & 0 deletions spec/clone_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,27 @@
expect(@app).to have_received :call
end

it 'should create a CloneVM spec with configured vlan' do
@machine.provider_config.stub(:vlan).and_return('vlan')
network = double('network', :name => 'vlan')
network.stub(:config).and_raise(StandardError)
@data_center.stub(:network).and_return([network])
call

expected_config = RbVmomi::VIM.VirtualMachineConfigSpec(:deviceChange => Array.new)
expected_dev_spec = RbVmomi::VIM.VirtualDeviceConfigSpec(:device => @device, :operation => "edit")
expected_config[:deviceChange].push expected_dev_spec

expect(@template).to have_received(:CloneVM_Task).with({
:folder => @data_center,
:name => NAME,
:spec => {:location =>
{:pool => @child_resource_pool},
:config => expected_config
}
})
end

it 'should set static IP when given config spec' do
@machine.provider_config.stub(:customization_spec_name).and_return('spec')
call
Expand Down
14 changes: 12 additions & 2 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ def call
:clone_from_vm => nil,
:linked_clone => nil,
:proxy_host => nil,
:proxy_port => nil)
:proxy_port => nil,
:vlan => nil)
vm_config = double(
:vm => double('config_vm',
:box => nil,
Expand Down Expand Up @@ -101,11 +102,20 @@ def call
:pretty_path => "data_center/#{vm_folder}",
:find_compute_resource => double('compute resource', :resourcePool => @root_resource_pool))

@device = RbVmomi::VIM::VirtualEthernetCard.new
@device.stub(:backing).and_return(RbVmomi::VIM::VirtualEthernetCardNetworkBackingInfo.new)

@virtual_hardware = double('virtual_hardware',
:device => [@device])
@template_config = double('template_config',
:hardware => @virtual_hardware)

@template = double('template_vm',
:parent => @data_center,
:pretty_path => "#{@data_center.pretty_path}/template_vm",
:CloneVM_Task => double('result',
:wait_for_completion => double('new_vm', :config => double('config', :uuid => NEW_UUID))))
:wait_for_completion => double('new_vm', :config => double('config', :uuid => NEW_UUID))),
:config => @template_config)

@data_center.stub(:find_vm).with(TEMPLATE).and_return(@template)

Expand Down