natxo has asked for the wisdom of the Perl Monks concerning the following question:

Using the sdk for perl I can retrieve info about, among other things, snapshots of the virtual machines hosted on the vmware platform.

I am having trouble returning the right thing from one subroutine to the other.

sub _remove_snap { my ( $vm ) = @_; # skip if no snapshots on vm unless ( defined $vm->snapshot ) { print $vm->name . " has no snapshots, skipping\n"; return; } my $snaps = _find_snapname( $vm->snapshot->currentSnapshot, $vm->snapshot->rootSnapshotList ); print Dumper $snaps; } sub _find_snapname { my ( $ref, $tree ) = @_; my $counter = 0; my @snaps; foreach my $node (@$tree) { # print $node->name, "\t" . $node->createTime . "\n"; if ( $node->name eq $snapname ) { push @snaps, $node } _find_snapname( $ref, $node->childSnapshotList ); } print Dumper @snaps; return \@snaps; }
This code returns this:
$VAR1 = bless( { 'createTime' => '2017-12-07T21:10:30.643627Z', 'name' => '2', 'state' => bless( { 'val' => 'poweredOff' }, 'VirtualMachinePowerState' ), 'description' => '', 'id' => '42', 'snapshot' => bless( { 'type' => 'VirtualMachineSnaps +hot', 'value' => '38-snapshot-42' }, 'ManagedObjectReference' ), 'vm' => bless( { 'value' => '38', 'type' => 'VirtualMachine' }, 'ManagedObjectReference' ), 'childSnapshotList' => [ bless( { 'snapshot' => bless +( { + 'type' => 'VirtualMachineSnapshot', + 'value' => '38-snapshot-43' + }, 'ManagedObjectReference' ), 'vm' => bless( { 'v +alue' => '38', 't +ype' => 'VirtualMachine' }, ' +ManagedObjectReference' ), 'replaySupported' = +> '0', 'quiesced' => '0', 'state' => bless( { + 'val' => 'poweredOff' } +, 'VirtualMachinePowerState' ), 'name' => '3', 'createTime' => '20 +17-12-07T21:11:45.892562Z', 'id' => '43', 'description' => '' }, 'VirtualMachineSna +pshotTree' ) ], 'quiesced' => '0', 'replaySupported' => '0' }, 'VirtualMachineSnapshotTree' ); $VAR1 = [];
The first $VAR1 is from _find_snapname() and the second (empty array ref) from _remove_snap(). I don't understand why the second one is empty, I must be missing something very obvious ...

Replies are listed 'Best First'.
Re: trouble matching snapshot info vmware sdk
by trippledubs (Deacon) on Dec 12, 2017 at 20:10 UTC
    looks like _find_snapname is built recursive but incorrect, after the first call the value is not saved
    print caller(),"\n";

    to make sure those dumps are coming from the right subs.

    I would think you need something like
    push @snaps, _find_snapname($ref,$node->childSnapshotList);