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>
This commit is contained in:
Abhishek Kumar 2021-03-11 13:15:40 +05:30 committed by GitHub
parent 057ad2b7d9
commit 8aa765ac39
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 15 additions and 20 deletions

View File

@ -25,12 +25,6 @@ import java.util.concurrent.ExecutionException;
import javax.inject.Inject;
import com.cloud.configuration.Config;
import com.cloud.deployasis.dao.TemplateDeployAsIsDetailsDao;
import com.cloud.storage.dao.VMTemplateDetailsDao;
import com.cloud.utils.db.Transaction;
import com.cloud.utils.db.TransactionCallback;
import com.cloud.utils.db.TransactionStatus;
import org.apache.cloudstack.agent.directdownload.CheckUrlAnswer;
import org.apache.cloudstack.agent.directdownload.CheckUrlCommand;
import org.apache.cloudstack.api.command.user.iso.DeleteIsoCmd;
@ -61,14 +55,17 @@ import org.apache.cloudstack.storage.datastore.db.TemplateDataStoreDao;
import org.apache.cloudstack.storage.datastore.db.TemplateDataStoreVO;
import org.apache.cloudstack.storage.image.datastore.ImageStoreEntity;
import org.apache.cloudstack.utils.security.DigestHelper;
import org.apache.commons.collections.CollectionUtils;
import org.apache.log4j.Logger;
import com.cloud.agent.AgentManager;
import com.cloud.agent.api.Answer;
import com.cloud.alert.AlertManager;
import com.cloud.configuration.Config;
import com.cloud.configuration.Resource.ResourceType;
import com.cloud.dc.DataCenterVO;
import com.cloud.dc.dao.DataCenterDao;
import com.cloud.deployasis.dao.TemplateDeployAsIsDetailsDao;
import com.cloud.event.EventTypes;
import com.cloud.event.UsageEventUtils;
import com.cloud.exception.InvalidParameterValueException;
@ -86,6 +83,7 @@ import com.cloud.storage.VMTemplateStorageResourceAssoc.Status;
import com.cloud.storage.VMTemplateVO;
import com.cloud.storage.VMTemplateZoneVO;
import com.cloud.storage.dao.VMTemplateDao;
import com.cloud.storage.dao.VMTemplateDetailsDao;
import com.cloud.storage.dao.VMTemplateZoneDao;
import com.cloud.storage.download.DownloadMonitor;
import com.cloud.template.VirtualMachineTemplate.State;
@ -94,6 +92,9 @@ import com.cloud.utils.Pair;
import com.cloud.utils.UriUtils;
import com.cloud.utils.db.DB;
import com.cloud.utils.db.EntityManager;
import com.cloud.utils.db.Transaction;
import com.cloud.utils.db.TransactionCallback;
import com.cloud.utils.db.TransactionStatus;
import com.cloud.utils.exception.CloudRuntimeException;
public class HypervisorTemplateAdapter extends TemplateAdapterBase {
@ -157,6 +158,12 @@ public class HypervisorTemplateAdapter extends TemplateAdapterBase {
return ans.getTemplateSize();
}
private void checkZoneImageStores(final List<Long> zoneIdList) {
if (zoneIdList != null && CollectionUtils.isEmpty(storeMgr.getImageStoresByScope(new ZoneScope(zoneIdList.get(0))))) {
throw new InvalidParameterValueException("Failed to find a secondary storage in the specified zone.");
}
}
@Override
public TemplateProfile prepare(RegisterIsoCmd cmd) throws ResourceAllocationException {
TemplateProfile profile = super.prepare(cmd);
@ -613,29 +620,17 @@ public class HypervisorTemplateAdapter extends TemplateAdapterBase {
public TemplateProfile prepareDelete(DeleteTemplateCmd cmd) {
TemplateProfile profile = super.prepareDelete(cmd);
VMTemplateVO template = profile.getTemplate();
List<Long> zoneIdList = profile.getZoneIdList();
if (template.getTemplateType() == TemplateType.SYSTEM) {
throw new InvalidParameterValueException("The DomR template cannot be deleted.");
}
if (zoneIdList != null && (storeMgr.getImageStoreWithFreeCapacity(zoneIdList.get(0)) == null)) {
throw new InvalidParameterValueException("Failed to find a secondary storage in the specified zone.");
}
checkZoneImageStores(profile.getZoneIdList());
return profile;
}
@Override
public TemplateProfile prepareDelete(DeleteIsoCmd cmd) {
TemplateProfile profile = super.prepareDelete(cmd);
List<Long> zoneIdList = profile.getZoneIdList();
if (zoneIdList != null &&
(storeMgr.getImageStoreWithFreeCapacity(zoneIdList.get(0)) == null)) {
throw new InvalidParameterValueException("Failed to find a secondary storage in the specified zone.");
}
checkZoneImageStores(profile.getZoneIdList());
return profile;
}
}