Tag Archives: VI Java

Getting started with YAVI JAVA (Opensource JAVA SDK for VMware vSphere): Step by step guide for beginners

You might have read my last post on YAVI JAVA. In this post I am going to take you through setting up eclipse environment in order to get started with YAVI JAVA. This post is written by keeping not only existing VMware users in mind but also college students.

There are 4 ways listed here to get started with YAVI JAVA, one of the most used,easiest and quickest one is the first one i.e. obtaining YAVI JAVA package from Maven central repository. Here we go.

Step 1:: Download the Eclipse that has built in Maven integration. However, Maven is integrated with other IDEs such as NetBeans, Intelli IDEA etc. I like Eclipse so I am going to show you steps for eclipse, you could follow the same for other IDEs, it should work fine. Below is the Eclipse version I have downloaded which has Maven built in.
MavenEclipse

For getting started with YAVI JAVA, you are not required to be expert in Maven, even it is OK if you do not know anything about Maven. If you are interested, do read what is Maven here.

Step 2: Create a Maven project as shown in below screenshot.

Maven project

Step 3: Select the “Create Simple project” as shown below.

Maven_2

Step 4 : Next is to specify the Group id, Artifact id, version, Name etc. This can be anything of your choice as shown below and click on Finish.

Maven_3

Once clicked on FINISH, below is how it looks like when Maven project is created.

Maven_4

Step 5: Next we need to modify the POM.xml file. With every Maven project, one POM.xml file gets generated. Below is the overview of the existing POM.xml file got created when we created the project. POM.xml file is the one which tracks/resolves all the dependencies required for your project.

Maven_5

Step 6: Click on the dependencies tab in the bottom as shown below screenshot.

Maven_6

Now click on ADD button and enter the YAVI JAVA Maven group id, artifact id and version as shown in below screenshot
groupId : com.toastcoders
artifactId: yavijava
version :6.0.01 (or any YAVI JAVA version of your choice): Recommendation is to use latest available version on Maven central repository
Maven_9

Step 7: As soon as you click on OK in above step, make sure you can see entry in POM.xml file and required Maven dependencies jars as shown below.

Maven_10

That’s all. Now you are all set to play with new APIs introduced in vSphere 6.0. There are other 3 more ways to deploy the YAVI JAVA, if that works for you, please go ahead with that. Let me know if you face any issues.

All the samples and tutorials specified in this post will work just fine.

Note: YAVI JAVA supports VMware vSphere 4.0 and above (4.1, 5.0, 5.1, 5.5, 6.0).

Please do stay tuned for all the interesting samples on new features introduced in vSphere 6.0.

Great news: YAVI JAVA : fork of VI JAVA open source project : Supports VMware vSphere 6.0

Since last stable vSphere 5.5 release of VI JAVA open source project , for more than 15+ months there was not any update on VI java, no any questions on the community were getting answered. There was lot of uncertainty among VI JAVA users on the future of the VI JAVA, discussions were picking up among VI JAVA on how to mitigate the impact if VI JAVA becomes dead and Michael Rice came to the rescue and puts the end to this speculation. He forked the VI JAVA project into YAVI JAVA (Yet Another VI JAVA) in order to give new life to this project which is for sure asset to many VMware users. I really appreciate the great proactive work done by Michael, YAVI JAVA is result of Machael’s dedication & passion for contributing to open source community. I am sure so many VI JAVA users have reason to celebrate this. I would like to thank other contributors as well. At the same time we just can not forget awesome work done by Steve Jin by leading VI JAVA project for several past releases. YAVI JAVA now available on GIT HUB and community contribution is getting increased. I would recommend all of you to spread the word about this project, start contributing, using and help in making this project a great success. Without community contribution, no open source project can become great. Along with supporting VMware vSphere 6.0, there are several new features added to YAVI JAVA, great thing about this project is: your old projects those developed using VI JAVA will work just fine. how cool it is!

Below are some of important hyperlinks about YAVI JAVA links, releases, tutorials, samples etc.

1. YAVI JAVA website

2. GIT HUB repository

3. Basic tutorials

4. All the releases of YAVI JAVA

5. All the Samples : Do not worry as I specified all these samples will just work fine on YAVI JAVA as well. Please stay tuned for my blogs on sample wrt to new APIs introduced in vSphere 6.0

Please do stay tuned for step by step getting started guide on YAVI JAVA.

Tutorial PART-I :How to initailize connection with ESXi host or vCenter server using vSphere API

If you want to manage VMware Infrastructure efficiently, it is must that we should leverage vSphere APIs & automate admin tasks wherever possible.  When we start automating vSphere admin tasks, first question comes in mind that how to connect to VMware ESXi host or VMware vCenter Server using VI JAVA. Once we connect what is next? Next is how to navigate through the VMware ESXi host inventory or vCenter Server inventory & retrieve various inventory objects such as Datacenters, Clusters, Hosts, Virtual Machine etc. If you also have same queries then this tutorial is for you. Before getting into this tutorial in detail, please do make sure that you have already setup your Java Environment, if not, please refer my blog post on Getting Started with vSphere API. In part I of this tutorial, we will see how we can initialize the connection with VMware ESXi & vCenter. Let us start addressing first question

How to initialize the connection with VMware ESXi & vCenter Server.
vSphere API client application begins by connecting to a server & obtaining reference to “ServiceInstance”. ServiceInstance managed object is the singleton root object of the inventory on both vCenter and standalone ESXi host. In order to access VMware infrastructure objects such as Datacenters, Clusters, Hosts, Virtual Machine etc we first should create ServiceInstance managed object. Here we go.


package com.vmware.vim25.mo.Samples.cluster;
import java.net.URL;
import com.vmware.vim25.mo.ServiceInstance;
public class VCESXInitializer {
public static void main(String[] args) throws Exception {
if (args.length != 3) {
System.out.println("Usage: java SearchDatastore url "
+ "username password");
return;
}
ServiceInstance si = new ServiceInstance(new URL(args[0]), args[1],
args[2], true);
System.out.println(si);
}
}

In above snippet, we need to pass 3 parameters from eclipse as shown in below screen shot
Program Argument
Note:You may be wondering on 4th parameter that we are passing in ServiceInstance, it is “IgnoreCert” boolean parameter, at the moment, we are just passing “TRUE”. I will write other blog post on when to care about SSL certificates while initializing connection. However note that even if we pass TRUE, all the communication with server is encrypted.

Alternatively we can create ServiceInstance object the way shown below & use it wherever required.


package com.vmware.vim25.mo.Samples.cluster;
import java.net.MalformedURLException;
import java.net.URL;
import java.rmi.RemoteException;
import com.vmware.vim25.mo.ServiceInstance;
public class VCESXInitializer {
public ServiceInstance initialization() throws RemoteException,
MalformedURLException {

ServiceInstance si = new ServiceInstance(new URL(
"https://192.168.1.1/sdk"), "username", "password", true);
return si;
}
}

Note:IP address that we PASS as parameter can be of ESXi host or vCenter server, if you want to deal with just standalone ESXi host, then just pass ESXi IP address, otherwise, pass vCenter Server IP.
Creating ServiceInstance object is just 1 liner code, is not it too simple? Once we get ServiceInstance Object, we can access/navigate to all the VMware ESXi or vCenter Server objects.

Please stay tuned for my next tutorial on navigating various VMware Infrastructure objects such as Datacenter, Hosts, Resource pools, Virtual Machines etc.

To learn more on ServiceInstance managed object & lot: Refer: ServiceInstance Managed object API reference