Skip to content

Commit 4d3ad22

Browse files
author
Julien Hervot de Mattos Vaz
committed
Fix QuotaSummaryCmd
1 parent 2800239 commit 4d3ad22

File tree

4 files changed

+95
-16
lines changed

4 files changed

+95
-16
lines changed

plugins/database/quota/src/main/java/org/apache/cloudstack/api/command/QuotaSummaryCmd.java

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,20 @@
1616
//under the License.
1717
package org.apache.cloudstack.api.command;
1818

19-
import com.cloud.user.Account;
2019
import com.cloud.utils.Pair;
2120

21+
22+
import org.apache.cloudstack.api.ACL;
2223
import org.apache.cloudstack.api.APICommand;
2324
import org.apache.cloudstack.api.ApiConstants;
2425
import org.apache.cloudstack.api.BaseListCmd;
2526
import org.apache.cloudstack.api.Parameter;
27+
import org.apache.cloudstack.api.response.AccountResponse;
2628
import org.apache.cloudstack.api.response.DomainResponse;
27-
import org.apache.cloudstack.api.response.ListResponse;
2829
import org.apache.cloudstack.api.response.QuotaResponseBuilder;
2930
import org.apache.cloudstack.api.response.QuotaSummaryResponse;
31+
import org.apache.cloudstack.api.response.ProjectResponse;
32+
import org.apache.cloudstack.api.response.ListResponse;
3033
import org.apache.cloudstack.quota.QuotaAccountStateFilter;
3134
import org.apache.cloudstack.quota.QuotaService;
3235
import org.apache.commons.lang3.ObjectUtils;
@@ -41,6 +44,16 @@
4144
requestHasSensitiveInfo = false, responseHasSensitiveInfo = false)
4245
public class QuotaSummaryCmd extends BaseListCmd {
4346

47+
@Inject
48+
QuotaResponseBuilder quotaResponseBuilder;
49+
50+
@Inject
51+
QuotaService quotaService;
52+
53+
@ACL
54+
@Parameter(name = ApiConstants.ACCOUNT_ID, type = CommandType.UUID, entityType = AccountResponse.class, description = "ID of the account for which balance will be listed. Can not be specified with projectId.")
55+
private Long accountId;
56+
4457
@Parameter(name = ApiConstants.ACCOUNT, type = CommandType.STRING, required = false, description = "Optional, Account Id for which statement needs to be generated")
4558
private String accountName;
4659

@@ -52,15 +65,13 @@ public class QuotaSummaryCmd extends BaseListCmd {
5265
private Boolean listAll;
5366

5467
@Parameter(name = ApiConstants.ACCOUNT_STATE_TO_SHOW, type = CommandType.STRING, description = "Possible values are [ALL, ACTIVE, REMOVED]. ALL will list summaries for " +
55-
"active and removed accounts; ACTIVE will list summaries only for active accounts; REMOVED will list summaries only for removed accounts. The default value is ACTIVE.”,
56-
since = 4.21.0)
68+
"active and removed accounts; ACTIVE will list summaries only for active accounts; REMOVED will list summaries only for removed accounts. The default value is ACTIVE.",
69+
since = "4.21.0")
5770
private String accountStateToShow;
5871

59-
@Inject
60-
QuotaResponseBuilder quotaResponseBuilder;
61-
62-
@Inject
63-
QuotaService quotaService;
72+
@ACL
73+
@Parameter(name = ApiConstants.PROJECT_ID, type = CommandType.UUID, entityType = ProjectResponse.class, description = "Project Id for which balance will be listed. Can not be specified with accountId.")
74+
private Long projectId;
6475

6576
@Override
6677
public void execute() {
@@ -72,6 +83,14 @@ public void execute() {
7283
setResponseObject(response);
7384
}
7485

86+
public Long getAccountId() {
87+
return accountId;
88+
}
89+
90+
public void setAccountId(Long accountId) {
91+
this.accountId = accountId;
92+
}
93+
7594
public String getAccountName() {
7695
return accountName;
7796
}
@@ -96,6 +115,10 @@ public void setListAll(Boolean listAll) {
96115
this.listAll = listAll;
97116
}
98117

118+
public Long getProjectId() {
119+
return projectId;
120+
}
121+
99122
public QuotaAccountStateFilter getAccountStateToShow() {
100123
if (StringUtils.isNotBlank(accountStateToShow)) {
101124
QuotaAccountStateFilter state = QuotaAccountStateFilter.getValue(accountStateToShow);
@@ -109,7 +132,9 @@ public QuotaAccountStateFilter getAccountStateToShow() {
109132

110133
@Override
111134
public long getEntityOwnerId() {
112-
return Account.ACCOUNT_ID_SYSTEM;
135+
if (ObjectUtils.allNull(accountId, accountName, projectId)) {
136+
return -1;
137+
}
138+
return quotaService.finalizeAccountId(accountId, accountName, domainId, projectId);
113139
}
114-
115140
}

plugins/database/quota/src/main/java/org/apache/cloudstack/api/response/QuotaSummaryResponse.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,11 @@ public class QuotaSummaryResponse extends BaseResponse {
5353
private State state;
5454

5555
@SerializedName("domainremoved")
56-
@Param(description = "If the domain is removed or not")
56+
@Param(description = "If the domain is removed or not", since = "4.21.0")
5757
private boolean domainRemoved;
5858

5959
@SerializedName("accountremoved")
60-
@Param(description = "If the account is removed or not")
60+
@Param(description = "If the account is removed or not", since = "4.21.0")
6161
private boolean accountRemoved;
6262

6363
@SerializedName("quota")
@@ -81,15 +81,15 @@ public class QuotaSummaryResponse extends BaseResponse {
8181
private boolean quotaEnabled;
8282

8383
@SerializedName("projectname")
84-
@Param(description = "Name of the project")
84+
@Param(description = "Name of the project", since = "4.21.0")
8585
private String projectName;
8686

8787
@SerializedName("projectid")
88-
@Param(description = "Project's id")
88+
@Param(description = "Project's id", since = "4.21.0")
8989
private String projectId;
9090

9191
@SerializedName("projectremoved")
92-
@Param(description = "Whether the project is removed or not")
92+
@Param(description = "Whether the project is removed or not", since = "4.21.0")
9393
private Boolean projectRemoved;
9494

9595
public String getAccountId() {

plugins/database/quota/src/main/java/org/apache/cloudstack/quota/QuotaService.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,6 @@ public interface QuotaService extends PluggableService {
4040

4141
boolean saveQuotaAccount(AccountVO account, BigDecimal aggrUsage, Date endDate);
4242

43+
Long finalizeAccountId(Long accountId, String accountName, Long domainId, Long projectId);
44+
4345
}

plugins/database/quota/src/main/java/org/apache/cloudstack/quota/QuotaServiceImpl.java

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@
2626
import javax.inject.Inject;
2727
import javax.naming.ConfigurationException;
2828

29+
import com.cloud.projects.Project;
30+
import com.cloud.projects.ProjectManager;
31+
import com.cloud.user.AccountService;
32+
import org.apache.cloudstack.api.ApiConstants;
33+
import org.apache.cloudstack.api.ApiErrorCode;
34+
import org.apache.cloudstack.api.ServerApiException;
2935
import org.apache.cloudstack.api.command.QuotaBalanceCmd;
3036
import org.apache.cloudstack.api.command.QuotaConfigureEmailCmd;
3137
import org.apache.cloudstack.api.command.QuotaCreditsCmd;
@@ -74,6 +80,8 @@ public class QuotaServiceImpl extends ManagerBase implements QuotaService, Confi
7480
@Inject
7581
private AccountDao _accountDao;
7682
@Inject
83+
private AccountService accountService;
84+
@Inject
7785
private QuotaAccountDao _quotaAcc;
7886
@Inject
7987
private QuotaUsageDao _quotaUsageDao;
@@ -85,6 +93,8 @@ public class QuotaServiceImpl extends ManagerBase implements QuotaService, Confi
8593
private QuotaBalanceDao _quotaBalanceDao;
8694
@Inject
8795
private QuotaResponseBuilder _respBldr;
96+
@Inject
97+
private ProjectManager projectMgr;
8898

8999
private TimeZone _usageTimezone;
90100

@@ -275,6 +285,48 @@ public boolean saveQuotaAccount(final AccountVO account, final BigDecimal aggrUs
275285
}
276286
}
277287

288+
/**
289+
* Returns the Id of the account that will be used when provided with either accountId, projectId or accountName and domainId.
290+
*/
291+
@Override
292+
public Long finalizeAccountId(Long accountId, String accountName, Long domainId, Long projectId) {
293+
if (projectId != null) {
294+
if (accountId != null || accountName != null) {
295+
throw new ServerApiException(ApiErrorCode.PARAM_ERROR, "Project and account can not be specified together.");
296+
}
297+
final Project project = projectMgr.getProject(projectId);
298+
if (project == null) {
299+
throw new ServerApiException(ApiErrorCode.PARAM_ERROR, String.format("Unable to find project with id: [%s].", projectId));
300+
}
301+
if (project.getState() != Project.State.Active) {
302+
throw new ServerApiException(ApiErrorCode.PARAM_ERROR, String.format("Project with projectId [%s] is not active.", projectId));
303+
}
304+
return project.getProjectAccountId();
305+
}
306+
307+
if (accountId != null) {
308+
if (accountService.getActiveAccountById(accountId) != null) {
309+
return accountId;
310+
}
311+
throw new InvalidParameterValueException(String.format("Unable to find account with accountId: [%s].", accountId));
312+
}
313+
314+
if (accountName == null && domainId == null) {
315+
throw new ServerApiException(ApiErrorCode.PARAM_ERROR, String.format("Either %s or %s is required.", ApiConstants.ACCOUNT_ID, ApiConstants.PROJECT_ID));
316+
}
317+
try {
318+
Account activeAccount = accountService.getActiveAccountByName(accountName, domainId);
319+
if (activeAccount != null) {
320+
return activeAccount.getId();
321+
}
322+
} catch (InvalidParameterValueException exception) {
323+
throw new ServerApiException(ApiErrorCode.PARAM_ERROR, String.format("Both %s and %s are needed if using either. Consider using %s instead.",
324+
ApiConstants.ACCOUNT, ApiConstants.DOMAIN_ID, ApiConstants.ACCOUNT_ID));
325+
}
326+
throw new InvalidParameterValueException(String.format("Unable to find account by name: [%s] on domain: [%s]", accountName, domainId));
327+
}
328+
329+
278330
@Override
279331
public void setMinBalance(Long accountId, Double balance) {
280332
QuotaAccountVO acc = _quotaAcc.findByIdQuotaAccount(accountId);

0 commit comments

Comments
 (0)