Python – VMWare vSphere (WEB SDK) – SUDS

pythonsudsvmware

How do i contact the vSphere (or VMWare) form Python either with some form of library or via SUDS to get the number of number of vCPU or a specific host/guest/virtual-machine?

Currently i'm trying:

from suds.client import Client
from suds.sudsobject import Property

client = Client("https://<server>/sdk/vimService?wsdl")
queryCon = client.wsdl.services[0].ports[0].methods['QueryConnectionInfo']
print queryCon

And that works, and it gives me some form of output:

(Method){
   name = "QueryConnectionInfo"
   location = "https://localhost/sdk/vimService"
   binding =
      (binding){
         input = <suds.bindings.document.Document instance at 0x0775C080>
         output = <suds.bindings.document.Document instance at 0x0775C080>
      }
   soap =
      (soap){
         action = ""urn:vim25/4.1""
         style = "document"
         input =
            (Input){
               body =
                  (Body){
                     parts[] =
                        (Part){
                           root = <part name="parameters" element="vim25:QueryConnectionInfo"/>
                           name = "parameters"
                           qname[] =
                              "parameters",
                              "urn:vim25",
                           element = "(u'QueryConnectionInfo', u'urn:vim25')"
                           type = "None"
                        },
                     use = "literal"
                     namespace[] =
                        "vim25",
                        "urn:vim25",
                     wrapped = True
                  }
               headers[] = <empty>
            }
         output =
            (Output){
               body =
                  (Body){
                     parts[] =
                        (Part){
                           root = <part name="parameters" element="vim25:QueryConnectionInfoResponse"/>
                           name = "parameters"
                           qname[] =
                              "parameters",
                              "urn:vim25",
                           element = "(u'QueryConnectionInfoResponse', u'urn:vim25')"
                           type = "None"
                        },
                     use = "literal"
                     namespace[] =
                        "vim25",
                        "urn:vim25",
                     wrapped = True
                  }
               headers[] = <empty>
            }
         faults[] =
            (Fault){
               name = "InvalidLoginFault"
               use = "literal"
               parts[] =
                  (Part){
                     root = <part name="fault" element="vim25:InvalidLoginFault"/>
                     name = "fault"
                     qname[] =
                        "fault",
                        "urn:vim25",
                     element = "(u'InvalidLoginFault', u'urn:vim25')"
                     type = "None"
                  },
            },
            (Fault){
               name = "HostConnectFaultFault"
               use = "literal"
               parts[] =
                  (Part){
                     root = <part name="fault" element="vim25:HostConnectFaultFault"/>
                     name = "fault"
                     qname[] =
                        "fault",
                        "urn:vim25",
                     element = "(u'HostConnectFaultFault', u'urn:vim25')"
                     type = "None"
                  },
            },
            (Fault){
               name = "RuntimeFault"
               use = "literal"
               parts[] =
                  (Part){
                     root = <part name="fault" element="vim25:RuntimeFaultFault"/>
                     name = "fault"
                     qname[] =
                        "fault",
                        "urn:vim25",
                     element = "(u'RuntimeFaultFault', u'urn:vim25')"
                     type = "None"
                  },
            },
      }
 }

I've tried following the following "guides":

SUDS – programmatic access to methods and types

http://www.vmware.com/support/developer/vc-sdk/visdk41pubs/ApiReference/vim.VirtualMachine.html#field_detail

http://communities.vmware.com/thread/273616

I know that all the information probably is here, i just can't see the entire picture :/

After a while of trying this is where i get stuck:

client = Client("https://<server>/sdk/vimService?wsdl")
#queryCon = client.wsdl.services[0].ports[0].methods['QueryConnectionInfo']
print client.service.QueryConnectionInfo("https://<server>/sdk", None, r'domain\user', 'Password')

And the output is:

urllib2.URLError: <urlopen error [Errno 1] _ssl.c:490: error:140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown protocol>

Best Answer

You might want to try psphere, a Python library (based on suds) which will give you access the entire vSphere Web Services SDK.

Install it:

$ pip install psphere

Find a virtual machine and print the number of CPUs it has:

>>> from psphere.client import Client
>>> from psphere.managedobjects import VirtualMachine
>>> client = Client(server="vcenter.mydomain.com", username="Administrator", password="strong")
>>> vm = VirtualMachine.get(client, name="genesis")
>>> vm
<psphere.managedobjects.VirtualMachine object at 0xd3fbccc>
>>> print("%s has %s CPUs" % (vm.name, vm.config.hardware.numCPU))
genesis has 2 CPUs

You can find more examples in the documentation.

Disclaimer: I am the author of psphere.