mirror of https://github.com/apache/cloudstack.git
quota: added min_balance and quota enforce for each account
This commit is contained in:
parent
92269d2636
commit
bd1d12d830
|
|
@ -33,7 +33,7 @@ public class QuotaAccountDaoImpl extends GenericDaoBase<QuotaAccountVO, Long> im
|
|||
QuotaAccountVO result=null;
|
||||
final short opendb = TransactionLegacy.currentTxn().getDatabaseId();
|
||||
TransactionLegacy.open(TransactionLegacy.USAGE_DB).close();
|
||||
result = findById(id);
|
||||
result = super.findById(id);
|
||||
TransactionLegacy.open(opendb).close();
|
||||
return result;
|
||||
}
|
||||
|
|
@ -43,7 +43,7 @@ public class QuotaAccountDaoImpl extends GenericDaoBase<QuotaAccountVO, Long> im
|
|||
QuotaAccountVO result=null;
|
||||
final short opendb = TransactionLegacy.currentTxn().getDatabaseId();
|
||||
TransactionLegacy.open(TransactionLegacy.USAGE_DB).close();
|
||||
result = persist(entity);
|
||||
result = super.persist(entity);
|
||||
TransactionLegacy.open(opendb).close();
|
||||
return result;
|
||||
}
|
||||
|
|
@ -53,7 +53,7 @@ public class QuotaAccountDaoImpl extends GenericDaoBase<QuotaAccountVO, Long> im
|
|||
boolean result=false;
|
||||
final short opendb = TransactionLegacy.currentTxn().getDatabaseId();
|
||||
TransactionLegacy.open(TransactionLegacy.USAGE_DB).close();
|
||||
result=update(id, entity);
|
||||
result=super.update(id, entity);
|
||||
TransactionLegacy.open(opendb).close();
|
||||
return result;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -28,6 +28,7 @@ import org.apache.cloudstack.api.response.DomainResponse;
|
|||
import org.apache.cloudstack.api.response.QuotaCreditsResponse;
|
||||
import org.apache.cloudstack.api.response.QuotaResponseBuilder;
|
||||
import org.apache.cloudstack.context.CallContext;
|
||||
import org.apache.cloudstack.quota.QuotaService;
|
||||
import org.apache.log4j.Logger;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
|
@ -38,6 +39,9 @@ public class QuotaCreditsCmd extends BaseCmd {
|
|||
@Inject
|
||||
QuotaResponseBuilder _responseBuilder;
|
||||
|
||||
@Inject
|
||||
QuotaService _quotaService;
|
||||
|
||||
public static final Logger s_logger = Logger.getLogger(QuotaStatementCmd.class.getName());
|
||||
|
||||
private static final String s_name = "quotacreditsresponse";
|
||||
|
|
@ -51,6 +55,28 @@ public class QuotaCreditsCmd extends BaseCmd {
|
|||
@Parameter(name = ApiConstants.VALUE, type = CommandType.DOUBLE, required = true, description = "Value of the credits to be added+, subtracted-")
|
||||
private Double value;
|
||||
|
||||
@Parameter(name = "min_balance", type = CommandType.DOUBLE, required = false, description = "Minimum balance threshold of the account")
|
||||
private Double minBalance;
|
||||
|
||||
@Parameter(name = "quota_enforce", type = CommandType.BOOLEAN, required = false, description = "Account for which quota enforce is set to false will not be locked when there is no credit balance")
|
||||
private Boolean quotaEnforce;
|
||||
|
||||
public Double getMinBalance() {
|
||||
return minBalance;
|
||||
}
|
||||
|
||||
public void setMinBalance(Double minBalance) {
|
||||
this.minBalance = minBalance;
|
||||
}
|
||||
|
||||
public Boolean getQuotaEnforce() {
|
||||
return quotaEnforce;
|
||||
}
|
||||
|
||||
public void setQuotaEnforce(Boolean quotaEnforce) {
|
||||
this.quotaEnforce = quotaEnforce;
|
||||
}
|
||||
|
||||
public String getAccountName() {
|
||||
return accountName;
|
||||
}
|
||||
|
|
@ -79,7 +105,6 @@ public class QuotaCreditsCmd extends BaseCmd {
|
|||
super();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String getCommandName() {
|
||||
return s_name;
|
||||
|
|
@ -94,6 +119,15 @@ public class QuotaCreditsCmd extends BaseCmd {
|
|||
if (value == null) {
|
||||
throw new ServerApiException(ApiErrorCode.PARAM_ERROR, "Please send a valid non-empty quota value");
|
||||
}
|
||||
if (getQuotaEnforce() != null) {
|
||||
_quotaService.setLockAccount(accountId, getQuotaEnforce());
|
||||
}
|
||||
if (getMinBalance() != null) {
|
||||
_quotaService.setMinBalance(accountId, getMinBalance());
|
||||
}
|
||||
else {
|
||||
_quotaService.setMinBalance(accountId, 0.2 * value);
|
||||
}
|
||||
|
||||
final QuotaCreditsResponse response = _responseBuilder.addQuotaCredits(accountId, domainId, value, CallContext.current().getCallingUserId());
|
||||
response.setResponseName(getCommandName());
|
||||
|
|
|
|||
|
|
@ -32,4 +32,8 @@ public interface QuotaService extends PluggableService {
|
|||
|
||||
Date computeAdjustedTime(Date date);
|
||||
|
||||
void setLockAccount(Long accountId, Boolean state);
|
||||
|
||||
void setMinBalance(Long accountId, Double balance);
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -40,8 +40,10 @@ import org.apache.cloudstack.framework.config.ConfigKey;
|
|||
import org.apache.cloudstack.framework.config.Configurable;
|
||||
import org.apache.cloudstack.framework.config.dao.ConfigurationDao;
|
||||
import org.apache.cloudstack.quota.constant.QuotaConfig;
|
||||
import org.apache.cloudstack.quota.dao.QuotaAccountDao;
|
||||
import org.apache.cloudstack.quota.dao.QuotaBalanceDao;
|
||||
import org.apache.cloudstack.quota.dao.QuotaUsageDao;
|
||||
import org.apache.cloudstack.quota.vo.QuotaAccountVO;
|
||||
import org.apache.cloudstack.quota.vo.QuotaBalanceVO;
|
||||
import org.apache.cloudstack.quota.vo.QuotaUsageVO;
|
||||
import org.apache.cloudstack.utils.usage.UsageUtils;
|
||||
|
|
@ -68,6 +70,8 @@ public class QuotaServiceImpl extends ManagerBase implements QuotaService, Confi
|
|||
@Inject
|
||||
private AccountDao _accountDao;
|
||||
@Inject
|
||||
private QuotaAccountDao _quotaAcc;
|
||||
@Inject
|
||||
private QuotaUsageDao _quotaUsageDao;
|
||||
@Inject
|
||||
private DomainDao _domainDao;
|
||||
|
|
@ -161,7 +165,6 @@ public class QuotaServiceImpl extends ManagerBase implements QuotaService, Confi
|
|||
|
||||
startDate = startDate == null ? new Date() : startDate;
|
||||
|
||||
|
||||
if (endDate == null) {
|
||||
// adjust start date to end of day as there is no end date
|
||||
Date adjustedStartDate = computeAdjustedTime(_respBldr.startOfNextDay(startDate));
|
||||
|
|
@ -173,8 +176,7 @@ public class QuotaServiceImpl extends ManagerBase implements QuotaService, Confi
|
|||
} else {
|
||||
return qbrecords;
|
||||
}
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
Date adjustedStartDate = computeAdjustedTime(startDate);
|
||||
if (endDate.after(_respBldr.startOfNextDay())) {
|
||||
throw new InvalidParameterValueException("Incorrect Date Range. End date:" + endDate + " should not be in future. ");
|
||||
|
|
@ -258,4 +260,30 @@ public class QuotaServiceImpl extends ManagerBase implements QuotaService, Confi
|
|||
return calTS.getTime();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setLockAccount(Long accountId, Boolean state) {
|
||||
QuotaAccountVO acc = _quotaAcc.findById(accountId);
|
||||
if (acc == null) {
|
||||
acc = new QuotaAccountVO(accountId);
|
||||
acc.setQuotaEnforce(state ? 1 : 0);
|
||||
_quotaAcc.persist(acc);
|
||||
} else {
|
||||
acc.setQuotaEnforce(state ? 1 : 0);
|
||||
_quotaAcc.update(accountId, acc);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setMinBalance(Long accountId, Double balance) {
|
||||
QuotaAccountVO acc = _quotaAcc.findById(accountId);
|
||||
if (acc == null) {
|
||||
acc = new QuotaAccountVO(accountId);
|
||||
acc.setQuotaMinBalance(new BigDecimal(balance));
|
||||
_quotaAcc.persist(acc);
|
||||
} else {
|
||||
acc.setQuotaMinBalance(new BigDecimal(balance));
|
||||
_quotaAcc.update(accountId, acc);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -43,8 +43,8 @@ ALTER TABLE `cloud_usage`.`cloud_usage` ADD COLUMN `quota_calculated` tinyint(1)
|
|||
DROP TABLE IF EXISTS `cloud_usage`.`quota_account`;
|
||||
CREATE TABLE `quota_account` (
|
||||
`account_id` int(11) NOT NULL,
|
||||
`quota_balance` decimal(15,2) NOT NULL,
|
||||
`quota_balance_date` datetime NOT NULL,
|
||||
`quota_balance` decimal(15,2) NULL,
|
||||
`quota_balance_date` datetime NULL,
|
||||
`quota_enforce` int(1) DEFAULT NULL,
|
||||
`quota_min_balance` decimal(15,2) DEFAULT NULL,
|
||||
`quota_alert_date` datetime DEFAULT NULL,
|
||||
|
|
|
|||
Loading…
Reference in New Issue