|
77 | 77 | import com.cloud.user.Account; |
78 | 78 | import com.cloud.user.AccountService; |
79 | 79 | import com.cloud.utils.Pair; |
| 80 | +import com.cloud.utils.StringUtils; |
80 | 81 | import com.cloud.utils.db.Filter; |
81 | 82 | import com.cloud.utils.db.SearchBuilder; |
82 | 83 | import com.cloud.utils.db.SearchCriteria; |
@@ -161,29 +162,50 @@ protected TemplateJoinDaoImpl() { |
161 | 162 | _count = "select count(distinct temp_zone_pair) from template_view WHERE "; |
162 | 163 | } |
163 | 164 |
|
| 165 | + private enum TemplateStatus { |
| 166 | + SUCCESSFULLY_INSTALLED("Successfully Installed"), |
| 167 | + INSTALLING_TEMPLATE("Installing Template"), |
| 168 | + INSTALLING_ISO("Installing ISO"), |
| 169 | + BYPASSED_SECONDARY_STORAGE("Bypassed Secondary Storage"), |
| 170 | + PROCESSING("Processing"), |
| 171 | + DOWNLOADING("%d%% Downloaded"), |
| 172 | + DOWNLOAD_COMPLETE("Download Complete"); |
| 173 | + |
| 174 | + private final String status; |
| 175 | + TemplateStatus(String status) { |
| 176 | + this.status = status; |
| 177 | + } |
| 178 | + public String getStatus() { |
| 179 | + return status; |
| 180 | + } |
| 181 | + // For statuses that have dynamic details (e.g. "75% Downloaded"). |
| 182 | + public String format(int percent) { |
| 183 | + return String.format(status, percent); |
| 184 | + } |
| 185 | + } |
| 186 | + |
164 | 187 | private String getTemplateStatus(TemplateJoinVO template) { |
165 | | - String templateStatus = null; |
166 | | - if (template.getDownloadState() != Status.DOWNLOADED) { |
167 | | - templateStatus = "Processing"; |
168 | | - if (template.getDownloadState() == Status.DOWNLOAD_IN_PROGRESS) { |
169 | | - if (template.getDownloadPercent() == 100) { |
170 | | - templateStatus = "Installing Template"; |
171 | | - } else { |
172 | | - templateStatus = template.getDownloadPercent() + "% Downloaded"; |
173 | | - } |
174 | | - } else if (template.getDownloadState() == Status.BYPASSED) { |
175 | | - templateStatus = "Bypassed Secondary Storage"; |
176 | | - } else if (template.getErrorString() == null) { |
177 | | - templateStatus = template.getTemplateState().toString(); |
| 188 | + if (template == null) { |
| 189 | + return null; |
| 190 | + } |
| 191 | + boolean isIso = Storage.ImageFormat.ISO == template.getFormat(); |
| 192 | + TemplateStatus templateStatus; |
| 193 | + if (template.getDownloadState() == Status.DOWNLOADED) { |
| 194 | + templateStatus = isIso ? TemplateStatus.SUCCESSFULLY_INSTALLED : TemplateStatus.DOWNLOAD_COMPLETE; |
| 195 | + } else if (template.getDownloadState() == Status.DOWNLOAD_IN_PROGRESS) { |
| 196 | + if (template.getDownloadPercent() == 100) { |
| 197 | + templateStatus = isIso ? TemplateStatus.INSTALLING_ISO : TemplateStatus.INSTALLING_TEMPLATE; |
178 | 198 | } else { |
179 | | - templateStatus = template.getErrorString().trim(); |
| 199 | + return TemplateStatus.DOWNLOADING.format(template.getDownloadPercent()); |
180 | 200 | } |
181 | | - } else if (template.getDownloadState() == Status.DOWNLOADED) { |
182 | | - templateStatus = "Download Complete"; |
| 201 | + } else if (template.getDownloadState() == Status.BYPASSED) { |
| 202 | + templateStatus = TemplateStatus.BYPASSED_SECONDARY_STORAGE; |
| 203 | + } else if (StringUtils.isNotBlank(template.getErrorString())) { |
| 204 | + return template.getErrorString().trim(); |
183 | 205 | } else { |
184 | | - templateStatus = "Successfully Installed"; |
| 206 | + templateStatus = TemplateStatus.PROCESSING; |
185 | 207 | } |
186 | | - return templateStatus; |
| 208 | + return templateStatus.getStatus(); |
187 | 209 | } |
188 | 210 |
|
189 | 211 | @Override |
@@ -511,24 +533,9 @@ public TemplateResponse newIsoResponse(TemplateJoinVO iso, ResponseView view) { |
511 | 533 | // If the user is an admin, add the template download status |
512 | 534 | if (isAdmin || caller.getId() == iso.getAccountId()) { |
513 | 535 | // add download status |
514 | | - if (iso.getDownloadState() != Status.DOWNLOADED) { |
515 | | - String isoStatus = "Processing"; |
516 | | - if (iso.getDownloadState() == Status.DOWNLOADED) { |
517 | | - isoStatus = "Download Complete"; |
518 | | - } else if (iso.getDownloadState() == Status.DOWNLOAD_IN_PROGRESS) { |
519 | | - if (iso.getDownloadPercent() == 100) { |
520 | | - isoStatus = "Installing ISO"; |
521 | | - } else { |
522 | | - isoStatus = iso.getDownloadPercent() + "% Downloaded"; |
523 | | - } |
524 | | - } else if (iso.getDownloadState() == Status.BYPASSED) { |
525 | | - isoStatus = "Bypassed Secondary Storage"; |
526 | | - } else { |
527 | | - isoStatus = iso.getErrorString(); |
528 | | - } |
529 | | - isoResponse.setStatus(isoStatus); |
530 | | - } else { |
531 | | - isoResponse.setStatus("Successfully Installed"); |
| 536 | + String templateStatus = getTemplateStatus(iso); |
| 537 | + if (templateStatus != null) { |
| 538 | + isoResponse.setStatus(templateStatus); |
532 | 539 | } |
533 | 540 | isoResponse.setUrl(iso.getUrl()); |
534 | 541 | List<TemplateDataStoreVO> isosInStore = _templateStoreDao.listByTemplateNotBypassed(iso.getId()); |
|
0 commit comments