use of com.vmware.vim25.mo.Datacenter in project coprhd-controller by CoprHD.
the class VcenterApiClient method checkVMsOnHostVolume.
/**
* Verifies if the host can see the volume, if so then check for VMs (powered on/off)
* on the datastore identified for the given volume wwn.
*
* @param datacenterName {@link String} name of the dataCenter
* @param clusterNameOrMoRef {@link String} name of the cluster
* @param hostName {@link String} name of the host
* @param volumewwn {@link String} volume wwn
* @return true when there are any VMs on the datastore identified by the volume wwn, otherwise false
* @throws VcenterSystemException
* @throws VcenterObjectNotFoundException
* @throws VcenterObjectConnectionException
*/
public boolean checkVMsOnHostVolume(String datacenterName, String clusterNameOrMoRef, String hostName, String volumewwn) throws VcenterSystemException, VcenterObjectNotFoundException, VcenterObjectConnectionException {
boolean isVMsPresent = false;
try {
_log.info("Request to check VMs on volume " + volumewwn + " host " + hostName + " to datacenter " + datacenterName + " cluster " + clusterNameOrMoRef);
HostSystem hostSystem = (HostSystem) createManagedEntityMap(datacenterName, clusterNameOrMoRef, hostName, false).get("HostSystem");
if (volumewwn == null || volumewwn.trim().equals("")) {
_log.error("Volume UUID not specified");
throw new VcenterSystemException("Volume UUID not specified");
}
Datastore[] datastores = hostSystem.getDatastores();
if (datastores != null && datastores.length > 0) {
_log.info("Found {} datastores for host {}", datastores.length, hostName);
String specifiedVolumeDevicePath = null;
HostStorageSystem hostStorageSystem = hostSystem.getHostStorageSystem();
HostStorageDeviceInfo hostStorageDeviceInfo = hostStorageSystem.getStorageDeviceInfo();
ScsiLun[] hostScsiLuns = hostStorageDeviceInfo.getScsiLun();
for (ScsiLun scsiLun : hostScsiLuns) {
if (scsiLun instanceof HostScsiDisk) {
HostScsiDisk hostScsiDisk = (HostScsiDisk) scsiLun;
// Check if host is able to see the specified volume
if (hostScsiDisk.getUuid().toLowerCase().contains(volumewwn.toLowerCase())) {
_log.info("Found disk " + hostScsiDisk.getUuid() + " on " + hostName + " for volume UUID " + volumewwn);
specifiedVolumeDevicePath = hostScsiDisk.getDevicePath();
break;
}
}
}
if (specifiedVolumeDevicePath != null) {
Datastore bootVolDatastore = null;
for (Datastore datastore : datastores) {
_log.info("Find existing datastore on volume " + volumewwn + " for host " + hostName);
if (datastore.getInfo() instanceof VmfsDatastoreInfo) {
VmfsDatastoreInfo vmfsDatastoreInfo = (VmfsDatastoreInfo) datastore.getInfo();
String diskName = vmfsDatastoreInfo.getVmfs().getExtent()[0].getDiskName();
if (specifiedVolumeDevicePath.contains(diskName) && diskName.contains(volumewwn.toLowerCase())) {
_log.info("Found datastore " + vmfsDatastoreInfo.getName() + " on disk " + diskName);
bootVolDatastore = datastore;
break;
}
}
}
if (bootVolDatastore != null) {
if (CollectionUtils.isNotEmpty(Arrays.asList(bootVolDatastore.getVms()))) {
isVMsPresent = true;
_log.info("Found {} VMs on the datastore {}", bootVolDatastore.getVms().length, bootVolDatastore.getName());
} else {
_log.info("No VMs found on datastore {}", bootVolDatastore.getName());
}
} else {
_log.warn("Could not find datastore for host {} on volume wwn {}", hostName, volumewwn);
}
} else {
_log.warn("Could not find disk on host {} for volume UUID {}", hostName, volumewwn);
}
} else {
_log.info("No datastores found for host {}, hence inferring that no VMs are present on the given volume wwn {}", hostName, volumewwn);
}
} catch (VcenterSystemException | VcenterObjectNotFoundException | VcenterObjectConnectionException e) {
_log.error("Vcenter exception checkVMsOnHostVolume : {}", e);
throw e;
} catch (Exception e) {
_log.error("Exception checkVMsOnHostVolume : {}", e);
throw new VcenterSystemException(e.getLocalizedMessage());
}
return isVMsPresent;
}
use of com.vmware.vim25.mo.Datacenter in project coprhd-controller by CoprHD.
the class VcenterApiClient method createCluster.
public String createCluster(String datacenterName, String clusterNameOrMoRef) throws VcenterSystemException, VcenterObjectNotFoundException {
try {
// Ensures datacenter exists
createManagedEntityMap(datacenterName, null, null, false).get("Datacenter");
ClusterComputeResource clusterComputeResource = null;
try {
clusterComputeResource = searchClusterComputeResource(datacenterName, clusterNameOrMoRef);
} catch (VcenterObjectNotFoundException e) {
_log.info("Ignore VcenterObjectNotFoundException on cluster search since we are creating new cluster");
}
if (clusterComputeResource != null) {
_log.error("Cluster " + clusterNameOrMoRef + " already exists");
throw new VcenterSystemException("Cluster " + clusterNameOrMoRef + " already exists");
}
return createOrUpdateCluster(datacenterName, clusterNameOrMoRef);
} catch (VcenterSystemException | VcenterObjectNotFoundException e) {
throw e;
} catch (Exception e) {
_log.error("createCluster exception " + e);
throw new VcenterSystemException(e.getLocalizedMessage());
}
}
use of com.vmware.vim25.mo.Datacenter in project coprhd-controller by CoprHD.
the class VcenterApiClient method createOrUpdateCluster.
private String createOrUpdateCluster(String datacenterName, String clusterNameOrMoRef) throws VcenterSystemException, VcenterObjectNotFoundException {
try {
_log.info("Request to create or update cluster " + clusterNameOrMoRef + " in datacenter " + datacenterName);
Map<String, ManagedEntity> managedEntityMap = createManagedEntityMap(datacenterName, null, null, false);
// Ensure datacenter exists
Datacenter datacenter = (Datacenter) managedEntityMap.get("Datacenter");
ClusterComputeResource clusterComputeResource = null;
try {
clusterComputeResource = searchClusterComputeResource(datacenterName, clusterNameOrMoRef);
_log.info("Cluster " + clusterNameOrMoRef + " already exists so no work to do");
} catch (VcenterObjectNotFoundException e) {
_log.info("Cluster " + clusterNameOrMoRef + " does not exist and will be created");
ClusterConfigSpecEx clusterConfigSpecEx = clusterConfigurer.configure(propertyInfo);
clusterComputeResource = datacenter.getHostFolder().createClusterEx(clusterNameOrMoRef, clusterConfigSpecEx);
_log.info("Cluster " + clusterNameOrMoRef + " created with key " + clusterComputeResource.getMOR().getVal());
}
return clusterComputeResource.getMOR().getVal();
} catch (VcenterSystemException | VcenterObjectNotFoundException e) {
throw e;
} catch (Exception e) {
_log.error("Exception creating cluster: " + e);
throw new VcenterSystemException(e.getLocalizedMessage());
}
}
use of com.vmware.vim25.mo.Datacenter in project coprhd-controller by CoprHD.
the class VcenterApiClient method removeCluster.
public void removeCluster(String datacenterName, String clusterNameOrMoRef) throws VcenterSystemException, VcenterObjectNotFoundException, VcenterObjectConnectionException {
try {
_log.info("Request to remove cluster in datacenter " + datacenterName + " cluster " + clusterNameOrMoRef);
try {
ClusterComputeResource clusterComputeResource = (ClusterComputeResource) createManagedEntityMap(datacenterName, clusterNameOrMoRef, null, false).get("ClusterComputeResource");
String clusterName = clusterComputeResource.getName();
_log.info("Attempt to delete cluster " + clusterName);
// Remove
Integer clusterOperationTimeout = Integer.parseInt(propertyInfo.getProperty("vcenter_operation_timeout"));
VcenterTaskMonitor taskMonitor = new VcenterTaskMonitor(clusterOperationTimeout);
Task deleteClusterTask = clusterComputeResource.destroy_Task();
// call blocks
VcenterTaskMonitor.TaskStatus taskStatus = taskMonitor.monitor(deleteClusterTask);
if (taskStatus == VcenterTaskMonitor.TaskStatus.SUCCESS) {
_log.info("Delete cluster " + clusterName + " task succeeded");
} else if (taskStatus == VcenterTaskMonitor.TaskStatus.ERROR) {
String errorMessage = "Delete cluster " + clusterName + " task failed - " + taskMonitor.errorDescription;
_log.error(errorMessage);
throw new VcenterSystemException(errorMessage);
} else if (taskStatus == VcenterTaskMonitor.TaskStatus.TIMED_OUT) {
_log.error("Delete cluster " + clusterName + " task timed out at " + taskMonitor.progressPercent);
throw new VcenterSystemException("Delete cluster " + clusterName + " task timed out at " + taskMonitor.progressPercent);
} else {
// Should not execute - Just here in case someone ever added a new state so we catch it
_log.error("Unknown task status encountered tracking delete cluster " + taskStatus);
throw new VcenterSystemException("Unknown task status encountered tracking delete cluster " + taskStatus);
}
} catch (VcenterObjectNotFoundException e) {
_log.info("Cluster not found thus no delete necessary");
}
} catch (VcenterSystemException | VcenterObjectNotFoundException | VcenterObjectConnectionException e) {
throw e;
} catch (Exception e) {
_log.error("Exception removing cluster: " + e);
throw new VcenterSystemException(e.getLocalizedMessage());
}
}
use of com.vmware.vim25.mo.Datacenter in project coprhd-controller by CoprHD.
the class VcenterApiClient method checkHostConnectedPoweredOn.
public void checkHostConnectedPoweredOn(String datacenterName, String clusterNameOrMoRef, String hostname) throws VcenterSystemException, VcenterObjectNotFoundException, VcenterObjectConnectionException {
try {
_log.info("Request to check connected and powered on for host " + hostname + " in datacenter " + datacenterName + " cluster " + clusterNameOrMoRef);
HostSystem hostSystem = (HostSystem) createManagedEntityMap(datacenterName, clusterNameOrMoRef, hostname, true).get(// checkHostConnectedPoweredOn is performed in this call
"HostSystem");
} catch (VcenterSystemException | VcenterObjectNotFoundException | VcenterObjectConnectionException e) {
throw e;
} catch (Exception e) {
_log.error("checkHostConnectedPoweredOn exception " + e);
throw new VcenterSystemException(e.getLocalizedMessage());
}
}
Aggregations