Skip to content

Commit 8aa765a

Browse files
authored
server: delete template on storage over capacity threshold (#4691)
* server: delete template on storage over capacity threshold While deleting template for a specific zone, check should be done only for writable secondary storages and not for storages with available capacity threshold. Signed-off-by: Abhishek Kumar <abhishek.mrt22@gmail.com> * fix for ISOs and refactor Signed-off-by: Abhishek Kumar <abhishek.mrt22@gmail.com> * remove writable store check Signed-off-by: Abhishek Kumar <abhishek.mrt22@gmail.com> * fix exception message Signed-off-by: Abhishek Kumar <abhishek.mrt22@gmail.com>
1 parent 057ad2b commit 8aa765a

File tree

1 file changed

+15
-20
lines changed

1 file changed

+15
-20
lines changed

server/src/main/java/com/cloud/template/HypervisorTemplateAdapter.java

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,6 @@
2525

2626
import javax.inject.Inject;
2727

28-
import com.cloud.configuration.Config;
29-
import com.cloud.deployasis.dao.TemplateDeployAsIsDetailsDao;
30-
import com.cloud.storage.dao.VMTemplateDetailsDao;
31-
import com.cloud.utils.db.Transaction;
32-
import com.cloud.utils.db.TransactionCallback;
33-
import com.cloud.utils.db.TransactionStatus;
3428
import org.apache.cloudstack.agent.directdownload.CheckUrlAnswer;
3529
import org.apache.cloudstack.agent.directdownload.CheckUrlCommand;
3630
import org.apache.cloudstack.api.command.user.iso.DeleteIsoCmd;
@@ -61,14 +55,17 @@
6155
import org.apache.cloudstack.storage.datastore.db.TemplateDataStoreVO;
6256
import org.apache.cloudstack.storage.image.datastore.ImageStoreEntity;
6357
import org.apache.cloudstack.utils.security.DigestHelper;
58+
import org.apache.commons.collections.CollectionUtils;
6459
import org.apache.log4j.Logger;
6560

6661
import com.cloud.agent.AgentManager;
6762
import com.cloud.agent.api.Answer;
6863
import com.cloud.alert.AlertManager;
64+
import com.cloud.configuration.Config;
6965
import com.cloud.configuration.Resource.ResourceType;
7066
import com.cloud.dc.DataCenterVO;
7167
import com.cloud.dc.dao.DataCenterDao;
68+
import com.cloud.deployasis.dao.TemplateDeployAsIsDetailsDao;
7269
import com.cloud.event.EventTypes;
7370
import com.cloud.event.UsageEventUtils;
7471
import com.cloud.exception.InvalidParameterValueException;
@@ -86,6 +83,7 @@
8683
import com.cloud.storage.VMTemplateVO;
8784
import com.cloud.storage.VMTemplateZoneVO;
8885
import com.cloud.storage.dao.VMTemplateDao;
86+
import com.cloud.storage.dao.VMTemplateDetailsDao;
8987
import com.cloud.storage.dao.VMTemplateZoneDao;
9088
import com.cloud.storage.download.DownloadMonitor;
9189
import com.cloud.template.VirtualMachineTemplate.State;
@@ -94,6 +92,9 @@
9492
import com.cloud.utils.UriUtils;
9593
import com.cloud.utils.db.DB;
9694
import com.cloud.utils.db.EntityManager;
95+
import com.cloud.utils.db.Transaction;
96+
import com.cloud.utils.db.TransactionCallback;
97+
import com.cloud.utils.db.TransactionStatus;
9798
import com.cloud.utils.exception.CloudRuntimeException;
9899

99100
public class HypervisorTemplateAdapter extends TemplateAdapterBase {
@@ -157,6 +158,12 @@ private Long performDirectDownloadUrlValidation(final String url) {
157158
return ans.getTemplateSize();
158159
}
159160

161+
private void checkZoneImageStores(final List<Long> zoneIdList) {
162+
if (zoneIdList != null && CollectionUtils.isEmpty(storeMgr.getImageStoresByScope(new ZoneScope(zoneIdList.get(0))))) {
163+
throw new InvalidParameterValueException("Failed to find a secondary storage in the specified zone.");
164+
}
165+
}
166+
160167
@Override
161168
public TemplateProfile prepare(RegisterIsoCmd cmd) throws ResourceAllocationException {
162169
TemplateProfile profile = super.prepare(cmd);
@@ -613,29 +620,17 @@ public boolean delete(TemplateProfile profile) {
613620
public TemplateProfile prepareDelete(DeleteTemplateCmd cmd) {
614621
TemplateProfile profile = super.prepareDelete(cmd);
615622
VMTemplateVO template = profile.getTemplate();
616-
List<Long> zoneIdList = profile.getZoneIdList();
617-
618623
if (template.getTemplateType() == TemplateType.SYSTEM) {
619624
throw new InvalidParameterValueException("The DomR template cannot be deleted.");
620625
}
621-
622-
if (zoneIdList != null && (storeMgr.getImageStoreWithFreeCapacity(zoneIdList.get(0)) == null)) {
623-
throw new InvalidParameterValueException("Failed to find a secondary storage in the specified zone.");
624-
}
625-
626+
checkZoneImageStores(profile.getZoneIdList());
626627
return profile;
627628
}
628629

629630
@Override
630631
public TemplateProfile prepareDelete(DeleteIsoCmd cmd) {
631632
TemplateProfile profile = super.prepareDelete(cmd);
632-
List<Long> zoneIdList = profile.getZoneIdList();
633-
634-
if (zoneIdList != null &&
635-
(storeMgr.getImageStoreWithFreeCapacity(zoneIdList.get(0)) == null)) {
636-
throw new InvalidParameterValueException("Failed to find a secondary storage in the specified zone.");
637-
}
638-
633+
checkZoneImageStores(profile.getZoneIdList());
639634
return profile;
640635
}
641636
}

0 commit comments

Comments
 (0)