reubenur-rahman / vmware-pyvmomi-examples

The example codes are written using the VMware official python library pyvmomi
Apache License 2.0
89 stars 60 forks source link

Remove snapshot does not work #3

Open zch-bit opened 8 years ago

zch-bit commented 8 years ago

in the example of create_and_remove_snapshot, the code for removing the snapshot does not work, for the code should go deep into the ChildShnapshotTree to see if the name equals the ChildSnapshot.

erancx commented 8 years ago

In order to get a ChildSnapshot, I suggest you try something like:

def list_snapshot(vm, vm_name):
    import collections
    try:
        snapshots = vm.snapshot.rootSnapshotList
    except AttributeError as e:
        mylogger.error("No snapshots found for {0}".format(vm_name))
        sys.exit(1)
    d = collections.deque(snapshots)
    all_snapshots = []
    while d:
        snapshot = d.pop()
        all_snapshots.append(snapshot)
        d.extend(reversed(snapshot.childSnapshotList))
    return all_snapshots

Now you can every child too, for example:

all_snapshots = list_snapshot(vm, args.vm_name)
for snapshot in all_snapshots:
    print snapshot.name, snapshot.createTime
erancx commented 8 years ago

Or better, in order to get first child first, last child last, use snapshot.childSnapshotList[::-1]