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());
}
}
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;
}
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;
}
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();
}
}
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;
}
}
Aggregations