Tuesday, April 23, 2013

More fun with the CloudStack API

http://kirkjantzer.blogspot.com/2013/04/more-fun-with-cloudstack-api.html

Tuesday, April 23, 2013 11:05 AMMore fun with the CloudStack APIThe blog of Kirk JantzerKirk
So, as I stated in a previous post, I've been playing with the CloudStack API in an effort to make deployment a mass amount of servers with as little effort as possible. In my first attempt, I had predetermined some of the settings for deploying an instance, since this was really only a test/POC. With my latest iteration, I was able to use the API to query those settings and create automatically populated drop-down lists :-D

Again, I am in no way a master a of PHP, much less dealing with an API; but that doesn't mean I'm afraid to try new things :-) I welcome any suggestions on improving this page and it is open for you to copy/modify/use/improve. Also of mention, this is certainly a work in progress. I currently have a very small setup: only one master server, with only one basic zone, and a minimal amount of disk and service offerings. I fully intend to grow this page as our environment grows, but I needed to be able to hit the ground running. 

On to the nerdy bits... Here is the code I have so far. It's quite ugly and could really use some optimization, but it works for what I need: 

 <html>  
 <head>  
 <title>CloudStack API</title>  
 </head>  
 <?php  
 $apikey = "apikey=******************************************************";  
 $secretkey = "******************************************************";  
 if(isset($_POST['submit']))   
 {   
      $prefix = $_POST['prefix'];  
      $endnum = ($_POST['startnum'] + $_POST['quantity']) - 1;  
      $startnum = $_POST['startnum'];  
      $zoneId = "zoneId=" . $_POST['zoneid'];  
      $serviceOfferingId = "serviceOfferingId=" . $_POST['serviceOfferingId'];  
      $diskOfferingId = "diskOfferingId=" . $_POST['diskOfferingId'];  
      $templateId = "templateId=" . $_POST['isoid'];  
      if (empty($prefix) || empty($startnum) || empty($_POST['quantity']))  
      {  
           echo "<b>You missed a field!</b><br />";  
      }  
      else  
      {  
           echo "You have submitted for the following servers to be built: <br />";  
           for ($i=$startnum; $i<=$endnum; $i++)  
           {  
                echo $prefix . $i . "<br />";  
                $hypervisor = "hypervisor=XenServer";  
                $response = "response=json";  
                $urlcommand = "command=deployVirtualMachine&" . $zoneId . "&" . $diskOfferingId . "&displayname=" . $prefix . $i . "&" . $hypervisor . "&name=" . $prefix . $i . "&" . $serviceOfferingId . "&" . $templateId . "&" . $apikey;  
                $hashcommand = "apikey=" . $apikey . "&command=deployVirtualMachine&zoneId=" . $zoneId . "&" . $diskOfferingId . "&displayname=" . $prefix . $i . "&" . $hypervisor . "&name=" . $prefix . $i . "&" . $serviceOfferingId . "&" . $templateId;  
                $hash = hash_hmac("sha1", strtolower($hashcommand), $secretkey.true, TRUE);  
                $base64encoded = base64_encode($hash);  
                $signature = "signature=" . urlencode($base64encoded);  
                $url = "http://FQDN:8096/client/api?" . $urlcommand . "&" . $signature . "&" . $response;  
                //echo $url . "<br/><br/>";  
                $responseconents = file_get_contents($url);  
                $responsejson = json_decode($responsecontents);  
           }  
      }  
      echo "<br />";  
 }  
 ?>  
 <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">  
      <div id="wrapper" style="width: 900px;margin: auto;border: 1px solid black">  
           <div id="header">  
                <h1>Welcome</h1>  
                <p>This is the CloudStack API frontend. Please use the fields below to build your VMs in mass :-)</p>  
           </div>  
           <!--<div id="content">  
                <div id="col1" style="float:left;width:448px;text-align:right">API Key: </div>  
                <div id="col2" style="float:left;width:448px;"><input type="text" name="apikey" placeholder="From CloudStack UI"><br /></div>  
           </div>  
           <div id="content">  
                <div id="col1" style="float:left;width:448px;text-align:right">API Secret Key: </div>  
                <div id="col2" style="float:left;width:448px;"><input type="text" name="secretkey" placeholder="From CloudStack UI"><br /></div>  
           </div>  
           -->  
           <div id="content">  
                <div id="col1" style="float:left;width:448px;text-align:right">Zone: </div>  
                <div id="col2" style="float:left;width:448px;">  
                <?php  
                     $hash = hash_hmac("sha1", strtolower("listZones"), $secretkey.true, TRUE);  
                     $base64encoded = base64_encode($hash);  
                     $signature = "signature=" . urlencode($base64encoded);  
                     $response = "response=json";  
                     $url = "http://FQDN:8096/client/api?command=listZones&" . $apikey . $signature . "&" . $response;  
                     $html = file_get_contents($url);  
                     $json = preg_replace('/,\s*([\]}])/m', '$1', utf8_encode($html));  
                     $json = json_decode($json);  
                     echo "<select name=\"zoneid\">";  
                     foreach ($json->listzonesresponse->zone as $zone) {  
                          echo "<option value=\"" . $zone->id . "\">" . $zone->name . "<br /></option>";  
                     }  
                     echo "</select>";  
                ?>  
                <br /></div>  
           </div>  
           <div id="content">  
                <div id="col1" style="float:left;width:448px;text-align:right">Service Offering: </div>  
                <div id="col2" style="float:left;width:448px;">  
                <?php  
                     $hash = hash_hmac("sha1", strtolower("listServiceOfferings"), $secretkey.true, TRUE);  
                     $base64encoded = base64_encode($hash);  
                     $signature = "signature=" . urlencode($base64encoded);  
                     $response = "response=json";  
                     $url = "http://FQDN:8096/client/api?command=listServiceOfferings&" . $apikey . $signature . "&" . $response;  
                     $html = file_get_contents($url);  
                     $json = preg_replace('/,\s*([\]}])/m', '$1', utf8_encode($html));  
                     $json = json_decode($json);  
                     echo "<select name=\"serviceOfferingId[]\">";  
                     foreach ($json->listserviceofferingsresponse->serviceoffering as $serviceoffering) {  
                          echo "<option value=\"" . $serviceoffering->id . "\">" . $serviceoffering->name . " (" . $serviceoffering->displaytext . ")<br /></option>";  
                     }  
                     echo "</select>";  
                ?>  
                <br /></div>  
           </div>  
           <div id="content">  
                <div id="col1" style="float:left;width:448px;text-align:right">ISO to build from: </div>  
                <div id="col2" style="float:left;width:448px;">  
                <?php  
                     $hash = hash_hmac("sha1", strtolower("listIsos&isofilter=all"), $secretkey.true, TRUE);  
                     $base64encoded = base64_encode($hash);  
                     $signature = "signature=" . urlencode($base64encoded);  
                     $response = "response=json";  
                     $url = "http://FQDN:8096/client/api?command=listIsos&isofilter=all&" . $apikey . $signature . "&" . $response;  
                     $html = file_get_contents($url);  
                     $json = preg_replace('/,\s*([\]}])/m', '$1', utf8_encode($html));  
                     $json = json_decode($json);  
                     echo "<select name=\"isoid\">";  
                     foreach ($json->listisosresponse->iso as $iso) {  
                          echo "<option value=\"" . $iso->id . "\">" . $iso->name . "<br /></option>";  
                     }  
                     echo "</select>";  
                ?>  
                <br /></div>  
           </div>  
           <div id="content">  
                <div id="col1" style="float:left;width:448px;text-align:right">Disk Offering: </div>  
                <div id="col2" style="float:left;width:448px;">  
                <?php  
                     $hash = hash_hmac("sha1", strtolower("listDiskOfferings"), $secretkey.true, TRUE);  
                     $base64encoded = base64_encode($hash);  
                     $signature = "signature=" . urlencode($base64encoded);  
                     $response = "response=json";  
                     $url = "http://FQDN:8096/client/api?command=listDiskOfferings&" . $apikey . $signature . "&" . $response;  
                     $html = file_get_contents($url);  
                     $json = preg_replace('/,\s*([\]}])/m', '$1', utf8_encode($html));  
                     $json = json_decode($json);  
                     echo "<select name=\"diskOfferingId\">";  
                     foreach ($json->listdiskofferingsresponse->diskoffering as $diskoffering) {  
                          echo "<option value=\"" . $diskoffering->id . "\">" . $diskoffering->name . " (" . $diskoffering->displaytext . ")<br /></option>";  
                     }  
                     echo "</select>";  
                ?>  
                <br /></div>  
           </div>  
           <div id="content">  
                <div id="col1" style="float:left;width:448px;text-align:right">Server Name Prefix: </div>  
                <div id="col2" style="float:left;width:448px;"><input type="text" name="prefix" placeholder="Example: RACKPOOL"><br /></div>  
           </div>  
           <div id="content">  
                <div id="col1" style="float:left;width:448px;text-align:right">Start Number: </div>  
                <div id="col2" style="float:left;width:448px;"><input type="text" name="startnum" placeholder="Example: 21"><br /></div>  
           </div>  
           <div id="content">  
                <div id="col1" style="float:left;width:448px;text-align:right">Quantity: </div>  
                <div id="col2" style="float:left;width:448px;"><input type="text" name="quantity" placeholder="Example: 5"><br /><br /></div>  
           </div>  
           <div id="footer" align="center">  
                <input type="submit" name="submit" value="Build 'em!">  
           </div>  
      </div>  
 </form>  
 </html>  

And here is what it looks like:




See? Simple, yet effective! Ping me if you have questions/comments/critiques.


No comments:

Post a Comment