quota: fix quota refresh API, reuse success response

Signed-off-by: Rohit Yadav <rohit.yadav@shapeblue.com>
This commit is contained in:
Rohit Yadav 2015-07-14 13:53:12 +05:30
parent 9dc60bba61
commit ccbe8decb3
4 changed files with 46 additions and 92 deletions

View File

@ -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

View File

@ -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;
}
}

View File

@ -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<List<QuotaUsageVO>,Integer> getQuotaUsage(QuotaStatementCmd cmd);
}

View File

@ -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")