Skip to content

Commit d14ba0f

Browse files
authored
fix(cls): [120871100] tencentcloud_cls_alarm support multi_conditions (#2985)
* add * add * add * add
1 parent 6bf32d3 commit d14ba0f

File tree

4 files changed

+240
-35
lines changed

4 files changed

+240
-35
lines changed

.changelog/2985.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:enhancement
2+
resource/tencentcloud_cls_alarm: support `multi_conditions`
3+
```

tencentcloud/services/cls/resource_tc_cls_alarm.go

Lines changed: 95 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -99,16 +99,41 @@ func ResourceTencentCloudClsAlarm() *schema.Resource {
9999
},
100100

101101
"condition": {
102-
Required: true,
103-
Type: schema.TypeString,
104-
Description: "triggering conditions.",
102+
Optional: true,
103+
Type: schema.TypeString,
104+
ExactlyOneOf: []string{"multi_conditions"},
105+
Description: "Trigger condition.",
105106
},
106107

107108
"alarm_level": {
108-
Optional: true,
109-
Computed: true,
110-
Type: schema.TypeInt,
111-
Description: "Alarm level. 0: Warning; 1: Info; 2: Critical. Default is 0.",
109+
Optional: true,
110+
Computed: true,
111+
Type: schema.TypeInt,
112+
ConflictsWith: []string{"multi_conditions"},
113+
RequiredWith: []string{"condition"},
114+
Description: "Alarm level. 0: Warning; 1: Info; 2: Critical. Default is 0.",
115+
},
116+
117+
"multi_conditions": {
118+
Optional: true,
119+
Type: schema.TypeList,
120+
ExactlyOneOf: []string{"condition"},
121+
Description: "Multiple triggering conditions.",
122+
Elem: &schema.Resource{
123+
Schema: map[string]*schema.Schema{
124+
"condition": {
125+
Type: schema.TypeString,
126+
Optional: true,
127+
Description: "Trigger condition.",
128+
},
129+
"alarm_level": {
130+
Type: schema.TypeInt,
131+
Optional: true,
132+
Computed: true,
133+
Description: "Alarm level. 0: Warning; 1: Info; 2: Critical. Default is 0.",
134+
},
135+
},
136+
},
112137
},
113138

114139
"trigger_count": {
@@ -287,15 +312,34 @@ func resourceTencentCloudClsAlarmCreate(d *schema.ResourceData, meta interface{}
287312
request.MonitorTime = &monitorTime
288313
}
289314

290-
if v, ok := d.GetOk("condition"); ok {
315+
var changeCondition bool
316+
if v, ok := d.GetOk("condition"); ok && v.(string) != "" {
291317
request.Condition = helper.String(v.(string))
292318
request.AlarmLevel = helper.IntUint64(0)
319+
changeCondition = true
293320
}
294321

295-
if v, ok := d.GetOkExists("alarm_level"); ok {
322+
if v, ok := d.GetOkExists("alarm_level"); ok && changeCondition {
296323
request.AlarmLevel = helper.IntUint64(v.(int))
297324
}
298325

326+
if v, ok := d.GetOk("multi_conditions"); ok {
327+
for _, item := range v.([]interface{}) {
328+
dMap := item.(map[string]interface{})
329+
multiCondition := cls.MultiCondition{}
330+
if v, ok := dMap["condition"]; ok {
331+
multiCondition.Condition = helper.String(v.(string))
332+
multiCondition.AlarmLevel = helper.IntUint64(0)
333+
}
334+
335+
if v, ok := dMap["alarm_level"]; ok {
336+
multiCondition.AlarmLevel = helper.IntUint64(v.(int))
337+
}
338+
339+
request.MultiConditions = append(request.MultiConditions, &multiCondition)
340+
}
341+
}
342+
299343
if v, ok := d.GetOkExists("trigger_count"); ok {
300344
request.TriggerCount = helper.IntInt64(v.(int))
301345
}
@@ -486,12 +530,29 @@ func resourceTencentCloudClsAlarmRead(d *schema.ResourceData, meta interface{})
486530
_ = d.Set("monitor_time", []interface{}{monitorTimeMap})
487531
}
488532

489-
if alarm.Condition != nil {
533+
if alarm.Condition != nil && *alarm.Condition != "" {
490534
_ = d.Set("condition", alarm.Condition)
535+
if alarm.AlarmLevel != nil {
536+
_ = d.Set("alarm_level", alarm.AlarmLevel)
537+
}
491538
}
492539

493-
if alarm.AlarmLevel != nil {
494-
_ = d.Set("alarm_level", alarm.AlarmLevel)
540+
if alarm.MultiConditions != nil {
541+
tmpList := make([]map[string]interface{}, 0)
542+
for _, item := range alarm.MultiConditions {
543+
dMap := make(map[string]interface{})
544+
if item.Condition != nil {
545+
dMap["condition"] = *item.Condition
546+
}
547+
548+
if item.AlarmLevel != nil {
549+
dMap["alarm_level"] = *item.AlarmLevel
550+
}
551+
552+
tmpList = append(tmpList, dMap)
553+
}
554+
555+
_ = d.Set("multi_conditions", tmpList)
495556
}
496557

497558
if alarm.TriggerCount != nil {
@@ -597,7 +658,7 @@ func resourceTencentCloudClsAlarmUpdate(d *schema.ResourceData, meta interface{}
597658
request.AlarmId = &alarmId
598659
mutableArgs := []string{
599660
"name", "alarm_targets", "monitor_time", "condition", "alarm_level",
600-
"trigger_count", "alarm_period", "alarm_notice_ids",
661+
"multi_conditions", "trigger_count", "alarm_period", "alarm_notice_ids",
601662
"status", "message_template", "call_back", "analysis",
602663
}
603664

@@ -662,15 +723,34 @@ func resourceTencentCloudClsAlarmUpdate(d *schema.ResourceData, meta interface{}
662723
request.MonitorTime = &monitorTime
663724
}
664725

665-
if v, ok := d.GetOk("condition"); ok {
726+
var changeCondition bool
727+
if v, ok := d.GetOk("condition"); ok && v.(string) != "" {
666728
request.Condition = helper.String(v.(string))
667729
request.AlarmLevel = helper.IntUint64(0)
730+
changeCondition = true
668731
}
669732

670-
if v, ok := d.GetOkExists("alarm_level"); ok {
733+
if v, ok := d.GetOkExists("alarm_level"); ok && changeCondition {
671734
request.AlarmLevel = helper.IntUint64(v.(int))
672735
}
673736

737+
if v, ok := d.GetOk("multi_conditions"); ok {
738+
for _, item := range v.([]interface{}) {
739+
dMap := item.(map[string]interface{})
740+
multiCondition := cls.MultiCondition{}
741+
if v, ok := dMap["condition"]; ok {
742+
multiCondition.Condition = helper.String(v.(string))
743+
multiCondition.AlarmLevel = helper.IntUint64(0)
744+
}
745+
746+
if v, ok := dMap["alarm_level"]; ok {
747+
multiCondition.AlarmLevel = helper.IntUint64(v.(int))
748+
}
749+
750+
request.MultiConditions = append(request.MultiConditions, &multiCondition)
751+
}
752+
}
753+
674754
if v, ok := d.GetOkExists("trigger_count"); ok {
675755
request.TriggerCount = helper.IntInt64(v.(int))
676756
}

tencentcloud/services/cls/resource_tc_cls_alarm.md

Lines changed: 68 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,73 @@ Provides a resource to create a cls alarm
22

33
Example Usage
44

5+
Use single condition
6+
57
```hcl
68
resource "tencentcloud_cls_alarm" "example" {
7-
name = "tf-example"
9+
name = "tf-example"
810
alarm_notice_ids = [
9-
"notice-0850756b-245d-4bc7-bb27-2a58fffc780b",
11+
"notice-c2af43ee-1a4b-4c4a-ae3e-f81481280101",
1012
]
1113
alarm_period = 15
12-
condition = "test"
13-
alarm_level = 0
14+
condition = "$1.source='10.0.0.1'"
15+
alarm_level = 1
1416
message_template = "{{.Label}}"
1517
status = true
16-
tags = {
17-
"createdBy" = "terraform"
18-
}
19-
trigger_count = 1
18+
trigger_count = 1
2019
2120
alarm_targets {
21+
logset_id = "e74efb8e-f647-48b2-a725-43f11b122081"
22+
topic_id = "59cf3ec0-1612-4157-be3f-341b2e7a53cb"
23+
query = "status:>500 | select count(*) as errorCounts"
24+
start_time_offset = -15
2225
end_time_offset = 0
23-
logset_id = "33aaf0ae-6163-411b-a415-9f27450f68db"
2426
number = 1
27+
syntax_rule = 1
28+
}
29+
30+
analysis {
31+
content = "__FILENAME__"
32+
name = "terraform"
33+
type = "field"
34+
35+
config_info {
36+
key = "QueryIndex"
37+
value = "1"
38+
}
39+
}
40+
41+
monitor_time {
42+
time = 1
43+
type = "Period"
44+
}
45+
46+
tags = {
47+
createdBy = "terraform"
48+
}
49+
}
50+
```
51+
52+
Use multi conditions
53+
54+
```hcl
55+
resource "tencentcloud_cls_alarm" "example" {
56+
name = "tf-example"
57+
alarm_notice_ids = [
58+
"notice-c2af43ee-1a4b-4c4a-ae3e-f81481280101",
59+
]
60+
alarm_period = 15
61+
message_template = "{{.Label}}"
62+
status = true
63+
trigger_count = 1
64+
65+
alarm_targets {
66+
logset_id = "e74efb8e-f647-48b2-a725-43f11b122081"
67+
topic_id = "59cf3ec0-1612-4157-be3f-341b2e7a53cb"
2568
query = "status:>500 | select count(*) as errorCounts"
2669
start_time_offset = -15
27-
topic_id = "88735a07-bea4-4985-8763-e9deb6da4fad"
70+
end_time_offset = 0
71+
number = 1
2872
syntax_rule = 1
2973
}
3074
@@ -39,10 +83,24 @@ resource "tencentcloud_cls_alarm" "example" {
3983
}
4084
}
4185
86+
multi_conditions {
87+
condition = "[$1.__QUERYCOUNT__]> 0"
88+
alarm_level = 1
89+
}
90+
91+
multi_conditions {
92+
condition = "$1.source='10.0.0.1'"
93+
alarm_level = 2
94+
}
95+
4296
monitor_time {
4397
time = 1
4498
type = "Period"
4599
}
100+
101+
tags = {
102+
createdBy = "terraform"
103+
}
46104
}
47105
```
48106

0 commit comments

Comments
 (0)