Skip to content

Commit 371ce12

Browse files
winterhazelDaanHooglandGutoVeronezi
authored
Normalize dates in Usage and Quota APIs (apache#8243)
* Normalize dates in Usage and Quota APIs * Apply Daan's sugestions Co-authored-by: dahn <[email protected]> * Restore removed sinces * Add missing space * Change param descriptions for quotaBalance and quotaStatement * Apply Daniel's suggestions Co-authored-by: Daniel Augusto Veronezi Salvador <[email protected]> --------- Co-authored-by: dahn <[email protected]> Co-authored-by: Daniel Augusto Veronezi Salvador <[email protected]>
1 parent acce88f commit 371ce12

File tree

16 files changed

+79
-187
lines changed

16 files changed

+79
-187
lines changed

api/src/main/java/org/apache/cloudstack/api/ApiConstants.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1142,6 +1142,14 @@ public String toString() {
11421142
}
11431143
}
11441144

1145+
public static final String PARAMETER_DESCRIPTION_START_DATE_POSSIBLE_FORMATS = "The recommended format is \"yyyy-MM-dd'T'HH:mm:ssZ\" (e.g.: \"2023-01-01T12:00:00+0100\"); " +
1146+
"however, the following formats are also accepted: \"yyyy-MM-dd HH:mm:ss\" (e.g.: \"2023-01-01 12:00:00\") and \"yyyy-MM-dd\" (e.g.: \"2023-01-01\" - if the time is not " +
1147+
"added, it will be interpreted as \"00:00:00\"). If the recommended format is not used, the date will be considered in the server timezone.";
1148+
1149+
public static final String PARAMETER_DESCRIPTION_END_DATE_POSSIBLE_FORMATS = "The recommended format is \"yyyy-MM-dd'T'HH:mm:ssZ\" (e.g.: \"2023-01-01T12:00:00+0100\"); " +
1150+
"however, the following formats are also accepted: \"yyyy-MM-dd HH:mm:ss\" (e.g.: \"2023-01-01 12:00:00\") and \"yyyy-MM-dd\" (e.g.: \"2023-01-01\" - if the time is not " +
1151+
"added, it will be interpreted as \"23:59:59\"). If the recommended format is not used, the date will be considered in the server timezone.";
1152+
11451153
public enum BootType {
11461154
UEFI, BIOS;
11471155

api/src/main/java/org/apache/cloudstack/api/command/admin/usage/ListUsageRecordsCmd.java

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -53,16 +53,12 @@ public class ListUsageRecordsCmd extends BaseListCmd {
5353
@Parameter(name = ApiConstants.DOMAIN_ID, type = CommandType.UUID, entityType = DomainResponse.class, description = "List usage records for the specified domain.")
5454
private Long domainId;
5555

56-
@Parameter(name = ApiConstants.END_DATE,
57-
type = CommandType.DATE,
58-
required = true,
59-
description = "End date range for usage record query (use format \"yyyy-MM-dd\" or the new format \"yyyy-MM-dd HH:mm:ss\", e.g. startDate=2015-01-01 or startdate=2015-01-01 10:30:00).")
56+
@Parameter(name = ApiConstants.END_DATE, type = CommandType.DATE, required = true, description = "End date range for usage record query. " +
57+
ApiConstants.PARAMETER_DESCRIPTION_END_DATE_POSSIBLE_FORMATS)
6058
private Date endDate;
6159

62-
@Parameter(name = ApiConstants.START_DATE,
63-
type = CommandType.DATE,
64-
required = true,
65-
description = "Start date range for usage record query (use format \"yyyy-MM-dd\" or the new format \"yyyy-MM-dd HH:mm:ss\", e.g. startDate=2015-01-01 or startdate=2015-01-01 11:00:00).")
60+
@Parameter(name = ApiConstants.START_DATE, type = CommandType.DATE, required = true, description = "Start date range for usage record query. " +
61+
ApiConstants.PARAMETER_DESCRIPTION_START_DATE_POSSIBLE_FORMATS)
6662
private Date startDate;
6763

6864
@Parameter(name = ApiConstants.ACCOUNT_ID, type = CommandType.UUID, entityType = AccountResponse.class, description = "List usage records for the specified account")
@@ -137,11 +133,11 @@ public void setDomainId(Long domainId) {
137133
}
138134

139135
public void setEndDate(Date endDate) {
140-
this.endDate = endDate == null ? null : new Date(endDate.getTime());
136+
this.endDate = endDate;
141137
}
142138

143139
public void setStartDate(Date startDate) {
144-
this.startDate = startDate == null ? null : new Date(startDate.getTime());
140+
this.startDate = startDate;
145141
}
146142

147143
public void setAccountId(Long accountId) {
@@ -167,8 +163,8 @@ public Boolean isRecursive() {
167163
@Override
168164
public void execute() {
169165
Pair<List<? extends Usage>, Integer> usageRecords = _usageService.getUsageRecords(this);
170-
ListResponse<UsageRecordResponse> response = new ListResponse<UsageRecordResponse>();
171-
List<UsageRecordResponse> usageResponses = new ArrayList<UsageRecordResponse>();
166+
ListResponse<UsageRecordResponse> response = new ListResponse<>();
167+
List<UsageRecordResponse> usageResponses = new ArrayList<>();
172168
Map<String, Set<ResourceTagResponse>> resourceTagResponseMap = null;
173169
if (usageRecords != null) {
174170
//read the resource tags details for all the resources in usage data and store in Map

api/src/main/java/org/apache/cloudstack/api/response/UsageRecordResponse.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
// under the License.
1717
package org.apache.cloudstack.api.response;
1818

19+
import java.util.Date;
1920
import java.util.LinkedHashSet;
2021
import java.util.Set;
2122

@@ -133,11 +134,11 @@ public class UsageRecordResponse extends BaseResponseWithTagInformation implemen
133134

134135
@SerializedName(ApiConstants.START_DATE)
135136
@Param(description = "start date of the usage record")
136-
private String startDate;
137+
private Date startDate;
137138

138139
@SerializedName(ApiConstants.END_DATE)
139140
@Param(description = "end date of the usage record")
140-
private String endDate;
141+
private Date endDate;
141142

142143
@SerializedName("issourcenat")
143144
@Param(description = "True if the IPAddress is source NAT")
@@ -160,7 +161,7 @@ public class UsageRecordResponse extends BaseResponseWithTagInformation implemen
160161
private String vpcId;
161162

162163
public UsageRecordResponse() {
163-
tags = new LinkedHashSet<ResourceTagResponse>();
164+
tags = new LinkedHashSet<>();
164165
}
165166

166167
public void setTags(Set<ResourceTagResponse> tags) {
@@ -245,11 +246,11 @@ public void setSize(Long size) {
245246
this.size = size;
246247
}
247248

248-
public void setStartDate(String startDate) {
249+
public void setStartDate(Date startDate) {
249250
this.startDate = startDate;
250251
}
251252

252-
public void setEndDate(String endDate) {
253+
public void setEndDate(Date endDate) {
253254
this.endDate = endDate;
254255
}
255256

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,12 @@ public class QuotaBalanceCmd extends BaseCmd {
4343
@Parameter(name = ApiConstants.DOMAIN_ID, type = CommandType.UUID, required = true, entityType = DomainResponse.class, description = "If domain Id is given and the caller is domain admin then the statement is generated for domain.")
4444
private Long domainId;
4545

46-
@Parameter(name = ApiConstants.END_DATE, type = CommandType.DATE, description = "End date range for quota query. Use yyyy-MM-dd as the date format, e.g. startDate=2009-06-03.")
46+
@Parameter(name = ApiConstants.END_DATE, type = CommandType.DATE, description = "End of the period of the Quota balance." +
47+
ApiConstants.PARAMETER_DESCRIPTION_END_DATE_POSSIBLE_FORMATS)
4748
private Date endDate;
4849

49-
@Parameter(name = ApiConstants.START_DATE, type = CommandType.DATE, description = "Start date range quota query. Use yyyy-MM-dd as the date format, e.g. startDate=2009-06-01.")
50+
@Parameter(name = ApiConstants.START_DATE, type = CommandType.DATE, description = "Start of the period of the Quota balance. " +
51+
ApiConstants.PARAMETER_DESCRIPTION_START_DATE_POSSIBLE_FORMATS)
5052
private Date startDate;
5153

5254
@Parameter(name = ApiConstants.ACCOUNT_ID, type = CommandType.UUID, entityType = AccountResponse.class, description = "List usage records for the specified account")

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,12 @@ public class QuotaStatementCmd extends BaseCmd {
4545
@Parameter(name = ApiConstants.DOMAIN_ID, type = CommandType.UUID, required = true, entityType = DomainResponse.class, description = "Optional, If domain Id is given and the caller is domain admin then the statement is generated for domain.")
4646
private Long domainId;
4747

48-
@Parameter(name = ApiConstants.END_DATE, type = CommandType.DATE, required = true, description = "End date range for quota query. Use yyyy-MM-dd as the date format, e.g. startDate=2009-06-03.")
48+
@Parameter(name = ApiConstants.END_DATE, type = CommandType.DATE, required = true, description = "End of the period of the Quota statement. " +
49+
ApiConstants.PARAMETER_DESCRIPTION_END_DATE_POSSIBLE_FORMATS)
4950
private Date endDate;
5051

51-
@Parameter(name = ApiConstants.START_DATE, type = CommandType.DATE, required = true, description = "Start date range quota query. Use yyyy-MM-dd as the date format, e.g. startDate=2009-06-01.")
52+
@Parameter(name = ApiConstants.START_DATE, type = CommandType.DATE, required = true, description = "Start of the period of the Quota statement. " +
53+
ApiConstants.PARAMETER_DESCRIPTION_START_DATE_POSSIBLE_FORMATS)
5254
private Date startDate;
5355

5456
@Parameter(name = ApiConstants.TYPE, type = CommandType.INTEGER, description = "List quota usage records for the specified usage type")

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,12 @@ public class QuotaTariffCreateCmd extends BaseCmd {
6060
"value will be applied.", length = 65535)
6161
private String activationRule;
6262

63-
@Parameter(name = ApiConstants.START_DATE, type = CommandType.DATE, description = "The effective start date on/after which the quota tariff is effective. Use yyyy-MM-dd as"
64-
+ " the date format, e.g. startDate=2009-06-03. Inform null to use the current date.")
63+
@Parameter(name = ApiConstants.START_DATE, type = CommandType.DATE, description = "The effective start date on/after which the quota tariff is effective. Inform null to " +
64+
"use the current date. " + ApiConstants.PARAMETER_DESCRIPTION_START_DATE_POSSIBLE_FORMATS)
6565
private Date startDate;
6666

67-
@Parameter(name = ApiConstants.END_DATE, type = CommandType.DATE, description = "The end date of the quota tariff. Use yyyy-MM-dd as the date format, e.g."
68-
+ " endDate=2009-06-03.")
67+
@Parameter(name = ApiConstants.END_DATE, type = CommandType.DATE, description = "The end date of the quota tariff. If not informed, the tariff will be valid indefinitely. " +
68+
ApiConstants.PARAMETER_DESCRIPTION_END_DATE_POSSIBLE_FORMATS)
6969
private Date endDate;
7070

7171
@Override

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,18 +44,18 @@ public class QuotaTariffListCmd extends BaseListCmd {
4444
@Parameter(name = ApiConstants.USAGE_TYPE, type = CommandType.INTEGER, description = "Usage type of the resource")
4545
private Integer usageType;
4646

47-
@Parameter(name = ApiConstants.START_DATE, type = CommandType.DATE, description = "The start date of the quota tariff. Use yyyy-MM-dd as the date format, "
48-
+ "e.g. startDate=2009-06-03.")
47+
@Parameter(name = ApiConstants.START_DATE, type = CommandType.DATE, description = "The start date of the quota tariff. " +
48+
ApiConstants.PARAMETER_DESCRIPTION_START_DATE_POSSIBLE_FORMATS)
4949
private Date effectiveDate;
5050

51-
@Parameter(name = ApiConstants.END_DATE, type = CommandType.DATE, description = "The end date of the quota tariff. Use yyyy-MM-dd as the date format, e.g. "
52-
+ "endDate=2021-11-03.", since = "4.18.0.0")
51+
@Parameter(name = ApiConstants.END_DATE, type = CommandType.DATE, description = "The end date of the quota tariff. " +
52+
ApiConstants.PARAMETER_DESCRIPTION_END_DATE_POSSIBLE_FORMATS, since = "4.18.0.0")
5353
private Date endDate;
5454

5555
@Parameter(name = ApiConstants.NAME, type = CommandType.STRING, description = "The name of the quota tariff.", since = "4.18.0.0")
5656
private String name;
5757

58-
@Parameter(name = ApiConstants.LIST_ALL, type = CommandType.BOOLEAN, description = "False will list only not removed quota tariffs. If set to True, we will "
58+
@Parameter(name = ApiConstants.LIST_ALL, type = CommandType.BOOLEAN, description = "False will list only not removed quota tariffs. If set to true, we will "
5959
+ "list all, including the removed ones. The default is false.", since = "4.18.0.0")
6060
private boolean listAll = false;
6161

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ public class QuotaTariffUpdateCmd extends BaseCmd {
5252
"Use yyyy-MM-dd as the date format, e.g. startDate=2009-06-03.")
5353
private Date startDate;
5454

55-
@Parameter(name = ApiConstants.END_DATE, type = CommandType.DATE, description = "The end date of the quota tariff. Use yyyy-MM-dd as the date format, e.g."
56-
+ " endDate=2009-06-03.", since = "4.18.0.0")
55+
@Parameter(name = ApiConstants.END_DATE, type = CommandType.DATE, description = "The end date of the quota tariff. " +
56+
ApiConstants.PARAMETER_DESCRIPTION_END_DATE_POSSIBLE_FORMATS, since = "4.18.0.0")
5757
private Date endDate;
5858

5959
@Parameter(name = ApiConstants.NAME, type = CommandType.STRING, required = true, description = "Quota tariff's name", length = 65535, since = "4.18.0.0")

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

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ public QuotaTariffResponse createQuotaTariffResponse(QuotaTariffVO tariff) {
134134
response.setName(tariff.getName());
135135
response.setEndDate(tariff.getEndDate());
136136
response.setDescription(tariff.getDescription());
137-
response.setUuid(tariff.getUuid());
137+
response.setId(tariff.getUuid());
138138
response.setRemoved(tariff.getRemoved());
139139
return response;
140140
}
@@ -376,8 +376,8 @@ public int compare(QuotaUsageVO o1, QuotaUsageVO o2) {
376376

377377
@Override
378378
public Pair<List<QuotaTariffVO>, Integer> listQuotaTariffPlans(final QuotaTariffListCmd cmd) {
379-
Date startDate = _quotaService.computeAdjustedTime(cmd.getEffectiveDate());
380-
Date endDate = _quotaService.computeAdjustedTime(cmd.getEndDate());
379+
Date startDate = cmd.getEffectiveDate();
380+
Date endDate = cmd.getEndDate();
381381
Integer usageType = cmd.getUsageType();
382382
String name = cmd.getName();
383383
boolean listAll = cmd.isListAll();
@@ -395,10 +395,10 @@ public Pair<List<QuotaTariffVO>, Integer> listQuotaTariffPlans(final QuotaTariff
395395
public QuotaTariffVO updateQuotaTariffPlan(QuotaTariffUpdateCmd cmd) {
396396
String name = cmd.getName();
397397
Double value = cmd.getValue();
398-
Date endDate = _quotaService.computeAdjustedTime(cmd.getEndDate());
398+
Date endDate = cmd.getEndDate();
399399
String description = cmd.getDescription();
400400
String activationRule = cmd.getActivationRule();
401-
Date now = _quotaService.computeAdjustedTime(new Date());
401+
Date now = new Date();
402402

403403
warnQuotaTariffUpdateDeprecatedFields(cmd);
404404

@@ -488,7 +488,7 @@ protected void validateEndDateOnCreatingNewQuotaTariff(QuotaTariffVO newQuotaTar
488488
endDate, startDate));
489489
}
490490

491-
Date now = _quotaService.computeAdjustedTime(new Date());
491+
Date now = new Date();
492492
if (endDate.compareTo(now) < 0) {
493493
throw new InvalidParameterValueException(String.format("The quota tariff's end date [%s] cannot be less than now [%s].",
494494
endDate, now));
@@ -499,7 +499,7 @@ protected void validateEndDateOnCreatingNewQuotaTariff(QuotaTariffVO newQuotaTar
499499

500500
@Override
501501
public QuotaCreditsResponse addQuotaCredits(Long accountId, Long domainId, Double amount, Long updatedBy, Boolean enforce) {
502-
Date despositedOn = _quotaService.computeAdjustedTime(new Date());
502+
Date despositedOn = new Date();
503503
QuotaBalanceVO qb = _quotaBalanceDao.findLaterBalanceEntry(accountId, domainId, despositedOn);
504504

505505
if (qb != null) {
@@ -643,8 +643,8 @@ public QuotaTariffVO createQuotaTariff(QuotaTariffCreateCmd cmd) {
643643
int usageType = cmd.getUsageType();
644644
Date startDate = cmd.getStartDate();
645645
Date now = new Date();
646-
startDate = _quotaService.computeAdjustedTime(startDate == null ? now : startDate);
647-
Date endDate = _quotaService.computeAdjustedTime(cmd.getEndDate());
646+
startDate = startDate == null ? now : startDate;
647+
Date endDate = cmd.getEndDate();
648648
Double value = cmd.getValue();
649649
String description = cmd.getDescription();
650650
String activationRule = cmd.getActivationRule();
@@ -675,10 +675,8 @@ public boolean deleteQuotaTariff(String quotaTariffUuid) {
675675
throw new ServerApiException(ApiErrorCode.PARAM_ERROR, "Quota tariff with the provided UUID does not exist.");
676676
}
677677

678-
quotaTariff.setRemoved(_quotaService.computeAdjustedTime(new Date()));
679-
678+
quotaTariff.setRemoved(new Date());
680679
CallContext.current().setEventResourceId(quotaTariff.getId());
681-
682680
return _quotaTariffDao.updateQuotaTariff(quotaTariff);
683681
}
684682

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

Lines changed: 8 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import com.cloud.serializer.Param;
2020
import com.google.gson.annotations.SerializedName;
2121

22+
import org.apache.cloudstack.api.ApiConstants;
2223
import org.apache.cloudstack.api.BaseResponse;
2324

2425
import java.math.BigDecimal;
@@ -74,9 +75,9 @@ public class QuotaTariffResponse extends BaseResponse {
7475
@Param(description = "description")
7576
private String description;
7677

77-
@SerializedName("uuid")
78-
@Param(description = "uuid")
79-
private String uuid;
78+
@SerializedName(ApiConstants.ID)
79+
@Param(description = "the ID of the tariff")
80+
private String id;
8081

8182
@SerializedName("removed")
8283
@Param(description = "when the quota tariff was removed")
@@ -87,15 +88,6 @@ public QuotaTariffResponse() {
8788
this.setObjectName("quotatariff");
8889
}
8990

90-
public QuotaTariffResponse(final int usageType) {
91-
super();
92-
this.usageType = usageType;
93-
}
94-
95-
public String getUsageName() {
96-
return usageName;
97-
}
98-
9991
public void setUsageName(String usageName) {
10092
this.usageName = usageName;
10193
}
@@ -108,18 +100,10 @@ public void setUsageType(int usageType) {
108100
this.usageType = usageType;
109101
}
110102

111-
public String getUsageUnit() {
112-
return usageUnit;
113-
}
114-
115103
public void setUsageUnit(String usageUnit) {
116104
this.usageUnit = usageUnit;
117105
}
118106

119-
public String getUsageDiscriminator() {
120-
return usageDiscriminator;
121-
}
122-
123107
public void setUsageDiscriminator(String usageDiscriminator) {
124108
this.usageDiscriminator = usageDiscriminator;
125109
}
@@ -132,26 +116,14 @@ public void setTariffValue(BigDecimal tariffValue) {
132116
this.tariffValue = tariffValue;
133117
}
134118

135-
public String getUsageTypeDescription() {
136-
return usageTypeDescription;
137-
}
138-
139119
public void setUsageTypeDescription(String usageTypeDescription) {
140120
this.usageTypeDescription = usageTypeDescription;
141121
}
142122

143-
public Date getEffectiveOn() {
144-
return effectiveOn;
145-
}
146-
147123
public void setEffectiveOn(Date effectiveOn) {
148124
this.effectiveOn = effectiveOn;
149125
}
150126

151-
public String getCurrency() {
152-
return currency;
153-
}
154-
155127
public void setCurrency(String currency) {
156128
this.currency = currency;
157129
}
@@ -188,16 +160,12 @@ public void setDescription(String description) {
188160
this.description = description;
189161
}
190162

191-
public String getUuid() {
192-
return uuid;
193-
}
194-
195-
public void setUuid(String uuid) {
196-
this.uuid = uuid;
163+
public String getId() {
164+
return id;
197165
}
198166

199-
public Date getRemoved() {
200-
return removed;
167+
public void setId(String id) {
168+
this.id = id;
201169
}
202170

203171
public void setRemoved(Date removed) {

0 commit comments

Comments
 (0)