bug 8493: Check for resource limit for copy templates/ISO between zones.

This commit is contained in:
nit 2011-02-09 15:46:21 +05:30
parent 3bd3dacf9e
commit 67a92e3f1d
5 changed files with 27 additions and 11 deletions

4
api/src/com/cloud/api/commands/CopyIsoCmd.java Normal file → Executable file
View File

@ -29,6 +29,7 @@ import com.cloud.api.ServerApiException;
import com.cloud.api.response.TemplateResponse;
import com.cloud.async.AsyncJob;
import com.cloud.event.EventTypes;
import com.cloud.exception.ResourceAllocationException;
import com.cloud.exception.StorageUnavailableException;
import com.cloud.template.VirtualMachineTemplate;
import com.cloud.user.Account;
@ -119,6 +120,9 @@ public class CopyIsoCmd extends BaseAsyncCmd {
} catch (StorageUnavailableException ex) {
s_logger.warn("Exception: ", ex);
throw new ServerApiException(BaseCmd.RESOURCE_UNAVAILABLE_ERROR, ex.getMessage());
} catch (ResourceAllocationException ex) {
s_logger.warn("Exception: ", ex);
throw new ServerApiException(BaseCmd.RESOURCE_ALLOCATION_ERROR, ex.getMessage());
}
}
}

4
api/src/com/cloud/api/commands/CopyTemplateCmd.java Normal file → Executable file
View File

@ -29,6 +29,7 @@ import com.cloud.api.ServerApiException;
import com.cloud.api.response.TemplateResponse;
import com.cloud.async.AsyncJob;
import com.cloud.event.EventTypes;
import com.cloud.exception.ResourceAllocationException;
import com.cloud.exception.StorageUnavailableException;
import com.cloud.template.VirtualMachineTemplate;
import com.cloud.user.Account;
@ -121,6 +122,9 @@ public class CopyTemplateCmd extends BaseAsyncCmd {
} catch (StorageUnavailableException ex) {
s_logger.warn("Exception: ", ex);
throw new ServerApiException(BaseCmd.RESOURCE_UNAVAILABLE_ERROR, ex.getMessage());
} catch (ResourceAllocationException ex) {
s_logger.warn("Exception: ", ex);
throw new ServerApiException(BaseCmd.RESOURCE_ALLOCATION_ERROR, ex.getMessage());
}
}
}

4
api/src/com/cloud/template/TemplateService.java Normal file → Executable file
View File

@ -39,8 +39,8 @@ public interface TemplateService {
VirtualMachineTemplate registerTemplate(RegisterTemplateCmd cmd) throws URISyntaxException, ResourceAllocationException;
VirtualMachineTemplate registerIso(RegisterIsoCmd cmd) throws IllegalArgumentException, ResourceAllocationException;
VirtualMachineTemplate copyIso(CopyIsoCmd cmd) throws StorageUnavailableException;
VirtualMachineTemplate copyTemplate(CopyTemplateCmd cmd) throws StorageUnavailableException;
VirtualMachineTemplate copyIso(CopyIsoCmd cmd) throws StorageUnavailableException, ResourceAllocationException;
VirtualMachineTemplate copyTemplate(CopyTemplateCmd cmd) throws StorageUnavailableException, ResourceAllocationException;
boolean detachIso(DetachIsoCmd cmd);
boolean attachIso(AttachIsoCmd cmd);
/**

View File

@ -22,6 +22,7 @@ import java.util.List;
import com.cloud.exception.InternalErrorException;
import com.cloud.exception.InvalidParameterValueException;
import com.cloud.exception.ResourceAllocationException;
import com.cloud.exception.StorageUnavailableException;
import com.cloud.storage.Storage.ImageFormat;
import com.cloud.storage.Storage.TemplateType;
@ -104,9 +105,10 @@ public interface TemplateManager {
throw new IllegalArgumentException("The template hasnt been downloaded ");
}
* @throws StorageUnavailableException
* @throws ResourceAllocationException
* @throws InvalidParameterValueException
*/
boolean copy(long userId, long templateId, long sourceZoneId, long destZoneId) throws StorageUnavailableException;
boolean copy(long userId, long templateId, long sourceZoneId, long destZoneId) throws StorageUnavailableException, ResourceAllocationException;
/**
* Deletes a template from secondary storage servers

View File

@ -700,7 +700,7 @@ public class TemplateManagerImpl implements TemplateManager, Manager, TemplateSe
@Override
@DB
public boolean copy(long userId, long templateId, long sourceZoneId, long destZoneId) throws StorageUnavailableException {
public boolean copy(long userId, long templateId, long sourceZoneId, long destZoneId) throws StorageUnavailableException, ResourceAllocationException {
HostVO srcSecHost = _storageMgr.getSecondaryStorageHost(sourceZoneId);
HostVO dstSecHost = _storageMgr.getSecondaryStorageHost(destZoneId);
DataCenterVO destZone = _dcDao.findById(destZoneId);
@ -733,8 +733,14 @@ public class TemplateManagerImpl implements TemplateManager, Manager, TemplateSe
throw new InvalidParameterValueException("Please specify a template that is installed on secondary storage host: " + srcSecHost.getName());
}
AccountVO account = _accountDao.findById(vmTemplate.getAccountId());
if (_accountMgr.resourceLimitExceeded(account, ResourceType.template)) {
ResourceAllocationException rae = new ResourceAllocationException("Maximum number of templates and ISOs for account: " + account.getAccountName() + " has been exceeded.");
rae.setResourceType("template");
throw rae;
}
// Event details
Account account = _accountDao.findById(vmTemplate.getAccountId());
String copyEventType;
String createEventType;
if (vmTemplate.getFormat().equals(ImageFormat.ISO)){
@ -788,7 +794,7 @@ public class TemplateManagerImpl implements TemplateManager, Manager, TemplateSe
}
@Override
public VirtualMachineTemplate copyIso(CopyIsoCmd cmd) throws StorageUnavailableException {
public VirtualMachineTemplate copyIso(CopyIsoCmd cmd) throws StorageUnavailableException, ResourceAllocationException {
Long isoId = cmd.getId();
Long userId = UserContext.current().getCallerUserId();
Long sourceZoneId = cmd.getSourceZoneId();
@ -822,7 +828,7 @@ public class TemplateManagerImpl implements TemplateManager, Manager, TemplateSe
@Override
public VirtualMachineTemplate copyTemplate(CopyTemplateCmd cmd) throws StorageUnavailableException {
public VirtualMachineTemplate copyTemplate(CopyTemplateCmd cmd) throws StorageUnavailableException, ResourceAllocationException {
Long templateId = cmd.getId();
Long userId = UserContext.current().getCallerUserId();
Long sourceZoneId = cmd.getSourceZoneId();