| 1 | package VMOMI::ManagedObject; | ||
| 2 | use parent 'VMOMI::ComplexType'; | ||
| 3 | |||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | |||
| 7 | use Scalar::Util qw(weaken); | ||
| 8 | |||
| 9 | our @class_members = ( ); | ||
| 10 | our @class_ancestors = ( ); | ||
| 11 | |||
| 12 | sub AUTOLOAD { | ||
| 13 | my $self = shift; | ||
| 14 | my $name = our $AUTOLOAD; | ||
| 15 | |||
| 16 | return if $name =~ /::DESTROY$/; | ||
| 17 | $name =~ s/.*://; | ||
| 18 | |||
| 19 | my $class = ref($self); | ||
| 20 | my $type = $class; | ||
| 21 | $type =~ s/.*:://; | ||
| 22 | my ($info) = grep { $_->[0] eq $name } $class->get_class_members; | ||
| 23 | |||
| 24 | # TODO: Persist properties to reduce API calls, check for previously fetched properties first | ||
| 25 | if (defined $info) { | ||
| 26 | my $options = new VMOMI::RetrieveOptions(maxObjects => 1); | ||
| 27 | my $pcoll = new VMOMI::ManagedObjectReference( | ||
| 28 | type => 'PropertyCollector', | ||
| 29 | value => 'propertyCollector' | ||
| 30 | ); | ||
| 31 | |||
| 32 | my $spec = new VMOMI::PropertyFilterSpec(reportMissingObjectsInResult => 0); | ||
| 33 | my $pSet = [ new VMOMI::PropertySpec( | ||
| 34 | all => 0, | ||
| 35 | type => $self->moref->type, | ||
| 36 | pathSet => [ $name ], | ||
| 37 | )]; | ||
| 38 | my $oSet = [ new VMOMI::ObjectSpec(obj => $self->moref, skip => 0) ]; | ||
| 39 | $spec->objectSet($oSet); | ||
| 40 | $spec->propSet($pSet); | ||
| 41 | |||
| 42 | my $result = $self->stub->RetrievePropertiesEx( | ||
| 43 | _this => $pcoll, | ||
| 44 | specSet => [$spec], | ||
| 45 | options => $options | ||
| 46 | ); | ||
| 47 | |||
| 48 | my $value = undef; | ||
| 49 | # Ignoring token, *shouldn't require more than one iteration* | ||
| 50 | # Also adding some hacked detection for permission errors when retrieving properties | ||
| 51 | # before a login. | ||
| 52 | for my $object (@{$result->objects}) { | ||
| 53 | if (defined $object->missingSet) { | ||
| 54 | my $fault = $object->missingSet->[0]->fault->fault; | ||
| 55 | my $fault_type = ref $fault; | ||
| 56 | $fault_type =~ s/.*:://; | ||
| 57 | Exception::SoapFault->throw( | ||
| 58 | message => "fault: $fault_type", | ||
| 59 | ); | ||
| 60 | } | ||
| 61 | for my $property (@{$object->propSet}) { | ||
| 62 | if ($property->name eq $name) { | ||
| 63 | $self->{$name} = $property->val; | ||
| 64 | return $property->val; | ||
| 65 | } | ||
| 66 | } | ||
| 67 | } | ||
| 68 | return $value; | ||
| 69 | } | ||
| 70 | |||
| 71 | # Just set or retrieve if the value is defined, should *pass-through* non SDK properties | ||
| 72 | if (exists $self->{$name}) { | ||
| 73 | $self->{$name} = shift if @_; | ||
| 74 | return $self->{$name}; | ||
| 75 | } | ||
| 76 | |||
| 77 | # Try a method invocation against the API otherwise | ||
| 78 | my %args = @_; | ||
| 79 | $args{'_this'} = $self->{'moref'}; | ||
| 80 | my $method = $self->stub->can($name); | ||
| 81 | unless ($method) { | ||
| 82 | Exception::Autoload->throw(message => "Unknown method '$name' in " . ref($self)); | ||
| 83 | } | ||
| 84 | return $self->stub->$method(%args); | ||
| 85 | } | ||
| 86 | |||
| 87 | sub new { | ||
| 88 | my ($class, $stub, $moref, %args) = @_; | ||
| 89 | my $self = $class->SUPER::new(%args); | ||
| 90 | |||
| 91 | if (ref($stub) ne 'VMOMI::SoapStub') { | ||
| 92 | die "Parameter (0) to class '$class' constructor must be VMOMI::SoapStub: "; | ||
| 93 | } | ||
| 94 | if (ref($moref) ne 'VMOMI::ManagedObjectReference') { | ||
| 95 | die "Parameter (1) to class '$class' constructor must be VMOMI::ManagedObjectReference"; | ||
| 96 | } | ||
| 97 | $self->{'stub'} = $stub; | ||
| 98 | $self->{'moref'} = $moref; | ||
| 99 | |||
| 100 | weaken $self->{'stub'}; | ||
| 101 | return bless $self, $class; | ||
| 102 | } | ||
| 103 | |||
| 104 | sub get_class_ancestors { | ||
| 105 | return @class_ancestors; | ||
| 106 | } | ||
| 107 | |||
| 108 | sub get_class_members { | ||
| 109 | my $class = shift; | ||
| 110 | my @super_members = $class->SUPER::get_class_members(); | ||
| 111 | return (@super_members, @class_members); | ||
| 112 | } | ||
| 113 | |||
| 114 | 1; |