Skip to content

Commit c3ea4c8

Browse files
author
Matt Richardson
committed
#62 - Use root resource pool when cloning from template
1 parent 7836f90 commit c3ea4c8

File tree

5 files changed

+29
-12
lines changed

5 files changed

+29
-12
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ This provider has the following settings, all are required unless noted:
9090
* `password` - password for connecting to vSphere
9191
* `data_center_name` - _Optional_ datacenter containing the computed resource, the template and where the new VM will be created, if not specified the first datacenter found will be used
9292
* `compute_resource_name` - _Required if cloning from template_ the name of the host containing the resource pool for the new VM
93-
* `resource_pool_name` - _Required if cloning from template_ the resource pool for the new VM
93+
* `resource_pool_name` - the resource pool for the new VM. If not supplied, and cloning from a template, uses the root resource pool
9494
* `clone_from_vm` - _Optional_ use a virtual machine instead of a template as the source for the cloning operation
9595
* `template_name` - the VM or VM template to clone
9696
* `name` - _Optional_ name of the new VM, if missing the name will be auto generated

lib/vSphere/config.rb

+1-2
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,8 @@ def validate(machine)
2828
errors << I18n.t('vsphere.config.password') if password.nil?
2929
errors << I18n.t('vsphere.config.template') if template_name.nil?
3030

31-
# These are only required if we're cloning from an actual template
31+
# Only required if we're cloning from an actual template
3232
errors << I18n.t('vsphere.config.compute_resource') if compute_resource_name.nil? and not clone_from_vm
33-
errors << I18n.t('vsphere.config.resource_pool') if resource_pool_name.nil? and not clone_from_vm
3433

3534
{ 'vSphere Provider' => errors }
3635
end

lib/vSphere/util/vim_helpers.rb

+5-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@ def get_vm_by_uuid(connection, machine)
1414

1515
def get_resource_pool(connection, machine)
1616
cr = get_datacenter(connection, machine).find_compute_resource(machine.provider_config.compute_resource_name) or fail Errors::VSphereError, :message => I18n.t('vsphere.errors.missing_compute_resource')
17-
cr.resourcePool.find(machine.provider_config.resource_pool_name) or fail Errors::VSphereError, :message => I18n.t('vsphere.errors.missing_resource_pool')
17+
rp = cr.resourcePool
18+
if !(machine.provider_config.resource_pool_name.nil?)
19+
rp = cr.resourcePool.find(machine.provider_config.resource_pool_name) or fail Errors::VSphereError, :message => I18n.t('vsphere.errors.missing_resource_pool')
20+
end
21+
rp
1822
end
1923

2024
def get_customization_spec_info_by_name(connection, machine)

spec/clone_spec.rb

+13-3
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66
end
77

88
it 'should create a CloneVM task' do
9-
call
9+
call
1010
@template.should have_received(:CloneVM_Task).with({
1111
:folder => @data_center,
1212
:name => NAME,
13-
:spec => {}
13+
:spec => {:location => {:pool => @child_resource_pool} }
1414
})
1515
end
1616

@@ -23,10 +23,20 @@
2323
call
2424
@app.should have_received :call
2525
end
26-
26+
2727
it 'should set static IP when given config spec' do
2828
@machine.provider_config.stub(:customization_spec_name).and_return('spec')
2929
call
3030
@ip.should have_received(:ipAddress=).with('0.0.0.0')
3131
end
32+
33+
it 'should use root resource pool when cloning from template and no resource pool specified' do
34+
@machine.provider_config.stub(:resource_pool_name).and_return(nil)
35+
call
36+
@template.should have_received(:CloneVM_Task).with({
37+
:folder => @data_center,
38+
:name => NAME,
39+
:spec => {:location => {:pool => @root_resource_pool } }
40+
})
41+
end
3242
end

spec/spec_helper.rb

+9-5
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,12 @@ def call
7474
vm_folder.stub(:findByUuid).with(MISSING_UUID).and_return(nil)
7575
vm_folder.stub(:findByUuid).with(nil).and_return(nil)
7676

77+
@child_resource_pool = double('testresourcepool')
78+
@root_resource_pool = double('pools', :find => @child_resource_pool)
79+
7780
@data_center = double('data_center',
7881
:vmFolder => vm_folder,
79-
:find_compute_resource => double('compute resource', :resourcePool => double('pools', :find => {})))
82+
:find_compute_resource => double('compute resource', :resourcePool => @root_resource_pool))
8083

8184
@template = double('template_vm',
8285
:parent => @data_center,
@@ -87,14 +90,15 @@ def call
8790

8891
service_instance = double 'service_instance', :find_datacenter => @data_center
8992
@ip = double 'ip', :ipAddress= => nil
90-
customization_spec = double 'customization spec', :nicSettingMap => [double('nic setting', :adapter => double('adapter', :ip => @ip))]
91-
customization_spec.stub(:clone).and_return(customization_spec)
92-
customization_spec_manager = double 'customization spec manager', :GetCustomizationSpec => double('spec info', :spec => customization_spec)
93+
@customization_spec = double 'customization spec', :nicSettingMap => [double('nic setting', :adapter => double('adapter', :ip => @ip))]
94+
@customization_spec.stub(:clone).and_return(@customization_spec)
95+
customization_spec_manager = double 'customization spec manager', :GetCustomizationSpec => double('spec info', :spec => @customization_spec)
9396
service_content = double 'service content', :customizationSpecManager => customization_spec_manager
9497
@vim = double 'vim', :serviceInstance => service_instance, :close => true, :serviceContent => service_content
9598

9699
VIM.stub(:connect).and_return(@vim)
97100
VIM.stub(:VirtualMachineRelocateSpec).and_return({})
98-
VIM.stub(:VirtualMachineCloneSpec).and_return({})
101+
VIM.stub(:VirtualMachineCloneSpec) do |location, powerOn, template| { :location => location[:location] } end
102+
99103
end
100104
end

0 commit comments

Comments
 (0)