From 9fdbd35415f9dd384cec360daf8148c70f8d86cb Mon Sep 17 00:00:00 2001 From: Eric Stonfer Date: Fri, 24 Feb 2012 23:47:41 -0500 Subject: [PATCH] add host based vmotion --- lib/fog/vsphere/compute.rb | 1 + lib/fog/vsphere/models/compute/server.rb | 7 ++++ .../vsphere/requests/compute/vm_migrate.rb | 37 +++++++++++++++++++ .../requests/compute/vm_migrate_tests.rb | 16 ++++++++ 4 files changed, 61 insertions(+) create mode 100644 lib/fog/vsphere/requests/compute/vm_migrate.rb create mode 100644 tests/vsphere/requests/compute/vm_migrate_tests.rb diff --git a/lib/fog/vsphere/compute.rb b/lib/fog/vsphere/compute.rb index fd5a990e4..92c3b630b 100644 --- a/lib/fog/vsphere/compute.rb +++ b/lib/fog/vsphere/compute.rb @@ -22,6 +22,7 @@ module Fog request :vm_clone request :vm_create request :vm_destroy + request :vm_migrate request :datacenters request :vm_reconfig_hardware request :vm_reconfig_memory diff --git a/lib/fog/vsphere/models/compute/server.rb b/lib/fog/vsphere/models/compute/server.rb index 60d6dcad8..fd0aeac5b 100644 --- a/lib/fog/vsphere/models/compute/server.rb +++ b/lib/fog/vsphere/models/compute/server.rb @@ -68,6 +68,13 @@ module Fog requires :instance_uuid connection.vm_destroy('instance_uuid' => instance_uuid) end + + def migrate(options = {}) + options = { :priority => 'defaultPriority' }.merge(options) + requires :instance_uuid + connection.vm_migrate('instance_uuid' => instance_uuid, 'priority' => options[:priority]) + end + def create(options ={}) requires :name, :path new_vm = self.class.new(create_results['vm_attributes']) diff --git a/lib/fog/vsphere/requests/compute/vm_migrate.rb b/lib/fog/vsphere/requests/compute/vm_migrate.rb new file mode 100644 index 000000000..edc34b916 --- /dev/null +++ b/lib/fog/vsphere/requests/compute/vm_migrate.rb @@ -0,0 +1,37 @@ +module Fog + module Compute + class Vsphere + class Real + + def vm_migrate(options = {}) + #priority is the only required option, and it has a sane default option. + priority = options['priority'].nil? ? 'defaultPriority' : options["priority"] + raise ArgumentError, "instance_uuid is a required parameter" unless options.has_key? 'instance_uuid' + + # Find the VM Object + search_filter = { :uuid => options['instance_uuid'], 'vmSearch' => true, 'instanceUuid' => true } + vm_mob_ref = @connection.searchIndex.FindAllByUuid(search_filter).first + + unless vm_mob_ref.kind_of? RbVmomi::VIM::VirtualMachine + raise Fog::Vsphere::Errors::NotFound, + "Could not find VirtualMachine with instance uuid #{options['instance_uuid']}" + end + task = vm_mob_ref.MigrateVM_Task(:pool => options['pool'], :host => options['host'], :priority => "#{priority}", :state => options['state'] ) + task.wait_for_completion + { 'task_state' => task.info.state } + end + + end + + class Mock + + def vm_migrate(options = {}) + priority = options['priority'].nil? ? 'defaultPriority' : options["priority"] + raise ArgumentError, "instance_uuid is a required parameter" unless options.has_key? 'instance_uuid' + { 'task_state' => 'success' } + end + + end + end + end +end diff --git a/tests/vsphere/requests/compute/vm_migrate_tests.rb b/tests/vsphere/requests/compute/vm_migrate_tests.rb new file mode 100644 index 000000000..252642506 --- /dev/null +++ b/tests/vsphere/requests/compute/vm_migrate_tests.rb @@ -0,0 +1,16 @@ +Shindo.tests('Fog::Compute[:vsphere] | vm_migrate request', ['vsphere']) do + + compute = Fog::Compute[:vsphere] + + powered_on_vm = '50137835-88a1-436e-768e-9b2677076e67' + + tests('The response should') do + response = compute.vm_migrate('instance_uuid' => powered_on_vm) + test('be a kind of Hash') { response.kind_of? Hash } + test('should have a task_state key') { response.has_key? 'task_state' } + end + + tests('The expected options') do + raises(ArgumentError, 'raises ArgumentError when instance_uuid option is missing') { compute.vm_migrate } + end +end