Skip to content

Commit

Permalink
add
Browse files Browse the repository at this point in the history
  • Loading branch information
SevenEarth committed Dec 2, 2024
1 parent 5120ae2 commit 601bd3d
Show file tree
Hide file tree
Showing 3 changed files with 226 additions and 28 deletions.
92 changes: 84 additions & 8 deletions tencentcloud/services/cls/resource_tc_cls_alarm.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,16 +99,40 @@ func ResourceTencentCloudClsAlarm() *schema.Resource {
},

"condition": {
Required: true,
Type: schema.TypeString,
Description: "triggering conditions.",
Optional: true,
Type: schema.TypeString,
ConflictsWith: []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"},
Description: "Alarm level. 0: Warning; 1: Info; 2: Critical. Default is 0.",
},

"multi_conditions": {
Optional: true,
Type: schema.TypeList,
ConflictsWith: []string{"condition", "alarm_level"},
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 @@ -296,6 +320,23 @@ func resourceTencentCloudClsAlarmCreate(d *schema.ResourceData, meta interface{}
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 @@ -494,6 +535,24 @@ func resourceTencentCloudClsAlarmRead(d *schema.ResourceData, meta interface{})
_ = 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 {
_ = d.Set("trigger_count", alarm.TriggerCount)
}
Expand Down Expand Up @@ -597,7 +656,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 @@ -671,6 +730,23 @@ func resourceTencentCloudClsAlarmUpdate(d *schema.ResourceData, meta interface{}
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
84 changes: 74 additions & 10 deletions website/docs/r/cls_alarm.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,73 @@ Provides a resource to create a cls alarm

## Example Usage

### Use single condition

```hcl
resource "tencentcloud_cls_alarm" "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 @@ -50,10 +94,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 All @@ -64,14 +122,15 @@ The following arguments are supported:
* `alarm_notice_ids` - (Required, Set: [`String`]) list of alarm notice id.
* `alarm_period` - (Required, Int) alarm repeat cycle.
* `alarm_targets` - (Required, List) list of alarm target.
* `condition` - (Required, String) triggering conditions.
* `monitor_time` - (Required, List) monitor task execution time.
* `name` - (Required, String) log alarm name.
* `trigger_count` - (Required, Int) continuous cycle.
* `alarm_level` - (Optional, Int) Alarm level. 0: Warning; 1: Info; 2: Critical. Default is 0.
* `analysis` - (Optional, List) multidimensional analysis.
* `call_back` - (Optional, List) user define callback.
* `condition` - (Optional, String) Trigger condition.
* `message_template` - (Optional, String) user define alarm notice.
* `multi_conditions` - (Optional, List) Multiple triggering conditions.
* `status` - (Optional, Bool) whether to enable the alarm policy.
* `tags` - (Optional, Map) Tag description list.

Expand Down Expand Up @@ -107,6 +166,11 @@ The `monitor_time` object supports the following:
* `time` - (Required, Int) time period or point in time.
* `type` - (Required, String) Period for periodic execution, Fixed for regular execution.

The `multi_conditions` object supports the following:

* `alarm_level` - (Optional, Int) Alarm level. 0: Warning; 1: Info; 2: Critical. Default is 0.
* `condition` - (Optional, String) Trigger condition.

## Attributes Reference

In addition to all arguments above, the following attributes are exported:
Expand Down

0 comments on commit 601bd3d

Please sign in to comment.