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