Search in sources :

Example 6 with ClusterComputeResource

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());
    }
}
Also used : ManagedEntity(com.vmware.vim25.mo.ManagedEntity) HostSystem(com.vmware.vim25.mo.HostSystem) ArrayList(java.util.ArrayList) InventoryNavigator(com.vmware.vim25.mo.InventoryNavigator) VcenterSystemException(com.emc.storageos.vcentercontroller.exceptions.VcenterSystemException) VcenterServerConnectionException(com.emc.storageos.vcentercontroller.exceptions.VcenterServerConnectionException) VcenterObjectConnectionException(com.emc.storageos.vcentercontroller.exceptions.VcenterObjectConnectionException) VcenterObjectNotFoundException(com.emc.storageos.vcentercontroller.exceptions.VcenterObjectNotFoundException) VcenterSystemException(com.emc.storageos.vcentercontroller.exceptions.VcenterSystemException)

Example 7 with ClusterComputeResource

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());
    }
}
Also used : VcenterObjectNotFoundException(com.emc.storageos.vcentercontroller.exceptions.VcenterObjectNotFoundException) ClusterComputeResource(com.vmware.vim25.mo.ClusterComputeResource) VcenterSystemException(com.emc.storageos.vcentercontroller.exceptions.VcenterSystemException) VcenterSystemException(com.emc.storageos.vcentercontroller.exceptions.VcenterSystemException) VcenterServerConnectionException(com.emc.storageos.vcentercontroller.exceptions.VcenterServerConnectionException) VcenterObjectConnectionException(com.emc.storageos.vcentercontroller.exceptions.VcenterObjectConnectionException) VcenterObjectNotFoundException(com.emc.storageos.vcentercontroller.exceptions.VcenterObjectNotFoundException)

Example 8 with ClusterComputeResource

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());
    }
}
Also used : ManagedEntity(com.vmware.vim25.mo.ManagedEntity) VcenterObjectNotFoundException(com.emc.storageos.vcentercontroller.exceptions.VcenterObjectNotFoundException) ClusterComputeResource(com.vmware.vim25.mo.ClusterComputeResource) Datacenter(com.vmware.vim25.mo.Datacenter) VcenterSystemException(com.emc.storageos.vcentercontroller.exceptions.VcenterSystemException) VcenterSystemException(com.emc.storageos.vcentercontroller.exceptions.VcenterSystemException) VcenterServerConnectionException(com.emc.storageos.vcentercontroller.exceptions.VcenterServerConnectionException) VcenterObjectConnectionException(com.emc.storageos.vcentercontroller.exceptions.VcenterObjectConnectionException) VcenterObjectNotFoundException(com.emc.storageos.vcentercontroller.exceptions.VcenterObjectNotFoundException) ClusterConfigSpecEx(com.vmware.vim25.ClusterConfigSpecEx)

Example 9 with ClusterComputeResource

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());
    }
}
Also used : Task(com.vmware.vim25.mo.Task) VcenterObjectNotFoundException(com.emc.storageos.vcentercontroller.exceptions.VcenterObjectNotFoundException) ClusterComputeResource(com.vmware.vim25.mo.ClusterComputeResource) VcenterObjectConnectionException(com.emc.storageos.vcentercontroller.exceptions.VcenterObjectConnectionException) VcenterSystemException(com.emc.storageos.vcentercontroller.exceptions.VcenterSystemException) VcenterSystemException(com.emc.storageos.vcentercontroller.exceptions.VcenterSystemException) VcenterServerConnectionException(com.emc.storageos.vcentercontroller.exceptions.VcenterServerConnectionException) VcenterObjectConnectionException(com.emc.storageos.vcentercontroller.exceptions.VcenterObjectConnectionException) VcenterObjectNotFoundException(com.emc.storageos.vcentercontroller.exceptions.VcenterObjectNotFoundException)

Example 10 with ClusterComputeResource

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());
    }
}
Also used : ManagedEntity(com.vmware.vim25.mo.ManagedEntity) VcenterObjectNotFoundException(com.emc.storageos.vcentercontroller.exceptions.VcenterObjectNotFoundException) ClusterComputeResource(com.vmware.vim25.mo.ClusterComputeResource) Datacenter(com.vmware.vim25.mo.Datacenter) VcenterSystemException(com.emc.storageos.vcentercontroller.exceptions.VcenterSystemException) InventoryNavigator(com.vmware.vim25.mo.InventoryNavigator) VcenterSystemException(com.emc.storageos.vcentercontroller.exceptions.VcenterSystemException) VcenterServerConnectionException(com.emc.storageos.vcentercontroller.exceptions.VcenterServerConnectionException) VcenterObjectConnectionException(com.emc.storageos.vcentercontroller.exceptions.VcenterObjectConnectionException) VcenterObjectNotFoundException(com.emc.storageos.vcentercontroller.exceptions.VcenterObjectNotFoundException)

Aggregations

ClusterComputeResource (com.vmware.vim25.mo.ClusterComputeResource)14 HostSystem (com.vmware.vim25.mo.HostSystem)13 ManagedObjectReference (com.vmware.vim25.ManagedObjectReference)10 VcenterObjectConnectionException (com.emc.storageos.vcentercontroller.exceptions.VcenterObjectConnectionException)9 VcenterObjectNotFoundException (com.emc.storageos.vcentercontroller.exceptions.VcenterObjectNotFoundException)9 ObjectSpec (com.vmware.vim25.ObjectSpec)9 PropertyFilterSpec (com.vmware.vim25.PropertyFilterSpec)9 PropertySpec (com.vmware.vim25.PropertySpec)9 TraversalSpec (com.vmware.vim25.TraversalSpec)9 ObjectContent (com.vmware.vim25.ObjectContent)8 VcenterServerConnectionException (com.emc.storageos.vcentercontroller.exceptions.VcenterServerConnectionException)7 VcenterSystemException (com.emc.storageos.vcentercontroller.exceptions.VcenterSystemException)7 ArrayList (java.util.ArrayList)7 HostScsiDisk (com.vmware.vim25.HostScsiDisk)6 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)5 Datastore (com.vmware.vim25.mo.Datastore)5 Pair (com.cloud.utils.Pair)4 ExpandVmfsDatastore (com.emc.sa.service.vmware.block.tasks.ExpandVmfsDatastore)4 ExtendVmfsDatastore (com.emc.sa.service.vmware.block.tasks.ExtendVmfsDatastore)4 VcenterDataCenter (com.emc.storageos.db.client.model.VcenterDataCenter)4