Skip to content

Commit 139bab2

Browse files
author
hellertang
authored
monitor alarm policy support create tke rules (#756)
1 parent 6d31189 commit 139bab2

File tree

2 files changed

+271
-0
lines changed

2 files changed

+271
-0
lines changed

tencentcloud/resource_tc_monitor_alarm_policy.go

Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ Provides a alarm policy resource for monitor.
33
44
Example Usage
55
6+
cvm_device alarm policy
7+
68
```hcl
79
resource "tencentcloud_monitor_alarm_policy" "group" {
810
policy_name = "hello"
@@ -36,6 +38,99 @@ resource "tencentcloud_monitor_alarm_policy" "group" {
3638
}
3739
}
3840
```
41+
42+
k8s_cluster alarm policy
43+
44+
```hcl
45+
resource "tencentcloud_monitor_alarm_policy" "policy" {
46+
enable = 1
47+
monitor_type = "MT_QCE"
48+
namespace = "k8s_cluster"
49+
notice_ids = [
50+
"notice-l9ziyxw6",
51+
]
52+
policy_name = "TkeClusterNew"
53+
project_id = 1244035
54+
55+
conditions {
56+
is_union_rule = 0
57+
58+
rules {
59+
continue_period = 3
60+
description = "Allocatable Pods"
61+
is_power_notice = 0
62+
metric_name = "K8sClusterAllocatablePodsTotal"
63+
notice_frequency = 3600
64+
operator = "gt"
65+
period = 60
66+
rule_type = "STATIC"
67+
unit = "Count"
68+
value = "10"
69+
70+
filter {
71+
dimensions = jsonencode(
72+
[
73+
[
74+
{
75+
Key = "region"
76+
Operator = "eq"
77+
Value = [
78+
"ap-guangzhou",
79+
]
80+
},
81+
{
82+
Key = "tke_cluster_instance_id"
83+
Operator = "in"
84+
Value = [
85+
"cls-czhtobea",
86+
]
87+
},
88+
],
89+
]
90+
)
91+
type = "DIMENSION"
92+
}
93+
}
94+
rules {
95+
continue_period = 3
96+
description = "Total CPU Cores"
97+
is_power_notice = 0
98+
metric_name = "K8sClusterCpuCoreTotal"
99+
notice_frequency = 3600
100+
operator = "gt"
101+
period = 60
102+
rule_type = "STATIC"
103+
unit = "Core"
104+
value = "2"
105+
106+
filter {
107+
dimensions = jsonencode(
108+
[
109+
[
110+
{
111+
Key = "region"
112+
Operator = "eq"
113+
Value = [
114+
"ap-guangzhou",
115+
]
116+
},
117+
{
118+
Key = "tke_cluster_instance_id"
119+
Operator = "in"
120+
Value = [
121+
"cls-czhtobea",
122+
]
123+
},
124+
],
125+
]
126+
)
127+
type = "DIMENSION"
128+
}
129+
}
130+
}
131+
}
132+
```
133+
39134
Import
40135
41136
Alarm policy instance can be imported, e.g.
@@ -98,6 +193,26 @@ func AlarmPolicyRule() map[string]*schema.Schema {
98193
Computed: true,
99194
Description: "Whether the alarm frequency increases exponentially.",
100195
},
196+
"filter": {
197+
Type: schema.TypeList,
198+
Optional: true,
199+
MaxItems: 1,
200+
Description: "Filter condition for one single trigger rule.",
201+
Elem: &schema.Resource{
202+
Schema: map[string]*schema.Schema{
203+
"type": {
204+
Type: schema.TypeString,
205+
Required: true,
206+
Description: "Filter condition type. Valid values: DIMENSION (uses dimensions for filtering).",
207+
},
208+
"dimensions": {
209+
Type: schema.TypeString,
210+
Required: true,
211+
Description: "JSON string generated by serializing the AlarmPolicyDimension two-dimensional array.",
212+
},
213+
},
214+
},
215+
},
101216
"description": {
102217
Type: schema.TypeString,
103218
Optional: true,
@@ -315,6 +430,16 @@ func resourceTencentMonitorAlarmPolicyCreate(d *schema.ResourceData, meta interf
315430
if m["is_power_notice"] != nil {
316431
alarmPolicyRule.IsPowerNotice = helper.IntInt64(m["is_power_notice"].(int))
317432
}
433+
if m["filter"] != nil {
434+
filters := m["filter"].([]interface{})
435+
filter := filters[0].(map[string]interface{})
436+
alarmPolicyFilter := monitor.AlarmPolicyFilter{
437+
Type: helper.String(filter["type"].(string)),
438+
Dimensions: helper.String(filter["dimensions"].(string)),
439+
}
440+
alarmPolicyRule.Filter = &alarmPolicyFilter
441+
}
442+
318443
if m["description"] != nil {
319444
alarmPolicyRule.Description = helper.String(m["description"].(string))
320445
}
@@ -356,6 +481,15 @@ func resourceTencentMonitorAlarmPolicyCreate(d *schema.ResourceData, meta interf
356481
if m["is_power_notice"] != nil {
357482
alarmPolicyRule.IsPowerNotice = helper.IntInt64(m["is_power_notice"].(int))
358483
}
484+
if m["filter"] != nil {
485+
filters := m["filter"].([]interface{})
486+
filter := filters[0].(map[string]interface{})
487+
alarmPolicyFilter := monitor.AlarmPolicyFilter{
488+
Type: helper.String(filter["type"].(string)),
489+
Dimensions: helper.String(filter["dimensions"].(string)),
490+
}
491+
alarmPolicyRule.Filter = &alarmPolicyFilter
492+
}
359493
if m["description"] != nil {
360494
alarmPolicyRule.Description = helper.String(m["description"].(string))
361495
}
@@ -461,13 +595,21 @@ func resourceTencentMonitorAlarmPolicyRead(d *schema.ResourceData, meta interfac
461595

462596
var rules = make([]interface{}, 0, 100)
463597
for _, rule := range policy.Condition.Rules {
598+
var filter = make([]interface{}, 0, 10)
599+
alarmPolicyFilter := map[string]interface{}{
600+
"type": rule.Filter.Type,
601+
"dimensions": rule.Filter.Dimensions,
602+
}
603+
filter = append(filter, alarmPolicyFilter)
604+
464605
m := map[string]interface{}{
465606
"metric_name": rule.MetricName,
466607
"period": rule.Period,
467608
"operator": rule.Operator,
468609
"value": rule.Value,
469610
"continue_period": rule.ContinuePeriod,
470611
"notice_frequency": rule.NoticeFrequency,
612+
"filter": filter,
471613
"description": rule.Description,
472614
"unit": rule.Unit,
473615
"rule_type": rule.RuleType,
@@ -483,6 +625,13 @@ func resourceTencentMonitorAlarmPolicyRead(d *schema.ResourceData, meta interfac
483625

484626
eventConditions := make([]map[string]interface{}, 0, len(policy.EventCondition.Rules))
485627
for _, eventRule := range policy.EventCondition.Rules {
628+
var filter = make([]interface{}, 0, 10)
629+
alarmPolicyFilter := map[string]interface{}{
630+
"type": eventRule.Filter.Type,
631+
"dimensions": eventRule.Filter.Dimensions,
632+
}
633+
filter = append(filter, alarmPolicyFilter)
634+
486635
m := make(map[string]interface{}, 5)
487636
m["metric_name"] = eventRule.MetricName
488637
m["period"] = eventRule.Period
@@ -492,6 +641,7 @@ func resourceTencentMonitorAlarmPolicyRead(d *schema.ResourceData, meta interfac
492641
m["notice_frequency"] = eventRule.NoticeFrequency
493642
m["is_power_notice"] = eventRule.IsPowerNotice
494643
m["notice_frequency"] = eventRule.NoticeFrequency
644+
m["filter"] = filter
495645
m["description"] = eventRule.Description
496646
m["unit"] = eventRule.Unit
497647
m["rule_type"] = eventRule.RuleType
@@ -595,6 +745,16 @@ func resourceTencentMonitorAlarmPolicyUpdate(d *schema.ResourceData, meta interf
595745
if m["is_power_notice"] != nil {
596746
alarmPolicyRule.IsPowerNotice = helper.IntInt64(m["is_power_notice"].(int))
597747
}
748+
if m["filter"] != nil {
749+
filters := m["filter"].([]interface{})
750+
// Max Items is 1
751+
filter := filters[0].(map[string]interface{})
752+
alarmPolicyFilter := monitor.AlarmPolicyFilter{
753+
Type: helper.String(filter["type"].(string)),
754+
Dimensions: helper.String(filter["dimensions"].(string)),
755+
}
756+
alarmPolicyRule.Filter = &alarmPolicyFilter
757+
}
598758
if m["description"] != nil {
599759
alarmPolicyRule.Description = helper.String(m["description"].(string))
600760
}
@@ -635,6 +795,16 @@ func resourceTencentMonitorAlarmPolicyUpdate(d *schema.ResourceData, meta interf
635795
if m["is_power_notice"] != nil {
636796
alarmPolicyRule.IsPowerNotice = helper.IntInt64(m["is_power_notice"].(int))
637797
}
798+
if m["filter"] != nil {
799+
filters := m["filter"].([]interface{})
800+
// Max Items is 1
801+
filter := filters[0].(map[string]interface{})
802+
alarmPolicyFilter := monitor.AlarmPolicyFilter{
803+
Type: helper.String(filter["type"].(string)),
804+
Dimensions: helper.String(filter["dimensions"].(string)),
805+
}
806+
alarmPolicyRule.Filter = &alarmPolicyFilter
807+
}
638808
if m["description"] != nil {
639809
alarmPolicyRule.Description = helper.String(m["description"].(string))
640810
}

website/docs/r/monitor_alarm_policy.html.markdown

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ Provides a alarm policy resource for monitor.
1313

1414
## Example Usage
1515

16+
cvm_device alarm policy
17+
1618
```hcl
1719
resource "tencentcloud_monitor_alarm_policy" "group" {
1820
policy_name = "hello"
@@ -47,6 +49,98 @@ resource "tencentcloud_monitor_alarm_policy" "group" {
4749
}
4850
```
4951

52+
k8s_cluster alarm policy
53+
54+
```hcl
55+
resource "tencentcloud_monitor_alarm_policy" "policy" {
56+
enable = 1
57+
monitor_type = "MT_QCE"
58+
namespace = "k8s_cluster"
59+
notice_ids = [
60+
"notice-l9ziyxw6",
61+
]
62+
policy_name = "TkeClusterNew"
63+
project_id = 1244035
64+
65+
conditions {
66+
is_union_rule = 0
67+
68+
rules {
69+
continue_period = 3
70+
description = "Allocatable Pods"
71+
is_power_notice = 0
72+
metric_name = "K8sClusterAllocatablePodsTotal"
73+
notice_frequency = 3600
74+
operator = "gt"
75+
period = 60
76+
rule_type = "STATIC"
77+
unit = "Count"
78+
value = "10"
79+
80+
filter {
81+
dimensions = jsonencode(
82+
[
83+
[
84+
{
85+
Key = "region"
86+
Operator = "eq"
87+
Value = [
88+
"ap-guangzhou",
89+
]
90+
},
91+
{
92+
Key = "tke_cluster_instance_id"
93+
Operator = "in"
94+
Value = [
95+
"cls-czhtobea",
96+
]
97+
},
98+
],
99+
]
100+
)
101+
type = "DIMENSION"
102+
}
103+
}
104+
rules {
105+
continue_period = 3
106+
description = "Total CPU Cores"
107+
is_power_notice = 0
108+
metric_name = "K8sClusterCpuCoreTotal"
109+
notice_frequency = 3600
110+
operator = "gt"
111+
period = 60
112+
rule_type = "STATIC"
113+
unit = "Core"
114+
value = "2"
115+
116+
filter {
117+
dimensions = jsonencode(
118+
[
119+
[
120+
{
121+
Key = "region"
122+
Operator = "eq"
123+
Value = [
124+
"ap-guangzhou",
125+
]
126+
},
127+
{
128+
Key = "tke_cluster_instance_id"
129+
Operator = "in"
130+
Value = [
131+
"cls-czhtobea",
132+
]
133+
},
134+
],
135+
]
136+
)
137+
type = "DIMENSION"
138+
}
139+
}
140+
}
141+
}
142+
```
143+
50144
## Argument Reference
51145

52146
The following arguments are supported:
@@ -72,6 +166,7 @@ The `event_conditions` object supports the following:
72166

73167
* `continue_period` - (Optional) Number of periods.
74168
* `description` - (Optional) Metric display name, which is used in the output parameter.
169+
* `filter` - (Optional) Filter condition for one single trigger rule.
75170
* `is_power_notice` - (Optional) Whether the alarm frequency increases exponentially.
76171
* `metric_name` - (Optional) Metric name or event name.
77172
* `notice_frequency` - (Optional) Alarm interval in seconds.
@@ -81,10 +176,16 @@ The `event_conditions` object supports the following:
81176
* `unit` - (Optional) Unit, which is used in the output parameter.
82177
* `value` - (Optional) Threshold.
83178

179+
The `filter` object supports the following:
180+
181+
* `dimensions` - (Required) JSON string generated by serializing the AlarmPolicyDimension two-dimensional array.
182+
* `type` - (Required) Filter condition type. Valid values: DIMENSION (uses dimensions for filtering).
183+
84184
The `rules` object supports the following:
85185

86186
* `continue_period` - (Optional) Number of periods.
87187
* `description` - (Optional) Metric display name, which is used in the output parameter.
188+
* `filter` - (Optional) Filter condition for one single trigger rule.
88189
* `is_power_notice` - (Optional) Whether the alarm frequency increases exponentially.
89190
* `metric_name` - (Optional) Metric name or event name.
90191
* `notice_frequency` - (Optional) Alarm interval in seconds.

0 commit comments

Comments
 (0)