1
0
Fork 0
mirror of https://github.com/fog/fog.git synced 2022-11-09 13:51:43 -05:00

Merge pull request #704 from estonfer/blank_vms

This patch allows the ability to create 'blank' vms in vsphere
This commit is contained in:
Jeff McCune 2012-01-23 08:40:00 -08:00
commit 0ebad09a45
4 changed files with 125 additions and 1 deletions

View file

@ -20,6 +20,7 @@ module Fog
request :vm_power_on
request :vm_reboot
request :vm_clone
request :vm_create
request :vm_destroy
request :datacenters

View file

@ -56,7 +56,13 @@ module Fog
requires :instance_uuid
connection.vm_destroy('instance_uuid' => instance_uuid)
end
def create(options ={})
requires :name, :path
new_vm = self.class.new(create_results['vm_attributes'])
new_vm.collection = self.collection
new_vm.connection = self.connection
new_vm
end
def clone(options = {})
requires :name, :path
# Convert symbols to strings

View file

@ -0,0 +1,97 @@
module Fog
module Compute
class Vsphere
module Shared
private
def vm_create_check_options(options)
options = { 'force' => false }.merge(options)
required_options = %w{ path name cluster }
required_options.each do |param|
raise ArgumentError, "#{required_options.join(', ')} are required" unless options.has_key? param
end
# The tap removes the leading empty string
path_elements = options['path'].split('/').tap { |o| o.shift }
first_folder = path_elements.shift
if first_folder != 'Datacenters' then
raise ArgumentError, "vm_create path option must start with /Datacenters. Got: #{options['path']}"
end
dc_name = path_elements.shift
if not self.datacenters.include? dc_name then
raise ArgumentError, "Datacenter #{dc_name} does not exist, only datacenters #{self.datacenters.join(",")} are accessible."
end
options
end
end
class Real
include Shared
def vm_create(options = {})
# Option handling
options = vm_create_check_options(options)
path_elements = options['path'].split('/').tap { |ary| ary.shift 2 }
dc_name = path_elements.shift
vm_cfg = {
:name => options['name'],
:guestId => options['guest_id'] ? options['guest_id'] : 'otherGuest',
:files => { :vmPathName => "[#{options['datastore']}]" },
:numCPUs => options['num_cpus'] ? options['num_cpus'] : 1 ,
:memoryMB => options['memory'] ? options['memory'] : 512,
:memoryHotAddEnabled => options['memory_hot_add_enabled'] ? options['memory_hot_add_enabled'] : 0,
:cpuHotAddEnabled => options['cpu_hot_add_enabled'] ? options['cpu_hot_add_enabled'] : 0,
:deviceChange => options['device_array'].class == Array ? options['device_array'] : nil,
:extraConfig => options['extra_config'].class == Array ? options['extra_config'] : nil,
}
self.datacenters
dc = @datacenters[dc_name]
vm_folder = dc.vmFolder
folder = path_elements.inject(vm_folder) do |current_folder, sub_folder_name|
sub_folder = current_folder.find(sub_folder_name, RbVmomi::VIM::Folder)
raise ArgumentError, "Could not descend into #{sub_folder_name}. Please check your path." unless sub_folder
sub_folder
end
clusters = dc.hostFolder.children
build_cluster=''
clusters.each { |my_cluster|
if "#{my_cluster.name}" == "#{options['cluster']}"
build_cluster=my_cluster
end
}
resource_pool = build_cluster.resourcePool
task=folder.CreateVM_Task(:config => vm_cfg, :pool => resource_pool)
if options['wait'] then
new_vm = task.wait_for_completion
else
tries = 0
new_vm = begin
folder.find(options['name'], RbVmomi::VIM::VirtualMachine) or raise Fog::Vsphere::Errors::NotFound
rescue Fog::Vsphere::Errors::NotFound
tries += 1
if tries <= 10 then
sleep 15
retry
end
nil
end
end
{
'vm_ref' => new_vm ? new_vm._ref : nil,
'vm_attributes' => new_vm ? convert_vm_mob_ref_to_attr_hash(new_vm) : {},
'task_ref' => task._ref
}
end
end
class Mock
include Shared
def vm_create(options = {})
# Option handling
options = vm_create_check_options(options)
{
'vm_ref' => 'vm-123',
'task_ref' => 'task-1234'
}
end
end
end
end
end

View file

@ -0,0 +1,20 @@
Shindo.tests("Fog::Compute[:vsphere] | vm_create request", 'vsphere') do
#require 'guid'
path = "/Datacenters/Solutions"
compute = Fog::Compute[:vsphere]
tests("The return value should") do
response = compute.vm_create('path' => path, 'name' => 'fog_test_vm', 'cluster' => 'cluster01')
test("be a kind of Hash") { response.kind_of? Hash }
%w{ vm_ref task_ref }.each do |key|
test("have a #{key} key") { response.has_key? key }
end
end
tests("When invalid input is presented") do
raises(ArgumentError, 'it should raise ArgumentError') { compute.vm_create(:foo => 1) }
raises(Fog::Compute::Vsphere::NotFound, 'it should raise Fog::Compute::Vsphere::NotFound when the UUID is not a string') do
pending # require 'guid'
compute.vm_create('instance_uuid' => Guid.from_s(template), 'name' => 'jefftestfoo')
end
end
end