Created
August 8, 2015 17:15
-
-
Save asvignesh/cd02090562b8cf2ee0e3 to your computer and use it in GitHub Desktop.
Expand the VMFS Datastore to its maximum size
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.asvignesh.common.MyException; | |
import com.vmware.vim25.DatastoreHostMount; | |
import com.vmware.vim25.HostScsiDisk; | |
import com.vmware.vim25.VmfsDatastoreExpandSpec; | |
import com.vmware.vim25.VmfsDatastoreOption; | |
import com.vmware.vim25.mo.*; | |
import java.rmi.RemoteException; | |
/** | |
* Created by Vignesh on 8/8/2015. | |
*/ | |
public class ExpandDatastore extends VmwareCommon { | |
public static void main(String[] args) throws MyException { | |
String vCenter = args[0]; | |
String userName = args[1]; | |
String password = args[2]; | |
String datastoreName = args[3]; | |
ExpandDatastore expandDatastoreObj = new ExpandDatastore(); | |
ServiceInstance serviceInstance = expandDatastoreObj.createServiceInstance(vCenter, userName, password); | |
expandDatastoreObj.expandDatastoreToMaxAvailSize(serviceInstance, datastoreName); | |
return; | |
} | |
public void expandDatastoreToMaxAvailSize(ServiceInstance serviceInstance, String datastoreName) throws MyException { | |
Folder rootFolder = getFolder(serviceInstance); | |
Datastore datastore = getDatastorebyName(datastoreName, rootFolder); | |
DatastoreHostMount[] datastoreHostMounts = datastore.getHost(); | |
HostSystem hostSystem = null; | |
for (final DatastoreHostMount datastoreHostMount : datastoreHostMounts) { | |
try { | |
hostSystem = getHostSystem(rootFolder.getServerConnection(), datastoreHostMount.getKey()); | |
if (!isHostConnected(hostSystem)) { | |
continue; | |
} | |
break; | |
} catch (MyException e) { | |
//Warn... | |
//Failed in this host, retry on next one if available | |
} | |
} | |
if (hostSystem == null) { | |
throw new MyException("Datastore connected Hosts are not accessible"); | |
} | |
HostDatastoreSystem hostDatastoreSystem = getHostDatastoreSystem(hostSystem); | |
HostScsiDisk[] hostScsiDiskList; | |
try { | |
hostScsiDiskList = hostDatastoreSystem.queryAvailableDisksForVmfs(datastore); | |
} catch (Exception e) { | |
throw new MyException("Unable to query available disk for datastore"); | |
} | |
if (hostScsiDiskList == null) { | |
throw new MyException("Error in fetching host disk "); | |
} | |
VmfsDatastoreOption[] vmfsDatastoreOption; | |
try { | |
vmfsDatastoreOption = hostDatastoreSystem.queryVmfsDatastoreExpandOptions(datastore); | |
} catch (Exception e) { | |
throw new MyException(e.getMessage(), e); | |
} | |
if (vmfsDatastoreOption != null) { | |
VmfsDatastoreExpandSpec vmfsDatastoreExpandSpec = (VmfsDatastoreExpandSpec) vmfsDatastoreOption[0].getSpec(); | |
try { | |
hostDatastoreSystem.expandVmfsDatastore(datastore, vmfsDatastoreExpandSpec); | |
} catch (RemoteException e) { | |
throw new MyException(e.getMessage(), e); | |
} | |
} else { | |
throw new MyException("Datastore already utilized the maximum storage, nothing to expand"); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.asvignesh.common; | |
/** | |
* Created by Vignesh on 8/8/2015. | |
*/ | |
public class MyException extends Exception { | |
private static final long serialVersionUID = 1L; | |
public MyException(String message, Exception e) { | |
super(message, e); | |
} | |
public MyException(String message) { | |
super(message); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.asvignesh.common.MyConstant; | |
import com.asvignesh.common.MyException; | |
import com.vmware.vim25.HostSystemConnectionState; | |
import com.vmware.vim25.ManagedObjectReference; | |
import com.vmware.vim25.mo.*; | |
import com.vmware.vim25.mo.util.MorUtil; | |
import java.net.MalformedURLException; | |
import java.net.URL; | |
import java.rmi.RemoteException; | |
/** | |
* Created by Vignesh on 8/8/2015. | |
*/ | |
public class VmwareCommon { | |
public ServiceInstance createServiceInstance(String vCenter, String userName, String password) throws MyException { | |
String rootContext = "https://" + vCenter + "/sdk"; | |
ServiceInstance serviceInstance; | |
try { | |
serviceInstance = new ServiceInstance(new URL(rootContext), userName, password, true); | |
} catch (RemoteException e) { | |
throw new MyException("Failed to connect with the vCenter server"); | |
} catch (MalformedURLException e) { | |
throw new MyException("Failed to connect with the vCenter server, Malformed URL"); | |
} | |
return serviceInstance; | |
} | |
public Folder getFolder(ServiceInstance serviceInstance) throws MyException { | |
Folder rootFolder; | |
try { | |
rootFolder = serviceInstance.getRootFolder(); | |
} catch (final Exception e) { | |
throw new MyException(e.getMessage(), e); | |
} | |
return rootFolder; | |
} | |
public Datastore getDatastorebyName(String datastoreName, Folder rootFolder) throws MyException { | |
Datastore datastoreObject; | |
try { | |
datastoreObject = (Datastore) new InventoryNavigator(rootFolder).searchManagedEntity(MyConstant.DATASTORE, datastoreName); | |
} catch (RemoteException e) { | |
throw new MyException(e.getMessage(), e); | |
} | |
if (datastoreObject == null) { | |
throw new MyException("Datastore doesnt exist"); | |
} | |
return datastoreObject; | |
} | |
public HostSystem getHostSystem(ServerConnection serverConnection, ManagedObjectReference mor) throws MyException { | |
HostSystem hostSystem; | |
try { | |
hostSystem = (HostSystem) MorUtil.createExactManagedObject(serverConnection, mor); | |
if (hostSystem == null) { | |
throw new MyException("vmware.host.error.fetchhostsystem"); | |
} | |
} catch (Exception e) { | |
throw new MyException(e.getMessage()); | |
} | |
return hostSystem; | |
} | |
public boolean isHostConnected(HostSystem hostSystem) { | |
boolean resp = false; | |
if (hostSystem.getRuntime().connectionState.equals(HostSystemConnectionState.connected)) { | |
resp = true; | |
} | |
return resp; | |
} | |
HostDatastoreSystem getHostDatastoreSystem(HostSystem hostSystem) throws MyException { | |
HostDatastoreSystem hostDatastoreSystem; | |
try { | |
hostDatastoreSystem = hostSystem.getHostDatastoreSystem(); | |
} catch (Exception e) { | |
throw new MyException(e.getMessage(), e); | |
} | |
if (hostDatastoreSystem == null) { | |
throw new MyException("Unable to fetch datastore system"); | |
} | |
return hostDatastoreSystem; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment