Search in sources :

Example 1 with VcenterSystemException

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

the class VcenterApiClient method createManagedEntityMap.

/*
     * Provide names of the vCenter elements and method will locate the MangedEntity representation
     * Each search is done within context of previous entity thus there is a dependency
     * Cluster
     * Datacenter
     * Host
     * Parameters are optional (ie, leave host null to only search datacenter and cluster)
     * Must provide parent element name or child will not be searched
     * hostConnectedPoweredOn ensures host is operational and ready for calls
     */
private Map<String, ManagedEntity> createManagedEntityMap(String datacenterName, String clusterNameOrMoRef, String hostname, boolean hostConnectedPoweredOn) throws VcenterSystemException, VcenterObjectNotFoundException, VcenterObjectConnectionException {
    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 {
        Map<String, ManagedEntity> vcenterManagedEntityMap = new HashMap<String, ManagedEntity>();
        if (datacenterName != null && !datacenterName.trim().equals("")) {
            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");
            }
            vcenterManagedEntityMap.put("Datacenter", datacenter);
            if (clusterNameOrMoRef != null && !clusterNameOrMoRef.trim().equals("")) {
                ClusterComputeResource clusterComputeResource = searchClusterComputeResource(datacenterName, clusterNameOrMoRef);
                vcenterManagedEntityMap.put("ClusterComputeResource", clusterComputeResource);
                if (hostname != null && !hostname.trim().equals("")) {
                    HostSystem hostSystem = findByHostname(clusterComputeResource, hostname);
                    if (hostSystem == null) {
                        _log.error("Host " + hostname + " does not exist");
                        throw new VcenterObjectNotFoundException("Host " + hostname + " does not exist");
                    }
                    if (hostConnectedPoweredOn) {
                        checkHostConnectedPoweredOn(hostSystem);
                    }
                    vcenterManagedEntityMap.put("HostSystem", hostSystem);
                }
            }
        }
        return vcenterManagedEntityMap;
    } catch (VcenterSystemException | VcenterObjectNotFoundException | VcenterObjectConnectionException e) {
        throw e;
    } catch (Exception e) {
        _log.error("getVcenterObjects exception " + e);
        throw new VcenterSystemException(e.getLocalizedMessage());
    }
}
Also used : ManagedEntity(com.vmware.vim25.mo.ManagedEntity) ClusterComputeResource(com.vmware.vim25.mo.ClusterComputeResource) HashMap(java.util.HashMap) VcenterObjectConnectionException(com.emc.storageos.vcentercontroller.exceptions.VcenterObjectConnectionException) 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) VcenterObjectNotFoundException(com.emc.storageos.vcentercontroller.exceptions.VcenterObjectNotFoundException) Datacenter(com.vmware.vim25.mo.Datacenter) HostSystem(com.vmware.vim25.mo.HostSystem) VcenterSystemException(com.emc.storageos.vcentercontroller.exceptions.VcenterSystemException)

Example 2 with VcenterSystemException

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

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

the class VcenterApiClient method checkVMsOnHostVolume.

/**
 * Verifies if the host can see the volume, if so then check for VMs (powered on/off)
 * on the datastore identified for the given volume wwn.
 *
 * @param datacenterName {@link String} name of the dataCenter
 * @param clusterNameOrMoRef {@link String} name of the cluster
 * @param hostName {@link String} name of the host
 * @param volumewwn {@link String} volume wwn
 * @return true when there are any VMs on the datastore identified by the volume wwn, otherwise false
 * @throws VcenterSystemException
 * @throws VcenterObjectNotFoundException
 * @throws VcenterObjectConnectionException
 */
public boolean checkVMsOnHostVolume(String datacenterName, String clusterNameOrMoRef, String hostName, String volumewwn) throws VcenterSystemException, VcenterObjectNotFoundException, VcenterObjectConnectionException {
    boolean isVMsPresent = false;
    try {
        _log.info("Request to check VMs on volume " + volumewwn + " host " + hostName + " to datacenter " + datacenterName + " cluster " + clusterNameOrMoRef);
        HostSystem hostSystem = (HostSystem) createManagedEntityMap(datacenterName, clusterNameOrMoRef, hostName, false).get("HostSystem");
        if (volumewwn == null || volumewwn.trim().equals("")) {
            _log.error("Volume UUID not specified");
            throw new VcenterSystemException("Volume UUID not specified");
        }
        Datastore[] datastores = hostSystem.getDatastores();
        if (datastores != null && datastores.length > 0) {
            _log.info("Found {} datastores for host {}", datastores.length, hostName);
            String specifiedVolumeDevicePath = null;
            HostStorageSystem hostStorageSystem = hostSystem.getHostStorageSystem();
            HostStorageDeviceInfo hostStorageDeviceInfo = hostStorageSystem.getStorageDeviceInfo();
            ScsiLun[] hostScsiLuns = hostStorageDeviceInfo.getScsiLun();
            for (ScsiLun scsiLun : hostScsiLuns) {
                if (scsiLun instanceof HostScsiDisk) {
                    HostScsiDisk hostScsiDisk = (HostScsiDisk) scsiLun;
                    // Check if host is able to see the specified volume
                    if (hostScsiDisk.getUuid().toLowerCase().contains(volumewwn.toLowerCase())) {
                        _log.info("Found disk " + hostScsiDisk.getUuid() + " on " + hostName + " for volume UUID " + volumewwn);
                        specifiedVolumeDevicePath = hostScsiDisk.getDevicePath();
                        break;
                    }
                }
            }
            if (specifiedVolumeDevicePath != null) {
                Datastore bootVolDatastore = null;
                for (Datastore datastore : datastores) {
                    _log.info("Find existing datastore on volume " + volumewwn + " for host " + hostName);
                    if (datastore.getInfo() instanceof VmfsDatastoreInfo) {
                        VmfsDatastoreInfo vmfsDatastoreInfo = (VmfsDatastoreInfo) datastore.getInfo();
                        String diskName = vmfsDatastoreInfo.getVmfs().getExtent()[0].getDiskName();
                        if (specifiedVolumeDevicePath.contains(diskName) && diskName.contains(volumewwn.toLowerCase())) {
                            _log.info("Found datastore " + vmfsDatastoreInfo.getName() + " on disk " + diskName);
                            bootVolDatastore = datastore;
                            break;
                        }
                    }
                }
                if (bootVolDatastore != null) {
                    if (CollectionUtils.isNotEmpty(Arrays.asList(bootVolDatastore.getVms()))) {
                        isVMsPresent = true;
                        _log.info("Found {} VMs on the datastore {}", bootVolDatastore.getVms().length, bootVolDatastore.getName());
                    } else {
                        _log.info("No VMs found on datastore {}", bootVolDatastore.getName());
                    }
                } else {
                    _log.warn("Could not find datastore for host {} on volume wwn {}", hostName, volumewwn);
                }
            } else {
                _log.warn("Could not find disk on host {} for volume UUID {}", hostName, volumewwn);
            }
        } else {
            _log.info("No datastores found for host {}, hence inferring that no VMs are present on the given volume wwn {}", hostName, volumewwn);
        }
    } catch (VcenterSystemException | VcenterObjectNotFoundException | VcenterObjectConnectionException e) {
        _log.error("Vcenter exception checkVMsOnHostVolume : {}", e);
        throw e;
    } catch (Exception e) {
        _log.error("Exception checkVMsOnHostVolume : {}", e);
        throw new VcenterSystemException(e.getLocalizedMessage());
    }
    return isVMsPresent;
}
Also used : VmfsDatastoreInfo(com.vmware.vim25.VmfsDatastoreInfo) ScsiLun(com.vmware.vim25.ScsiLun) VcenterObjectConnectionException(com.emc.storageos.vcentercontroller.exceptions.VcenterObjectConnectionException) HostStorageSystem(com.vmware.vim25.mo.HostStorageSystem) HostStorageDeviceInfo(com.vmware.vim25.HostStorageDeviceInfo) 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) VcenterObjectNotFoundException(com.emc.storageos.vcentercontroller.exceptions.VcenterObjectNotFoundException) Datastore(com.vmware.vim25.mo.Datastore) HostSystem(com.vmware.vim25.mo.HostSystem) HostScsiDisk(com.vmware.vim25.HostScsiDisk) VcenterSystemException(com.emc.storageos.vcentercontroller.exceptions.VcenterSystemException)

Example 4 with VcenterSystemException

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

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

the class VcenterApiClient method enterMaintenanceMode.

public void enterMaintenanceMode(String datacenterName, String clusterNameOrMoRef, String hostname) throws VcenterSystemException, VcenterObjectNotFoundException, VcenterObjectConnectionException {
    try {
        _log.info("Request to enter maintenance mode for host " + hostname + " in datacenter " + datacenterName + " cluster " + clusterNameOrMoRef);
        enterMaintenanceModeHost((HostSystem) createManagedEntityMap(datacenterName, clusterNameOrMoRef, hostname, true).get("HostSystem"));
    } catch (VcenterSystemException | VcenterObjectNotFoundException | VcenterObjectConnectionException e) {
        throw e;
    } catch (Exception e) {
        _log.error("enterMaintenanceMode exception " + e);
        throw new VcenterSystemException(e.getLocalizedMessage());
    }
}
Also used : VcenterObjectNotFoundException(com.emc.storageos.vcentercontroller.exceptions.VcenterObjectNotFoundException) 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)

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