Home > Blogs > VMware {code} > Tag Archives: java

Tag Archives: java

Getting Started with the vSphere Automation SDK for Java

There has been another exciting open-sourced release in the vSphere Automation SDK lineup. This time, it is the vSphere Automation SDK for Java!

That means it’s time for another blog post in our series on ‘Getting Started with the vSphere Automation SDKs’. The posts available in the series so far are as follows:

We’ll be taking a look at how to easily get started with this SDK on a Mac. Before diving in, there are a couple prerequisites we’ll need to download and install.

Prerequisites

The first thing we’ll want to do before getting started with this SDK is to clone the repository to the local system. We can do that with the following command:

Example: cloning repo local

Next, we’ll want to make sure we have the appropriate Java Development Kit (JDK) installed and available. The vSphere Automation SDK for Java has been developed to work with JDK 1.8. We can verify which version of Java is available by the following command:

Example: java version output

In this situation, my system doesn’t have Java installed at all. I’ll need to head out to the JDK8 download page to download and install the appropriate version. Once complete, repeat the last command to ensure JDK 1.8 is installed. Verifying and/or updating the JAVA_HOME and PATH variables is the last step to the install process. We’ll start by finding the current value for the JAVA_HOME variable, then updating it, if necessary, to the Home directory of the newly installed JDK. We’ll then repeat that process for the PATH variable as well. This can be done with the following commands:

Example of updating system variables

We will also need to ensure Maven, a build automation tool, is installed. We will use Maven to build out the SDK and the samples from source so we can start using it. My system doesn’t have Maven installed yet, so we’ll go to the download page and install it according to the instructions on Maven’s site. Alternatively, we can do it with the following commands:

Example of installing Maven

The last prerequisite, before starting to user the SDK, is instructing Maven to build out all the samples with Maven. We’ll first use the initialize command, within the cloned repository directory, to pre-stage any dependencies that are not available publicly. We’ll then follow that with install command. This can be done with the following commands:

Example of downloading SDK prereqs and building out SDK from source

We are now all setup to start using the vSphere Automation SDK for Java!

Running Samples

There are quite a few samples which are included with the SDK. Each sample can be run by using its fully qualified class name.

The first example we’ll take a look at is the ListVMs sample. In terminal, from the root of the SDK directory, we’ll call java, enabling assertions with the ‘ea’ parameter, using the ‘cp’ parameter to call the class path of ‘target/vsphere-samples-6.5.0.jar’, then the fully qualified class name. An example of this command is as follows:

Example parameters available for use with the ListVMs sample in the vSphere Automation SDK for Java

Based on the output, we see quite a few parameters that still need to be referenced in order to authenticate and run the sample. The required parameters are server, username, and password. However, there are also some considerations needed around the handling of the vCenter’s certificate. I’m using a lab environment that still has the vCenter’s self-signed certificate in use, so I’ll be using the ‘skip-server-verification’ parameter. Lastly, there are the optional paramaters, most of which are straight forward, but the config-file parameter is a convenient one. For now, we’ll keep it simple and call each parameter as part of the command. I’ll discuss using a configuration file later in this post.

Performing the ListVMs sample in my environment, with all of the required parameters, looks like the following:

Example output when listing VMs

There’s another sample which takes a VM through the lifecycle of power states. This sample can be discovered in the ‘../vmware/samples/vcenter/vm/power’ directory. The public class we’ll be using is ‘PowerLifeCyle’. If we run that without any parameters, we can see that we’ll need to add the ‘vmname’ parameter and call a specific VM by name. Running this sample can be done with the following command:

Example of taking a VM through the lifecycle of power states

The last example we’ll look at is the modifying the memory configured for a VM. This sample can be found in the ‘../vmware/samples/vcenter/vm/hardware/memory’ directory. The public class we’ll be using is ‘MemoryConfiguration’. If we run that without any parameters, we can see that we will again need to add the ‘vmname’ parameter and call a specific VM by name.

This sample will set the RAM to 8GB then enables hot-add. We can go execute this sample with the following command:

Example of memory configuration options

One last thing before wrapping this up, let’s walk through using the configuration file. The configuration file can be any text-based file which contains lines that begin with the parameter names. In the following example, we create a new file and modify the file to include the following lines:

We can now reference this config file in any of the examples to authenticate against a vCenter server. Reusing the ‘ListVMs’ sample, the command to run that again but instead using a configuration file will look like the following:

Example of using the configuration file

Summary

This post shows you how to easily get started with the vSphere Automation SDK for Java. It takes you through the setup of your local development environment, as well as running some of the individual samples. You can then take whichever is most relevant to you and apply to your environment or, pull parts of this code out and use it to automate or integrate as needed.

With our SDKs now being open sourced, we are intent on making sure these samples are of a high quality. If you notice areas of improvement, or come up with some new samples, please feel free to raise issues or pull requests on our GitHub repository.

vSphere Automation SDKs Updated for VMware Cloud on AWS

The vSphere Automation SDKs have recently received an exciting update! These open-sourced SDKs can now also interact with the VMware Cloud on AWS service. The first two SDK languages updated to include this feature set is Python and Java. The remaining SDK languages will follow soon.

The VMware Cloud on AWS service offers a couple different RESTful APIs to interact with. The updated SDKs will focus on the VMware Cloud on AWS (VMC) API, as well as provide the authentication method through the Cloud Services Platform (CSP) API. To find out more about the APIs available as part of the VMware Cloud on AWS service, see this blog post: An Overview of VMware Cloud on AWS APIs

On this initial release, both the Python and Java SDKs include a handful of sample frameworks. These include the following functions:

  • Listing all Organizations associated with a user
  • Creation and removal of an SDDC
  • Managing the host count for a deployed SDDC

Let’s take a look at a couple of these in action.

vSphere Automation SDK for Python

One of the vSphere Automation SDK for Python samples has the functionality to perform two methods. The first will list all of the organizations the user’s refresh token is associated with. These orgs will be displayed by ID and display name. The second will list more detailed information for the first organization, such as ID, display name, name, create and updated dates, and SLA. This sample can be executed with the following command:

Python SDK Sample to list Org information

This SDK also has a new way to interact with the API and that is through the interactive interpreter! This can be done by creating the service object using the ‘create_vmc_client’ function while passing our refresh token. At this point, we can start accessing the available collections. The collections can by discovered in two main ways. First, by referring to the documentation that’s located in the ‘vmc.zip’ file that’s located in the ‘doc’ folder. Second, by using tab-complete. Some example code to obtain similar output to our prior example while in the interactive shell:

Interactive Shell Python Example Usage

vSphere Automation SDK for Java

The vSphere Automation SDK for Java offers a similar sample to the first Python example from above. However, this particular sample operates a little differently. The output from this sample will be the complete details of each org the user is associated with. This sample can be executed with the following command:

Java SDK Sample to list Org information

Summary

The open-sourced vSphere Automation SDKs have recently received an update. This update allows these SDKs to work with the RESTful API service of VMware Cloud on AWS. There are also a number of samples available within each SDK to list information about organizations, add and/or remove SDDCs, and manage the host count within an SDDC. These samples are easy to have operational within a few minutes of updating your local repo and are quite simple to integrate into your existing code!

Stay tuned for a more detailed look at getting started with each of these SDKs with the VMware Cloud on AWS service in the future.

For more information about the VMware Cloud on AWS service, see the following blog post: VMware Cloud on AWS Technical Overview
For more information about the VMware Cloud on AWS APIs, see the following blog post: An Overview of VMware Cloud on AWS APIs