Hello All,
I have been working with the perl SDK for VMware and I am at the stage of my project where I need to display snapshot tree.
My project is a web based form that have has many functions to handle using the perl SDK.
Here is my CGI script to display the VM Snapshot tree:
#!/usr/bin/perl
$ENV{PERL_LWP_SSL_VERIFY_HOSTNAME} = 0;
use strict;
use warnings;
use CGI qw(:standard);
use VMware::VIRuntime;
use VMware::VILib;
##subroutine to remove leading/trailing space
sub trim
{
my $string = shift;
$string =~ s/^\s+//;
$string =~ s/\s+$//;
return $string;
}
# My Variables for VMWare
my $query = new CGI;
my $username = "user";
my $password = "thepass";
my $url = "https://vcenter/sdk/webServer";
my $VMNameField= $query->param('vmName');
my @snaplist;
my %snapOrder;
my $num;
my $remaining;
my $vmSnapshotResults = `/var/www/dev2/lib/vmware-vcli/apps/vm/snapshotmanager.pl --url=$url --username=$username --password=$password --vmname=$VMNameField --operation=list`;
print $query->header;
while ($vmSnapshotResults =~ /(.+)(\d{4}-\d{2}-\d{2}T\d{2}:\d{2})/g) {
push(@snaplist, trim($1));
$snapOrder{$1} = trim($2);
}
if (@snaplist) {
$num = scalar(grep {defined $_} @snaplist);
$remaining = 2-$num;
}
else {
$num = 0;
$remaining = 2;
}
print '<div id="snapshotTree">';
print '<div id="treeName">';
print '<table id="vmNameTable">';
print '<tr>';
print '<td id='.$VMNameField.'><b>'.$VMNameField.'</b></td>';
print '</tr>';
print '<table id="snaptable">';
foreach my $tmp (sort {$snapOrder{$a} cmp $snapOrder{$b}} keys %snapOrder) {
print '<tr>';
print '<td id='.trim($tmp).'>'.trim($tmp).'</td>';
print '</tr>';
}
print '</table>';
print '<table id="snapButtons>';
print '<tr>';
if ($num == 0) {
print '<td id="deleteVM"><img alt="" src="./images/DeleteSnapshot_disabled.png"></img></td>';
print '</tr>';
print '<tr>';
print '<td id="resetVM"><img alt="" src="./images/RevertSnapshot_disabled.png"></img></td>';
}
else {
print '<td id="deleteVM"><a href=\'Javascript:deleteSnap("'.$VMNameField.'")\'><img alt="" src="./images/DeleteSnapshot.png"></img></a></td>';
print '</tr>';
print '<tr>';
print '<td id="resetVM"><a href=\'Javascript:revertSnap("'.$VMNameField.'")\'><img alt="" src="./images/RevertSnapshot.png"></img></a></td>';
}
print '</tr>';
print '</table>';
print '</div>';
print '<div id="SnapQuota">';
print 'Your Snapshot Quota is <b>2</b><br>';
print 'Your current snapshot count is <b>'.$num.'</b></br>';
print 'You are allowed <b>'.$remaining.'</b> more snapshot(s).<br>';
if ($num < 2) {
print 'Name: <input type="text" id="newSnap"><br>';
print '<a href=\'Javascript:createSnap("'.$VMNameField.'")\'><img alt="" src="./images/CreateSnapshot.png"></img></a>';
print '</div>';
}
else {
print 'Name: <input type="text" id="newSnap" disabled="disabled"><br>';
print '<img alt="" src="./images/CreateSnapshot_disabled.png"></img>';
print '</div>';
}
print '</div>';
This successfully displayed the VMName and the snapshots listed in order however I would like to implement a 'You are Here' section in my tree.
I have searched this forum as well as google but have been unsuccessful in finding an example or guide that would help me find the code necessary to create an accurate 'You are here' on my page.
Any assistance would be helpful.
Thanks,