code refactoring

This commit is contained in:
Harikrishna Patnala 2026-01-05 13:32:46 +05:30
parent 4928d9bd15
commit fb053ca7a1
2 changed files with 19 additions and 16 deletions

View File

@ -665,6 +665,11 @@ public class TemplateServiceImpl implements TemplateService {
continue;
}
Map<String, TemplateProp> templates = listTemplate(store);
if (templates == null || !templates.containsKey(tmplt.getUniqueName())) {
continue;
}
if (tmpl.getInstallPath() == null) {
logger.debug("Template [{}] found in image store [{}] but install path is null. Skipping.",
tmplt.getUniqueName(), store.getName());
@ -678,25 +683,15 @@ public class TemplateServiceImpl implements TemplateService {
private boolean searchAndCopyWithinZone(VMTemplateVO tmplt, DataStore destStore) {
Long destZoneId = destStore.getScope().getScopeId();
List<DataStore> storesInSameZone = _storeMgr.getImageStoresByZoneIds(destZoneId);
for (DataStore sourceStore : storesInSameZone) {
Map<String, TemplateProp> existingTemplatesInSourceStore = listTemplate(sourceStore);
if (existingTemplatesInSourceStore == null ||
!existingTemplatesInSourceStore.containsKey(tmplt.getUniqueName())) {
logger.debug("Template [{}] does not exist on image store [{}]; searching another.", tmplt.getUniqueName(), sourceStore.getName());
continue;
}
TemplateObject sourceTmpl = (TemplateObject) _templateFactory.getTemplate(tmplt.getId(), sourceStore);
if (sourceTmpl.getInstallPath() == null) {
logger.warn("Cannot copy template [{}] from image store [{}]; install path is null.", tmplt.getUniqueName(), sourceStore.getName());
continue;
TemplateObject sourceTmpl = findUsableTemplate(tmplt, storesInSameZone);
if (sourceTmpl == null) {
return false;
}
storageOrchestrator.orchestrateTemplateCopyToImageStore(sourceTmpl, destStore);
return true;
}
return false;
}
private boolean copyTemplateAcrossZones(DataStore destStore, TemplateObject sourceTmpl) {
Long dstZoneId = destStore.getScope().getScopeId();

View File

@ -302,6 +302,8 @@ public class TemplateServiceImplTest {
Mockito.when(storeWithNullPath.getName()).thenReturn("store-null");
DataStore storeWithValidPath = Mockito.mock(DataStore.class);
Mockito.when(storeWithValidPath.getName()).thenReturn("store-valid");
TemplateObject tmplWithNullPath = Mockito.mock(TemplateObject.class);
Mockito.when(tmplWithNullPath.getInstallPath()).thenReturn(null);
@ -311,6 +313,12 @@ public class TemplateServiceImplTest {
Mockito.doReturn(tmplWithNullPath).when(templateDataFactoryMock).getTemplate(10L, storeWithNullPath);
Mockito.doReturn(tmplWithValidPath).when(templateDataFactoryMock).getTemplate(10L, storeWithValidPath);
Map<String, TemplateProp> templates = new HashMap<>();
templates.put("test-template", Mockito.mock(TemplateProp.class));
Mockito.doReturn(templates).when(templateService).listTemplate(storeWithNullPath);
Mockito.doReturn(templates).when(templateService).listTemplate(storeWithValidPath);
List<DataStore> imageStores = List.of(storeWithNullPath, storeWithValidPath);
TemplateObject result = templateService.findUsableTemplate(template, imageStores);