CLOUDSTACK-8687: Update prepare template api to seed/prepare a template

only on a given primary storage. Currently, the prepare template api
will seed/prepare a given template on all the primary storage pools in
a zone. If however, a user wishes to prepare a template only a
particular storage pool, it isn't possible. Updated the api to take
storage pool id as an optional parameter. If the pool id is provided
then the template is prepared only on the given primary storage pool
This commit is contained in:
Devdeep Singh 2015-07-28 12:05:35 +05:30
parent 3006b1614d
commit adf6b588dd
3 changed files with 52 additions and 22 deletions

View File

@ -51,7 +51,7 @@ public interface TemplateApiService {
VirtualMachineTemplate copyTemplate(CopyTemplateCmd cmd) throws StorageUnavailableException, ResourceAllocationException;
VirtualMachineTemplate prepareTemplate(long templateId, long zoneId);
VirtualMachineTemplate prepareTemplate(long templateId, long zoneId, Long storageId);
boolean detachIso(long vmId);

View File

@ -28,6 +28,7 @@ import org.apache.cloudstack.api.BaseCmd;
import org.apache.cloudstack.api.Parameter;
import org.apache.cloudstack.api.ResponseObject.ResponseView;
import org.apache.cloudstack.api.response.ListResponse;
import org.apache.cloudstack.api.response.StoragePoolResponse;
import org.apache.cloudstack.api.response.TemplateResponse;
import org.apache.cloudstack.api.response.ZoneResponse;
@ -60,6 +61,15 @@ public class PrepareTemplateCmd extends BaseCmd {
description = "template ID of the template to be prepared in primary storage(s).")
private Long templateId;
@ACL(accessType = AccessType.OperateEntry)
@Parameter(name = ApiConstants.STORAGE_ID,
type = CommandType.UUID,
entityType = StoragePoolResponse.class,
required = false,
description = "storage pool ID of the primary storage pool to which the template should be prepared. If it is not provided the template" +
" is prepared on all the available primary storage pools.")
private Long storageId;
/////////////////////////////////////////////////////
/////////////////// Accessors ///////////////////////
/////////////////////////////////////////////////////
@ -72,6 +82,10 @@ public class PrepareTemplateCmd extends BaseCmd {
return templateId;
}
public Long getStorageId() {
return storageId;
}
/////////////////////////////////////////////////////
/////////////// API Implementation///////////////////
/////////////////////////////////////////////////////
@ -90,7 +104,7 @@ public class PrepareTemplateCmd extends BaseCmd {
public void execute() {
ListResponse<TemplateResponse> response = new ListResponse<TemplateResponse>();
VirtualMachineTemplate vmTemplate = _templateService.prepareTemplate(templateId, zoneId);
VirtualMachineTemplate vmTemplate = _templateService.prepareTemplate(templateId, zoneId, storageId);
List<TemplateResponse> templateResponses = _responseGenerator.createTemplateResponses(ResponseView.Full, vmTemplate, zoneId, true);
response.setResponses(templateResponses);
response.setResponseName(getCommandName());

View File

@ -436,7 +436,7 @@ public class TemplateManagerImpl extends ManagerBase implements TemplateManager,
}
@Override
public VirtualMachineTemplate prepareTemplate(long templateId, long zoneId) {
public VirtualMachineTemplate prepareTemplate(long templateId, long zoneId, Long storageId) {
VMTemplateVO vmTemplate = _tmpltDao.findById(templateId);
if (vmTemplate == null) {
@ -445,7 +445,19 @@ public class TemplateManagerImpl extends ManagerBase implements TemplateManager,
_accountMgr.checkAccess(CallContext.current().getCallingAccount(), AccessType.OperateEntry, true, vmTemplate);
prepareTemplateInAllStoragePools(vmTemplate, zoneId);
if (storageId != null) {
StoragePoolVO pool = _poolDao.findById(storageId);
if (pool != null) {
if (pool.getStatus() == StoragePoolStatus.Up && pool.getDataCenterId() == zoneId) {
prepareTemplateInOneStoragePool(vmTemplate, pool);
} else {
s_logger.warn("Skip loading template " + vmTemplate.getId() + " into primary storage " + pool.getId() + " as either the pool zone "
+ pool.getDataCenterId() + " is different from the requested zone " + zoneId + " or the pool is currently not available.");
}
}
} else {
prepareTemplateInAllStoragePools(vmTemplate, zoneId);
}
return vmTemplate;
}
@ -556,28 +568,32 @@ public class TemplateManagerImpl extends ManagerBase implements TemplateManager,
}
}
private void prepareTemplateInOneStoragePool(final VMTemplateVO template, final StoragePoolVO pool) {
s_logger.info("Schedule to preload template " + template.getId() + " into primary storage " + pool.getId());
_preloadExecutor.execute(new ManagedContextRunnable() {
@Override
protected void runInContext() {
try {
reallyRun();
} catch (Throwable e) {
s_logger.warn("Unexpected exception ", e);
}
}
private void reallyRun() {
s_logger.info("Start to preload template " + template.getId() + " into primary storage " + pool.getId());
StoragePool pol = (StoragePool)_dataStoreMgr.getPrimaryDataStore(pool.getId());
prepareTemplateForCreate(template, pol);
s_logger.info("End of preloading template " + template.getId() + " into primary storage " + pool.getId());
}
});
}
public void prepareTemplateInAllStoragePools(final VMTemplateVO template, long zoneId) {
List<StoragePoolVO> pools = _poolDao.listByStatus(StoragePoolStatus.Up);
for (final StoragePoolVO pool : pools) {
if (pool.getDataCenterId() == zoneId) {
s_logger.info("Schedule to preload template " + template.getId() + " into primary storage " + pool.getId());
_preloadExecutor.execute(new ManagedContextRunnable() {
@Override
protected void runInContext() {
try {
reallyRun();
} catch (Throwable e) {
s_logger.warn("Unexpected exception ", e);
}
}
private void reallyRun() {
s_logger.info("Start to preload template " + template.getId() + " into primary storage " + pool.getId());
StoragePool pol = (StoragePool)_dataStoreMgr.getPrimaryDataStore(pool.getId());
prepareTemplateForCreate(template, pol);
s_logger.info("End of preloading template " + template.getId() + " into primary storage " + pool.getId());
}
});
prepareTemplateInOneStoragePool(template, pool);
} else {
s_logger.info("Skip loading template " + template.getId() + " into primary storage " + pool.getId() + " as pool zone " + pool.getDataCenterId() +
" is different from the requested zone " + zoneId);