vSphere API Usage

Hi, I am looking for some guidance to working with the vSphere API and Ruby. Here are some questions someone might be able to help me with;

  1. Do you have any advice on how to learn/work with the SOAP API in vSphere. Fairly open ended question so maybe the next two give some more context.
  2. Here is a Ruby script that does the initial authentication to get the cookie that is required for subsequent calls, can you check the script and give me guidance on who it is actually written, I am concerned that I have actually written out the soap envelope, when actually this could be done by a ruby method? Also is this your experience, having to get the cookie first?


  1. The ManagedObjectReference seems to be required everywhere in the SOAP API, e.g. you cannot call any of the SOAP API without having to give it some sort of MOR, do you have some code as examples how you have navigated through vSphere MOR? I am thinking things like the UI Folders view must use that extensively?

I do have a use case, and thats “Importing an OVA form HTTP source into vSphere”, I know this needs to be done using the importvapp function in the api, but is proving very difficult to work with, I guess if I can get a good grounding in the api first then any function will be easily understood.

Many thanks

The pastebin isn’t working for me, so I’m not sure of your approach, but my first thought is to leverage the existing objects we have directly. That is, don’t “do it yourself” with respect to that API, since we’ve done most of the heavy lifting already.

Eventually, we could probably extract our vSphere interaction classes into a gem to allow one-off scripts for interacting with vSphere.

cc @rpo

How do you view all the vSphere objects that are available?

The vSphere Web Services SDK documentation is very extensive.


Given the foundation, you can use the API reference to determine types of objects and their relationships.

Sorry… I’m new to the product, and trying to get a grasp on the structure. Where are the methods stored in ManageIQ that make the vSphere SDK calls? I’m wanting to automate the process of a storage vMotion, and trying to figure out how to create the method.

Ah, the implementation of the API binding is in: lib/VMwareWebService.

The VimService.rb file contains the low-level implementation of each SOAP call; and MiqVimClientBase.rb shows the http initialization and authentication sequence.

There is also a test directory with examples. However, I don’t think there’s an example of storage vmotion.

Thank You. This helps. I haven’t seen an example of a storage vmotion, so every bit helps.

How would I insert MiqVimVm into a method in Automate?

You’ll need to use RelocateVM_Task.

There’s no example in the test directory, but there is code implemented in MiqVimVm.rb, namely the relocateVM method.

Hope this helps.

Here is an example using the savon gem that ships with the appliance:


Description: This method changes a VMware VM’s guestID to RHEL7 x86_64

Reference: http://pubs.vmware.com/vsphere-55/index.jsp?topic=%2Fcom.vmware.wssdk.apiref.doc%2Fvim.vm.GuestOsDescriptor.GuestOsIdentifier.html

require ‘savon’

def login(client, username, password)
result = client.call(:login) do
message( :_this => “SessionManager”, :userName => username, :password => password )
client.globals.headers({ “Cookie” => result.http.headers[“Set-Cookie”] })

def logout(client)
client.call(:logout) do
message(:_this => “SessionManager”)
rescue => logouterr
$evm.log(:error, “Error logging out #{logouterr.class} #{logouterr}”)

Get vm object from root

vm = $evm.root[‘vm’]
raise “VM object not found” if vm.nil?

This method only works with VMware VMs currently

raise “Invalid vendor:<#{vm.vendor}>” unless vm.vendor.downcase == ‘vmware’

$evm.log(:info,“Detected VM: #{vm.name} vendor: #{vm.vendor} provider: #{vm.ext_management_system.name} ems_ref: #{vm.ems_ref}”)

get servername and credentials from vm.ext_management_system

servername = vm.ext_management_system.ipaddress
username = vm.ext_management_system.authentication_userid
password = vm.ext_management_system.authentication_password

client = Savon.client(
:wsdl => “https://#{servername}/sdk/vim.wsdl”,
:endpoint => “https://#{servername}/sdk/”,
:ssl_verify_mode => :none,
:ssl_version => :TLSv1,
:raise_errors => false,
:log_level => :info,
:log => false
#client.operations.sort.each { |operation| $evm.log(:info, “Savon Operation: #{operation}”) }

login and set cookie

login(client, username, password)

reconfig_vm_task_result = client.call(:reconfig_vm_task) do
message( ‘_this’ => vm.ems_ref, :attributes! => { ‘type’ => ‘VirtualMachine’ },
‘spec’ => {‘guestId’ => [‘rhel7_64Guest’]}, :attributes! => { ‘type’ => ‘VirtualMachineConfigSpec’ } ).to_hash




$evm.log(:warn, “reconfig_vm_task_result: #{reconfig_vm_task_result.inspect}”)
$evm.log(:info, “reconfig_vm_task_result success?: #{reconfig_vm_task_result.success?}”)


hey there,

maybe there is another way to get some informations from the vcenter ?!
i don’t know if it is technically possible or not… but let me think loud.

we can use the

ems.object_send('instance_eval', 'def...')

method we can read out some informations…

vmObject  = vm.object_send('instance_eval', 'self')
ems       = vm.ext_management_system

ems.object_send('instance_eval', '
  def device_lookup(vm)
    vm.with_provider_object do | vimVm |
      matchedDev = vimVm.send(:getProp, "config.hardware")
      $log.info("VM Informations: #{matchedDev["config"]["hardware"]["device"]}")
ems.object_send('device_lookup', vmObject)

if you run this, you will get out some informations about a VM in evm.log requested from the vCenter.

Now, the question:
is it possible to get this data back to the statemachine ?

1 Like

no further comment here ?