Skip to content

Commit f008118

Browse files
nvazquezDaanHoogland
authored andcommitted
CLOUDSTACK-10250: Refactor
1 parent a2235d1 commit f008118

1 file changed

Lines changed: 22 additions & 12 deletions

File tree

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

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6102,18 +6102,7 @@ public UserVm restoreVMInternal(Account caller, UserVmVO vm, Long newTemplateId)
61026102
}
61036103
}
61046104

6105-
TemplateDataStoreVO tmplStore;
6106-
if (!template.isDirectDownload()) {
6107-
tmplStore = _templateStoreDao.findByTemplateZoneReady(template.getId(), vm.getDataCenterId());
6108-
if (tmplStore == null) {
6109-
throw new InvalidParameterValueException("Cannot restore the vm as the template " + template.getUuid() + " isn't available in the zone");
6110-
}
6111-
} else {
6112-
tmplStore = _templateStoreDao.findByTemplate(templateId, DataStoreRole.Image);
6113-
if (tmplStore == null || (tmplStore != null && !tmplStore.getDownloadState().equals(VMTemplateStorageResourceAssoc.Status.BYPASSED))) {
6114-
throw new InvalidParameterValueException("Cannot restore the vm as the bypassed template " + template.getUuid() + " isn't available in the zone");
6115-
}
6116-
}
6105+
checkRestoreVmFromTemplate(vm, template);
61176106

61186107
if (needRestart) {
61196108
try {
@@ -6227,6 +6216,27 @@ public UserVm restoreVMInternal(Account caller, UserVmVO vm, Long newTemplateId)
62276216

62286217
}
62296218

6219+
/**
6220+
* Perform basic checkings to make sure restore is possible. If not, #InvalidParameterValueException is thrown
6221+
* @param vm vm
6222+
* @param template template
6223+
* @throws InvalidParameterValueException if restore is not possible
6224+
*/
6225+
private void checkRestoreVmFromTemplate(UserVmVO vm, VMTemplateVO template) {
6226+
TemplateDataStoreVO tmplStore;
6227+
if (!template.isDirectDownload()) {
6228+
tmplStore = _templateStoreDao.findByTemplateZoneReady(template.getId(), vm.getDataCenterId());
6229+
if (tmplStore == null) {
6230+
throw new InvalidParameterValueException("Cannot restore the vm as the template " + template.getUuid() + " isn't available in the zone");
6231+
}
6232+
} else {
6233+
tmplStore = _templateStoreDao.findByTemplate(template.getId(), DataStoreRole.Image);
6234+
if (tmplStore == null || (tmplStore != null && !tmplStore.getDownloadState().equals(VMTemplateStorageResourceAssoc.Status.BYPASSED))) {
6235+
throw new InvalidParameterValueException("Cannot restore the vm as the bypassed template " + template.getUuid() + " isn't available in the zone");
6236+
}
6237+
}
6238+
}
6239+
62306240
private void handleManagedStorage(UserVmVO vm, VolumeVO root) {
62316241
if (Volume.State.Allocated.equals(root.getState())) {
62326242
return;

0 commit comments

Comments
 (0)