Thursday, April 25, 2013

Round 3 of CloudStack API fun

http://kirkjantzer.blogspot.com/2013/04/round-3-of-cloudstack-api-fun.html

Thursday, April 25, 2013 11:00 AMRound 3 of CloudStack API funThe blog of Kirk JantzerKirk
Sorry for the short post, but did a quick, but I think significant update to my API page for my CloudStck install. See my previous posts/iterations of the page here and here.

It came to me last night that someone using this page may not want to create a bulk of servers with a sequential naming scheme. So, I made some more modifications to the page, and voila! Now you can build "WEB1-100", or paste in a list of server names, "WEB1,WEB2,APP1,APP2,MSG1,MSG2,etc..."



And, for the nerds, the code:


 <html>  
<head>
<title>CloudStack API</title>
</head>
<?php
$apikey = "apikey=**************************************************";
$secretkey = "**************************************************";
if(isset($_POST['submit']))
{
function buildservers($servernames,$count)
{
echo "You have submitted for the following servers to be built: <br />";
for ($i=0; $i<$count; $i++)
{
$zoneId = "zoneId=" . $_POST['zoneid'];
$serviceOfferingId = "serviceOfferingId=" . $_POST['serviceOfferingId'];
$diskOfferingId = "diskOfferingId=" . $_POST['diskOfferingId'];
$templateId = "templateId=" . $_POST['isoid'];
$server = trim($servernames[$i]);
echo $server . "<br />";
$hypervisor = "hypervisor=XenServer";
$response = "response=json";
$urlcommand = "command=deployVirtualMachine&" . $zoneId . "&" . $diskOfferingId . "&displayname=" . $server . "&" . $hypervisor . "&name=" . $server . "&" . $serviceOfferingId . "&" . $templateId . "&" . $apikey;
$hashcommand = "apikey=" . $apikey . "&command=deployVirtualMachine&zoneId=" . $zoneId . "&" . $diskOfferingId . "&displayname=" . $server . "&" . $hypervisor . "&name=" . $server . "&" . $serviceOfferingId . "&" . $templateId;
$hash = hash_hmac("sha1", strtolower($hashcommand), $secretkey.true, TRUE);
$base64encoded = base64_encode($hash);
$signature = "signature=" . urlencode($base64encoded);
$url = "http://master:8096/client/api?" . $urlcommand . "&" . $signature . "&" . $response;
echo $url . "<br/><br/>";
$responseconents = file_get_contents($url);
$responsejson = json_decode($responsecontents);
}
}
if ($_POST['servernameradio'] == "sequence")
{
if (empty($_POST['prefix']) || empty($_POST['startnum']) || empty($_POST['quantity']))
{
echo "<b>You missed a field!</b><br />";
}
else
{
$prefix = $_POST['prefix'];
$endnum = ($_POST['startnum'] + $_POST['quantity']) - 1;
$startnum = $_POST['startnum'];
$servernames = array();
for ($i=$startnum; $i<=$endnum; $i++)
{
$servernames[] = $prefix . $i;
$count=count($servernames);
}
buildservers($servernames,$count);
}
}
elseif ($_POST['servernameradio'] == "customlist")
{ if (empty($_POST['serverlist']))
{
echo "<b>You forgot to input servers!</b><br />";
}
else
{
foreach(explode("\n", $_POST['serverlist']) as $servername) {
$servernames[] = $servername;
$count=count($servernames);
}
buildservers($servernames,$count);
}
}
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"><b>Select the build options:</b>&nbsp;&nbsp;</div>
<div id="col2" style="float:left;width:448px;">&nbsp;</div>
</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"><b>Zone:</b>&nbsp;&nbsp;</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://master: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"><b>Service Offering:</b>&nbsp;&nbsp;</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://master: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"><b>ISO to build from:</b>&nbsp;&nbsp;</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://master: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"><b>Disk Offering:</b>&nbsp;&nbsp;</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://master: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 /><br /></div>
</div>
<div id="content">
<div id="col1" style="float:left;width:448px;text-align:right"><b>Build a sequential set of servers:</b>&nbsp;&nbsp;</div>
<div id="col2" style="float:left;width:448px;"><input type="radio" name="servernameradio" value="sequence">&nbsp;</div>
</div>
<div id="content">
<div id="col1" style="float:left;width:448px;text-align:right"><b>Server Name Prefix:</b>&nbsp;&nbsp;</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"><b>Start Number:</b>&nbsp;&nbsp;</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"><b>Quantity:</b>&nbsp;&nbsp;</div>
<div id="col2" style="float:left;width:448px;"><input type="text" name="quantity" placeholder="Example: 5"><br /><br /></div>
</div>
<div id="content">
<div id="col1" style="float:left;width:448px;text-align:right"><b>Or, paste in a list of servers to build:</b>&nbsp;&nbsp;</div>
<div id="col2" style="float:left;width:448px;"><input type="radio" name="servernameradio" value="customlist">&nbsp;</div>
</div>
<div id="content">
<div id="col1" style="float:left;width:448px;text-align:right"><b>List:</b>&nbsp;&nbsp;</div>
<div id="col2" style="float:left;width:448px;"><textarea rows="10" cols="30" name="serverlist" placeholder="Each server on a new line
Example:
RACK1POOLONE1
RACK1POOLONE2
RACK1POOLTWO1
RACK1POOLTWO2"></textarea><br /><br /></div>
</div>
<div id="footer" align="center">
<input type="submit" name="submit" value="Build 'em!">
</div>
</div>
</form>
</html>





No comments:

Post a Comment