Skip to content

vmware/vsphere-automation-sdk-java

Repository files navigation

VMware vSphere Automation SDK for Java

Table of Contents

Abstract

This document describes the vSphere Automation Java SDK samples that use the vSphere Automation java client library. Additionally, some of the samples demonstrate the combined use of the vSphere Automation and vSphere Web Service APIs. The samples have been developed to work with JDK 1.8+

Supported OnPrem vCenter Releases:

vCenter 7.0, 7.0U1, 7.0U2, 7.0U3, 8.0, 8.0U1 and 8.0U2

Please refer to the notes in each sample for detailed compatibility information.

Supported NSX-T Releases

NSX-T 2.2, 2.3, 3.0, 4.0.0.0, 4.0.1

Latest VMware Cloud on AWS Release:

VMC M24 (1.24) (Release Notes)

Quick Start Guide

This document will walk you through getting up and running with the Java SDK Samples. Prior to running the samples you will need to setup a vCenter test environment and install maven, the following steps will take you through this process. Before you can run the SDK samples we'll need to walk you through the following steps:

  1. Setting up maven
  2. Setting up a vSphere test environment

Setting up maven

The SDK requires maven to build the samples.

  1. Download the latest maven from https://maven.apache.org/download.cgi and extract it to your machine.
  2. Install JDK 8 and set JAVA_HOME to the directory where JDK is installed.
    export JAVA_HOME=<jdk-install-dir>
  3. Update PATH environment variable to include the maven and jdk "bin" directories.
    export PATH=<maven-bin-dir>:$JAVA_HOME/bin:$PATH

Setting up a vSphere Test Environment

NOTE: The samples are intended to be run against a freshly installed non-Production vSphere setup as the scripts may make changes to the test environment and in some cases can destroy items when needed.

To run the samples a vSphere test environment is required with the following minimum configuration

  • 1 vCenter Server
  • 2 ESX hosts
  • 1 NFS Datastore with at least 3GB of free capacity

Apart from the above, each individual sample may require additional setup. Please refer to the sample parameters for more information on that.

Building the Samples

In the root directory of your folder after cloning the repository, run the below maven commands -

mvn initialize

mvn clean install

Running the Samples

When running the samples, parameters can be provided either on the command line, in a configuration file (using the --config-file parameter), or a combination of both. The parameter values specified on the command line will override those specified in the configuration file. When using a configuration file, each required parameter for the sample must be specified either in the configuration file or as a command line parameter. Each parameter specified in the configuration file should be in the "key=value" format. For example:

vmname=TestVM

cluster=Cluster1

Note: Please specify the fully qualified “hostname” of the server for running the samples to avoid hostname verification errors.

Use a command like the following to display usage information for a particular sample.

$java -ea -cp target/vsphere-samples-8.0.2.1.jar vmware.samples.vcenter.vm.list.ListVMs

java -cp target/vsphere-samples-8.0.2.1.jar vmware.samples.vcenter.vm.list.ListVMs [--config-file <CONFIGURATION FILE>]
       --server <SERVER> --username <USERNAME> --password <PASSWORD> --cluster <CLUSTER> [--truststorepath <ABSOLUTE PATH OF JAVA TRUSTSTORE FILE>]
       [--truststorepassword <JAVA TRUSTSTORE PASSWORD>] [--cleardata] [--skip-server-verification]

Sample Options:
    --config-file <CONFIGURATION FILE>                         OPTIONAL: Absolute path to  the configuration file containing the sample options.
                                                               NOTE: Parameters can be specified either in the configuration file or on the command
                                                               line. Command line parameters will override values specified in the configuration file.
    --server <SERVER>                                          hostname of vCenter Server
    --username <USERNAME>                                      username to login to the vCenter Server
    --password <PASSWORD>                                      password to login to the vCenter Server
    --truststorepath <ABSOLUTE PATH OF JAVA TRUSTSTORE FILE>   Specify the absolute path to the file containing the trusted server certificates. This
                                                               option can be skipped if the parameter skip-server-verification is specified.
    --truststorepassword <JAVA TRUSTSTORE PASSWORD>            Specify the password for the java truststore. This option can be skipped if the
                                                               parameter skip-server-verification is specified.
    --cleardata                                                OPTIONAL: Specify this option to undo all persistent results of running the sample.
    --skip-server-verification                                 OPTIONAL: Specify this option if you do not want to perform SSL certificate
                                                               verification.
                                                               NOTE: Circumventing SSL trust in this manner is unsafe and should not be used with
                                                               production code. This is ONLY FOR THE PURPOSE OF DEVELOPMENT ENVIRONMENT.

Use a command like the following to run a sample using only command line parameters:

$java -ea -cp target/vsphere-samples-8.0.2.1.jar vmware.samples.vcenter.vm.list.ListVMs --server servername --username [email protected] --password password --skip-server-verification

Use a command like the following to run a sample using only a configuration file:

$java -ea -cp target/vsphere-samples-8.0.2.1.jar vmware.samples.vcenter.vm.list.ListVMs --config-file sample.properties

Use the following command to run the sample using a combination of configuration file and command line parameters:

$java -ea -cp target/vsphere-samples-8.0.2.1.jar vmware.samples.vcenter.vm.list.ListVMs --config-file sample.properties --server servername

Connect to VMware Cloud on AWS

using csp api_token (also called refresh_token)

import com.vmware.vmc.Orgs;
import com.vmware.vmc.model.Organization;
import com.vmware.vapi.vmc.client.VmcClients;

ApiClient apiClient = VmcClients.createDefault("<refresh_token>");
Orgs orgsStub = apiClient.createStub(Orgs.class);
Organization orgDetails = orgsStub.get("<org_id>");
System.out.println(orgDetails);

Output in console:

Organization (com.vmware.vmc.model.organization) => {
    updated = 8/18/22 20:41:34 +0000,
    userId = xxxx-xxxx-xxxx,
    updatedByUserId = xxxx-xxxx-xxxx,
    created = 10/3/17 22:19:31 +0000,
    version = 4,
    updatedByUserName = xxxx-xxxx-xxxx,
    userName = xxxx-xxxx-xxxx,
    id = xxxx-xxxx-xxxx,
    orgType = INTERNAL_NON_CORE,
    displayName = xxxx/xxxx/xxxx,
    name = xxxx,
    projectState = CREATED,
    properties = OrgProperties (com.vmware.vmc.model.org_properties) => {
        values = {...}
    }
    [dynamic fields]: {
        org_seller_info = <unset>
    }
}

using client_credentials (Server to Server OAuth app's id and secret)

import com.vmware.vmc.Orgs;
import com.vmware.vmc.model.Organization;
import com.vmware.vapi.vmc.client.VmcClients;

ApiClient apiClient = VmcClients.createDefault("<client_id>", "<client_secret>", "<org_id>");
orgsStub = apiClient.createStub(Orgs.class);
Organization orgDetails = orgsStub.get("<org_id>");
System.out.println(orgDetails);

Output in console:

Organization (com.vmware.vmc.model.organization) => {
    updated = 8/18/22 20:41:34 +0000,
    userId = xxxx-xxxx-xxxx,
    updatedByUserId = xxxx-xxxx-xxxx,
    created = 10/3/17 22:19:31 +0000,
    version = 4,
    updatedByUserName = xxxx-xxxx-xxxx,
    userName = xxxx-xxxx-xxxx,
    id = xxxx-xxxx-xxxx,
    orgType = INTERNAL_NON_CORE,
    displayName = xxxx/xxxx/xxxx,
    name = xxxx,
    projectState = CREATED,
    properties = OrgProperties (com.vmware.vmc.model.org_properties) => {
        values = {...}
    }
    [dynamic fields]: {
        org_seller_info = <unset>
    }
}

Importing the samples to eclipse

To generate the eclipse project files for the samples run the below command

mvn eclipse:clean eclipse:eclipse

Once generated, follow below steps to import the project to eclipse:

  1. Go to File -> Import.
  2. Select Existing Projects into Workspace.
  3. Select the root directory as the directory where the samples are located.
  4. Click Finish

Adding a new sample

Once the eclipse project is imported, follow below steps to add a new sample using the sample template:

  1. Right click on the project and select New -> Package. Specify a package name in the wizard and click Finish.
  2. Right click on the newly created package and click Import -> General -> File System. In the dialog box, click "Browse" and select the "sample-template" folder in the root directory.
  3. Select the "SampleClass" in the dialog box and click finish.

This will import a basic sample class to your package, which you can then customize according to your needs.

API Documentation

vSphere API Documentation

VMware Cloud on AWS API Documentation

NSX API Documentation

Support

For official support, users with Active - Premium SDK Support, Standard SDK Support, Premier/Success 360, and VMware Select Support can reach out to VMware Global Support for any issues in line with our SDK and API Support policy. For community support, please open a Github issue or start a Discussion.

Submitting samples

Developer Certificate of Origin

Before you start working with this project, please read our Developer Certificate of Origin. All contributions to this repository must be signed as described on that page. Your signature certifies that you wrote the patch or have the right to pass it on as an open-source patch.

Required Information

The following information must be included in the README.md for the sample.

  • Author Name
    • This can include full name, email address or other identifiable piece of information that would allow interested parties to contact author with questions.
  • Date
    • Date the sample was originally written
  • Minimal/High Level Description
    • What does the sample do ?
  • Any KNOWN limitations or dependencies

Versioning

vSphere Automation Java SDK - X.Y.Z.U.P

vCenter-related: X.Y - (Major release), Z - (Update release), U - (Patch)

vSphere Automation Java SDK - related: P - (vSphere Automation Java SDK Maintenance patches)

Suggested Information

The following information should be included when possible. Inclusion of information provides valuable information to consumers of the resource.

  • vSphere version against which the sample was developed/tested
  • SDK version against which the sample was developed/tested
  • Java version against which the sample was developed/tested

Contribution Process

  • Follow the GitHub process
    • Please use one branch per sample or change-set
    • Please use one commit and pull request per sample
    • Please post the sample output along with the pull request
    • If you include a license with your sample, use the project license

Code Style

Please conform to oracle java coding standards. http://www.oracle.com/technetwork/articles/javase/codeconvtoc-136057.html

Resource Maintenance

Maintenance Ownership

Ownership of any and all submitted samples are maintained by the submitter.

Filing Issues

Any bugs or other issues should be filed within GitHub by way of the repository’s Issue Tracker.

Resolving Issues

Any community member can resolve issues within the repository, however only the board member can approve the update. Once approved, assuming the resolution involves a pull request, only a board member will be able to merge and close the request.

VMware Sample Exchange

It is highly recommended to add any and all submitted samples to the VMware Sample Exchange: https://code.vmware.com/samples

Sample Exchange can be allowed to access your GitHub resources, by way of a linking process, where they can be indexed and searched by the community. There are VMware social media accounts which will advertise resources posted to the site and there's no additional accounts needed, as the VMware Sample Exchange uses MyVMware credentials.

Repository Administrator Resources

Board Members

Board members are volunteers from the SDK community and VMware staff members, board members are not held responsible for any issues which may occur from running of samples from this repository.

Members:

  • Ankit Agrawal (VMware)
  • Martin Cvetanov (VMware)
  • Shweta Purohit (VMware)
  • Jobin George (VMware)
  • Kunal Singh (VMware)

Approval of Additions

Items added to the repository, including items from the Board members, require 2 votes from the board members before being added to the repository. The approving members will have ideally downloaded and tested the item. When two “Approved for Merge” comments are added from board members, the pull can then be committed to the repository.

VMware Resources