Skip to content

Commit adf6b58

Browse files
committed
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
1 parent 3006b16 commit adf6b58

File tree

3 files changed

+52
-22
lines changed

3 files changed

+52
-22
lines changed

api/src/com/cloud/template/TemplateApiService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public interface TemplateApiService {
5151

5252
VirtualMachineTemplate copyTemplate(CopyTemplateCmd cmd) throws StorageUnavailableException, ResourceAllocationException;
5353

54-
VirtualMachineTemplate prepareTemplate(long templateId, long zoneId);
54+
VirtualMachineTemplate prepareTemplate(long templateId, long zoneId, Long storageId);
5555

5656
boolean detachIso(long vmId);
5757

api/src/org/apache/cloudstack/api/command/admin/template/PrepareTemplateCmd.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.apache.cloudstack.api.Parameter;
2929
import org.apache.cloudstack.api.ResponseObject.ResponseView;
3030
import org.apache.cloudstack.api.response.ListResponse;
31+
import org.apache.cloudstack.api.response.StoragePoolResponse;
3132
import org.apache.cloudstack.api.response.TemplateResponse;
3233
import org.apache.cloudstack.api.response.ZoneResponse;
3334

@@ -60,6 +61,15 @@ public class PrepareTemplateCmd extends BaseCmd {
6061
description = "template ID of the template to be prepared in primary storage(s).")
6162
private Long templateId;
6263

64+
@ACL(accessType = AccessType.OperateEntry)
65+
@Parameter(name = ApiConstants.STORAGE_ID,
66+
type = CommandType.UUID,
67+
entityType = StoragePoolResponse.class,
68+
required = false,
69+
description = "storage pool ID of the primary storage pool to which the template should be prepared. If it is not provided the template" +
70+
" is prepared on all the available primary storage pools.")
71+
private Long storageId;
72+
6373
/////////////////////////////////////////////////////
6474
/////////////////// Accessors ///////////////////////
6575
/////////////////////////////////////////////////////
@@ -72,6 +82,10 @@ public Long getTemplateId() {
7282
return templateId;
7383
}
7484

85+
public Long getStorageId() {
86+
return storageId;
87+
}
88+
7589
/////////////////////////////////////////////////////
7690
/////////////// API Implementation///////////////////
7791
/////////////////////////////////////////////////////
@@ -90,7 +104,7 @@ public long getEntityOwnerId() {
90104
public void execute() {
91105
ListResponse<TemplateResponse> response = new ListResponse<TemplateResponse>();
92106

93-
VirtualMachineTemplate vmTemplate = _templateService.prepareTemplate(templateId, zoneId);
107+
VirtualMachineTemplate vmTemplate = _templateService.prepareTemplate(templateId, zoneId, storageId);
94108
List<TemplateResponse> templateResponses = _responseGenerator.createTemplateResponses(ResponseView.Full, vmTemplate, zoneId, true);
95109
response.setResponses(templateResponses);
96110
response.setResponseName(getCommandName());

server/src/com/cloud/template/TemplateManagerImpl.java

Lines changed: 36 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,7 @@ public String extract(ExtractTemplateCmd cmd) {
436436
}
437437

438438
@Override
439-
public VirtualMachineTemplate prepareTemplate(long templateId, long zoneId) {
439+
public VirtualMachineTemplate prepareTemplate(long templateId, long zoneId, Long storageId) {
440440

441441
VMTemplateVO vmTemplate = _tmpltDao.findById(templateId);
442442
if (vmTemplate == null) {
@@ -445,7 +445,19 @@ public VirtualMachineTemplate prepareTemplate(long templateId, long zoneId) {
445445

446446
_accountMgr.checkAccess(CallContext.current().getCallingAccount(), AccessType.OperateEntry, true, vmTemplate);
447447

448-
prepareTemplateInAllStoragePools(vmTemplate, zoneId);
448+
if (storageId != null) {
449+
StoragePoolVO pool = _poolDao.findById(storageId);
450+
if (pool != null) {
451+
if (pool.getStatus() == StoragePoolStatus.Up && pool.getDataCenterId() == zoneId) {
452+
prepareTemplateInOneStoragePool(vmTemplate, pool);
453+
} else {
454+
s_logger.warn("Skip loading template " + vmTemplate.getId() + " into primary storage " + pool.getId() + " as either the pool zone "
455+
+ pool.getDataCenterId() + " is different from the requested zone " + zoneId + " or the pool is currently not available.");
456+
}
457+
}
458+
} else {
459+
prepareTemplateInAllStoragePools(vmTemplate, zoneId);
460+
}
449461
return vmTemplate;
450462
}
451463

@@ -556,28 +568,32 @@ public void prepareIsoForVmProfile(VirtualMachineProfile profile) {
556568
}
557569
}
558570

571+
private void prepareTemplateInOneStoragePool(final VMTemplateVO template, final StoragePoolVO pool) {
572+
s_logger.info("Schedule to preload template " + template.getId() + " into primary storage " + pool.getId());
573+
_preloadExecutor.execute(new ManagedContextRunnable() {
574+
@Override
575+
protected void runInContext() {
576+
try {
577+
reallyRun();
578+
} catch (Throwable e) {
579+
s_logger.warn("Unexpected exception ", e);
580+
}
581+
}
582+
583+
private void reallyRun() {
584+
s_logger.info("Start to preload template " + template.getId() + " into primary storage " + pool.getId());
585+
StoragePool pol = (StoragePool)_dataStoreMgr.getPrimaryDataStore(pool.getId());
586+
prepareTemplateForCreate(template, pol);
587+
s_logger.info("End of preloading template " + template.getId() + " into primary storage " + pool.getId());
588+
}
589+
});
590+
}
591+
559592
public void prepareTemplateInAllStoragePools(final VMTemplateVO template, long zoneId) {
560593
List<StoragePoolVO> pools = _poolDao.listByStatus(StoragePoolStatus.Up);
561594
for (final StoragePoolVO pool : pools) {
562595
if (pool.getDataCenterId() == zoneId) {
563-
s_logger.info("Schedule to preload template " + template.getId() + " into primary storage " + pool.getId());
564-
_preloadExecutor.execute(new ManagedContextRunnable() {
565-
@Override
566-
protected void runInContext() {
567-
try {
568-
reallyRun();
569-
} catch (Throwable e) {
570-
s_logger.warn("Unexpected exception ", e);
571-
}
572-
}
573-
574-
private void reallyRun() {
575-
s_logger.info("Start to preload template " + template.getId() + " into primary storage " + pool.getId());
576-
StoragePool pol = (StoragePool)_dataStoreMgr.getPrimaryDataStore(pool.getId());
577-
prepareTemplateForCreate(template, pol);
578-
s_logger.info("End of preloading template " + template.getId() + " into primary storage " + pool.getId());
579-
}
580-
});
596+
prepareTemplateInOneStoragePool(template, pool);
581597
} else {
582598
s_logger.info("Skip loading template " + template.getId() + " into primary storage " + pool.getId() + " as pool zone " + pool.getDataCenterId() +
583599
" is different from the requested zone " + zoneId);

0 commit comments

Comments
 (0)