|
36 | 36 | import org.apache.cloudstack.secstorage.heuristics.HeuristicType; |
37 | 37 | import org.apache.cloudstack.storage.datastore.db.ImageStoreDao; |
38 | 38 | import org.apache.cloudstack.storage.datastore.db.ImageStoreVO; |
| 39 | +import org.apache.cloudstack.storage.datastore.db.TemplateDataStoreDao; |
| 40 | +import org.apache.cloudstack.storage.datastore.db.TemplateDataStoreVO; |
39 | 41 | import org.apache.cloudstack.storage.heuristics.presetvariables.Account; |
40 | 42 | import org.apache.cloudstack.storage.heuristics.presetvariables.Backup; |
41 | 43 | import org.apache.cloudstack.storage.heuristics.presetvariables.Domain; |
| 44 | +import org.apache.cloudstack.storage.heuristics.presetvariables.DownloadDetails; |
42 | 45 | import org.apache.cloudstack.storage.heuristics.presetvariables.PresetVariables; |
43 | 46 | import org.apache.cloudstack.storage.heuristics.presetvariables.SecondaryStorage; |
44 | 47 | import org.apache.cloudstack.storage.heuristics.presetvariables.Snapshot; |
@@ -77,6 +80,9 @@ public class HeuristicRuleHelper { |
77 | 80 | @Inject |
78 | 81 | private AccountDao accountDao; |
79 | 82 |
|
| 83 | + @Inject |
| 84 | + private TemplateDataStoreDao templateDataStoreDao; |
| 85 | + |
80 | 86 | @Inject |
81 | 87 | private BackupOfferingDao backupOfferingDao; |
82 | 88 |
|
@@ -197,6 +203,22 @@ protected Template setTemplatePresetVariable(VMTemplateVO templateVO) { |
197 | 203 | template.setName(templateVO.getName()); |
198 | 204 | template.setFormat(templateVO.getFormat()); |
199 | 205 | template.setHypervisorType(templateVO.getHypervisorType()); |
| 206 | + template.setTemplateType(templateVO.getTemplateType()); |
| 207 | + template.setPublic(templateVO.isPublicTemplate()); |
| 208 | + |
| 209 | + List<DownloadDetails> downloadDetails = new ArrayList<>(); |
| 210 | + List<TemplateDataStoreVO> templateDataStoreVOs = templateDataStoreDao.listByTemplate(templateVO.getId()); |
| 211 | + |
| 212 | + for (TemplateDataStoreVO templateDataStoreVO : templateDataStoreVOs) { |
| 213 | + ImageStoreVO imageStore = imageStoreDao.findById(templateDataStoreVO.getDataStoreId()); |
| 214 | + |
| 215 | + DownloadDetails downloadDetail = new DownloadDetails(); |
| 216 | + downloadDetail.setDataStoreId(imageStore.getUuid()); |
| 217 | + downloadDetail.setDownloadState(templateDataStoreVO.getDownloadState()); |
| 218 | + downloadDetails.add(downloadDetail); |
| 219 | + } |
| 220 | + |
| 221 | + template.setDownloadDetails(downloadDetails); |
200 | 222 |
|
201 | 223 | return template; |
202 | 224 | } |
@@ -268,30 +290,30 @@ protected Domain setDomainPresetVariable(long domainId) { |
268 | 290 | * in the code scope. |
269 | 291 | * <br> |
270 | 292 | * <br> |
271 | | - * The JS script needs to return a valid UUID ({@link String}) of a secondary storage, otherwise a {@link CloudRuntimeException} is thrown. |
| 293 | + * The JS script needs to either return the valid UUID ({@link String}) of a secondary storage or nothing. If a valid UUID is returned, |
| 294 | + * this method returns the specific secondary storage; if nothing is returned, this method returns null in order to allow allocation in any |
| 295 | + * available secondary storage; otherwise a {@link CloudRuntimeException} is thrown. |
272 | 296 | * @param rule the {@link String} representing the JS script. |
273 | 297 | * @param heuristicType used for building the preset variables accordingly to the {@link HeuristicType} specified. |
274 | 298 | * @param obj can be from the following classes: {@link VMTemplateVO}, {@link SnapshotInfo} and {@link VolumeVO}. |
275 | 299 | * They are used to retrieve attributes for injecting in the JS rule. |
276 | 300 | * @param zoneId used for injecting the {@link SecondaryStorage} preset variables. |
277 | | - * @return the {@link DataStore} returned by the script. |
| 301 | + * @return the {@link DataStore} returned by the script, or null. |
278 | 302 | */ |
279 | 303 | public DataStore interpretHeuristicRule(String rule, HeuristicType heuristicType, Object obj, long zoneId) { |
280 | 304 | try (JsInterpreter jsInterpreter = new JsInterpreter(HEURISTICS_SCRIPT_TIMEOUT, StorageManager.HEURISTICS_SCRIPT_TIMEOUT.key())) { |
281 | 305 | buildPresetVariables(jsInterpreter, heuristicType, zoneId, obj); |
282 | 306 | Object scriptReturn = jsInterpreter.executeScript(rule); |
283 | 307 |
|
284 | 308 | if (!(scriptReturn instanceof String)) { |
285 | | - throw new CloudRuntimeException(String.format("Error while interpreting heuristic rule [%s], the rule did not return a String.", rule)); |
| 309 | + logger.debug("Script did not return a string; allocating resource in any available secondary storage."); |
| 310 | + return null; |
286 | 311 | } |
287 | 312 |
|
288 | 313 | DataStore dataStore = dataStoreManager.getImageStoreByUuid((String) scriptReturn); |
289 | | - |
290 | 314 | if (dataStore == null) { |
291 | | - 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 " + |
292 | | - "returning a valid UUID.", scriptReturn, rule)); |
| 315 | + logger.debug("Script did not return a valid secondary storage; allocating resource in any available secondary storage."); |
293 | 316 | } |
294 | | - |
295 | 317 | return dataStore; |
296 | 318 | } catch (IOException ex) { |
297 | 319 | String message = String.format("Error while executing script [%s].", rule); |
|
0 commit comments