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 memory configuration #94

Merged
merged 3 commits into from
Oct 1, 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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ This provider has the following settings, all are required unless noted:
* `proxy_host` - _Optional_ proxy host name for connecting to vSphere via proxy
* `proxy_port` - _Optional_ proxy port number for connecting to vSphere via proxy
* `vlan` - _Optional_ vlan to connect the first NIC to
* `memory_mb` - _Optional_ Configure the amount of memory (in MB) for the new VM

### Cloning from a VM rather than a template

Expand Down
8 changes: 7 additions & 1 deletion lib/vSphere/action/clone.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,12 @@ def call(env)
begin
location = get_location connection, machine, config, template
spec = RbVmomi::VIM.VirtualMachineCloneSpec :location => location, :powerOn => true, :template => false
spec[:config] = RbVmomi::VIM.VirtualMachineConfigSpec
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?
add_custom_memory(spec, config.memory_mb) unless config.memory_mb.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 @@ -137,7 +139,7 @@ def get_vm_base_folder(dc, template, config)
end

def add_custom_vlan(template, dc, spec, vlan)
spec[:config] = RbVmomi::VIM.VirtualMachineConfigSpec(:deviceChange => Array.new)
spec[:config][:deviceChange] = []
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
Expand All @@ -151,6 +153,10 @@ def add_custom_vlan(template, dc, spec, vlan)
dev_spec = RbVmomi::VIM.VirtualDeviceConfigSpec(:device => card, :operation => "edit")
spec[:config][:deviceChange].push dev_spec
end

def add_custom_memory(spec, memory_mb)
spec[:config][:memoryMB] = Integer(memory_mb)
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 @@ -20,6 +20,7 @@ class Config < Vagrant.plugin('2', :config)
attr_accessor :proxy_host
attr_accessor :proxy_port
attr_accessor :vlan
attr_accessor :memory_mb

def validate(machine)
errors = _detected_errors
Expand Down
22 changes: 18 additions & 4 deletions spec/clone_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
expect(@template).to have_received(:CloneVM_Task).with({
:folder => @data_center,
:name => NAME,
:spec => {:location => {:pool => @child_resource_pool} }
:spec => {:location => {:pool => @child_resource_pool},
:config => RbVmomi::VIM.VirtualMachineConfigSpec }
})
end

Expand All @@ -25,7 +26,8 @@
expect(@template).to have_received(:CloneVM_Task).with({
:folder => custom_base_folder,
:name => NAME,
:spec => {:location => {:pool => @child_resource_pool} }
:spec => {:location => {:pool => @child_resource_pool},
:config => RbVmomi::VIM.VirtualMachineConfigSpec }
})
end

Expand Down Expand Up @@ -55,11 +57,22 @@
:name => NAME,
:spec => {:location =>
{:pool => @child_resource_pool},
:config => expected_config
:config => expected_config
}
})
end

it 'should create a CloneVM spec with configured memory_mb' do
@machine.provider_config.stub(:memory_mb).and_return(2048)
call
expect(@template).to have_received(:CloneVM_Task).with({
:folder => @data_center,
:name => NAME,
:spec => {:location => {:pool => @child_resource_pool},
:config => RbVmomi::VIM.VirtualMachineConfigSpec(:memoryMB => 2048) },
})
end

it 'should set static IP when given config spec' do
@machine.provider_config.stub(:customization_spec_name).and_return('spec')
call
Expand All @@ -72,7 +85,8 @@
expect(@template).to have_received(:CloneVM_Task).with({
:folder => @data_center,
:name => NAME,
:spec => {:location => {:pool => @root_resource_pool } }
:spec => {:location => {:pool => @root_resource_pool },
:config => RbVmomi::VIM.VirtualMachineConfigSpec }
})
end
end
3 changes: 2 additions & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ def call
:linked_clone => nil,
:proxy_host => nil,
:proxy_port => nil,
:vlan => nil)
:vlan => nil,
:memory_mb => nil)
vm_config = double(
:vm => double('config_vm',
:box => nil,
Expand Down