Skip to content

Commit 223a9b8

Browse files
hsato03Henrique Sato
andauthored
Quota tariff events (#8030)
Co-authored-by: Henrique Sato <[email protected]>
1 parent 986d754 commit 223a9b8

File tree

13 files changed

+96
-9
lines changed

13 files changed

+96
-9
lines changed

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import org.apache.cloudstack.api.response.ZoneResponse;
3030
import org.apache.cloudstack.config.Configuration;
3131
import org.apache.cloudstack.ha.HAConfig;
32+
import org.apache.cloudstack.quota.QuotaTariff;
3233
import org.apache.cloudstack.usage.Usage;
3334

3435
import com.cloud.dc.DataCenter;
@@ -693,6 +694,11 @@ public class EventTypes {
693694
// SystemVM
694695
public static final String EVENT_LIVE_PATCH_SYSTEMVM = "LIVE.PATCH.SYSTEM.VM";
695696

697+
// Quota
698+
public static final String EVENT_QUOTA_TARIFF_CREATE = "QUOTA.TARIFF.CREATE";
699+
public static final String EVENT_QUOTA_TARIFF_DELETE = "QUOTA.TARIFF.DELETE";
700+
public static final String EVENT_QUOTA_TARIFF_UPDATE = "QUOTA.TARIFF.UPDATE";
701+
696702
static {
697703

698704
// TODO: need a way to force author adding event types to declare the entity details as well, with out braking
@@ -1118,6 +1124,11 @@ public class EventTypes {
11181124

11191125
entityEventDetails.put(EVENT_IMAGE_STORE_DATA_MIGRATE, ImageStore.class);
11201126
entityEventDetails.put(EVENT_LIVE_PATCH_SYSTEMVM, "SystemVMs");
1127+
1128+
// Quota
1129+
entityEventDetails.put(EVENT_QUOTA_TARIFF_CREATE, QuotaTariff.class);
1130+
entityEventDetails.put(EVENT_QUOTA_TARIFF_DELETE, QuotaTariff.class);
1131+
entityEventDetails.put(EVENT_QUOTA_TARIFF_UPDATE, QuotaTariff.class);
11211132
}
11221133

11231134
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
@@ -77,7 +77,8 @@ public enum ApiCommandResourceType {
7777
Pod(com.cloud.dc.Pod.class),
7878
VmSnapshot(com.cloud.vm.snapshot.VMSnapshot.class),
7979
Role(org.apache.cloudstack.acl.Role.class),
80-
VpnCustomerGateway(com.cloud.network.Site2SiteCustomerGateway.class);
80+
VpnCustomerGateway(com.cloud.network.Site2SiteCustomerGateway.class),
81+
QuotaTariff(org.apache.cloudstack.quota.QuotaTariff.class);
8182

8283
private final Class<?> clazz;
8384

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
@@ -1461,9 +1461,9 @@ public T persist(final T entity) {
14611461
} else {
14621462
_idField.set(entity, id);
14631463
}
1464-
} else {
1465-
id = (ID)_idField.get(entity);
14661464
}
1465+
1466+
id = (ID)_idField.get(entity);
14671467
}
14681468
} catch (final IllegalAccessException e) {
14691469
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
@@ -231,4 +231,14 @@ public QuotaTariffVO findByUuid(String uuid) {
231231

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

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
//under the License.
1717
package org.apache.cloudstack.quota.vo;
1818

19-
import org.apache.cloudstack.api.InternalIdentity;
19+
import org.apache.cloudstack.quota.QuotaTariff;
2020
import org.apache.cloudstack.quota.constant.QuotaTypes;
2121
import org.apache.cloudstack.utils.reflectiontostringbuilderutils.ReflectionToStringBuilderUtils;
2222

@@ -37,7 +37,7 @@
3737

3838
@Entity
3939
@Table(name = "quota_tariff")
40-
public class QuotaTariffVO implements InternalIdentity {
40+
public class QuotaTariffVO implements QuotaTariff {
4141
private static final long serialVersionUID = -7117933766387653203L;
4242

4343
@Id
@@ -240,6 +240,7 @@ public String getDescription() {
240240
return description;
241241
}
242242

243+
@Override
243244
public String getUuid() {
244245
return uuid;
245246
}

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
import org.apache.log4j.Logger;
3234

@@ -70,6 +72,7 @@ public class QuotaTariffCreateCmd extends BaseCmd {
7072

7173
@Override
7274
public void execute() {
75+
CallContext.current().setEventDetails(String.format("Tariff: %s, description: %s, value: %s", getName(), getDescription(), getValue()));
7376
QuotaTariffVO result = responseBuilder.createQuotaTariff(this);
7477

7578
if (result == null) {
@@ -134,4 +137,8 @@ public void setEndDate(Date endDate) {
134137
this.endDate = endDate;
135138
}
136139

140+
@Override
141+
public ApiCommandResourceType getApiResourceType() {
142+
return ApiCommandResourceType.QuotaTariff;
143+
}
137144
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,14 @@
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;
3032
import org.apache.log4j.Logger;
3133

3234
import javax.inject.Inject;
@@ -49,6 +51,7 @@ public String getId() {
4951

5052
@Override
5153
public void execute() {
54+
CallContext.current().setEventDetails(String.format("Tariff id: %s", getId()));
5255
boolean result = responseBuilder.deleteQuotaTariff(getId());
5356
SuccessResponse response = new SuccessResponse(getCommandName());
5457
response.setSuccess(result);
@@ -60,4 +63,8 @@ public long getEntityOwnerId() {
6063
return Account.ACCOUNT_ID_SYSTEM;
6164
}
6265

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

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
import org.apache.log4j.Logger;
3234

@@ -111,6 +113,7 @@ public QuotaTariffUpdateCmd() {
111113

112114
@Override
113115
public void execute() {
116+
CallContext.current().setEventDetails(String.format("Tariff: %s, description: %s, value: %s", getName(), getDescription(), getValue()));
114117
final QuotaTariffVO result = _responseBuilder.updateQuotaTariffPlan(this);
115118
if (result == null) {
116119
throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to update quota tariff plan");
@@ -125,4 +128,8 @@ public long getEntityOwnerId() {
125128
return Account.ACCOUNT_ID_SYSTEM;
126129
}
127130

131+
@Override
132+
public ApiCommandResourceType getApiResourceType() {
133+
return ApiCommandResourceType.QuotaTariff;
134+
}
128135
}

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
@@ -43,6 +43,7 @@
4343
import org.apache.cloudstack.api.command.QuotaTariffCreateCmd;
4444
import org.apache.cloudstack.api.command.QuotaTariffListCmd;
4545
import org.apache.cloudstack.api.command.QuotaTariffUpdateCmd;
46+
import org.apache.cloudstack.context.CallContext;
4647
import org.apache.cloudstack.quota.QuotaManager;
4748
import org.apache.cloudstack.quota.QuotaService;
4849
import org.apache.cloudstack.quota.QuotaStatement;
@@ -75,6 +76,8 @@
7576
import com.cloud.user.dao.UserDao;
7677
import com.cloud.utils.Pair;
7778
import com.cloud.utils.db.Filter;
79+
import com.cloud.event.ActionEvent;
80+
import com.cloud.event.EventTypes;
7881

7982
@Component
8083
public class QuotaResponseBuilderImpl implements QuotaResponseBuilder {
@@ -380,6 +383,7 @@ public Pair<List<QuotaTariffVO>, Integer> listQuotaTariffPlans(final QuotaTariff
380383
}
381384

382385
@Override
386+
@ActionEvent(eventType = EventTypes.EVENT_QUOTA_TARIFF_UPDATE, eventDescription = "updating Quota Tariff")
383387
public QuotaTariffVO updateQuotaTariffPlan(QuotaTariffUpdateCmd cmd) {
384388
String name = cmd.getName();
385389
Double value = cmd.getValue();
@@ -403,6 +407,9 @@ public QuotaTariffVO updateQuotaTariffPlan(QuotaTariffUpdateCmd cmd) {
403407
QuotaTariffVO newQuotaTariff = persistNewQuotaTariff(currentQuotaTariff, name, 0, currentQuotaTariffStartDate, cmd.getEntityOwnerId(), endDate, value, description,
404408
activationRule);
405409
_quotaTariffDao.updateQuotaTariff(currentQuotaTariff);
410+
411+
CallContext.current().setEventResourceId(newQuotaTariff.getId());
412+
406413
return newQuotaTariff;
407414
}
408415

@@ -619,6 +626,7 @@ private Date createDateAtTheStartOfNextDay(LocalDate localDate) {
619626
}
620627

621628
@Override
629+
@ActionEvent(eventType = EventTypes.EVENT_QUOTA_TARIFF_CREATE, eventDescription = "creating Quota Tariff")
622630
public QuotaTariffVO createQuotaTariff(QuotaTariffCreateCmd cmd) {
623631
String name = cmd.getName();
624632
int usageType = cmd.getUsageType();
@@ -640,9 +648,14 @@ public QuotaTariffVO createQuotaTariff(QuotaTariffCreateCmd cmd) {
640648
throw new InvalidParameterValueException(String.format("The quota tariff's start date [%s] cannot be less than now [%s]", startDate, now));
641649
}
642650

643-
return persistNewQuotaTariff(null, name, usageType, startDate, cmd.getEntityOwnerId(), endDate, value, description, activationRule);
651+
QuotaTariffVO newQuotaTariff = persistNewQuotaTariff(null, name, usageType, startDate, cmd.getEntityOwnerId(), endDate, value, description, activationRule);
652+
653+
CallContext.current().setEventResourceId(newQuotaTariff.getId());
654+
655+
return newQuotaTariff;
644656
}
645657

658+
@ActionEvent(eventType = EventTypes.EVENT_QUOTA_TARIFF_DELETE, eventDescription = "removing Quota Tariff")
646659
public boolean deleteQuotaTariff(String quotaTariffUuid) {
647660
QuotaTariffVO quotaTariff = _quotaTariffDao.findByUuid(quotaTariffUuid);
648661

@@ -651,6 +664,9 @@ public boolean deleteQuotaTariff(String quotaTariffUuid) {
651664
}
652665

653666
quotaTariff.setRemoved(_quotaService.computeAdjustedTime(new Date()));
667+
668+
CallContext.current().setEventResourceId(quotaTariff.getId());
669+
654670
return _quotaTariffDao.updateQuotaTariff(quotaTariff);
655671
}
656672
}

0 commit comments

Comments
 (0)