Halukkocaman
Enthusiast
Enthusiast

Revert to Snapshot

Hi All,

I'm looking in to "Revert to Snapshot" workflow and looks like you have to select the snapshot form the list. Which has a input of VC:VitualMachineSnapshot as you can see from the picture. I was wondering is there a way to make this input as a string. Reason I'm asking is that I will put this workflow in to ServiceNow and I want my users to be able to just right the Server name and snapshot name to be able to revert it back.

pastedImage_0.png

Thank you,

Haluk

0 Kudos
4 Replies
iiliev
VMware Employee
VMware Employee

Hi Haluk,

The problem is that the combination of server name and snapshot name is not an unique identity for a snapshot.

Consider the following case - a single vCenter named vc1 with thousands of VMs, and each one of VMs having a snapshot with name snap1. Obviously, using only the strings vc1 and snap1, you don't have enough information to uniquely identify the snapshot to revert.

0 Kudos
Halukkocaman
Enthusiast
Enthusiast

How about adding a VM name to list? That would be unique.

0 Kudos
iiliev
VMware Employee
VMware Employee

No, VM name is not unique. You can have many VMs with same name in different VM folders.

0 Kudos
pdjorg
Contributor
Contributor

Ilian Iliev has already mentioned why what you are suggesting can be problematic; it is technically possible to have more than one VM in inventory with the same name. I don't remember if it's a requirement that snapshot names be unique within given VM, but I wouldn't be surprised to find out that it is not.

If you feel comfortable that your organization's policies guarantee unique names, then what you are asking can of course be done. You probably would want to build some exception handling logic into this workflow so that if a search for a VM or snapshot by name results in more than one of each being returned, it returns some kind of friendly error message to the user and doesn't take any additional action. They can then bring it to your attention.

Maybe something like this? This code will accept a VM name as a string and a snapshot name as a string, and then search inventory for them. As long as there is exactly 1 VM with that name and that VM has exactly 1 snapshot with that name, it will return the VC:VirtualMachineSnapshot object, which you could then pass into any other workflow you like that accepts that as an input.

var vmName = 'MyVM';

var snapName = 'MyVM_snapshot';

var xpath = "xpath:name='" + vmName + "'";

var vms = VcPlugin.getAllVirtualMachines(null, xpath);

if(vms.length === 0) { throw "No VMs found with that name."; }

else if (vms.length > 1) { throw "More than one VM found with that name."; }

var vm = vms[0];

System.log("Found VM named '" + vm.name + "'");

var allSnapsOfVm = System.getModule("com.vmware.library.vc.vm.snapshot").getAllSnapshotsOfVM(vm);

if (allSnapsOfVm.length === 0) { throw "VM '" + vm.name + "' does not have any snapshots outstanding."; }

var countOfSnapsWithSameName = 0;

var snapIndexToReturn;

for (var k in allSnapsOfVm) {

  if (allSnapsOfVm[k].name === snapName) {

  countOfSnapsWithSameName++;

  snapIndexToReturn = k;

  }

}

if (countOfSnapsWithSameName > 1) {

  throw "More than one snapshot with that name found on VM '" + vm.name + "'";

}

else if (countOfSnapsWithSameName === 0) {

  throw "No snapshots with name '" + snapName + "' found on VM '" + vm.name + "'";

}

else {

  return allSnapsOfVm[snapIndexToReturn];

}

0 Kudos