diff --git a/server/src/com/cloud/storage/download/DownloadMonitorImpl.java b/server/src/com/cloud/storage/download/DownloadMonitorImpl.java index 70254def3f9..82665e361a1 100755 --- a/server/src/com/cloud/storage/download/DownloadMonitorImpl.java +++ b/server/src/com/cloud/storage/download/DownloadMonitorImpl.java @@ -922,26 +922,7 @@ public class DownloadMonitorImpl extends ManagerBase implements DownloadMonitor tmpltStore.setSize(tmpltInfo.getSize()); tmpltStore.setPhysicalSize(tmpltInfo.getPhysicalSize()); _vmTemplateStoreDao.persist(tmpltStore); - List dcs = new ArrayList(); - if (zoneId != null ){ - dcs.add(zoneId); - } - else{ - List zones = _dcDao.listAll(); - for (DataCenterVO zone : zones){ - dcs.add(zone.getId()); - } - } - for (Long id : dcs) { - VMTemplateZoneVO tmpltZoneVO = _vmTemplateZoneDao.findByZoneTemplate(id, tmplt.getId()); - if (tmpltZoneVO == null) { - tmpltZoneVO = new VMTemplateZoneVO(id, tmplt.getId(), new Date()); - _vmTemplateZoneDao.persist(tmpltZoneVO); - } else { - tmpltZoneVO.setLastUpdated(new Date()); - _vmTemplateZoneDao.update(tmpltZoneVO.getId(), tmpltZoneVO); - } - } + this.associateTemplateToZone(tmplt.getId(), zoneId); } continue; @@ -953,26 +934,7 @@ public class DownloadMonitorImpl extends ManagerBase implements DownloadMonitor s_logger.info("Template Sync did not find " + uniqueName + " on the server " + storeId + ", will request download shortly"); TemplateDataStoreVO templtStore = new TemplateDataStoreVO(storeId, tmplt.getId(), new Date(), 0, Status.NOT_DOWNLOADED, null, null, null, null, tmplt.getUrl()); _vmTemplateStoreDao.persist(templtStore); - List dcs = new ArrayList(); - if (zoneId != null ){ - dcs.add(zoneId); - } - else{ - List zones = _dcDao.listAll(); - for (DataCenterVO zone : zones){ - dcs.add(zone.getId()); - } - } - for (Long id : dcs) { - VMTemplateZoneVO tmpltZoneVO = _vmTemplateZoneDao.findByZoneTemplate(id, tmplt.getId()); - if (tmpltZoneVO == null) { - tmpltZoneVO = new VMTemplateZoneVO(id, tmplt.getId(), new Date()); - _vmTemplateZoneDao.persist(tmpltZoneVO); - } else { - tmpltZoneVO.setLastUpdated(new Date()); - _vmTemplateZoneDao.update(tmpltZoneVO.getId(), tmpltZoneVO); - } - } + this.associateTemplateToZone(tmplt.getId(), zoneId); } } @@ -1105,5 +1067,30 @@ public class DownloadMonitorImpl extends ManagerBase implements DownloadMonitor } } + // persist entry in template_zone_ref table. zoneId can be empty for region-wide image store, in that case, + // we will associate the template to all the zones. + private void associateTemplateToZone(long templateId, Long zoneId){ + List dcs = new ArrayList(); + if (zoneId != null ){ + dcs.add(zoneId); + } + else{ + List zones = _dcDao.listAll(); + for (DataCenterVO zone : zones){ + dcs.add(zone.getId()); + } + } + for (Long id : dcs) { + VMTemplateZoneVO tmpltZoneVO = _vmTemplateZoneDao.findByZoneTemplate(id, templateId); + if (tmpltZoneVO == null) { + tmpltZoneVO = new VMTemplateZoneVO(id, templateId, new Date()); + _vmTemplateZoneDao.persist(tmpltZoneVO); + } else { + tmpltZoneVO.setLastUpdated(new Date()); + _vmTemplateZoneDao.update(tmpltZoneVO.getId(), tmpltZoneVO); + } + } + } + }