Trying Self Paced Lab without Going to PEX 2010

I mentioned the vSphere API self paced lab at PEX in my previous blog. Not all the people who are interested in learning the API made it to PEX last month. A reader asked me when it can be online in his comment.

Here is the VI Java API part in the tutorial. We had the environment set up all together for you in the PEX lab including the Eclipse and all the related jar files. So it’s very easy to get started there. Without going to PEX, you need to do something extra by yourself. But that is not too hard at all. I promise it won’t take you much time at all. To get the basic one done, you probably need 5 to 30 minutes depending on your familiarity with Java.

Bothered by SLOW Web UI to manage vSphere? Want to manage ALL your VMware vCenters, AWS, Azure, Openstack, container behind a SINGLE pane of glass? Want to search, analyze, report, visualize VMs, hosts, networks, datastores, events as easily as Google the Web? Find out more about vSearch 3.0: the search engine for all your private and public clouds.

Ready to learn?

First, configure the environment. I have written a Getting Started Tutorial for VI Java API. You can easily follow this to set up the environment. It shouldn’t take more than 5 minutes for you to get these done. The last two steps there actually shows you how to create a new Java project and download a sample from VI Java API code repository, and then run the code. You can skip those two parts if you want.

Secondly, try the code I listed as follows. I have all the comments and hints for you to add new code. You can import this file into your project either new or an existing one as you worked earlier.

Note: If you need help, you can take a look at the finished code listed in the end. You still need to change the IP address, username, and password to run the finished code though.

Have a question, leave a comment or ask in the VI Java API forum.

Starting Point
/*================================================================================
Copyright (c) 2008 VMware, Inc. All Rights Reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

* Neither the name of VMware, Inc. nor the names of its contributors may be used
to endorse or promote products derived from this software without specific prior
written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS” AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL VMWARE, INC. OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
================================================================================*/

package com.vmware.vim25.mo.samples;

import java.net.URL;
import com.vmware.vim25.*;
import com.vmware.vim25.mo.*;

public class HelloVM
{
public static void main(String[] args) throws Exception
{
/***************************************************************
Change the URL and password according to your student manual
Note: don’t forget the s in https and the /sdk suffix.
*****************************************************************/
ServiceInstance si = new ServiceInstance(new URL(“https://8.8.8.8/sdk”), “root”, “password”, true);

/***************************************************************
Get the root folder object from the service instance.
Hint: type si and then ., you will see all available methods.
*****************************************************************/
Folder rootFolder =

/***************************************************************
Print out the name of the folder
Hint: type rootFolder and ., and you can pick the right method.
*****************************************************************/
String name =
System.out.println(“root:” + name);

/***************************************************************
List all the virtual machines on the server
Hint: Use InventoryNavigator, i.e.:
ManagedEntity[] mes = new InventoryNavigator(aFolder).searchManagedEntities(“DataCenter”);
NOTE: you can use rootFolder for aFolder, and “VirtualMachine” is the new type
*****************************************************************/
ManagedEntity[] mes =
if(mes==null || mes.length ==0)
{
return;
}

// Get the first virtual machines on the server
VirtualMachine vm = (VirtualMachine) mes[0];

/***************************************************************
Get the configuration of the VM, and capability of the VM
Hint: type vm and ., pick the right ones.
*****************************************************************/
String vmName =
VirtualMachineConfigInfo vminfo =
VirtualMachineCapability vmc =

/***************************************************************
Print out:
The Name of the VM
FullName of Guest OS
Whether Multiple SnapShot is supported
Hint: type . after vminfo and vmc and pick the right methods.
For boolean type of data, the method name starts with is*().
*****************************************************************/
System.out.println(“Hello ” + vmName);
System.out.println(“GuestOS: ” + vminfo.);
System.out.println(“Multiple snapshot supported: ” + vmc.);

si.getServerConnection().logout();
}
}

Solution Code

/*================================================================================
Copyright (c) 2008 VMware, Inc. All Rights Reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

* Neither the name of VMware, Inc. nor the names of its contributors may be used
to endorse or promote products derived from this software without specific prior
written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS” AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL VMWARE, INC. OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
================================================================================*/

package com.vmware.vim25.mo.samples;

import java.net.URL;
import com.vmware.vim25.*;
import com.vmware.vim25.mo.*;

public class HelloVM_Solution
{
public static void main(String[] args) throws Exception
{
/***************************************************************
Change the URL and password according to your student manual
Note: don’t forget the s in https and the /sdk suffix.
*****************************************************************/
ServiceInstance si = new ServiceInstance(new URL(“https://8.8.8.8/sdk”), “root”, “password”, true);

/***************************************************************
Get the root folder object from the service instance.
Hint: type si and then ., you will see all available methods.
*****************************************************************/
Folder rootFolder = si.getRootFolder();

/***************************************************************
Print out the name of the folder
Hint: type rootFolder and ., and you can pick the right method.
*****************************************************************/
String name = rootFolder.getName();
System.out.println(“root:” + name);

/***************************************************************
List all the virtual machines on the server
Hint: Use InventoryNavigator, i.e.:
ManagedEntity[] mes = new InventoryNavigator(aFolder).searchManagedEntities(“DataCenter”);
NOTE: you can use rootFolder for aFolder, and “VirtualMachine” is the new type
*****************************************************************/
ManagedEntity[] mes = new InventoryNavigator(rootFolder).searchManagedEntities(“VirtualMachine”);
if(mes==null || mes.length ==0)
{
return;
}

// Get the first virtual machines on the server
VirtualMachine vm = (VirtualMachine) mes[0];

/***************************************************************
Get the configuration of the VM, and capability of the VM
Hint: type vm and ., pick the right ones.
*****************************************************************/
String vmName = vm.getName();
VirtualMachineConfigInfo vminfo = vm.getConfig();
VirtualMachineCapability vmc = vm.getCapability();

/***************************************************************
Print out:
The Name of the VM
FullName of Guest OS
Whether Multiple SnapShot is supported
Hint: type . after vminfo and vmc and pick the right methods.
For boolean type of data, the method name starts with is*().
*****************************************************************/
System.out.println(“Hello ” + vmName);
System.out.println(“GuestOS: ” + vminfo.getGuestFullName());
System.out.println(“Multiple snapshot supported: ” + vmc.isMultipleSnapshotsSupported());

si.getServerConnection().logout();
}
}

This entry was posted in Software Development, vSphere API and tagged , , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

  • NEED HELP?


    My company has created products like vSearch ("Super vCenter"), vijavaNG APIs, EAM APIs, ICE tool. We also help clients with virtualization and cloud computing on customized development, training. Should you, or someone you know, need these products and services, please feel free to contact me: steve __AT__ doublecloud.org.

    Me: Steve Jin, VMware vExpert who authored the VMware VI and vSphere SDK by Prentice Hall, and created the de factor open source vSphere Java API while working at VMware engineering. Companies like Cisco, EMC, NetApp, HP, Dell, VMware, are among the users of the API and other tools I developed for their products, internal IT orchestration, and test automation.