Skip to content

Commit d86bfaa

Browse files
committed
Let resource alert rules be disabled and enabled again
1 parent 0739a04 commit d86bfaa

15 files changed

Lines changed: 127 additions & 3 deletions

File tree

‎engine/schema/src/main/resources/META-INF/db/schema-42300to2400.sql‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ CREATE TABLE IF NOT EXISTS `cloud`.`resource_alert_rules` (
3535
`message` varchar(4096) DEFAULT NULL,
3636
`email` tinyint(1) NOT NULL DEFAULT 0,
3737
`reset_interval` int unsigned NOT NULL DEFAULT 600 COMMENT 'minimum seconds between repeat firings of this rule',
38+
`state` varchar(32) NOT NULL DEFAULT 'Enabled' COMMENT 'Enabled or Disabled; disabled rules are not checked',
3839
`created` datetime DEFAULT NULL,
3940
`updated` datetime DEFAULT NULL,
4041
`removed` datetime DEFAULT NULL,

‎engine/schema/src/main/resources/META-INF/db/views/cloud.resource_alert_rule_view.sql‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ CREATE VIEW `cloud`.`resource_alert_rule_view` AS
3232
r.message,
3333
r.email,
3434
r.reset_interval,
35+
r.state,
3536
r.created,
3637
r.updated,
3738
r.removed,

‎plugins/resource-alerts/src/main/java/org/apache/cloudstack/resourcealert/ResourceAlertManagerImpl.java‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,9 @@ public void evaluateRules() {
214214
ruleDao.remove(rule.getId());
215215
continue;
216216
}
217+
if (ResourceAlertRule.State.Disabled.equals(rule.getState())) {
218+
continue;
219+
}
217220
evaluateRule(rule);
218221
} catch (Exception e) {
219222
logger.warn("Failed to evaluate resource alert rule {}", rule.getUuid(), e);

‎plugins/resource-alerts/src/main/java/org/apache/cloudstack/resourcealert/ResourceAlertRule.java‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ enum ResourceType {
2929
VirtualMachine, Volume, Host, StoragePool
3030
}
3131

32+
enum State {
33+
Enabled, Disabled
34+
}
35+
3236
String getName();
3337
ResourceType getResourceType();
3438
Long getResourceId();
@@ -39,5 +43,6 @@ enum ResourceType {
3943
String getMessage();
4044
boolean isEmail();
4145
int getResetInterval();
46+
State getState();
4247
Date getCreated();
4348
}

‎plugins/resource-alerts/src/main/java/org/apache/cloudstack/resourcealert/ResourceAlertServiceImpl.java‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,7 @@ public ResourceAlertRuleResponse updateResourceAlertRule(UpdateResourceAlertRule
214214
validateResetInterval(cmd.getResetInterval());
215215
rule.setResetInterval(cmd.getResetInterval());
216216
}
217+
if (StringUtils.isNotBlank(cmd.getState())) rule.setState(parseState(cmd.getState()));
217218
rule.setUpdated(new Date());
218219

219220
if (cmd.isCleanupWebhooks()) {
@@ -318,6 +319,7 @@ private ResourceAlertRuleResponse toRuleResponse(ResourceAlertRuleJoinVO vo) {
318319
r.setMessage(vo.getMessage());
319320
r.setEmail(vo.isEmail());
320321
r.setResetInterval(vo.getResetInterval());
322+
r.setState(vo.getState() != null ? vo.getState().name() : null);
321323
List<Pair<String, String>> webhooks = describeWebhooks(vo.getId());
322324
r.setWebhookIds(webhooks.stream().map(Pair::first).collect(Collectors.toList()));
323325
// Left out when empty so the UI hides it
@@ -553,6 +555,14 @@ private ResourceAlertRule.ResourceType parseResourceType(String value) {
553555
return type;
554556
}
555557

558+
private ResourceAlertRule.State parseState(String value) {
559+
ResourceAlertRule.State state = EnumUtils.getEnumIgnoreCase(ResourceAlertRule.State.class, value);
560+
if (state == null) {
561+
throw new InvalidParameterValueException("Invalid state: " + value + ". Valid values: Enabled, Disabled");
562+
}
563+
return state;
564+
}
565+
556566
private AlertCondition parseCondition(String value) {
557567
AlertCondition cond = EnumUtils.getEnum(AlertCondition.class, value != null ? value.toUpperCase() : null);
558568
if (cond == null) {

‎plugins/resource-alerts/src/main/java/org/apache/cloudstack/resourcealert/api/command/user/UpdateResourceAlertRuleCmd.java‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,10 @@ public class UpdateResourceAlertRuleCmd extends BaseCmd {
7979
description = "new minimum seconds between repeat firings")
8080
private Integer resetInterval;
8181

82+
@Parameter(name = ApiConstants.STATE, type = CommandType.STRING,
83+
description = "Enabled or Disabled; a disabled rule is not checked and fires no alerts")
84+
private String state;
85+
8286
@Parameter(name = ApiConstants.WEBHOOK_IDS, type = CommandType.LIST, collectionType = CommandType.STRING,
8387
description = "UUIDs of webhooks to deliver alerts of this rule to; replaces the current list")
8488
private List<String> webhookIds;
@@ -95,6 +99,7 @@ public class UpdateResourceAlertRuleCmd extends BaseCmd {
9599
public String getMessage() { return message; }
96100
public Boolean getEmail() { return email; }
97101
public Integer getResetInterval() { return resetInterval; }
102+
public String getState() { return state; }
98103
public List<String> getWebhookIds() { return webhookIds; }
99104
public boolean isCleanupWebhooks() { return Boolean.TRUE.equals(cleanupWebhooks); }
100105

‎plugins/resource-alerts/src/main/java/org/apache/cloudstack/resourcealert/api/response/ResourceAlertRuleResponse.java‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,10 @@ public class ResourceAlertRuleResponse extends BaseResponse {
7979
@Param(description = "minimum seconds between repeat firings of this rule")
8080
private int resetInterval;
8181

82+
@SerializedName(ApiConstants.STATE)
83+
@Param(description = "Enabled or Disabled; disabled rules are not checked")
84+
private String state;
85+
8286
@SerializedName(ApiConstants.WEBHOOK_IDS)
8387
@Param(description = "UUIDs of webhooks the rule delivers alerts to")
8488
private List<String> webhookIds;
@@ -123,6 +127,7 @@ public class ResourceAlertRuleResponse extends BaseResponse {
123127
public void setMessage(String message) { this.message = message; }
124128
public void setEmail(boolean email) { this.email = email; }
125129
public void setResetInterval(int resetInterval) { this.resetInterval = resetInterval; }
130+
public void setState(String state) { this.state = state; }
126131
public void setWebhookIds(List<String> webhookIds) { this.webhookIds = webhookIds; }
127132
public void setWebhookNames(List<String> webhookNames) { this.webhookNames = webhookNames; }
128133
public void setAccountName(String accountName) { this.accountName = accountName; }

‎plugins/resource-alerts/src/main/java/org/apache/cloudstack/resourcealert/dao/ResourceAlertRuleDaoImpl.java‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ public ResourceAlertRuleDaoImpl() {
6969
specificRuleSearch.and("resourceId", specificRuleSearch.entity().getResourceId(), SearchCriteria.Op.EQ);
7070
specificRuleSearch.and("accountId", specificRuleSearch.entity().getAccountId(), SearchCriteria.Op.EQ);
7171
specificRuleSearch.and("removed", specificRuleSearch.entity().getRemoved(), SearchCriteria.Op.NULL);
72+
specificRuleSearch.and("state", specificRuleSearch.entity().getState(), SearchCriteria.Op.EQ);
7273
specificRuleSearch.done();
7374

7475
resourceTypeSearch = createSearchBuilder();
@@ -136,6 +137,7 @@ public boolean existsSpecificRule(ResourceAlertRule.ResourceType resourceType, S
136137
sc.setParameters("metric", metric);
137138
sc.setParameters("resourceId", resourceId);
138139
sc.setParameters("accountId", accountId);
140+
sc.setParameters("state", ResourceAlertRule.State.Enabled);
139141
return getCount(sc) > 0;
140142
}
141143

‎plugins/resource-alerts/src/main/java/org/apache/cloudstack/resourcealert/vo/ResourceAlertRuleJoinVO.java‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,10 @@ public class ResourceAlertRuleJoinVO implements ControlledEntity {
7979
@Column(name = "reset_interval")
8080
private int resetInterval;
8181

82+
@Column(name = "state")
83+
@Enumerated(value = EnumType.STRING)
84+
private ResourceAlertRule.State state;
85+
8286
@Column(name = "created")
8387
private Date created;
8488

@@ -134,6 +138,7 @@ public ResourceAlertRuleJoinVO() {}
134138
public String getMessage() { return message; }
135139
public boolean isEmail() { return email; }
136140
public int getResetInterval() { return resetInterval; }
141+
public ResourceAlertRule.State getState() { return state; }
137142
public Date getCreated() { return created; }
138143
public Date getUpdated() { return updated; }
139144
public Date getRemoved() { return removed; }

‎plugins/resource-alerts/src/main/java/org/apache/cloudstack/resourcealert/vo/ResourceAlertRuleVO.java‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,10 @@ public class ResourceAlertRuleVO implements ResourceAlertRule {
8888
@Column(name = "reset_interval")
8989
private int resetInterval;
9090

91+
@Column(name = "state")
92+
@Enumerated(value = EnumType.STRING)
93+
private State state = State.Enabled;
94+
9195
@Column(name = GenericDao.CREATED_COLUMN)
9296
private Date created;
9397

@@ -134,6 +138,7 @@ public ResourceAlertRuleVO(String name, ResourceType resourceType, Long resource
134138
@Override public String getMessage() { return message; }
135139
@Override public boolean isEmail() { return email; }
136140
@Override public int getResetInterval() { return resetInterval; }
141+
@Override public State getState() { return state; }
137142
@Override public Date getCreated() { return created; }
138143

139144
@Override
@@ -151,6 +156,7 @@ public Class<?> getEntityType() {
151156
public void setMessage(String message) { this.message = message; }
152157
public void setEmail(boolean email) { this.email = email; }
153158
public void setResetInterval(int resetInterval) { this.resetInterval = resetInterval; }
159+
public void setState(State state) { this.state = state; }
154160
public void setUpdated(Date updated) { this.updated = updated; }
155161
public void setRemoved(Date removed) { this.removed = removed; }
156162
}

0 commit comments

Comments
 (0)