Skip to content

Commit e11fd01

Browse files
hsato03Henrique Sato
authored andcommitted
Quota tariff events (apache#8030)
Co-authored-by: Henrique Sato <henrique.sato@scclouds.com.br>
1 parent acc6f46 commit e11fd01

13 files changed

Lines changed: 95 additions & 8 deletions

File tree

api/src/main/java/com/cloud/event/EventTypes.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import org.apache.cloudstack.ha.HAConfig;
3232
import org.apache.cloudstack.storage.object.Bucket;
3333
import org.apache.cloudstack.storage.object.ObjectStore;
34+
import org.apache.cloudstack.quota.QuotaTariff;
3435
import org.apache.cloudstack.usage.Usage;
3536
import org.apache.cloudstack.vm.schedule.VMSchedule;
3637

@@ -737,6 +738,10 @@ public class EventTypes {
737738
public static final String EVENT_BUCKET_CREATE = "BUCKET.CREATE";
738739
public static final String EVENT_BUCKET_DELETE = "BUCKET.DELETE";
739740
public static final String EVENT_BUCKET_UPDATE = "BUCKET.UPDATE";
741+
// Quota
742+
public static final String EVENT_QUOTA_TARIFF_CREATE = "QUOTA.TARIFF.CREATE";
743+
public static final String EVENT_QUOTA_TARIFF_DELETE = "QUOTA.TARIFF.DELETE";
744+
public static final String EVENT_QUOTA_TARIFF_UPDATE = "QUOTA.TARIFF.UPDATE";
740745

741746
static {
742747

@@ -1191,6 +1196,10 @@ public class EventTypes {
11911196
entityEventDetails.put(EVENT_BUCKET_CREATE, Bucket.class);
11921197
entityEventDetails.put(EVENT_BUCKET_UPDATE, Bucket.class);
11931198
entityEventDetails.put(EVENT_BUCKET_DELETE, Bucket.class);
1199+
// Quota
1200+
entityEventDetails.put(EVENT_QUOTA_TARIFF_CREATE, QuotaTariff.class);
1201+
entityEventDetails.put(EVENT_QUOTA_TARIFF_DELETE, QuotaTariff.class);
1202+
entityEventDetails.put(EVENT_QUOTA_TARIFF_UPDATE, QuotaTariff.class);
11941203
}
11951204

11961205
public static String getEntityForEvent(String eventName) {

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,8 @@ public enum ApiCommandResourceType {
8080
VpnCustomerGateway(com.cloud.network.Site2SiteCustomerGateway.class),
8181
ManagementServer(org.apache.cloudstack.management.ManagementServerHost.class),
8282
ObjectStore(org.apache.cloudstack.storage.object.ObjectStore.class),
83-
Bucket(org.apache.cloudstack.storage.object.Bucket.class);
83+
Bucket(org.apache.cloudstack.storage.object.Bucket.class),
84+
QuotaTariff(org.apache.cloudstack.quota.QuotaTariff.class);
8485

8586
private final Class<?> clazz;
8687

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
package org.apache.cloudstack.quota;
19+
20+
import org.apache.cloudstack.api.Identity;
21+
import org.apache.cloudstack.api.InternalIdentity;
22+
23+
public interface QuotaTariff extends InternalIdentity, Identity {
24+
25+
}

framework/db/src/main/java/com/cloud/utils/db/GenericDaoBase.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1502,9 +1502,9 @@ public T persist(final T entity) {
15021502
} else {
15031503
_idField.set(entity, id);
15041504
}
1505-
} else {
1506-
id = (ID)_idField.get(entity);
15071505
}
1506+
1507+
id = (ID)_idField.get(entity);
15081508
}
15091509
} catch (final IllegalAccessException e) {
15101510
throw new CloudRuntimeException("Yikes! ", e);

framework/quota/src/main/java/org/apache/cloudstack/quota/dao/QuotaTariffDaoImpl.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,4 +229,14 @@ public QuotaTariffVO findByUuid(String uuid) {
229229

230230
return quotaTariffs.get(0);
231231
}
232+
233+
@Override
234+
public QuotaTariffVO findByIdIncludingRemoved(Long id) {
235+
return Transaction.execute(TransactionLegacy.USAGE_DB, (TransactionCallback<QuotaTariffVO>) status -> super.findByIdIncludingRemoved(id));
236+
}
237+
238+
@Override
239+
public QuotaTariffVO findByUuidIncludingRemoved(String uuid) {
240+
return Transaction.execute(TransactionLegacy.USAGE_DB, (TransactionCallback<QuotaTariffVO>) status -> super.findByUuidIncludingRemoved(uuid));
241+
}
232242
}

framework/quota/src/main/java/org/apache/cloudstack/quota/vo/QuotaTariffVO.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import com.cloud.utils.DateUtil;
2020
import org.apache.cloudstack.api.InternalIdentity;
21+
import org.apache.cloudstack.quota.QuotaTariff;
2122
import org.apache.cloudstack.quota.constant.QuotaTypes;
2223
import org.apache.cloudstack.utils.reflectiontostringbuilderutils.ReflectionToStringBuilderUtils;
2324

@@ -40,7 +41,7 @@
4041

4142
@Entity
4243
@Table(name = "quota_tariff")
43-
public class QuotaTariffVO implements InternalIdentity {
44+
public class QuotaTariffVO implements QuotaTariff {
4445
private static final long serialVersionUID = -7117933766387653203L;
4546

4647
@Id
@@ -243,6 +244,7 @@ public String getDescription() {
243244
return description;
244245
}
245246

247+
@Override
246248
public String getUuid() {
247249
return uuid;
248250
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,15 @@
2020

2121
import org.apache.cloudstack.acl.RoleType;
2222
import org.apache.cloudstack.api.APICommand;
23+
import org.apache.cloudstack.api.ApiCommandResourceType;
2324
import org.apache.cloudstack.api.ApiConstants;
2425
import org.apache.cloudstack.api.ApiErrorCode;
2526
import org.apache.cloudstack.api.BaseCmd;
2627
import org.apache.cloudstack.api.Parameter;
2728
import org.apache.cloudstack.api.ServerApiException;
2829
import org.apache.cloudstack.api.response.QuotaResponseBuilder;
2930
import org.apache.cloudstack.api.response.QuotaTariffResponse;
31+
import org.apache.cloudstack.context.CallContext;
3032
import org.apache.cloudstack.quota.vo.QuotaTariffVO;
3133

3234
import javax.inject.Inject;
@@ -68,6 +70,7 @@ public class QuotaTariffCreateCmd extends BaseCmd {
6870

6971
@Override
7072
public void execute() {
73+
CallContext.current().setEventDetails(String.format("Tariff: %s, description: %s, value: %s", getName(), getDescription(), getValue()));
7174
QuotaTariffVO result = responseBuilder.createQuotaTariff(this);
7275

7376
if (result == null) {
@@ -132,4 +135,8 @@ public void setEndDate(Date endDate) {
132135
this.endDate = endDate;
133136
}
134137

138+
@Override
139+
public ApiCommandResourceType getApiResourceType() {
140+
return ApiCommandResourceType.QuotaTariff;
141+
}
135142
}

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,15 @@
2121
import org.apache.cloudstack.acl.RoleType;
2222
import org.apache.cloudstack.api.APICommand;
2323
import org.apache.cloudstack.api.ApiArgValidator;
24+
import org.apache.cloudstack.api.ApiCommandResourceType;
2425
import org.apache.cloudstack.api.ApiConstants;
2526
import org.apache.cloudstack.api.BaseCmd;
2627
import org.apache.cloudstack.api.Parameter;
2728
import org.apache.cloudstack.api.response.QuotaResponseBuilder;
2829
import org.apache.cloudstack.api.response.QuotaTariffResponse;
2930
import org.apache.cloudstack.api.response.SuccessResponse;
31+
import org.apache.cloudstack.context.CallContext;
32+
import org.apache.log4j.Logger;
3033

3134
import javax.inject.Inject;
3235

@@ -47,6 +50,7 @@ public String getId() {
4750

4851
@Override
4952
public void execute() {
53+
CallContext.current().setEventDetails(String.format("Tariff id: %s", getId()));
5054
boolean result = responseBuilder.deleteQuotaTariff(getId());
5155
SuccessResponse response = new SuccessResponse(getCommandName());
5256
response.setSuccess(result);
@@ -58,4 +62,8 @@ public long getEntityOwnerId() {
5862
return Account.ACCOUNT_ID_SYSTEM;
5963
}
6064

65+
@Override
66+
public ApiCommandResourceType getApiResourceType() {
67+
return ApiCommandResourceType.QuotaTariff;
68+
}
6169
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,15 @@
2020

2121
import org.apache.cloudstack.acl.RoleType;
2222
import org.apache.cloudstack.api.APICommand;
23+
import org.apache.cloudstack.api.ApiCommandResourceType;
2324
import org.apache.cloudstack.api.ApiConstants;
2425
import org.apache.cloudstack.api.ApiErrorCode;
2526
import org.apache.cloudstack.api.BaseCmd;
2627
import org.apache.cloudstack.api.Parameter;
2728
import org.apache.cloudstack.api.ServerApiException;
2829
import org.apache.cloudstack.api.response.QuotaResponseBuilder;
2930
import org.apache.cloudstack.api.response.QuotaTariffResponse;
31+
import org.apache.cloudstack.context.CallContext;
3032
import org.apache.cloudstack.quota.vo.QuotaTariffVO;
3133

3234
import javax.inject.Inject;
@@ -109,6 +111,7 @@ public QuotaTariffUpdateCmd() {
109111

110112
@Override
111113
public void execute() {
114+
CallContext.current().setEventDetails(String.format("Tariff: %s, description: %s, value: %s", getName(), getDescription(), getValue()));
112115
final QuotaTariffVO result = _responseBuilder.updateQuotaTariffPlan(this);
113116
if (result == null) {
114117
throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to update quota tariff plan");
@@ -123,4 +126,8 @@ public long getEntityOwnerId() {
123126
return Account.ACCOUNT_ID_SYSTEM;
124127
}
125128

129+
@Override
130+
public ApiCommandResourceType getApiResourceType() {
131+
return ApiCommandResourceType.QuotaTariff;
132+
}
126133
}

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

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
import org.apache.cloudstack.api.command.QuotaTariffCreateCmd;
4545
import org.apache.cloudstack.api.command.QuotaTariffListCmd;
4646
import org.apache.cloudstack.api.command.QuotaTariffUpdateCmd;
47+
import org.apache.cloudstack.context.CallContext;
4748
import org.apache.cloudstack.quota.QuotaManager;
4849
import org.apache.cloudstack.quota.QuotaManagerImpl;
4950
import org.apache.cloudstack.quota.QuotaService;
@@ -78,6 +79,8 @@
7879
import com.cloud.user.dao.UserDao;
7980
import com.cloud.utils.Pair;
8081
import com.cloud.utils.db.Filter;
82+
import com.cloud.event.ActionEvent;
83+
import com.cloud.event.EventTypes;
8184

8285
@Component
8386
public class QuotaResponseBuilderImpl implements QuotaResponseBuilder {
@@ -383,6 +386,7 @@ public Pair<List<QuotaTariffVO>, Integer> listQuotaTariffPlans(final QuotaTariff
383386
}
384387

385388
@Override
389+
@ActionEvent(eventType = EventTypes.EVENT_QUOTA_TARIFF_UPDATE, eventDescription = "updating Quota Tariff")
386390
public QuotaTariffVO updateQuotaTariffPlan(QuotaTariffUpdateCmd cmd) {
387391
String name = cmd.getName();
388392
Double value = cmd.getValue();
@@ -406,6 +410,9 @@ public QuotaTariffVO updateQuotaTariffPlan(QuotaTariffUpdateCmd cmd) {
406410
QuotaTariffVO newQuotaTariff = persistNewQuotaTariff(currentQuotaTariff, name, 0, currentQuotaTariffStartDate, cmd.getEntityOwnerId(), endDate, value, description,
407411
activationRule);
408412
_quotaTariffDao.updateQuotaTariff(currentQuotaTariff);
413+
414+
CallContext.current().setEventResourceId(newQuotaTariff.getId());
415+
409416
return newQuotaTariff;
410417
}
411418

@@ -625,6 +632,7 @@ private Date createDateAtTheStartOfNextDay(LocalDate localDate) {
625632
}
626633

627634
@Override
635+
@ActionEvent(eventType = EventTypes.EVENT_QUOTA_TARIFF_CREATE, eventDescription = "creating Quota Tariff")
628636
public QuotaTariffVO createQuotaTariff(QuotaTariffCreateCmd cmd) {
629637
String name = cmd.getName();
630638
int usageType = cmd.getUsageType();
@@ -647,9 +655,14 @@ public QuotaTariffVO createQuotaTariff(QuotaTariffCreateCmd cmd) {
647655
"Please, inform a date in the future or do not pass the parameter to use the current date and time.", startDate));
648656
}
649657

650-
return persistNewQuotaTariff(null, name, usageType, startDate, cmd.getEntityOwnerId(), endDate, value, description, activationRule);
658+
QuotaTariffVO newQuotaTariff = persistNewQuotaTariff(null, name, usageType, startDate, cmd.getEntityOwnerId(), endDate, value, description, activationRule);
659+
660+
CallContext.current().setEventResourceId(newQuotaTariff.getId());
661+
662+
return newQuotaTariff;
651663
}
652664

665+
@ActionEvent(eventType = EventTypes.EVENT_QUOTA_TARIFF_DELETE, eventDescription = "removing Quota Tariff")
653666
public boolean deleteQuotaTariff(String quotaTariffUuid) {
654667
QuotaTariffVO quotaTariff = _quotaTariffDao.findByUuid(quotaTariffUuid);
655668

@@ -658,6 +671,9 @@ public boolean deleteQuotaTariff(String quotaTariffUuid) {
658671
}
659672

660673
quotaTariff.setRemoved(_quotaService.computeAdjustedTime(new Date()));
674+
675+
CallContext.current().setEventResourceId(quotaTariff.getId());
676+
661677
return _quotaTariffDao.updateQuotaTariff(quotaTariff);
662678
}
663679
}

0 commit comments

Comments
 (0)