Open Source VMware PHP API experiment

I think this post is not useful anymore, now that VMware release their first PHP API for vCloud, anyway I’ve this little code I wrote as an experiment a few months ago that maybe could be used by someone having an old vCloud (the code you’ll see is wrote to work with VMware’s vCloud 2.5) or maybe who needs a simpler implementation.

I only wrote a few methods:

  • login to the VMware server
  • get info about all VMs
  • get info about a single VM
  • create a new VM setting the RAM amount, CPUs number, disk capacity
  • a useful methods that waits till a task (like a VM creation) is completed

The code you’ll download uses the old nusoap library and directly sends the raw XML to the server ’cause no other SOAP libraries/implementations were working at that time and for the time the experiment lived (I never used vCloud systems and I worked on this project less than a couple of days so…).

Here you have a set of sample calls to the library:

require “vim25.php”;
$vim25 = new vim25(“ip-address-of-the-vcloud-server”, “username”, “password”);

$service_content = $vim25->retrieveServiceContent();
$all_vm_info = $vim25->getAllVMInfo($service_content[“rootFolder”], VIM25_SUB_INFO_ALL);
$vm_info = $vim25->getVMInfo(“vm-16”, VIM25_SUB_INFO_GUEST);

$debug = $vim25->createVM(
“vm name”,
“group-id”,
“guest-id”,
“resgroup-id”,
“host-id”,
2, // num CPU
512, // RAM
10485760 // disk capacity in kB (10GB)
);

print_r($debug);

If you want to give a try to the code:

  • download the library code
  • rename to vim25.php
  • download and install nusoap in the “nusoap” folder (at the same level of the vim25.php file)
  • use the code above to script the vim25 class
  • extend it and use it :-)

This work is public domain thus do whatever you want with it and enjoy.

9 thoughts on “Open Source VMware PHP API experiment

  1. kaboon

    VMware changed the API with the release of vSphere 5 :(
    I will try to make this library work with their new version…

  2. kaboon

    @Fabrizio: I’m sorry, but I can’t find any information regarding an official VMware vCenter 5 PHP SDK. Where did you get that information?
    Only found something for vCloud but it is no longer available, it seems…

  3. jay jay

    well yes vmware released a php sdk the problem is it requireds you to license another one of their products vcloud director.. the sdk only talks and makes api trasactions over vcloud director not directly to vcenter.. which is not what every one will like to .. i do like your approach. nice little sample.

  4. Ai Ho

    $vm_info = $vim25->getVMInfo(“vm-16″, VIM25_SUB_INFO_GUEST);

    $debug = $vim25->createVM(
    “vm name”,
    “group-id”,
    “guest-id”,
    “resgroup-id”,
    “host-id”,
    2, // num CPU
    512, // RAM
    10485760 // disk capacity in kB (10GB)
    );

    this function may be wrong; when I run it return state: error

  5. Ai Ho

    Hi Fabrizio Balliano

    I can deploy VM from template

    but I have a big problem

    When I get XML from Onyx; it’s run Okie but when I send it to server it have error;

    pls help to review

    Array ( [faultcode] => ServerFaultCode [faultstring] => Unable to find specified dynamic type VirtualEthernetCardDistributedVirtualPortBackingInfo while parsing serialized DataObject of type vim.vm.device.VirtualDevice.BackingInfo at line 56, column 14 while parsing property “backing” of static type VirtualDeviceBackingInfo while parsing serialized DataObject of type vim.vm.device.VirtualE1000 at line 54, column 12 while parsing property “device” of static type VirtualDevice while parsing serialized DataObject of type vim.vm.device.VirtualDeviceSpec at line 52, column 5 while parsing property “deviceChange” of static type ArrayOfVirtualDeviceConfigSpec while parsing serialized DataObject of type vim.vm.ConfigSpec at line 19, column 2 while parsing property “config” of static type VirtualMachineConfigSpec while parsing serialized DataObject of type vim.vm.CloneSpec at line 7, column 6 while parsing call information for method CloneVM_Task at line 3, column 4 while parsing SOAP body at line 2, column 2 while parsing SOAP envelope at line 1, column 0 while parsing HTTP request for method clone on object of type vim.VirtualMachine at line 1, column 0 [detail] => Array ( [InvalidRequestFault] => ) )

    ===================================

    $xml=’

    ‘.$fromTemplate.’
    ‘.$group.’
    ‘.$Newname.’

    datastore-12
    resgroup-9

    2000
    datastore-12

    false

    ‘.$Newname.’

    [datastore1]

    2
    2048

    add
    create

    -100

    persistent
    false
    false
    true
    false

    true
    false
    true

    1000
    1
    8388608

    add

    -100

    b2 57 13 50 92 89 c3 d5-d4 23 15 6d 9f fb 34 7b
    dvportgroup-21

    true
    true
    true

    100
    generated
    true

    false

    ‘;

    ===============================

    add

    -100

    b2 57 13 50 92 89 c3 d5-d4 23 15 6d 9f fb 34 7b
    dvportgroup-21

    true
    true
    true

    100
    generated
    true

    ================================

    When I remve last xml it’s ok;

    the xml I get from ONyx

Leave a Reply

Your email address will not be published. Required fields are marked *