use of com.vmware.vim25.mo.ClusterComputeResource in project coprhd-controller by CoprHD.
the class VcenterApiClient method findByHostname.
private HostSystem findByHostname(ClusterComputeResource clusterComputeResource, String hostname) throws VcenterSystemException {
try {
ManagedEntity[] hostSystems = new InventoryNavigator(clusterComputeResource).searchManagedEntities("HostSystem");
_log.info("Search for host " + hostname + " by exact match");
for (ManagedEntity hostManagedEntity : hostSystems) {
HostSystem hostSystem = (HostSystem) hostManagedEntity;
if (hostSystem.getName().equalsIgnoreCase(hostname)) {
_log.info("Found host by exact match based search " + hostSystem.getName());
return hostSystem;
}
}
// Exact match failed so its qualified in one system but not in the other
_log.info("Search for host " + hostname + " by FQDN and unqualified searches");
Collection<HostSystem> hosts = new ArrayList<HostSystem>();
if (hostname.contains(".")) {
// FQDN
for (ManagedEntity hostManagedEntity : hostSystems) {
HostSystem hostSystem = (HostSystem) hostManagedEntity;
if (hostSystem.getName().toLowerCase().equalsIgnoreCase(hostname.split("\\.")[0])) {
_log.info("Found host by FQDN based search " + hostSystem.getName());
hosts.add(hostSystem);
}
}
} else {
// unqualified
for (ManagedEntity hostManagedEntity : hostSystems) {
HostSystem hostSystem = (HostSystem) hostManagedEntity;
if (hostSystem.getName().toLowerCase().startsWith(hostname.toLowerCase())) {
_log.info("Found host by unqualified based search " + hostSystem.getName());
hosts.add(hostSystem);
}
}
}
if (hosts.isEmpty()) {
_log.info("Host " + hostname + " not found");
} else if (hosts.size() > 1) {
_log.info("Host " + hostname + " search returned ambiguous result set of many hosts");
} else {
return hosts.iterator().next();
}
return null;
} catch (Exception e) {
_log.error("findByHostname exception " + e);
throw new VcenterSystemException(e.getLocalizedMessage());
}
}
use of com.vmware.vim25.mo.ClusterComputeResource 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.ClusterComputeResource 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.ClusterComputeResource 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.ClusterComputeResource in project coprhd-controller by CoprHD.
the class VcenterApiClient method searchClusterComputeResource.
private ClusterComputeResource searchClusterComputeResource(String datacenterName, String clusterNameOrMoRef) throws VcenterSystemException, VcenterObjectNotFoundException {
if (serviceInstance == null) {
_log.error("Invoke setup to open connection before using client");
throw new VcenterSystemException("Invoke setup to open connection before using client");
}
try {
// MoRef search first
_log.info("Search cluster by MoRef " + clusterNameOrMoRef);
ManagedEntity[] clusterComputeResources = new InventoryNavigator(serviceInstance.getRootFolder()).searchManagedEntities("ClusterComputeResource");
for (ManagedEntity managedEntity : clusterComputeResources) {
if (managedEntity.getMOR().getVal().equals(clusterNameOrMoRef)) {
_log.info("Found cluster " + managedEntity.getName() + " by MoRef " + clusterNameOrMoRef);
return (ClusterComputeResource) managedEntity;
}
}
// Then name
_log.info("Search cluster by name " + clusterNameOrMoRef + " in datacenter " + datacenterName);
Datacenter datacenter = (Datacenter) new InventoryNavigator(serviceInstance.getRootFolder()).searchManagedEntity("Datacenter", datacenterName);
if (datacenter == null) {
_log.error("Datacenter " + datacenterName + " does not exist");
throw new VcenterObjectNotFoundException("Datacenter " + datacenterName + " does not exist");
}
ClusterComputeResource clusterComputeResource = (ClusterComputeResource) new InventoryNavigator(datacenter).searchManagedEntity("ClusterComputeResource", clusterNameOrMoRef);
if (clusterComputeResource == null) {
_log.error("Cluster " + clusterNameOrMoRef + " does not exist");
throw new VcenterObjectNotFoundException("Cluster " + clusterNameOrMoRef + " does not exist");
}
return clusterComputeResource;
} catch (VcenterObjectNotFoundException e) {
throw e;
} catch (Exception e) {
_log.error("searchClusterComputeResources exception " + e);
throw new VcenterSystemException(e.getLocalizedMessage());
}
}
Aggregations