From ccbe8decb3be30fe8e6a3a333392dc01b57c0db6 Mon Sep 17 00:00:00 2001 From: Rohit Yadav Date: Tue, 14 Jul 2015 13:53:12 +0530 Subject: [PATCH] quota: fix quota refresh API, reuse success response Signed-off-by: Rohit Yadav --- .../api/command/QuotaRefreshCmd.java | 16 +++-- .../api/response/QuotaRefreshResponse.java | 48 -------------- .../apache/cloudstack/quota/QuotaManager.java | 10 ++- .../cloudstack/quota/QuotaManagerImpl.java | 64 +++++++++---------- 4 files changed, 46 insertions(+), 92 deletions(-) delete mode 100644 plugins/database/quota/src/org/apache/cloudstack/api/response/QuotaRefreshResponse.java diff --git a/plugins/database/quota/src/org/apache/cloudstack/api/command/QuotaRefreshCmd.java b/plugins/database/quota/src/org/apache/cloudstack/api/command/QuotaRefreshCmd.java index 4d783a78001..418cb93b8bf 100644 --- a/plugins/database/quota/src/org/apache/cloudstack/api/command/QuotaRefreshCmd.java +++ b/plugins/database/quota/src/org/apache/cloudstack/api/command/QuotaRefreshCmd.java @@ -18,15 +18,16 @@ package org.apache.cloudstack.api.command; import com.cloud.user.Account; import org.apache.cloudstack.api.APICommand; +import org.apache.cloudstack.api.ApiErrorCode; import org.apache.cloudstack.api.BaseCmd; import org.apache.cloudstack.api.ServerApiException; -import org.apache.cloudstack.api.response.QuotaRefreshResponse; +import org.apache.cloudstack.api.response.SuccessResponse; import org.apache.cloudstack.quota.QuotaManagerImpl; import org.apache.log4j.Logger; import javax.inject.Inject; -@APICommand(name = "quotaRefresh", responseObject = QuotaRefreshResponse.class, description = "Refresh the quota for all accounts if enabled", since = "4.2.0", requestHasSensitiveInfo = false, responseHasSensitiveInfo = false) +@APICommand(name = "quotaRefresh", responseObject = SuccessResponse.class, description = "Refresh the quota for all accounts if enabled", since = "4.2.0", requestHasSensitiveInfo = false, responseHasSensitiveInfo = false) public class QuotaRefreshCmd extends BaseCmd { public static final Logger s_logger = Logger.getLogger(QuotaStatementCmd.class.getName()); @@ -52,10 +53,13 @@ public class QuotaRefreshCmd extends BaseCmd { @Override public void execute() throws ServerApiException { - _quotaManager.calculateQuotaUsage(); - final QuotaRefreshResponse response = new QuotaRefreshResponse("Success"); - response.setResponseName(getCommandName()); - setResponseObject(response); + boolean result = _quotaManager.calculateQuotaUsage(); + if (result) { + SuccessResponse response = new SuccessResponse(getCommandName()); + this.setResponseObject(response); + } else { + throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to refresh quota records"); + } } @Override diff --git a/plugins/database/quota/src/org/apache/cloudstack/api/response/QuotaRefreshResponse.java b/plugins/database/quota/src/org/apache/cloudstack/api/response/QuotaRefreshResponse.java deleted file mode 100644 index fd255382869..00000000000 --- a/plugins/database/quota/src/org/apache/cloudstack/api/response/QuotaRefreshResponse.java +++ /dev/null @@ -1,48 +0,0 @@ -//Licensed to the Apache Software Foundation (ASF) under one -//or more contributor license agreements. See the NOTICE file -//distributed with this work for additional information -//regarding copyright ownership. The ASF licenses this file -//to you under the Apache License, Version 2.0 (the -//"License"); you may not use this file except in compliance -//with the License. You may obtain a copy of the License at -// -//http://www.apache.org/licenses/LICENSE-2.0 -// -//Unless required by applicable law or agreed to in writing, -//software distributed under the License is distributed on an -//"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -//KIND, either express or implied. See the License for the -//specific language governing permissions and limitations -//under the License. -package org.apache.cloudstack.api.response; - -import org.apache.cloudstack.api.BaseResponse; - -import com.cloud.serializer.Param; -import com.google.gson.annotations.SerializedName; - -public class QuotaRefreshResponse extends BaseResponse { - - @SerializedName("result") - @Param(description = "Execution result for the quota refresh command") - private String result; - - public QuotaRefreshResponse() { - super(); - // TODO Auto-generated constructor stub - } - - public QuotaRefreshResponse(String result) { - super(); - this.result = result; - } - - public String getResult() { - return result; - } - - public void setResult(String result) { - this.result = result; - } - -} diff --git a/plugins/database/quota/src/org/apache/cloudstack/quota/QuotaManager.java b/plugins/database/quota/src/org/apache/cloudstack/quota/QuotaManager.java index a3ce6777036..4d5edf03408 100644 --- a/plugins/database/quota/src/org/apache/cloudstack/quota/QuotaManager.java +++ b/plugins/database/quota/src/org/apache/cloudstack/quota/QuotaManager.java @@ -16,18 +16,16 @@ //under the License. package org.apache.cloudstack.quota; -import java.util.List; - -import org.apache.cloudstack.api.command.QuotaStatementCmd; - import com.cloud.utils.Pair; import com.cloud.utils.component.PluggableService; +import org.apache.cloudstack.api.command.QuotaStatementCmd; + +import java.util.List; public interface QuotaManager extends PluggableService { - public void calculateQuotaUsage(); + public boolean calculateQuotaUsage(); public Pair,Integer> getQuotaUsage(QuotaStatementCmd cmd); - } diff --git a/plugins/database/quota/src/org/apache/cloudstack/quota/QuotaManagerImpl.java b/plugins/database/quota/src/org/apache/cloudstack/quota/QuotaManagerImpl.java index 56ad5374612..493279af1b6 100644 --- a/plugins/database/quota/src/org/apache/cloudstack/quota/QuotaManagerImpl.java +++ b/plugins/database/quota/src/org/apache/cloudstack/quota/QuotaManagerImpl.java @@ -16,36 +16,6 @@ //under the License. package org.apache.cloudstack.quota; -import java.math.BigDecimal; -import java.math.RoundingMode; -import java.util.ArrayList; -import java.util.Calendar; -import java.util.Date; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.TimeZone; - -import javax.ejb.Local; -import javax.inject.Inject; -import javax.naming.ConfigurationException; - -import org.apache.cloudstack.api.command.QuotaMapping; -import org.apache.cloudstack.api.command.QuotaCreditsCmd; -import org.apache.cloudstack.api.command.QuotaEditMappingCmd; -import org.apache.cloudstack.api.command.QuotaEmailTemplateAddCmd; -import org.apache.cloudstack.api.command.QuotaRefreshCmd; -import org.apache.cloudstack.api.command.QuotaStatementCmd; -import org.apache.cloudstack.context.CallContext; -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.dao.QuotaMappingDao; -import org.apache.cloudstack.quota.dao.QuotaUsageDao; -import org.apache.cloudstack.utils.usage.UsageUtils; -import org.apache.log4j.Logger; -import org.springframework.stereotype.Component; - import com.cloud.configuration.Config; import com.cloud.domain.dao.DomainDao; import com.cloud.exception.InvalidParameterValueException; @@ -64,6 +34,34 @@ import com.cloud.utils.db.DB; import com.cloud.utils.db.Filter; import com.cloud.utils.db.SearchCriteria; import com.cloud.utils.db.TransactionLegacy; +import org.apache.cloudstack.api.command.QuotaCreditsCmd; +import org.apache.cloudstack.api.command.QuotaEditMappingCmd; +import org.apache.cloudstack.api.command.QuotaEmailTemplateAddCmd; +import org.apache.cloudstack.api.command.QuotaMapping; +import org.apache.cloudstack.api.command.QuotaRefreshCmd; +import org.apache.cloudstack.api.command.QuotaStatementCmd; +import org.apache.cloudstack.context.CallContext; +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.dao.QuotaMappingDao; +import org.apache.cloudstack.quota.dao.QuotaUsageDao; +import org.apache.cloudstack.utils.usage.UsageUtils; +import org.apache.log4j.Logger; +import org.springframework.stereotype.Component; + +import javax.ejb.Local; +import javax.inject.Inject; +import javax.naming.ConfigurationException; +import java.math.BigDecimal; +import java.math.RoundingMode; +import java.util.ArrayList; +import java.util.Calendar; +import java.util.Date; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.TimeZone; @Component @Local(value = QuotaManager.class) @@ -141,7 +139,8 @@ public class QuotaManagerImpl extends ManagerBase implements QuotaManager, Confi } @Override - public void calculateQuotaUsage() { + public boolean calculateQuotaUsage() { + boolean jobResult = false; TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.USAGE_DB); try { // get quota mappings @@ -218,13 +217,14 @@ public class QuotaManagerImpl extends ManagerBase implements QuotaManager, Confi } } } - + jobResult = true; } catch (Exception e) { s_logger.error("Quota Manager error", e); e.printStackTrace(); } finally { txn.close(); } + return jobResult; } @SuppressWarnings("deprecation")