2013
12.03

vCloud API in PHP

I manage several vCloud environments at the moment ranging from versions 1.5 to 5.1 right now (no 5.5’s yet) with permissions ranging from System Admin to Organization Admin. If you have ever managed a growing vCloud environment then you already know it is paramount to use some form of automation, scripts or even powershell to handle some of the very repetitive tasks you will be doing on a regular basis otherwise you will become overwhelmed by them.

What I have unfortunately noticed is that almost no one really talks about or documents anything beyond absolute basics so going forward I am going to share some of my tools on this blog starting with the basic and going all the way up to my OVF transfer and network1.5->network5.1 scripts.

Today we’ll start with an easy one.

lsorg

 #!/usr/bin/php
<?php
   /*
    * lsorg 0.2 by Phil Spencer 2013
    */
   $home = getenv('HOME');
   require_once $home . '/etc/config.php';

   // login
   $service = VMware_VCloud_SDK_Service::getService();
   $service->login($server, array('username'=>$user, 'password'=>$pswd), $httpConfig);

   // create an SDK Query object
   $sdkQuery = VMware_VCloud_SDK_Query::getInstance($service);

   $recsObj = $sdkQuery->queryReferences("organization",null);
   foreach($recsObj->getReference() as $orgRef) {
      echo $orgRef->get_name() . "\n";
   }
   echo "\n";

   $service->logout();
?>

This is a simple CLI utility I wrote that dumps out the Organizations in whatever cloud I am using at the time. The config.php file is the same config.php that comes with the PHP SDK samples. I have just put all my cloud config.php files into one directory and switch between them depending on what cloud I am accessing at the time.

As you can see in this case I create a VMware_VCloud_SDK_Query object then request all the organization records my permissions allow me access to with queryReferences(“organization”,null). If I were to replace null with search terms or a specific name I would get the record for just the Organizations that matched. Then with a foreach I loop through them and output the names. You might not see a reason for this script yet but when I am working with other scripts that require an Organization name sometimes it is easier to have the list in front of me to copy/paste quickly.

I also have this script in my /usr/bin folder for easy access, a quick lsorg on the command line and away I go.

So as I said, an easy one but there will be more to follow.

 

Facebook Comments

No Comment.

Add Your Comment

You must be logged in to post a comment.