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

#62 - Use root resource pool when cloning from template #63

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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ This provider has the following settings, all are required unless noted:
* `password` - password for connecting to vSphere
* `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
* `compute_resource_name` - _Required if cloning from template_ the name of the host containing the resource pool for the new VM
* `resource_pool_name` - _Required if cloning from template_ the resource pool for the new VM
* `resource_pool_name` - the resource pool for the new VM. If not supplied, and cloning from a template, uses the root resource pool
* `clone_from_vm` - _Optional_ use a virtual machine instead of a template as the source for the cloning operation
* `template_name` - the VM or VM template to clone
* `name` - _Optional_ name of the new VM, if missing the name will be auto generated
Expand Down
3 changes: 1 addition & 2 deletions lib/vSphere/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,8 @@ def validate(machine)
errors << I18n.t('vsphere.config.password') if password.nil?
errors << I18n.t('vsphere.config.template') if template_name.nil?

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

{ 'vSphere Provider' => errors }
end
Expand Down
6 changes: 5 additions & 1 deletion lib/vSphere/util/vim_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ def get_vm_by_uuid(connection, machine)

def get_resource_pool(connection, machine)
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')
cr.resourcePool.find(machine.provider_config.resource_pool_name) or fail Errors::VSphereError, :message => I18n.t('vsphere.errors.missing_resource_pool')
rp = cr.resourcePool
if !(machine.provider_config.resource_pool_name.nil?)
rp = cr.resourcePool.find(machine.provider_config.resource_pool_name) or fail Errors::VSphereError, :message => I18n.t('vsphere.errors.missing_resource_pool')
end
rp
end

def get_customization_spec_info_by_name(connection, machine)
Expand Down
16 changes: 13 additions & 3 deletions spec/clone_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
end

it 'should create a CloneVM task' do
call
call
@template.should have_received(:CloneVM_Task).with({
:folder => @data_center,
:name => NAME,
:spec => {}
:spec => {:location => {:pool => @child_resource_pool} }
})
end

Expand All @@ -23,10 +23,20 @@
call
@app.should have_received :call
end

it 'should set static IP when given config spec' do
@machine.provider_config.stub(:customization_spec_name).and_return('spec')
call
@ip.should have_received(:ipAddress=).with('0.0.0.0')
end

it 'should use root resource pool when cloning from template and no resource pool specified' do
@machine.provider_config.stub(:resource_pool_name).and_return(nil)
call
@template.should have_received(:CloneVM_Task).with({
:folder => @data_center,
:name => NAME,
:spec => {:location => {:pool => @root_resource_pool } }
})
end
end
14 changes: 9 additions & 5 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,12 @@ def call
vm_folder.stub(:findByUuid).with(MISSING_UUID).and_return(nil)
vm_folder.stub(:findByUuid).with(nil).and_return(nil)

@child_resource_pool = double('testresourcepool')
@root_resource_pool = double('pools', :find => @child_resource_pool)

@data_center = double('data_center',
:vmFolder => vm_folder,
:find_compute_resource => double('compute resource', :resourcePool => double('pools', :find => {})))
:find_compute_resource => double('compute resource', :resourcePool => @root_resource_pool))

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

service_instance = double 'service_instance', :find_datacenter => @data_center
@ip = double 'ip', :ipAddress= => nil
customization_spec = double 'customization spec', :nicSettingMap => [double('nic setting', :adapter => double('adapter', :ip => @ip))]
customization_spec.stub(:clone).and_return(customization_spec)
customization_spec_manager = double 'customization spec manager', :GetCustomizationSpec => double('spec info', :spec => customization_spec)
@customization_spec = double 'customization spec', :nicSettingMap => [double('nic setting', :adapter => double('adapter', :ip => @ip))]
@customization_spec.stub(:clone).and_return(@customization_spec)
customization_spec_manager = double 'customization spec manager', :GetCustomizationSpec => double('spec info', :spec => @customization_spec)
service_content = double 'service content', :customizationSpecManager => customization_spec_manager
@vim = double 'vim', :serviceInstance => service_instance, :close => true, :serviceContent => service_content

VIM.stub(:connect).and_return(@vim)
VIM.stub(:VirtualMachineRelocateSpec).and_return({})
VIM.stub(:VirtualMachineCloneSpec).and_return({})
VIM.stub(:VirtualMachineCloneSpec) do |location, powerOn, template| { :location => location[:location] } end

end
end