use of com.vmware.vim25.mo.Datacenter in project coprhd-controller by CoprHD.
the class ClusterService method validateVcenterClusterHosts.
/**
* Validate that the hosts in the cluster in our database matches the vCenter environment
*
* @param cluster the cluster to check
*/
public void validateVcenterClusterHosts(Cluster cluster) {
if (null == cluster) {
_log.error("Validation cluster is not set, not performing vCenter cluster validation");
return;
}
// We can only proceed if this cluster belongs to a datacenter
if (NullColumnValueGetter.isNullURI(cluster.getVcenterDataCenter())) {
_log.info("Cluster is not synced to vcenter");
return;
}
// Get a list of the cluster's hosts that are in our database.
List<URI> clusterHosts = ComputeSystemHelper.getChildrenUris(_dbClient, cluster.getId(), Host.class, "cluster");
VcenterDataCenter vcenterDataCenter = _dbClient.queryObject(VcenterDataCenter.class, cluster.getVcenterDataCenter());
// If the datacenter is not in our database, we must fail the validation.
if (vcenterDataCenter == null) {
throw APIException.badRequests.vCenterDataCenterNotFound(cluster.getVcenterDataCenter());
}
// If the datacenter has a null vCenter reference, we must fail the validation.
if (NullColumnValueGetter.isNullURI(vcenterDataCenter.getVcenter())) {
throw APIException.badRequests.vCenterDataCenterHasNullVcenter(vcenterDataCenter.forDisplay());
}
Vcenter vcenter = _dbClient.queryObject(Vcenter.class, vcenterDataCenter.getVcenter());
// If the vCenter is not in our database, we must fail the validation.
if (vcenter == null) {
throw APIException.badRequests.vCenterNotFound(vcenterDataCenter.getVcenter());
}
List<Host> dbHosts = _dbClient.queryObject(Host.class, clusterHosts);
VCenterAPI api = VcenterDiscoveryAdapter.createVCenterAPI(vcenter);
try {
// Query the vCenter to get a reference to the cluster so that we can compare the hosts between the actual
// environment and our database representation of the cluster.
ClusterComputeResource vcenterCluster = api.findCluster(vcenterDataCenter.getLabel(), cluster.getLabel());
// The cluster may not have been pushed to vCenter yet.
if (vcenterCluster == null) {
_log.info("Unable to find cluster %s in datacenter %s in vCenter environment %s", cluster.getLabel(), vcenterDataCenter.getLabel(), vcenter.getLabel());
return;
}
// Gather a set of all the host UUIDs in this vCenter cluster.
Set<String> vCenterHostUuids = Sets.newHashSet();
for (HostSystem hostSystem : vcenterCluster.getHosts()) {
if (hostSystem != null && hostSystem.getHardware() != null && hostSystem.getHardware().systemInfo != null) {
vCenterHostUuids.add(hostSystem.getHardware().systemInfo.uuid);
}
}
// Gather a set of all the host UUIDs in our database.
Set<String> dbHostUuids = Sets.newHashSet();
for (Host host : dbHosts) {
dbHostUuids.add(host.getUuid());
}
// If there are hosts in vCenter that are not in our database, fail the validation
if (!dbHostUuids.containsAll(vCenterHostUuids)) {
throw APIException.badRequests.clusterHostMismatch(cluster.forDisplay());
}
} finally {
if (api != null) {
api.logout();
}
}
}
Aggregations