VMware {code} Community
ChristopherByrd
Contributor
Contributor
Jump to solution

Setting VM annotation/notes via SDK.

I'm interested in setting the annotation/notes property of a VM programatically via the Java/vSphere Web service SDK. It seems that the annotation field is available in the following types:

  • VirtualMachineConfigSummary

  • VirtualMachineConfigInfo

  • VirtualMachineConfigSpec

So I guess I have a two part question. One, which of the above types should I operate on in order to set the "Notes" field and secondly, what is the most direct way to accomplish this via Java and the SDK?

Reply
0 Kudos
1 Solution

Accepted Solutions
admin
Immortal
Immortal
Jump to solution

Hi,

In order to change/set the VM annotation property, you can use VirtualMachineConfigSpec. Now the most direct way to accomplish the same is invoking ReconfigVM_task API (if VM exist) or CreateVM_Task API (if creating a new VM) passing the VirtualMachineConfigSpec and in VirtualMachineConfigSpec, set the annotation property. This way you can set the notes via vSphere SDK. The basic steps to be performed:

VirtualMachineConfigSpec vmConfigSpec = new VirtualMachineConfigSpec();

vmConfigSpec.setAnnotation = "" // desired value

reconfigVM_Task(vmMOR, vmConfigSpec); // invoke the API

Hope this is helpful.

View solution in original post

Reply
0 Kudos
3 Replies
admin
Immortal
Immortal
Jump to solution

Hi,

In order to change/set the VM annotation property, you can use VirtualMachineConfigSpec. Now the most direct way to accomplish the same is invoking ReconfigVM_task API (if VM exist) or CreateVM_Task API (if creating a new VM) passing the VirtualMachineConfigSpec and in VirtualMachineConfigSpec, set the annotation property. This way you can set the notes via vSphere SDK. The basic steps to be performed:

VirtualMachineConfigSpec vmConfigSpec = new VirtualMachineConfigSpec();

vmConfigSpec.setAnnotation = "" // desired value

reconfigVM_Task(vmMOR, vmConfigSpec); // invoke the API

Hope this is helpful.

Reply
0 Kudos
lamw
Community Manager
Community Manager
Jump to solution

Along with what Angela stated, here is a sample vSphere SDK for Perl script showing the methods that can be used (an equivalent is available in VI Java) :

=========================================================================

William Lam

VMware vExpert 2009

VMware ESX/ESXi scripts and resources at:

Twitter: @lamw

VMware Code Central - Scripts/Sample code for Developers and Administrators

VMware Developer Comuunity

If you find this information useful, please award points for "correct" or "helpful".

pikmaster
Contributor
Contributor
Jump to solution

I found this page, when trying to find a piece of code to read the annotations. So in case somebody wants the same, here is the code:

use strict;

use warnings;

use VMware::VIRuntime;

use VMware::VIM2Runtime;

use VMware::VILib;

# $Util::script_version = "1.0";

Opts::parse();

Opts::validate();

Util::connect();

use Data::Dumper;

my $vmname = "default_vmname";

if ( $ARGV[0] ) {

  $vmname = $ARGV[0];

  print "  passed vmname from CLI=$vmname\n";

}

# 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/^\s*$/) {

    print " - VM: $name  has empty notes\n";

  }

  else {

    print " - VM: $name  notes: '$notes'\n";

  }

}

print "No VMs matching the name '$vmname' found\n" if (0 == scalar (@{ $vms }));

Util::disconnect();

Reply
0 Kudos