Search in sources :

Example 6 with ResourceInUseException

use of com.cloud.exception.ResourceInUseException in project cloudstack by apache.

the class AddClusterCmdTest method testExecuteForNullResult.

@Test
public void testExecuteForNullResult() {
    ResourceService resourceService = Mockito.mock(ResourceService.class);
    try {
        Mockito.when(resourceService.discoverCluster(addClusterCmd)).thenReturn(null);
    } catch (ResourceInUseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IllegalArgumentException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (DiscoveryException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    addClusterCmd._resourceService = resourceService;
    try {
        addClusterCmd.execute();
    } catch (ServerApiException exception) {
        Assert.assertEquals("Failed to add cluster", exception.getDescription());
    }
}
Also used : ServerApiException(org.apache.cloudstack.api.ServerApiException) ResourceInUseException(com.cloud.exception.ResourceInUseException) ResourceService(com.cloud.resource.ResourceService) DiscoveryException(com.cloud.exception.DiscoveryException) Test(org.junit.Test)

Example 7 with ResourceInUseException

use of com.cloud.exception.ResourceInUseException in project cloudstack by apache.

the class VmwareManagerImpl method addVmwareDatacenter.

@Override
@DB
public VmwareDatacenterVO addVmwareDatacenter(AddVmwareDcCmd cmd) throws ResourceInUseException {
    VmwareDatacenterVO vmwareDc = null;
    Long zoneId = cmd.getZoneId();
    String userName = cmd.getUsername();
    String password = cmd.getPassword();
    String vCenterHost = cmd.getVcenter();
    String vmwareDcName = cmd.getName();
    // Validate username, password, VMware DC name and vCenter
    if (userName == null) {
        throw new InvalidParameterValueException("Missing or invalid parameter username.");
    }
    if (password == null) {
        throw new InvalidParameterValueException("Missing or invalid parameter username.");
    }
    if (vmwareDcName == null) {
        throw new InvalidParameterValueException("Missing or invalid parameter name. Please provide valid VMware datacenter name.");
    }
    if (vCenterHost == null) {
        throw new InvalidParameterValueException("Missing or invalid parameter name. " + "Please provide valid VMware vCenter server's IP address or fully qualified domain name.");
    }
    if (zoneId == null) {
        throw new InvalidParameterValueException("Missing or invalid parameter name. " + "Please provide valid zone id.");
    }
    // Zone validation
    validateZone(zoneId);
    VmwareDatacenterZoneMapVO vmwareDcZoneMap = _vmwareDcZoneMapDao.findByZoneId(zoneId);
    // Check if zone is associated with VMware DC
    if (vmwareDcZoneMap != null) {
        // Check if the associated VMware DC matches the one specified in API params
        // This check would yield success as the association exists between same entities (zone and VMware DC)
        // This scenario would result in if the API addVmwareDc is called more than once with same parameters.
        Long associatedVmwareDcId = vmwareDcZoneMap.getVmwareDcId();
        VmwareDatacenterVO associatedVmwareDc = _vmwareDcDao.findById(associatedVmwareDcId);
        if (associatedVmwareDc.getVcenterHost().equalsIgnoreCase(vCenterHost) && associatedVmwareDc.getVmwareDatacenterName().equalsIgnoreCase(vmwareDcName)) {
            s_logger.info("Ignoring API call addVmwareDc, because VMware DC " + vCenterHost + "/" + vmwareDcName + " is already associated with specified zone with id " + zoneId);
            return associatedVmwareDc;
        } else {
            throw new CloudRuntimeException("Zone " + zoneId + " is already associated with a VMware datacenter. " + "Only 1 VMware DC can be associated with a zone.");
        }
    }
    // Zone validation to check if the zone already has resources.
    // Association of VMware DC to zone is not allowed if zone already has resources added.
    validateZoneWithResources(zoneId, "add VMware datacenter to zone");
    // Check if DC is already part of zone
    // In that case vmware_data_center table should have the DC
    vmwareDc = _vmwareDcDao.getVmwareDatacenterByGuid(vmwareDcName + "@" + vCenterHost);
    if (vmwareDc != null) {
        throw new ResourceInUseException("This DC is already part of other CloudStack zone(s). Cannot add this DC to more zones.");
    }
    VmwareContext context = null;
    DatacenterMO dcMo = null;
    String dcCustomFieldValue;
    boolean addDcCustomFieldDef = false;
    boolean dcInUse = false;
    String guid;
    ManagedObjectReference dcMor;
    try {
        context = VmwareContextFactory.create(vCenterHost, userName, password);
        // Check if DC exists on vCenter
        dcMo = new DatacenterMO(context, vmwareDcName);
        dcMor = dcMo.getMor();
        if (dcMor == null) {
            String msg = "Unable to find VMware DC " + vmwareDcName + " in vCenter " + vCenterHost + ". ";
            s_logger.error(msg);
            throw new InvalidParameterValueException(msg);
        }
        // Check if DC is already associated with another cloudstack deployment
        // Get custom field property cloud.zone over this DC
        guid = vmwareDcName + "@" + vCenterHost;
        dcCustomFieldValue = dcMo.getCustomFieldValue(CustomFieldConstants.CLOUD_ZONE);
        if (dcCustomFieldValue == null) {
            addDcCustomFieldDef = true;
        }
        dcInUse = Boolean.parseBoolean(dcCustomFieldValue);
        if (dcInUse) {
            throw new ResourceInUseException("This DC is being managed by other CloudStack deployment. Cannot add this DC to zone.");
        }
        // Add DC to database into vmware_data_center table
        vmwareDc = new VmwareDatacenterVO(guid, vmwareDcName, vCenterHost, userName, password);
        vmwareDc = _vmwareDcDao.persist(vmwareDc);
        // Map zone with vmware datacenter
        vmwareDcZoneMap = new VmwareDatacenterZoneMapVO(zoneId, vmwareDc.getId());
        vmwareDcZoneMap = _vmwareDcZoneMapDao.persist(vmwareDcZoneMap);
        // Set custom field for this DC
        if (addDcCustomFieldDef) {
            dcMo.ensureCustomFieldDef(CustomFieldConstants.CLOUD_ZONE);
        }
        dcMo.setCustomFieldValue(CustomFieldConstants.CLOUD_ZONE, "true");
    } catch (Throwable e) {
        String msg = "Failed to add VMware DC to zone ";
        if (e instanceof RemoteException) {
            msg = "Encountered remote exception at vCenter. " + VmwareHelper.getExceptionMessage(e);
        } else {
            msg += "due to : " + e.getMessage();
        }
        throw new CloudRuntimeException(msg);
    } finally {
        if (context != null) {
            context.close();
        }
        context = null;
    }
    return vmwareDc;
}
Also used : VmwareDatacenterZoneMapVO(com.cloud.hypervisor.vmware.VmwareDatacenterZoneMapVO) VmwareContext(com.cloud.hypervisor.vmware.util.VmwareContext) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) VmwareDatacenterVO(com.cloud.hypervisor.vmware.VmwareDatacenterVO) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) ResourceInUseException(com.cloud.exception.ResourceInUseException) RemoteException(java.rmi.RemoteException) DatacenterMO(com.cloud.hypervisor.vmware.mo.DatacenterMO) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference) DB(com.cloud.utils.db.DB)

Example 8 with ResourceInUseException

use of com.cloud.exception.ResourceInUseException in project cloudstack by apache.

the class VmwareManagerImpl method removeVmwareDatacenter.

@Override
public boolean removeVmwareDatacenter(RemoveVmwareDcCmd cmd) throws ResourceInUseException {
    Long zoneId = cmd.getZoneId();
    // Validate zone
    validateZone(zoneId);
    // Zone validation to check if the zone already has resources.
    // Association of VMware DC to zone is not allowed if zone already has resources added.
    validateZoneWithResources(zoneId, "remove VMware datacenter to zone");
    // Get DC associated with this zone
    VmwareDatacenterVO vmwareDatacenter;
    String vmwareDcName;
    String vCenterHost;
    String userName;
    String password;
    DatacenterMO dcMo = null;
    final VmwareDatacenterZoneMapVO vmwareDcZoneMap = _vmwareDcZoneMapDao.findByZoneId(zoneId);
    // Check if zone is associated with VMware DC
    if (vmwareDcZoneMap == null) {
        throw new CloudRuntimeException("Zone " + zoneId + " is not associated with any VMware datacenter.");
    }
    final long vmwareDcId = vmwareDcZoneMap.getVmwareDcId();
    vmwareDatacenter = _vmwareDcDao.findById(vmwareDcId);
    vmwareDcName = vmwareDatacenter.getVmwareDatacenterName();
    vCenterHost = vmwareDatacenter.getVcenterHost();
    userName = vmwareDatacenter.getUser();
    password = vmwareDatacenter.getPassword();
    Transaction.execute(new TransactionCallbackNoReturn() {

        @Override
        public void doInTransactionWithoutResult(TransactionStatus status) {
            // Remove the VMware datacenter entry in table vmware_data_center
            _vmwareDcDao.remove(vmwareDcId);
            // Remove the map entry in table vmware_data_center_zone_map
            _vmwareDcZoneMapDao.remove(vmwareDcZoneMap.getId());
        }
    });
    // Construct context
    VmwareContext context = null;
    try {
        context = VmwareContextFactory.create(vCenterHost, userName, password);
        // Check if DC exists on vCenter
        try {
            dcMo = new DatacenterMO(context, vmwareDcName);
        } catch (Throwable t) {
            String msg = "Unable to find DC " + vmwareDcName + " in vCenter " + vCenterHost;
            s_logger.error(msg);
            throw new DiscoveryException(msg);
        }
        assert (dcMo != null);
        // Reset custom field property cloud.zone over this DC
        dcMo.setCustomFieldValue(CustomFieldConstants.CLOUD_ZONE, "false");
        s_logger.info("Sucessfully reset custom field property cloud.zone over DC " + vmwareDcName);
    } catch (Exception e) {
        String msg = "Unable to reset custom field property cloud.zone over DC " + vmwareDcName + " due to : " + VmwareHelper.getExceptionMessage(e);
        s_logger.error(msg);
        throw new CloudRuntimeException(msg);
    } finally {
        if (context != null) {
            context.close();
        }
        context = null;
    }
    return true;
}
Also used : VmwareDatacenterZoneMapVO(com.cloud.hypervisor.vmware.VmwareDatacenterZoneMapVO) TransactionStatus(com.cloud.utils.db.TransactionStatus) TransactionCallbackNoReturn(com.cloud.utils.db.TransactionCallbackNoReturn) DiscoveredWithErrorException(com.cloud.exception.DiscoveredWithErrorException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) IOException(java.io.IOException) ResourceInUseException(com.cloud.exception.ResourceInUseException) URISyntaxException(java.net.URISyntaxException) DiscoveryException(com.cloud.exception.DiscoveryException) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) RemoteException(java.rmi.RemoteException) ConfigurationException(javax.naming.ConfigurationException) VmwareContext(com.cloud.hypervisor.vmware.util.VmwareContext) VmwareDatacenterVO(com.cloud.hypervisor.vmware.VmwareDatacenterVO) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) DiscoveryException(com.cloud.exception.DiscoveryException) DatacenterMO(com.cloud.hypervisor.vmware.mo.DatacenterMO)

Example 9 with ResourceInUseException

use of com.cloud.exception.ResourceInUseException in project cloudstack by apache.

the class NetappManagerImpl method destroyVolumeOnFiler.

/**
     * This method destroys the volume on netapp filer
     * @param ipAddress -- ip address of filer
     * @param aggrName -- name of containing aggregate
     * @param volName -- name of volume to destroy
     * @throws ResourceInUseException
     * @throws NaException
     * @throws NaAPIFailedException
     */
@Override
@DB
public void destroyVolumeOnFiler(String ipAddress, String aggrName, String volName) throws ServerException, InvalidParameterValueException, ResourceInUseException {
    NaElement xi0;
    NaElement xi1;
    NetappVolumeVO volume = null;
    volume = _volumeDao.findVolume(ipAddress, aggrName, volName);
    if (volume == null) {
        s_logger.warn("The volume does not exist in our system");
        throw new InvalidParameterValueException("The given tuple:" + ipAddress + "," + aggrName + "," + volName + " doesn't exist in our system");
    }
    List<LunVO> lunsOnVol = _lunDao.listLunsByVolId(volume.getId());
    if (lunsOnVol != null && lunsOnVol.size() > 0) {
        s_logger.warn("There are luns on the volume");
        throw new ResourceInUseException("There are luns on the volume");
    }
    final TransactionLegacy txn = TransactionLegacy.currentTxn();
    txn.start();
    PoolVO pool = _poolDao.findById(volume.getPoolId());
    if (pool == null) {
        throw new InvalidParameterValueException("Failed to find pool for given volume");
    //FIXME: choose a better exception. this is a db integrity exception
    }
    pool = _poolDao.acquireInLockTable(pool.getId());
    if (pool == null) {
        throw new ConcurrentModificationException("Failed to acquire lock on pool " + volume.getPoolId());
    }
    NaServer s = null;
    try {
        s = getServer(volume.getIpAddress(), volume.getUsername(), volume.getPassword());
        //bring the volume down
        xi0 = new NaElement("volume-offline");
        xi0.addNewChild("name", volName);
        s.invokeElem(xi0);
        //now destroy it
        xi1 = new NaElement("volume-destroy");
        xi1.addNewChild("name", volName);
        s.invokeElem(xi1);
        //now delete from our records
        _volumeDao.remove(volume.getId());
        txn.commit();
    } catch (UnknownHostException uhe) {
        s_logger.warn("Unable to delete volume on filer ", uhe);
        throw new ServerException("Unable to delete volume on filer", uhe);
    } catch (NaAPIFailedException naf) {
        s_logger.warn("Unable to delete volume on filer ", naf);
        if (naf.getErrno() == 13040) {
            s_logger.info("Deleting the volume: " + volName);
            _volumeDao.remove(volume.getId());
            txn.commit();
        }
        throw new ServerException("Unable to delete volume on filer", naf);
    } catch (NaException nae) {
        txn.rollback();
        s_logger.warn("Unable to delete volume on filer ", nae);
        throw new ServerException("Unable to delete volume on filer", nae);
    } catch (IOException ioe) {
        txn.rollback();
        s_logger.warn("Unable to delete volume on filer ", ioe);
        throw new ServerException("Unable to delete volume on filer", ioe);
    } finally {
        if (pool != null) {
            _poolDao.releaseFromLockTable(pool.getId());
        }
        if (s != null)
            s.close();
    }
}
Also used : ConcurrentModificationException(java.util.ConcurrentModificationException) ServerException(java.rmi.ServerException) UnknownHostException(java.net.UnknownHostException) NaServer(netapp.manage.NaServer) IOException(java.io.IOException) NaElement(netapp.manage.NaElement) TransactionLegacy(com.cloud.utils.db.TransactionLegacy) NaAPIFailedException(netapp.manage.NaAPIFailedException) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) ResourceInUseException(com.cloud.exception.ResourceInUseException) NaException(netapp.manage.NaException) DB(com.cloud.utils.db.DB)

Example 10 with ResourceInUseException

use of com.cloud.exception.ResourceInUseException in project cloudstack by apache.

the class AddClusterCmd method execute.

@Override
public void execute() {
    try {
        List<? extends Cluster> result = _resourceService.discoverCluster(this);
        ListResponse<ClusterResponse> response = new ListResponse<ClusterResponse>();
        List<ClusterResponse> clusterResponses = new ArrayList<ClusterResponse>();
        if (result != null && result.size() > 0) {
            for (Cluster cluster : result) {
                ClusterResponse clusterResponse = _responseGenerator.createClusterResponse(cluster, false);
                clusterResponses.add(clusterResponse);
            }
        } else {
            throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to add cluster");
        }
        response.setResponses(clusterResponses);
        response.setResponseName(getCommandName());
        this.setResponseObject(response);
    } catch (DiscoveryException ex) {
        s_logger.warn("Exception: ", ex);
        throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, ex.getMessage());
    } catch (ResourceInUseException ex) {
        s_logger.warn("Exception: ", ex);
        ServerApiException e = new ServerApiException(ApiErrorCode.INTERNAL_ERROR, ex.getMessage());
        for (String proxyObj : ex.getIdProxyList()) {
            e.addProxyObject(proxyObj);
        }
        throw e;
    }
}
Also used : ListResponse(org.apache.cloudstack.api.response.ListResponse) ServerApiException(org.apache.cloudstack.api.ServerApiException) ResourceInUseException(com.cloud.exception.ResourceInUseException) ArrayList(java.util.ArrayList) ClusterResponse(org.apache.cloudstack.api.response.ClusterResponse) Cluster(com.cloud.org.Cluster) DiscoveryException(com.cloud.exception.DiscoveryException)

Aggregations

ResourceInUseException (com.cloud.exception.ResourceInUseException)22 ServerApiException (org.apache.cloudstack.api.ServerApiException)10 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)9 DiscoveryException (com.cloud.exception.DiscoveryException)8 DB (com.cloud.utils.db.DB)6 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)6 ArrayList (java.util.ArrayList)4 ConfigurationException (javax.naming.ConfigurationException)4 ServerApiException (com.cloud.api.ServerApiException)3 ClusterVO (com.cloud.dc.ClusterVO)3 DataCenterVO (com.cloud.dc.DataCenterVO)3 ResourceUnavailableException (com.cloud.exception.ResourceUnavailableException)3 HostVO (com.cloud.host.HostVO)3 VmwareDatacenterVO (com.cloud.hypervisor.vmware.VmwareDatacenterVO)3 VmwareContext (com.cloud.hypervisor.vmware.util.VmwareContext)3 Cluster (com.cloud.org.Cluster)3 TransactionStatus (com.cloud.utils.db.TransactionStatus)3 UnknownHostException (java.net.UnknownHostException)3 SuccessResponse (org.apache.cloudstack.api.response.SuccessResponse)3 ClusterVSMMapVO (com.cloud.dc.ClusterVSMMapVO)2