CreateVmfsDatastore.java
Copyright © 2008 VMware, Inc. All rights reserved.
// Copyright 2008 VMware, Inc. All rights reserved.
//#######################################################################################
// DISCLAIMER. THIS SCRIPT IS PROVIDED TO YOU "AS IS" WITHOUT WARRANTIES OR CONDITIONS
// OF ANY KIND, WHETHER ORAL OR WRITTEN, EXPRESS OR IMPLIED. THE AUTHOR SPECIFICALLY
// DISCLAIMS ANY IMPLIED WARRANTIES OR CONDITIONS OF MERCHANTABILITY, SATISFACTORY
// QUALITY, NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE.
//#######################################################################################
//#######################################################################################
//
// CreateVmfsDatastore.java
// Example script to create VmfsDatastore
// Parameters:
// datacenterName : specifies the name of the datacenter
// HostIp : Ip Address of "Host"
// datastoreName : Name of the datastore added.
// This script works with VMware VirtualCenter 2.0 or later.
// This script works with VMware ESX Server 3.0 or later.
//#######################################################################################
package com.vmware.samples.host;
import com.vmware.vim.*;
import com.vmware.apputils.*;
import com.vmware.apputils.vim.*;
import java.util.*;
import java.io.*;
import java.rmi.RemoteException;
public class CreateVmfsDatastore {
private static AppUtil cb = null;
private static ServiceContent content;
static VimPortType service; // All Methods
Log log = new Log();
private String getDatacenterName() throws Exception {
return cb.get_option("datacenterName");
}
private String getHostIp() throws Exception {
return cb.get_option("HostIp");
}
private String getDatastoreName() throws Exception {
return cb.get_option("datastoreName");
}
private void doCreate() throws Exception {
try {
String datacenterName = getDatacenterName();
String datastoreName = getDatastoreName();
String HostIp = getHostIp();
content = cb.getConnection().getServiceContent();
service = cb.getConnection().getService();
ManagedObjectReference taskMoRef = null;
// Find the Datacenter reference by using findByInventoryPath().
ManagedObjectReference datacenterRef =
cb.getConnection().getService().findByInventoryPath(content.getSearchIndex(),
datacenterName);
if (datacenterRef == null) {
System.out.println("Datacenter '" + datacenterName
+ "' not found");
} else {
// Find the host with specified Ip in this datacenter.
ManagedObjectReference hostRef = cb.getConnection().getService().findByIp(content.getSearchIndex(),
datacenterRef, HostIp, false);
//configManager property of host
HostConfigManager hostConfigManager = (HostConfigManager)
getObjectProperty(hostRef, "configManager");
ManagedObjectReference HostDatastoreSystemRef= null;
//Datastore Manager
HostDatastoreSystemRef= hostConfigManager.getDatastoreSystem();
//HostScsiDisk object
HostScsiDisk[] hostScsiDiskList = new HostScsiDisk[10];
//Query to list disks on Datastore Manager
hostScsiDiskList = cb.getConnection().getService().queryAvailableDisksForVmfs(HostDatastoreSystemRef, null);
if(hostScsiDiskList != null) {
HostScsiDisk hScsiDisk = new HostScsiDisk();
hScsiDisk= hostScsiDiskList[0];
VmfsDatastoreOption[] vmfsDatastoreOption = cb.getConnection().getService().queryVmfsDatastoreCreateOptions(HostDatastoreSystemRef, hScsiDisk.getDevicePath());
VmfsDatastoreCreateSpec vmfsDatastoreCreateSpec = (VmfsDatastoreCreateSpec)vmfsDatastoreOption[0].getSpec();
vmfsDatastoreCreateSpec.getVmfs().setVolumeName(datastoreName);
ManagedObjectReference datastoreRef = cb.getConnection().getService().createVmfsDatastore(HostDatastoreSystemRef,vmfsDatastoreCreateSpec);
if(datastoreRef == null) {
System.out.println("datastore '" + datastoreName
+ "' not formed");
} else {
System.out.println("datastore '" + datastoreName
+ "' created successfully");
}
}
else {
System.out.println("No extent vailable");
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
private static OptionSpec[] constructOptions() {
OptionSpec [] useroptions = new OptionSpec[3];
useroptions[0] = new OptionSpec("datacenterName","String",1
,"Specifies the name of the datacenter"
,null);
useroptions[1] = new OptionSpec("HostIp","String",1,
"Ip address of host to which datastore to be added",
null);
useroptions[2] = new OptionSpec("datastoreName","String",1,
"Name of the datastore being added",
null);
return useroptions;
}
public static void main(String[] args) throws Exception {
CreateVmfsDatastore app = new CreateVmfsDatastore();
cb = AppUtil.initialize("CreateVmfsDatastore", CreateVmfsDatastore.constructOptions(), args);
cb.connect();
app.doCreate();
cb.disConnect();
}
/*
* getStringProperty --
*
* Retrieve the specified string property.
* see getProperties.
*/
static String getStringProperty(ManagedObjectReference moRef, String propertyName)
throws RuntimeFault, RemoteException {
return (String) (getProperties(moRef, new String[] { propertyName })[0]);
}
/*
* getObjectProperty --
*
* Retrieves the specified object property.
* see getProperties.
*/
static Object getObjectProperty(ManagedObjectReference moRef, String propertyName)
throws RuntimeFault, RemoteException {
return getProperties(moRef, new String[] { propertyName })[0];
}
static Object[] getProperties(ManagedObjectReference moRef, String[] properties)
throws RuntimeFault, RemoteException {
// PropertySpec specifies what properties to
// retrieve and from type of Managed Object
PropertySpec pSpec = new PropertySpec();
pSpec.setType(moRef.getType());
pSpec.setPathSet(properties);
// ObjectSpec specifies the starting object and
// any TraversalSpecs used to specify other objects
// for consideration
ObjectSpec oSpec = new ObjectSpec();
oSpec.setObj(moRef);
// PropertyFilterSpec is used to hold the ObjectSpec and
// PropertySpec for the call
PropertyFilterSpec pfSpec = new PropertyFilterSpec();
pfSpec.setPropSet(new PropertySpec[] {pSpec});
pfSpec.setObjectSet(new ObjectSpec[] {oSpec});
// retrieveProperties() returns the properties
// selected from the PropertyFilterSpec
ObjectContent[] ocs = service.retrieveProperties(
content.getPropertyCollector(),
new PropertyFilterSpec[] {pfSpec});
// Return value, one object for each property specified
Object[] ret = new Object[properties.length];
if(ocs != null) {
for(int i=0; i<ocs.length; ++i) {
ObjectContent oc = ocs[i];
DynamicProperty[] dps = oc.getPropSet();
if(dps != null) {
for(int j=0; j<dps.length; ++j) {
DynamicProperty dp = dps[j];
// find property path index
for(int p=0; p<ret.length; ++p) {
if(properties[p].equals(dp.getName())) {
ret[p] = dp.getVal();
}
}
}
}
}
}
return ret;
}
}
The sample code is provided "AS-IS" for use, modification, and redistribution in source and binary forms, provided that the copyright notice and this following list of conditions are retained and/or reproduced in your distribution. To the maximum extent permitted by law, VMware, Inc., its subsidiaries and affiliates hereby disclaim all express, implied and/or statutory warranties, including duties or conditions of merchantability, fitness for a particular purpose, and non-infringement of intellectual property rights. IN NO EVENT WILL VMWARE, ITS SUBSIDIARIES OR AFFILIATES BE LIABLE TO ANY OTHER PARTY FOR THE COST OF PROCURING SUBSTITUTE GOODS OR SERVICES, LOST PROFITS, LOSS OF USE, LOSS OF DATA, OR ANY INCIDENTAL, CONSEQUENTIAL, DIRECT, INDIRECT, OR SPECIAL DAMAGES, ARISING OUT OF THIS OR ANY OTHER AGREEMENT RELATING TO THE SAMPLE CODE.
You agree to defend, indemnify and hold harmless VMware, and any of its directors, officers, employees, agents, affiliates, or subsidiaries from and against all losses, damages, costs and liabilities arising from your use, modification and distribution of the sample code.
VMware does not certify or endorse your use of the sample code, nor is any support or other service provided in connection with the sample code.