Skip to content

Commit b2b01bf

Browse files
author
prachi
committed
Bug 8830 - Return parent template ID from templates created off of a snapshot
Changes: - Added a new column `source_template_id` to vm_template table to carry the parent/source template ID from which the tempalte was created - Added the column in db upgrade 224 to 225 - Changed code to save the source_template_id if there is one associated to the volume/ volume from which the snapshot was taken - API response returns the sourcetemplateid field, if set, in all template usecases.
1 parent b636462 commit b2b01bf

7 files changed

Lines changed: 49 additions & 8 deletions

File tree

api/src/com/cloud/api/response/TemplateResponse.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,9 @@ public class TemplateResponse extends BaseResponse {
110110
@SerializedName("checksum") @Param(description="checksum of the template")
111111
private String checksum;
112112

113+
@SerializedName("sourcetemplateid") @Param(description="the template ID of the parent template if present")
114+
private Long sourcetemplateId;
115+
113116
public Long getObjectId() {
114117
return getId();
115118
}
@@ -336,5 +339,13 @@ public String getChecksum() {
336339

337340
public void setChecksum(String checksum) {
338341
this.checksum = checksum;
339-
}
342+
}
343+
344+
public Long getSourceTemplateId() {
345+
return sourcetemplateId;
346+
}
347+
348+
public void setSourceTemplateId(Long sourcetemplateId) {
349+
this.sourcetemplateId = sourcetemplateId;
350+
}
340351
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,4 +83,6 @@ public enum TemplateFilter {
8383
String getUrl();
8484

8585
String getChecksum();
86+
87+
Long getSourceTemplateId();
8688
}

core/src/com/cloud/storage/VMTemplateVO.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,10 @@ public class VMTemplateVO implements VirtualMachineTemplate {
112112
@Column(name="extractable")
113113
private boolean extractable = true;
114114

115-
@Override
115+
@Column(name="source_template_id")
116+
private Long sourceTemplateId;
117+
118+
@Override
116119
public String getUniqueName() {
117120
return uniqueName;
118121
}
@@ -355,6 +358,15 @@ public boolean isExtractable() {
355358
public void setExtractable(boolean extractable) {
356359
this.extractable = extractable;
357360
}
361+
362+
@Override
363+
public Long getSourceTemplateId() {
364+
return sourceTemplateId;
365+
}
366+
367+
public void setSourceTemplateId(Long sourceTemplateId) {
368+
this.sourceTemplateId = sourceTemplateId;
369+
}
358370

359371
@Override
360372
public long getDomainId() {

server/src/com/cloud/api/ApiResponseHelper.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1519,7 +1519,8 @@ public void createTemplateResponse(List<TemplateResponse> responses, Pair<Long,
15191519
}
15201520

15211521
templateResponse.setChecksum(template.getChecksum());
1522-
1522+
templateResponse.setSourceTemplateId(template.getSourceTemplateId());
1523+
15231524
templateResponse.setObjectName("template");
15241525
responses.add(templateResponse);
15251526
}
@@ -1580,7 +1581,8 @@ public ListResponse<TemplateResponse> createTemplateResponse2(VirtualMachineTemp
15801581
templateResponse.setHypervisor(template.getHypervisorType().toString());
15811582
templateResponse.setObjectName("template");
15821583
templateResponse.setChecksum(template.getChecksum());
1583-
1584+
templateResponse.setSourceTemplateId(template.getSourceTemplateId());
1585+
15841586
responses.add(templateResponse);
15851587
}
15861588
response.setResponses(responses);
@@ -1741,6 +1743,7 @@ public TemplateResponse createTemplateResponse(VirtualMachineTemplate template,
17411743
templateResponse.setPasswordEnabled(template.getEnablePassword());
17421744
templateResponse.setZoneId(destZoneId);
17431745
templateResponse.setZoneName(ApiDBUtils.findZoneById(destZoneId).getName());
1746+
templateResponse.setSourceTemplateId(template.getSourceTemplateId());
17441747

17451748
GuestOS os = ApiDBUtils.findGuestOSById(template.getGuestOSId());
17461749
if (os != null) {
@@ -1942,6 +1945,8 @@ public TemplateResponse createTemplateResponse(VirtualMachineTemplate template,
19421945
response.setZoneName(zone.getName());
19431946
}
19441947

1948+
response.setSourceTemplateId(template.getSourceTemplateId());
1949+
19451950
response.setObjectName("template");
19461951
return response;
19471952

server/src/com/cloud/vm/UserVmManagerImpl.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1374,12 +1374,20 @@ public VMTemplateVO createPrivateTemplateRecord(CreateTemplateCmd cmd) throws Re
13741374
Long nextTemplateId = _templateDao.getNextInSequence(Long.class, "id");
13751375
String description = cmd.getDisplayText();
13761376
boolean isExtractable = false;
1377+
Long sourceTemplateId = null;
13771378
if (volume != null) {
13781379
VMTemplateVO template = ApiDBUtils.findTemplateById(volume.getTemplateId());
13791380
isExtractable = template != null && template.isExtractable() && template.getTemplateType() != Storage.TemplateType.SYSTEM;
1381+
sourceTemplateId = template.getId();
13801382
}
13811383
privateTemplate = new VMTemplateVO(nextTemplateId, uniqueName, name, ImageFormat.RAW, isPublic, featured, isExtractable, TemplateType.USER, null, null, requiresHvmValue, bitsValue, accountId,
13821384
null, description, passwordEnabledValue, guestOS.getId(), true, hyperType);
1385+
if(sourceTemplateId != null){
1386+
if(s_logger.isDebugEnabled()){
1387+
s_logger.debug("This template is getting created from other template, setting source template Id to: "+sourceTemplateId);
1388+
}
1389+
}
1390+
privateTemplate.setSourceTemplateId(sourceTemplateId);
13831391

13841392
VMTemplateVO template = _templateDao.persist(privateTemplate);
13851393
// Increment the number of templates
@@ -1954,7 +1962,7 @@ protected boolean validPassword(String password) {
19541962
@Override
19551963
public UserVm createBasicSecurityGroupVirtualMachine(DataCenter zone, ServiceOffering serviceOffering, VirtualMachineTemplate template, List<Long> securityGroupIdList, Account owner,
19561964
String hostName, String displayName, Long diskOfferingId, Long diskSize, String group, HypervisorType hypervisor, String userData, String sshKeyPair, Host destinationHost)
1957-
throws InsufficientCapacityException, ConcurrentOperationException, ResourceUnavailableException, StorageUnavailableException, ResourceAllocationException {
1965+
throws InsufficientCapacityException, ConcurrentOperationException, ResourceUnavailableException, StorageUnavailableException, ResourceAllocationException {
19581966

19591967
Account caller = UserContext.current().getCaller();
19601968
List<NetworkVO> networkList = new ArrayList<NetworkVO>();
@@ -2052,7 +2060,7 @@ public UserVm createAdvancedSecurityGroupVirtualMachine(DataCenter zone, Service
20522060
@Override
20532061
public UserVm createAdvancedVirtualMachine(DataCenter zone, ServiceOffering serviceOffering, VirtualMachineTemplate template, List<Long> networkIdList, Account owner, String hostName,
20542062
String displayName, Long diskOfferingId, Long diskSize, String group, HypervisorType hypervisor, String userData, String sshKeyPair, Host destinationHost)
2055-
throws InsufficientCapacityException, ConcurrentOperationException, ResourceUnavailableException, StorageUnavailableException, ResourceAllocationException {
2063+
throws InsufficientCapacityException, ConcurrentOperationException, ResourceUnavailableException, StorageUnavailableException, ResourceAllocationException {
20562064

20572065
Account caller = UserContext.current().getCaller();
20582066
List<NetworkVO> networkList = new ArrayList<NetworkVO>();
@@ -2397,7 +2405,7 @@ public UserVm startVirtualMachine(DeployVMCmd cmd) throws ResourceUnavailableExc
23972405
}
23982406

23992407
protected UserVm startVirtualMachine(DeployVMCmd cmd, Map<VirtualMachineProfile.Param, Object> additonalParams) throws ResourceUnavailableException, InsufficientCapacityException,
2400-
ConcurrentOperationException {
2408+
ConcurrentOperationException {
24012409
long vmId = cmd.getEntityId();
24022410
UserVmVO vm = _vmDao.findById(vmId);
24032411
_vmDao.loadDetails(vm);
@@ -2955,7 +2963,7 @@ public HypervisorType getHypervisorTypeOfUserVM(long vmid) {
29552963

29562964
@Override
29572965
public UserVm createVirtualMachine(DeployVMCmd cmd) throws InsufficientCapacityException, ResourceUnavailableException, ConcurrentOperationException, StorageUnavailableException,
2958-
ResourceAllocationException {
2966+
ResourceAllocationException {
29592967
// TODO Auto-generated method stub
29602968
return null;
29612969
}

setup/db/create-schema.sql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -785,6 +785,7 @@ CREATE TABLE `cloud`.`vm_template` (
785785
`cross_zones` int(1) unsigned NOT NULL default 0 COMMENT 'Make this template available in all zones',
786786
`extractable` int(1) unsigned NOT NULL default 0 COMMENT 'Is this template extractable',
787787
`hypervisor_type` varchar(32) COMMENT 'hypervisor that the template is belonged to',
788+
`source_template_id` bigint unsigned COMMENT 'Id of the original template, if this template is created from snapshot',
788789
PRIMARY KEY (`id`)
789790
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
790791

setup/db/db/schema-224to225.sql

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,5 @@ ALTER TABLE `cloud`.`security_ingress_rule` DROP COLUMN `allowed_sec_grp_acct`;
4343

4444
ALTER TABLE `cloud`.`data_center` ADD COLUMN `zone_token` varchar(255);
4545
ALTER TABLE `cloud`.`data_center` ADD INDEX `i_data_center__zone_token`(`zone_token`);
46+
47+
ALTER TABLE `cloud`.`vm_template` ADD COLUMN `source_template_id` bigint unsigned COMMENT 'Id of the original template, if this template is created from snapshot';

0 commit comments

Comments
 (0)