sspark1
Contributor
Contributor

How to Provisioning a VM on Datastore Cluster

I want to create a VM from a template using the JAVA SDK.

* The template has 2 hard disks.
- Hard disk #1 is configured in "DatastoreCluster-1".
- Hard disk #2 is configured in "DatastoreCluster-4".


* I want to configure hard disk 1 in "DatastoreCluster-1".

* I want to configure the 2nd hard disk in "DatastoreCluster-2".

 

What settings should be added in the java example below?

If the example is wrong, can you tell me where to find an example I can refer to?

 

# Java Code

try {
Folder rootFolder = this.getRootFolder();
Datacenter datacenter = this.getEntity(Datacenter.class, "Datacenter", rootFolder, dcName);

StoragePod storagePod = this.dataStores("DatastoreCluster-1");

VirtualMachine template = this.getEntity(VirtualMachine.class, "VirtualMachine", rootFolder, tmplName);

// Create Resource Manager
ManagedObjectReference storageResourceMOR = this.serviceInstance.getServiceContent().getStorageResourceManager();
StorageResourceManager resourceManager = new StorageResourceManager(this.serviceInstance.getServerConnection(), storageResourceMOR);

// Set Storage Pod to Drs Pod Selection Space
StorageDrsPodSelectionSpec drsPodSelection = new StorageDrsPodSelectionSpec();
drsPodSelection.setStoragePod(storagePod.getMOR());

// Set template, Vm Name, Type, Folder Clone Space and pod selection space to StoragePlacementSpec
StoragePlacementSpec placementSpec = new StoragePlacementSpec();
placementSpec.setPodSelectionSpec(drsPodSelection);
placementSpec.setVm(template.getMOR());
placementSpec.setCloneName(vmName);
placementSpec.setType("clone");
placementSpec.setFolder(datacenter.getVmFolder().getMOR());

VirtualMachineCloneSpec vmCloneSpec = new VirtualMachineCloneSpec();
vmCloneSpec.setPowerOn(false);
vmCloneSpec.setTemplate(false);
VirtualMachineRelocateSpec vmRelocateSpec = new VirtualMachineRelocateSpec();

ResourcePool resourcePool = this.getEntity(ClusterComputeResource.class, "ClusterComputeResource", rootFolder, dcName).getResourcePool();
vmRelocateSpec.setPool(resourcePool.getMOR());

vmCloneSpec.setLocation(vmRelocateSpec);
placementSpec.setCloneSpec(vmCloneSpec);

// Call recommendDatastores method on resource manager
StoragePlacementResult storagePlacementResult = resourceManager.recommendDatastores(placementSpec);

String [] storages = new String [storagePlacementResult.getRecommendations().length];

for(int offset = 0; offset < storagePlacementResult.getRecommendations().length; offset++) {
ClusterRecommendation recommendation = storagePlacementResult.getRecommendations()[offset];
log.info("Key -> {}, Rating -> {}, Reason -> {}, ReasonText -> {}, Type -> {}, Target -> {}",
recommendation.getKey(),
recommendation.getRating(),
recommendation.getReason(),
recommendation.getReasonText(),
recommendation.getType(),
recommendation.getTarget().getVal()
);

storages[offset] = recommendation.getKey();
}

// Apply Multiple Recommandations
Task task = resourceManager.applyStorageDrsRecommendation_Task(storages);

resourceManager.applyStorageDrsRecommendationToPod_Task(storagePod, storages[0]);

} catch (Exception ex) {
log.error("", ex);
Assertions.fail();
}
}

StoragePod dataStores(String name) throws RemoteException {
Folder rootFolder = this.serviceInstance.getRootFolder();

for(ManagedEntity dataCenterManagedEntity : new InventoryNavigator(rootFolder).searchManagedEntities("Datacenter")) {
Datacenter dataCenter = (Datacenter) dataCenterManagedEntity;

for(Datastore datastore : dataCenter.getDatastores()) {
log.info("DS -> {}", datastore.getParent().getName());
if( datastore.getParent().getName().equals(name) ) {
return (StoragePod) datastore.getParent();
}
}
}

return null;
}

 

0 Kudos
0 Replies