Search in sources :

Example 6 with VcenterSystemException

use of com.emc.storageos.vcentercontroller.exceptions.VcenterSystemException 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 7 with VcenterSystemException

use of com.emc.storageos.vcentercontroller.exceptions.VcenterSystemException 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 8 with VcenterSystemException

use of com.emc.storageos.vcentercontroller.exceptions.VcenterSystemException 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());
    }
}
Also used : VcenterObjectNotFoundException(com.emc.storageos.vcentercontroller.exceptions.VcenterObjectNotFoundException) VcenterObjectConnectionException(com.emc.storageos.vcentercontroller.exceptions.VcenterObjectConnectionException) HostSystem(com.vmware.vim25.mo.HostSystem) 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 9 with VcenterSystemException

use of com.emc.storageos.vcentercontroller.exceptions.VcenterSystemException 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)

Example 10 with VcenterSystemException

use of com.emc.storageos.vcentercontroller.exceptions.VcenterSystemException in project coprhd-controller by CoprHD.

the class VcenterApiClient method getVirtualMachines.

private List<VirtualMachine> getVirtualMachines(ClusterComputeResource clusterComputeResource, boolean runningOnly) throws VcenterSystemException {
    try {
        List<VirtualMachine> virtualMachines = new ArrayList<VirtualMachine>();
        String clusterName = clusterComputeResource.getName();
        _log.info("Inspect cluster " + clusterName + " for virtual machines running only " + runningOnly);
        ResourcePool resourcePool = clusterComputeResource.getResourcePool();
        if (resourcePool != null) {
            _log.info("Inspect resource pool " + resourcePool.getName() + " for virtual machines");
            if (resourcePool.getVMs() != null) {
                for (VirtualMachine virtualMachine : resourcePool.getVMs()) {
                    _log.info("Found virtual machine " + virtualMachine.getName() + " in resource pool " + resourcePool.getName());
                    _log.info(toDetails(virtualMachine));
                    if (runningOnly) {
                        // Anything !poweredOff (poweredOn and suspended) will be considered running
                        if (!virtualMachine.getRuntime().getPowerState().equals(VirtualMachinePowerState.poweredOff)) {
                            virtualMachines.add(virtualMachine);
                        }
                    } else {
                        virtualMachines.add(virtualMachine);
                    }
                }
            }
        }
        _log.info("Cluster " + clusterName + " has " + virtualMachines.size() + " virtual machines");
        return virtualMachines;
    } catch (Exception e) {
        _log.error("getVirtualMachines clusterComputeResource exception " + e);
        throw new VcenterSystemException("Error checking cluster for virtual machines");
    }
}
Also used : ArrayList(java.util.ArrayList) ResourcePool(com.vmware.vim25.mo.ResourcePool) 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) VirtualMachine(com.vmware.vim25.mo.VirtualMachine)

Aggregations

VcenterObjectConnectionException (com.emc.storageos.vcentercontroller.exceptions.VcenterObjectConnectionException)21 VcenterObjectNotFoundException (com.emc.storageos.vcentercontroller.exceptions.VcenterObjectNotFoundException)21 VcenterServerConnectionException (com.emc.storageos.vcentercontroller.exceptions.VcenterServerConnectionException)21 VcenterSystemException (com.emc.storageos.vcentercontroller.exceptions.VcenterSystemException)21 HostSystem (com.vmware.vim25.mo.HostSystem)8 ClusterComputeResource (com.vmware.vim25.mo.ClusterComputeResource)7 Task (com.vmware.vim25.mo.Task)7 ManagedEntity (com.vmware.vim25.mo.ManagedEntity)4 Datacenter (com.vmware.vim25.mo.Datacenter)3 InventoryNavigator (com.vmware.vim25.mo.InventoryNavigator)3 ArrayList (java.util.ArrayList)3 HostScsiDisk (com.vmware.vim25.HostScsiDisk)2 HostStorageDeviceInfo (com.vmware.vim25.HostStorageDeviceInfo)2 ScsiLun (com.vmware.vim25.ScsiLun)2 VmfsDatastoreInfo (com.vmware.vim25.VmfsDatastoreInfo)2 Datastore (com.vmware.vim25.mo.Datastore)2 HostStorageSystem (com.vmware.vim25.mo.HostStorageSystem)2 VirtualMachine (com.vmware.vim25.mo.VirtualMachine)2 ClusterConfigSpecEx (com.vmware.vim25.ClusterConfigSpecEx)1 HostConfigFault (com.vmware.vim25.HostConfigFault)1