|
35 | 35 | import org.apache.cloudstack.secstorage.heuristics.HeuristicType; |
36 | 36 | import org.apache.cloudstack.storage.datastore.db.ImageStoreDao; |
37 | 37 | import org.apache.cloudstack.storage.datastore.db.ImageStoreVO; |
| 38 | +import org.apache.cloudstack.storage.datastore.db.TemplateDataStoreDao; |
| 39 | +import org.apache.cloudstack.storage.datastore.db.TemplateDataStoreVO; |
38 | 40 | import org.apache.cloudstack.storage.heuristics.presetvariables.Account; |
39 | 41 | import org.apache.cloudstack.storage.heuristics.presetvariables.Domain; |
| 42 | +import org.apache.cloudstack.storage.heuristics.presetvariables.DownloadDetails; |
40 | 43 | import org.apache.cloudstack.storage.heuristics.presetvariables.PresetVariables; |
41 | 44 | import org.apache.cloudstack.storage.heuristics.presetvariables.SecondaryStorage; |
42 | 45 | import org.apache.cloudstack.storage.heuristics.presetvariables.Snapshot; |
@@ -78,6 +81,9 @@ public class HeuristicRuleHelper { |
78 | 81 | @Inject |
79 | 82 | private DataCenterDao zoneDao; |
80 | 83 |
|
| 84 | + @Inject |
| 85 | + private TemplateDataStoreDao templateDataStoreDao; |
| 86 | + |
81 | 87 | /** |
82 | 88 | * Returns the {@link DataStore} object if the zone, specified by the ID, has an active heuristic rule for the given {@link HeuristicType}. |
83 | 89 | * It returns null otherwise. |
@@ -187,6 +193,23 @@ protected Template setTemplatePresetVariable(VMTemplateVO templateVO) { |
187 | 193 | template.setName(templateVO.getName()); |
188 | 194 | template.setFormat(templateVO.getFormat().toString()); |
189 | 195 | template.setHypervisorType(templateVO.getHypervisorType().toString()); |
| 196 | + template.setTemplateType(templateVO.getTemplateType().toString()); |
| 197 | + template.setPublic(templateVO.isPublicTemplate()); |
| 198 | + |
| 199 | + List<DownloadDetails> downloadDetails = new ArrayList<>(); |
| 200 | + List<TemplateDataStoreVO> templateDataStoreVOs = templateDataStoreDao.listByTemplate(templateVO.getId()); |
| 201 | + |
| 202 | + for (TemplateDataStoreVO templateDataStoreVO : templateDataStoreVOs) { |
| 203 | + ImageStoreVO imageStore = imageStoreDao.findById(templateDataStoreVO.getDataStoreId()); |
| 204 | + |
| 205 | + DownloadDetails downloadDetail = new DownloadDetails(); |
| 206 | + downloadDetail.setDataStoreId(imageStore.getUuid()); |
| 207 | + downloadDetail.setDownloadState(templateDataStoreVO.getDownloadState()); |
| 208 | + downloadDetails.add(downloadDetail); |
| 209 | + } |
| 210 | + |
| 211 | + template.setDownloadDetails(downloadDetails); |
| 212 | + |
190 | 213 |
|
191 | 214 | return template; |
192 | 215 | } |
@@ -248,28 +271,30 @@ protected Domain setDomainPresetVariable(long domainId) { |
248 | 271 | * in the code scope. |
249 | 272 | * <br> |
250 | 273 | * <br> |
251 | | - * The JS script needs to return a valid UUID ({@link String}) of a secondary storage, otherwise a {@link CloudRuntimeException} is thrown. |
| 274 | + * The JS script needs to either return the valid UUID ({@link String}) of a secondary storage or nothing. If a valid UUID is returned, |
| 275 | + * this method returns the specific secondary storage; if nothing is returned, this method returns null to allow allocation in any |
| 276 | + * available secondary storage; otherwise a {@link CloudRuntimeException} is thrown. |
252 | 277 | * @param rule the {@link String} representing the JS script. |
253 | 278 | * @param heuristicType used for building the preset variables accordingly to the {@link HeuristicType} specified. |
254 | 279 | * @param obj can be from the following classes: {@link VMTemplateVO}, {@link SnapshotInfo} and {@link VolumeVO}. |
255 | 280 | * They are used to retrieve attributes for injecting in the JS rule. |
256 | 281 | * @param zoneId used for injecting the {@link SecondaryStorage} preset variables. |
257 | | - * @return the {@link DataStore} returned by the script. |
| 282 | + * @return the {@link DataStore} returned by the script, or null. |
258 | 283 | */ |
259 | 284 | public DataStore interpretHeuristicRule(String rule, HeuristicType heuristicType, Object obj, long zoneId) { |
260 | 285 | try (JsInterpreter jsInterpreter = new JsInterpreter(HEURISTICS_SCRIPT_TIMEOUT)) { |
261 | 286 | buildPresetVariables(jsInterpreter, heuristicType, zoneId, obj); |
262 | 287 | Object scriptReturn = jsInterpreter.executeScript(rule); |
263 | 288 |
|
264 | 289 | if (!(scriptReturn instanceof String)) { |
265 | | - throw new CloudRuntimeException(String.format("Error while interpreting heuristic rule [%s], the rule did not return a String.", rule)); |
| 290 | + logger.debug("Script did not return a string; allocating resource in any available secondary storage."); |
| 291 | + return null; |
266 | 292 | } |
267 | 293 |
|
268 | 294 | DataStore dataStore = dataStoreManager.getImageStoreByUuid((String) scriptReturn); |
269 | 295 |
|
270 | 296 | if (dataStore == null) { |
271 | | - throw new CloudRuntimeException(String.format("Unable to find a secondary storage with the UUID [%s] returned by the heuristic rule [%s]. Check if the rule is " + |
272 | | - "returning a valid UUID.", scriptReturn, rule)); |
| 297 | + logger.debug("Script did not return a valid secondary storage; allocating resource in any available secondary storage."); |
273 | 298 | } |
274 | 299 |
|
275 | 300 | return dataStore; |
|
0 commit comments