Search in sources :

Example 76 with Datacenter

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();
        }
    }
}
Also used : VCenterAPI(com.iwave.ext.vmware.VCenterAPI) Vcenter(com.emc.storageos.db.client.model.Vcenter) ClusterComputeResource(com.vmware.vim25.mo.ClusterComputeResource) HostSystem(com.vmware.vim25.mo.HostSystem) VcenterDataCenter(com.emc.storageos.db.client.model.VcenterDataCenter) Host(com.emc.storageos.db.client.model.Host) URI(java.net.URI)

Aggregations

ManagedObjectReference (com.vmware.vim25.ManagedObjectReference)28 PropertyFilterSpec (com.vmware.vim25.PropertyFilterSpec)23 TraversalSpec (com.vmware.vim25.TraversalSpec)21 ObjectSpec (com.vmware.vim25.ObjectSpec)20 PropertySpec (com.vmware.vim25.PropertySpec)20 HostSystem (com.vmware.vim25.mo.HostSystem)20 ArrayList (java.util.ArrayList)17 RemoteException (java.rmi.RemoteException)15 ObjectContent (com.vmware.vim25.ObjectContent)14 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)12 VcenterObjectConnectionException (com.emc.storageos.vcentercontroller.exceptions.VcenterObjectConnectionException)11 VcenterObjectNotFoundException (com.emc.storageos.vcentercontroller.exceptions.VcenterObjectNotFoundException)11 VcenterServerConnectionException (com.emc.storageos.vcentercontroller.exceptions.VcenterServerConnectionException)11 VcenterSystemException (com.emc.storageos.vcentercontroller.exceptions.VcenterSystemException)11 SelectionSpec (com.vmware.vim25.SelectionSpec)11 ClusterComputeResource (com.vmware.vim25.mo.ClusterComputeResource)11 VirtualMachineMO (com.cloud.hypervisor.vmware.mo.VirtualMachineMO)10 VmwareHypervisorHost (com.cloud.hypervisor.vmware.mo.VmwareHypervisorHost)10 VcenterDataCenter (com.emc.storageos.db.client.model.VcenterDataCenter)10 Vcenter (com.emc.storageos.db.client.model.Vcenter)9