static DatastoreSummary findDatastoreSummary(VirtualMachine vm, String dsName) throws Exception { DatastoreSummary dsSum = null; VirtualMachineRuntimeInfo vmRuntimeInfo = vm.getRuntime(); EnvironmentBrowser envBrowser = vm.getEnvironmentBrowser(); ManagedObjectReference hmor = vmRuntimeInfo.getHost(); if(hmor == null) { System.out.println("No Datastore found"); return null; } ConfigTarget configTarget = envBrowser.queryConfigTarget(new HostSystem(vm.getServerConnection(), hmor)); VirtualMachineDatastoreInfo[] dis = configTarget.getDatastore(); for (int i=0; dis!=null && i<dis.length; i++) { dsSum = dis[i].getDatastore(); if (dsSum.isAccessible() && dsName.equals(dsSum.getName())) { break; } } return dsSum; }
public ManagedObjectReference getMorDataStore(String dataStoreName, ConnectionResources connectionResources, ManagedObjectReference vmMor, VmInputs vmInputs) throws InvalidPropertyFaultMsg, RuntimeFaultFaultMsg { ManagedObjectReference dataStore = null; if (isNotBlank(dataStoreName)) { ManagedObjectReference cloneHostMor = getMorHost(vmInputs.getCloneHost(), connectionResources, vmMor); ConfigTarget configTarget = getHostConfigTarget(connectionResources, cloneHostMor); List<VirtualMachineDatastoreInfo> dataStoreInfoList = configTarget.getDatastore(); for (VirtualMachineDatastoreInfo dataStoreInfo : dataStoreInfoList) { if (vmInputs.getCloneDataStore().equals(dataStoreInfo.getDatastore().getName())) { dataStore = getDataStoreRef(vmInputs.getCloneDataStore(), dataStoreInfoList); break; } } if (dataStore == null) { throw new RuntimeException(ErrorMessages.DATA_STORE_NOT_FOUND); } } else { ObjectContent[] objectContents = GetObjectProperties.getObjectProperties(connectionResources, vmMor, new String[]{ManagedObjectType.SUMMARY.getValue()}); for (ObjectContent objectItem : objectContents) { List<DynamicProperty> vmProperties = objectItem.getPropSet(); for (DynamicProperty propertyItem : vmProperties) { VirtualMachineSummary virtualMachineSummary = (VirtualMachineSummary) propertyItem.getVal(); String vmPathName = virtualMachineSummary.getConfig().getVmPathName(); dataStoreName = vmPathName.substring(1, vmPathName.indexOf(Constants.RIGHT_SQUARE_BRACKET)); dataStore = getDataStore(dataStoreName, connectionResources, vmMor); break; } break; } } return dataStore; }
ManagedObjectReference getDataStoreRef(String dataStoreName, List<VirtualMachineDatastoreInfo> dataStoresList) { for (VirtualMachineDatastoreInfo dataStore : dataStoresList) { DatastoreSummary dsSummary = dataStore.getDatastore(); if (dataStoreName.equals(dsSummary.getName())) { if (!dsSummary.isAccessible()) { throw new RuntimeException(ErrorMessages.DATA_STORE_NOT_ACCESSIBLE); } return dsSummary.getDatastore(); } } throw new RuntimeException(ErrorMessages.DATA_STORE_NOT_FOUND); }