简体   繁体   中英

Using vSphere VI Java API to get annotation notes

Anyone have a snippet of how to use the vSphere VI Java API to extract the values in the 'Annotations' section of the 'Summary' tab for a VM? I have looked around a lot as well as combed through the API docs but haven't seen it anywhere.

Have a look at the API Documentation of VirtualMachineConfigSpec first .

Then you can do this from java

VirtualMachineConfigSpec configSpec = new VirtualMachineConfigSpec();
configSpec.setAnnotation = "Your annotation string here" 
reconfigVM_Task(vmMOR, configSpec);

I did this in Perl SDK, but re-doing it to use Java API should be easy. You get the annotations from Virtual Machine object which has property named summary of class VirtualMachineSummary which has config of class VirtualMachineConfigSummary which has a annotation field of type string which is what you need.

my $vmname = "vmname you are looking for";
# Get all VMs
my $vms = Vim::find_entity_views(
        view_type => 'VirtualMachine',
        filter => {"config.name" => qr/^$vmname$/i},
);

# Iterate over the VMs, getting their annotations
foreach my $vm (@{ $vms }) {
  my $notes = $vm->summary->config->annotation;
  my $name = $vm->summary->config->name;
  if (not defined $notes) {
    print " - VM: $name  has no notes\n";
  }
  elsif ($notes =~ m/^\$*/) {
    print " - VM: $name  has empty notes\n";
  }
  else {
    print " - VM: $name  notes: '$notes'\n";
  }
}

Here is the full code: https://communities.vmware.com/message/2613855#2613855

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM