Skip to content

fix(cls): [120871100] tencentcloud_cls_alarm support multi_conditions #2985

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/2985.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
resource/tencentcloud_cls_alarm: support `multi_conditions`
```
110 changes: 95 additions & 15 deletions tencentcloud/services/cls/resource_tc_cls_alarm.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,16 +99,41 @@ func ResourceTencentCloudClsAlarm() *schema.Resource {
},

"condition": {
Required: true,
Type: schema.TypeString,
Description: "triggering conditions.",
Optional: true,
Type: schema.TypeString,
ExactlyOneOf: []string{"multi_conditions"},
Description: "Trigger condition.",
},

"alarm_level": {
Optional: true,
Computed: true,
Type: schema.TypeInt,
Description: "Alarm level. 0: Warning; 1: Info; 2: Critical. Default is 0.",
Optional: true,
Computed: true,
Type: schema.TypeInt,
ConflictsWith: []string{"multi_conditions"},
RequiredWith: []string{"condition"},
Description: "Alarm level. 0: Warning; 1: Info; 2: Critical. Default is 0.",
},

"multi_conditions": {
Optional: true,
Type: schema.TypeList,
ExactlyOneOf: []string{"condition"},
Description: "Multiple triggering conditions.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"condition": {
Type: schema.TypeString,
Optional: true,
Description: "Trigger condition.",
},
"alarm_level": {
Type: schema.TypeInt,
Optional: true,
Computed: true,
Description: "Alarm level. 0: Warning; 1: Info; 2: Critical. Default is 0.",
},
},
},
},

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

if v, ok := d.GetOk("condition"); ok {
var changeCondition bool
if v, ok := d.GetOk("condition"); ok && v.(string) != "" {
request.Condition = helper.String(v.(string))
request.AlarmLevel = helper.IntUint64(0)
changeCondition = true
}

if v, ok := d.GetOkExists("alarm_level"); ok {
if v, ok := d.GetOkExists("alarm_level"); ok && changeCondition {
request.AlarmLevel = helper.IntUint64(v.(int))
}

if v, ok := d.GetOk("multi_conditions"); ok {
for _, item := range v.([]interface{}) {
dMap := item.(map[string]interface{})
multiCondition := cls.MultiCondition{}
if v, ok := dMap["condition"]; ok {
multiCondition.Condition = helper.String(v.(string))
multiCondition.AlarmLevel = helper.IntUint64(0)
}

if v, ok := dMap["alarm_level"]; ok {
multiCondition.AlarmLevel = helper.IntUint64(v.(int))
}

request.MultiConditions = append(request.MultiConditions, &multiCondition)
}
}

if v, ok := d.GetOkExists("trigger_count"); ok {
request.TriggerCount = helper.IntInt64(v.(int))
}
Expand Down Expand Up @@ -486,12 +530,29 @@ func resourceTencentCloudClsAlarmRead(d *schema.ResourceData, meta interface{})
_ = d.Set("monitor_time", []interface{}{monitorTimeMap})
}

if alarm.Condition != nil {
if alarm.Condition != nil && *alarm.Condition != "" {
_ = d.Set("condition", alarm.Condition)
if alarm.AlarmLevel != nil {
_ = d.Set("alarm_level", alarm.AlarmLevel)
}
}

if alarm.AlarmLevel != nil {
_ = d.Set("alarm_level", alarm.AlarmLevel)
if alarm.MultiConditions != nil {
tmpList := make([]map[string]interface{}, 0)
for _, item := range alarm.MultiConditions {
dMap := make(map[string]interface{})
if item.Condition != nil {
dMap["condition"] = *item.Condition
}

if item.AlarmLevel != nil {
dMap["alarm_level"] = *item.AlarmLevel
}

tmpList = append(tmpList, dMap)
}

_ = d.Set("multi_conditions", tmpList)
}

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

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

if v, ok := d.GetOk("condition"); ok {
var changeCondition bool
if v, ok := d.GetOk("condition"); ok && v.(string) != "" {
request.Condition = helper.String(v.(string))
request.AlarmLevel = helper.IntUint64(0)
changeCondition = true
}

if v, ok := d.GetOkExists("alarm_level"); ok {
if v, ok := d.GetOkExists("alarm_level"); ok && changeCondition {
request.AlarmLevel = helper.IntUint64(v.(int))
}

if v, ok := d.GetOk("multi_conditions"); ok {
for _, item := range v.([]interface{}) {
dMap := item.(map[string]interface{})
multiCondition := cls.MultiCondition{}
if v, ok := dMap["condition"]; ok {
multiCondition.Condition = helper.String(v.(string))
multiCondition.AlarmLevel = helper.IntUint64(0)
}

if v, ok := dMap["alarm_level"]; ok {
multiCondition.AlarmLevel = helper.IntUint64(v.(int))
}

request.MultiConditions = append(request.MultiConditions, &multiCondition)
}
}

if v, ok := d.GetOkExists("trigger_count"); ok {
request.TriggerCount = helper.IntInt64(v.(int))
}
Expand Down
78 changes: 68 additions & 10 deletions tencentcloud/services/cls/resource_tc_cls_alarm.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,73 @@ Provides a resource to create a cls alarm

Example Usage

Use single condition

```hcl
resource "tencentcloud_cls_alarm" "example" {
name = "tf-example"
name = "tf-example"
alarm_notice_ids = [
"notice-0850756b-245d-4bc7-bb27-2a58fffc780b",
"notice-c2af43ee-1a4b-4c4a-ae3e-f81481280101",
]
alarm_period = 15
condition = "test"
alarm_level = 0
condition = "$1.source='10.0.0.1'"
alarm_level = 1
message_template = "{{.Label}}"
status = true
tags = {
"createdBy" = "terraform"
}
trigger_count = 1
trigger_count = 1

alarm_targets {
logset_id = "e74efb8e-f647-48b2-a725-43f11b122081"
topic_id = "59cf3ec0-1612-4157-be3f-341b2e7a53cb"
query = "status:>500 | select count(*) as errorCounts"
start_time_offset = -15
end_time_offset = 0
logset_id = "33aaf0ae-6163-411b-a415-9f27450f68db"
number = 1
syntax_rule = 1
}

analysis {
content = "__FILENAME__"
name = "terraform"
type = "field"

config_info {
key = "QueryIndex"
value = "1"
}
}

monitor_time {
time = 1
type = "Period"
}

tags = {
createdBy = "terraform"
}
}
```

Use multi conditions

```hcl
resource "tencentcloud_cls_alarm" "example" {
name = "tf-example"
alarm_notice_ids = [
"notice-c2af43ee-1a4b-4c4a-ae3e-f81481280101",
]
alarm_period = 15
message_template = "{{.Label}}"
status = true
trigger_count = 1

alarm_targets {
logset_id = "e74efb8e-f647-48b2-a725-43f11b122081"
topic_id = "59cf3ec0-1612-4157-be3f-341b2e7a53cb"
query = "status:>500 | select count(*) as errorCounts"
start_time_offset = -15
topic_id = "88735a07-bea4-4985-8763-e9deb6da4fad"
end_time_offset = 0
number = 1
syntax_rule = 1
}

Expand All @@ -39,10 +83,24 @@ resource "tencentcloud_cls_alarm" "example" {
}
}

multi_conditions {
condition = "[$1.__QUERYCOUNT__]> 0"
alarm_level = 1
}

multi_conditions {
condition = "$1.source='10.0.0.1'"
alarm_level = 2
}

monitor_time {
time = 1
type = "Period"
}

tags = {
createdBy = "terraform"
}
}
```

Expand Down
Loading
Loading