Solved

Deploying from Template using VMWare Infrastructure Java API

Posted on 2014-10-01
6
1,547 Views
Last Modified: 2014-10-14
I'm using the VI Java API for VMWare and the vCenter 5.0.0.  I can successfully clone a running VM but would like to clone/deploy a VM from template.  However, when I specify a template to clone instead of a VM I get this:

Clone virtual machine
chefDemo232_Template

A specified parameter was not correct.  
spec.location.pool


Not sure how to specify or if I even have a resource pool.  Here's my code:

public void cloneVM() throws Exception{
		
		 
			System.out.println("inClon<wbr ></wbr>eVM");

		    String vmname = "chefDemo232_Template";
		    String cloneName = "template clone test";
		
		    ServiceInstance si = new ServiceInstance(
		        new URL("my URL"), "my name", "my password", true);

		    System.out.println("afterU<wbr ></wbr>rl");
		    
		    Folder rootFolder = si.getRootFolder();
		    VirtualMachine vm = (VirtualMachine) new InventoryNavigator(
		        rootFolder).searchManagedE<wbr ></wbr>ntity(
		            "VirtualMachine", vmname);
		    
		 //   Datacenter dc = (Datacenter) new InventoryNavigator(
		        //    rootFolder).searchManagedE<wbr ></wbr>ntity("Dat<wbr ></wbr>acenter", dcName);
		 //   ResourcePool rp = (ResourcePool) new InventoryNavigator(
		           // dc).searchManagedEntities(<wbr ></wbr>"ResourceP<wbr ></wbr>ool")[0];


		    if(vm==null)
		    {
		      System.out.println("No VM " + vmname + " found");
		      si.getServerConnection().l<wbr ></wbr>ogout();
		      return;
		    }

		    VirtualMachineCloneSpec cloneSpec = 
		      new VirtualMachineCloneSpec();<wbr ></wbr>
		    
		    
		   
		    cloneSpec.setLocation(new VirtualMachineRelocateSpec<wbr ></wbr>());
		    cloneSpec.setPowerOn(false<wbr ></wbr>);
		    cloneSpec.setTemplate(fals<wbr ></wbr>e);
		   
		  

		    Task task = vm.cloneVM_Task((Folder) vm.getParent(), 
		        cloneName, cloneSpec);
		    System.out.println("Launch<wbr ></wbr>ing the VM clone task. " +
		    		"Please wait ...");

		    String status = task.waitForMe();
		    if(status==Task.SUCCESS)
		    {
		      System.out.println("VM got cloned successfully.");
		    }
		    else
		    {
		      System.out.println("Failur<wbr ></wbr>e -: VM cannot be cloned");
		    }
		}

Open in new window

0
Comment
Question by:HyperBPP
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 5

Assisted Solution

by:Uni Kitty
Uni Kitty earned 500 total points
ID: 40355627
Hi Hyper,

I'm not a programmer but I have some VMware resources that may help you troubleshoot this problem.

First have a look at the vSphere 5.0 SDK guides if you haven't already, the location.pool is only required for templates, which is probably why it works for a running VM if you're not specifying it.

Check out:
http://pubs.vmware.com/vsphere-50/index.jsp?topic=%2Fcom.vmware.wssdk.apiref.doc_50%2Fvim.VirtualMachine.html&path=5_0_2_5_3_21

Check out this fling tool from VMware, called Onyx, you can use this on the vCenter Server and have Onyx launch the vSphere client, then it will output the XML of whatever tasks you're doing. you might find this useful. It will tell you what things should look like.

Good luck!
Melly
0
 
LVL 5

Expert Comment

by:Uni Kitty
ID: 40359858
Hi There! Just checking back to see if you were able to figure it out?
0
 
LVL 6

Accepted Solution

by:
HyperBPP earned 0 total points
ID: 40370910
Yeah, sorry for the delay.  This is for work so my job dictates my schedule on when I can work on stuff.  I figured it out:

 VirtualMachine vm = (VirtualMachine) new InventoryNavigator(
                     rootFolder).searchManagedEntity(
                         "VirtualMachine", vmname);
                 
                 Datacenter dc = (Datacenter) new InventoryNavigator(
                         rootFolder).searchManagedEntity("Datacenter", dcName);
                 ResourcePool rp = (ResourcePool) new InventoryNavigator(
                         dc).searchManagedEntities("ResourcePool")[0];

 locateSpec.setPool(rp.getMOR());
            
              cloneSpec.setLocation(locateSpec);

Thanks!
0
 
LVL 5

Expert Comment

by:Uni Kitty
ID: 40370955
Great! I'm glad you got it working!
0
 
LVL 6

Author Closing Comment

by:HyperBPP
ID: 40379415
The actual solution which the expert comment led me too is shown in my answer.
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When converting a physical machine to a virtual machine using VMware vCenter Converter Standalone or vCenter Converter Enterprise, if an adapter type is not selected during the initial customization the resulting virtual machine may contain an IDE d…
In this article, I will show you HOW TO: Install VMware Tools for Windows on a VMware Windows virtual machine on a VMware vSphere Hypervisor 6.5 (ESXi 6.5) Host Server, using the VMware Host Client. The virtual machine has Windows Server 2016 instal…
Advanced tutorial on how to run the esxtop command to capture a batch file in csv format in order to export the file and use it for performance analysis. He demonstrates how to download the file using a vSphere web client (or vSphere client) and exp…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

690 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question