VMware {code} Community
hesco
Contributor
Contributor

How do I extract custom data from a Managed Object?

I'm working with the vSphere SDK for perl on a project related to automating the recyling of a virtual environment.

using Perl's Data::Dumper I have dumped the data structure for my connection in an attempt to better understand what I'm dealing with.

I found a key named 'availableField' whose value is an array reference to a number of blessed 'CustomFieldDef' objects (hash refernces), one of which has as its value for the 'name' key the specific custom field I'm searching for.  But the value for this custom field does not seem to be available.  What am I missing here?  How do I access this data, please?

Also, as I am on my search for this data, I'm wondering if there might be a far more elegant way to access it than this snippet I am currently using:

$vm->[0]->get_property('availableField')->[14]->{'name'};

Any guidance would be appreciated.

Thanks,

-- Hugh Esco

Tags (3)
0 Kudos
1 Reply
hesco
Contributor
Contributor

This seems to be doing the trick for me now:

sub extract_custom_data_by_name {
  my $self = shift;
  my $vm = shift;
  my $custom_field_name = shift;
  my $custom_field_value;

  foreach my $field ( @{$vm->get_property('availableField')} ){
    if( $custom_field_name eq $field->{'name'} ){
      my $key = $field->{'key'};
      foreach my $value ( @{$vm->get_property('customValue')} ){
        if( $value->{'key'} eq $key ){
          $custom_field_value = $value->{'value'};
        }
      }
    }
  }

  return $custom_field_value;
}

I guess that is a never mind.  Thanks anyway folks.   Perhaps this might prove useful for the next person.  Though I still search for a more elegant way to address some of these data structures, if such an animal exists.

-- Hugh Esco

0 Kudos