Skip to content

Commit

Permalink
Inherit RemoveQuotaCommand from QuotaCRUDCommand
Browse files Browse the repository at this point in the history
Change-Id: I4af00ee5ce7c170d9ce07d9f63312166a1ed9fab
Signed-off-by: Shmuel Melamud <smelamud@redhat.com>
  • Loading branch information
smelamud authored and ahadas committed Jun 28, 2022
1 parent 5f13222 commit b65bf43
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ public abstract class QuotaCRUDCommand extends CommandBase<QuotaCRUDParameters>

public QuotaCRUDCommand(QuotaCRUDParameters parameters, CommandContext cmdContext) {
super(parameters, cmdContext);
setStoragePoolId(getParameters().getQuota().getStoragePoolId());
if (getParameters().getQuota() != null) {
setStoragePoolId(getParameters().getQuota().getStoragePoolId());
}
}

public Quota getQuota() {
Expand All @@ -49,7 +51,7 @@ protected boolean validate() {
Quota quota = getParameters().getQuota();

// Cannot add or update a quota to be default using this command
if (quota.isDefault()) {
if (quota == null || quota.isDefault()) {
addValidationMessage(EngineMessage.ACTION_TYPE_FAILED_QUOTA_IS_NOT_VALID);
return false;
}
Expand Down Expand Up @@ -78,9 +80,11 @@ private boolean validateQuotaNameIsUnique(Quota quota) {
private void fillQuotaParameter() {
Quota quotaParameter = getParameters().getQuota();

setQuotaStorage(quotaParameter);
setQuotaCluster(quotaParameter);
setQuotaThresholdDefaults(quotaParameter);
if (quotaParameter != null) {
setQuotaStorage(quotaParameter);
setQuotaCluster(quotaParameter);
setQuotaThresholdDefaults(quotaParameter);
}
}

private void setQuotaStorage(Quota quota) {
Expand Down Expand Up @@ -157,7 +161,7 @@ public void addQuotaPermissionSubject(List<PermissionSubject> quotaPermissionLis
}

public String getQuotaName() {
return quota.getQuotaName();
return getQuota().getQuotaName();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,32 @@

import javax.inject.Inject;

import org.ovirt.engine.core.bll.CommandBase;
import org.ovirt.engine.core.bll.context.CommandContext;
import org.ovirt.engine.core.bll.utils.PermissionSubject;
import org.ovirt.engine.core.common.AuditLogType;
import org.ovirt.engine.core.common.VdcObjectType;
import org.ovirt.engine.core.common.action.IdParameters;
import org.ovirt.engine.core.common.action.QuotaCRUDParameters;
import org.ovirt.engine.core.common.businessentities.Quota;
import org.ovirt.engine.core.common.businessentities.QuotaEnforcementTypeEnum;
import org.ovirt.engine.core.common.errors.EngineMessage;
import org.ovirt.engine.core.dao.QuotaDao;

public class RemoveQuotaCommand extends CommandBase<IdParameters> {
public class RemoveQuotaCommand extends QuotaCRUDCommand {

@Inject
private QuotaDao quotaDao;

private Quota quota;

public RemoveQuotaCommand(IdParameters parameters, CommandContext cmdContext) {
public RemoveQuotaCommand(QuotaCRUDParameters parameters, CommandContext cmdContext) {
super(parameters, cmdContext);
}

@Override
protected void init() {
}

@Override
protected boolean validate() {
if (getParameters() == null || getParameters().getId() == null) {
if (getParameters() == null || getParameters().getQuotaId() == null) {
addValidationMessage(EngineMessage.ACTION_TYPE_FAILED_QUOTA_NOT_EXIST);
return false;
}
Expand Down Expand Up @@ -58,14 +59,14 @@ protected boolean validate() {

@Override
protected void executeCommand() {
quotaDao.remove(getParameters().getId());
getQuotaManager().removeQuotaFromCache(getQuota().getStoragePoolId(), getParameters().getId());
quotaDao.remove(getParameters().getQuotaId());
getQuotaManager().removeQuotaFromCache(getQuota().getStoragePoolId(), getParameters().getQuotaId());
getReturnValue().setSucceeded(true);
}

@Override
public List<PermissionSubject> getPermissionCheckSubjects() {
return Collections.singletonList(new PermissionSubject(getParameters().getId(),
return Collections.singletonList(new PermissionSubject(getParameters().getQuotaId(),
VdcObjectType.Quota, getActionType().getActionGroup()));
}

Expand All @@ -80,19 +81,4 @@ public AuditLogType getAuditLogTypeValue() {
return getSucceeded() ? AuditLogType.USER_DELETE_QUOTA : AuditLogType.USER_FAILED_DELETE_QUOTA;
}

public Quota getQuota() {
if (quota == null) {
setQuota(quotaDao.getById(getParameters().getId()));
}
return quota;
}

public void setQuota(Quota quota) {
this.quota = quota;
}

public String getQuotaName() {
return getQuota().getQuotaName();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import org.mockito.Mock;
import org.ovirt.engine.core.bll.BaseCommandTest;
import org.ovirt.engine.core.bll.ValidateTestUtils;
import org.ovirt.engine.core.common.action.IdParameters;
import org.ovirt.engine.core.common.action.QuotaCRUDParameters;
import org.ovirt.engine.core.common.businessentities.Quota;
import org.ovirt.engine.core.common.businessentities.QuotaCluster;
import org.ovirt.engine.core.common.businessentities.QuotaStorage;
Expand Down Expand Up @@ -60,7 +60,7 @@ public void testFailToRemoveDefaultQuota() {
}

private RemoveQuotaCommand createCommand() {
IdParameters param = new IdParameters(quotaGuid);
QuotaCRUDParameters param = new QuotaCRUDParameters(quotaGuid);
return new RemoveQuotaCommand(param, null);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import java.io.Serializable;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;

import org.ovirt.engine.core.common.businessentities.Quota;
import org.ovirt.engine.core.compat.Guid;
Expand All @@ -15,14 +14,17 @@ public class QuotaCRUDParameters extends StoragePoolParametersBase implements Se
private Guid quotaId;

@Valid
@NotNull
private Quota quota;

private boolean copyPermissions;

public QuotaCRUDParameters() {
}

public QuotaCRUDParameters(Guid quotaId) {
this.quotaId = quotaId;
}

public QuotaCRUDParameters(Quota quota) {
quotaId = quota.getId();
this.quota = quota;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import org.ovirt.engine.core.common.VdcObjectType;
import org.ovirt.engine.core.common.action.ActionParametersBase;
import org.ovirt.engine.core.common.action.ActionType;
import org.ovirt.engine.core.common.action.IdParameters;
import org.ovirt.engine.core.common.action.QuotaCRUDParameters;
import org.ovirt.engine.core.common.queries.GetPermissionsForObjectParameters;
import org.ovirt.engine.core.common.queries.IdQueryParameters;
Expand Down Expand Up @@ -55,7 +54,7 @@ public Quota update(Quota incoming) {
@Override
public Response remove() {
get();
IdParameters prms = new IdParameters(asGuid(id));
QuotaCRUDParameters prms = new QuotaCRUDParameters(asGuid(id));
return performAction(ActionType.RemoveQuota, prms);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import org.mockito.quality.Strictness;
import org.ovirt.engine.api.model.Quota;
import org.ovirt.engine.core.common.action.ActionType;
import org.ovirt.engine.core.common.action.IdParameters;
import org.ovirt.engine.core.common.action.QuotaCRUDParameters;
import org.ovirt.engine.core.common.queries.IdQueryParameters;
import org.ovirt.engine.core.common.queries.QueryType;
Expand Down Expand Up @@ -47,8 +46,8 @@ public void testRemove() {
setUpGetEntityExpectations();
setUriInfo(setUpActionExpectations(
ActionType.RemoveQuota,
IdParameters.class,
new String[] { "Id" },
QuotaCRUDParameters.class,
new String[] { "QuotaId" },
new Object[] { QUOTA_ID },
true,
true));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

import org.ovirt.engine.core.common.action.ActionParametersBase;
import org.ovirt.engine.core.common.action.ActionType;
import org.ovirt.engine.core.common.action.IdParameters;
import org.ovirt.engine.core.common.action.QuotaCRUDParameters;
import org.ovirt.engine.core.common.businessentities.Cluster;
import org.ovirt.engine.core.common.businessentities.Quota;
Expand Down Expand Up @@ -583,7 +582,7 @@ public void onRemove() {

ArrayList<ActionParametersBase> prms = new ArrayList<>();
for (Quota a : getSelectedItems()) {
IdParameters idParameters = new IdParameters(a.getId());
QuotaCRUDParameters idParameters = new QuotaCRUDParameters(a.getId());
prms.add(idParameters);
}

Expand Down

0 comments on commit b65bf43

Please sign in to comment.