Skip to content

Commit 35c357b

Browse files
authored
Merge pull request #2103 from tencentcloudstack/fix/tmp-alert_policy
Fix/tmp alert policy
2 parents e7dd860 + bc4fcbd commit 35c357b

File tree

3 files changed

+75
-6
lines changed

3 files changed

+75
-6
lines changed

.changelog/2103.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_tke_tmp_alert_policy: support modify 'alert_rule'.
3+
```

tencentcloud/resource_tc_monitor_tmp_tke_alert_policy.go

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -755,18 +755,18 @@ func resourceTencentCloudTkeTmpAlertPolicyUpdate(d *schema.ResourceData, meta in
755755
}
756756

757757
instanceId := ids[0]
758+
policyId := ids[1]
758759

759760
request.InstanceId = &instanceId
760761

761762
if d.HasChange("instance_id") {
762763
return fmt.Errorf("`instance_id` do not support change now.")
763764
}
764765

765-
if d.HasChange("alert_rule") {
766-
return fmt.Errorf("`alert_rule` do not support change now.")
767-
}
768766
if dMap, ok := helper.InterfacesHeadMap(d, "alert_rule"); ok {
769767
prometheusAlertPolicyItem := monitor.PrometheusAlertPolicyItem{}
768+
prometheusAlertPolicyItem.Id = &policyId
769+
770770
if v, ok := dMap["name"]; ok {
771771
prometheusAlertPolicyItem.Name = helper.String(v.(string))
772772
}
@@ -802,15 +802,25 @@ func resourceTencentCloudTkeTmpAlertPolicyUpdate(d *schema.ResourceData, meta in
802802
prometheusAlertRule.Annotations = append(prometheusAlertRule.Annotations, &label)
803803
}
804804
}
805+
if v, ok := RulesMap["labels"]; ok {
806+
for _, item := range v.([]interface{}) {
807+
labelsMap := item.(map[string]interface{})
808+
label := monitor.Label{}
809+
if v, ok := labelsMap["name"]; ok {
810+
label.Name = helper.String(v.(string))
811+
}
812+
if v, ok := labelsMap["value"]; ok {
813+
label.Value = helper.String(v.(string))
814+
}
815+
prometheusAlertRule.Labels = append(prometheusAlertRule.Labels, &label)
816+
}
817+
}
805818
if v, ok := RulesMap["rule_state"]; ok {
806819
prometheusAlertRule.RuleState = helper.IntInt64(v.(int))
807820
}
808821
prometheusAlertPolicyItem.Rules = append(prometheusAlertPolicyItem.Rules, &prometheusAlertRule)
809822
}
810823
}
811-
if v, ok := dMap["id"]; ok {
812-
prometheusAlertPolicyItem.Id = helper.String(v.(string))
813-
}
814824
if v, ok := dMap["template_id"]; ok {
815825
prometheusAlertPolicyItem.TemplateId = helper.String(v.(string))
816826
}

tencentcloud/resource_tc_monitor_tmp_tke_alert_policy_test.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,37 @@ func TestAccTencentCloudMonitorTmpTkeAlertPolicy_basic(t *testing.T) {
8787
ImportState: true,
8888
ImportStateVerify: true,
8989
},
90+
{
91+
Config: testTmpTkeAlertPolicyUp_basic,
92+
Check: resource.ComposeTestCheckFunc(
93+
testAccCheckTmpTkeAlertPolicyExists("tencentcloud_monitor_tmp_tke_alert_policy.basic"),
94+
resource.TestCheckResourceAttr("tencentcloud_monitor_tmp_tke_alert_policy.basic", "alert_rule.#", "1"),
95+
resource.TestCheckResourceAttr("tencentcloud_monitor_tmp_tke_alert_policy.basic", "alert_rule.0.cluster_id", ""),
96+
resource.TestCheckResourceAttr("tencentcloud_monitor_tmp_tke_alert_policy.basic", "alert_rule.0.id", ""),
97+
resource.TestCheckResourceAttr("tencentcloud_monitor_tmp_tke_alert_policy.basic", "alert_rule.0.name", "alert_rule-update"),
98+
resource.TestCheckResourceAttr("tencentcloud_monitor_tmp_tke_alert_policy.basic", "alert_rule.0.notification.#", "1"),
99+
resource.TestCheckResourceAttr("tencentcloud_monitor_tmp_tke_alert_policy.basic", "alert_rule.0.notification.0.enabled", "true"),
100+
resource.TestCheckResourceAttr("tencentcloud_monitor_tmp_tke_alert_policy.basic", "alert_rule.0.notification.0.phone_arrive_notice", "false"),
101+
resource.TestCheckResourceAttr("tencentcloud_monitor_tmp_tke_alert_policy.basic", "alert_rule.0.notification.0.phone_circle_interval", "0"),
102+
resource.TestCheckResourceAttr("tencentcloud_monitor_tmp_tke_alert_policy.basic", "alert_rule.0.notification.0.phone_circle_times", "0"),
103+
resource.TestCheckResourceAttr("tencentcloud_monitor_tmp_tke_alert_policy.basic", "alert_rule.0.notification.0.phone_inner_interval", "0"),
104+
resource.TestCheckResourceAttr("tencentcloud_monitor_tmp_tke_alert_policy.basic", "alert_rule.0.notification.0.repeat_interval", ""),
105+
resource.TestCheckResourceAttr("tencentcloud_monitor_tmp_tke_alert_policy.basic", "alert_rule.0.notification.0.time_range_end", ""),
106+
resource.TestCheckResourceAttr("tencentcloud_monitor_tmp_tke_alert_policy.basic", "alert_rule.0.notification.0.time_range_start", ""),
107+
resource.TestCheckResourceAttr("tencentcloud_monitor_tmp_tke_alert_policy.basic", "alert_rule.0.notification.0.type", "amp"),
108+
resource.TestCheckResourceAttr("tencentcloud_monitor_tmp_tke_alert_policy.basic", "alert_rule.0.notification.0.web_hook", ""),
109+
resource.TestCheckResourceAttr("tencentcloud_monitor_tmp_tke_alert_policy.basic", "alert_rule.0.rules.#", "1"),
110+
resource.TestCheckResourceAttr("tencentcloud_monitor_tmp_tke_alert_policy.basic", "alert_rule.0.rules.0.describe", ""),
111+
resource.TestCheckResourceAttr("tencentcloud_monitor_tmp_tke_alert_policy.basic", "alert_rule.0.rules.0.for", "3m"),
112+
resource.TestCheckResourceAttr("tencentcloud_monitor_tmp_tke_alert_policy.basic", "alert_rule.0.rules.0.labels.#", "1"),
113+
resource.TestCheckResourceAttr("tencentcloud_monitor_tmp_tke_alert_policy.basic", "alert_rule.0.rules.0.labels.0.name", "severity"),
114+
resource.TestCheckResourceAttr("tencentcloud_monitor_tmp_tke_alert_policy.basic", "alert_rule.0.rules.0.labels.0.value", "warning"),
115+
resource.TestCheckResourceAttr("tencentcloud_monitor_tmp_tke_alert_policy.basic", "alert_rule.0.rules.0.name", "rules-update"),
116+
resource.TestCheckResourceAttr("tencentcloud_monitor_tmp_tke_alert_policy.basic", "alert_rule.0.rules.0.rule", "(count(kube_node_status_allocatable_cpu_cores) by (cluster) -1) / count(kube_node_status_allocatable_cpu_cores) by (cluster)"),
117+
resource.TestCheckResourceAttr("tencentcloud_monitor_tmp_tke_alert_policy.basic", "alert_rule.0.rules.0.rule_state", "0"),
118+
resource.TestCheckResourceAttr("tencentcloud_monitor_tmp_tke_alert_policy.basic", "alert_rule.0.rules.0.template", "集群{{ $labels.cluster }}内Pod申请的CPU过载,当前CPU申请占比{{ $value | humanizePercentage }}"),
119+
),
120+
},
90121
},
91122
})
92123
}
@@ -181,3 +212,28 @@ resource "tencentcloud_monitor_tmp_tke_alert_policy" "basic" {
181212
}
182213
}
183214
}`
215+
216+
const testTmpTkeAlertPolicyUp_basic = testTmpTkeAlertPolicyVar + `
217+
resource "tencentcloud_monitor_tmp_tke_alert_policy" "basic" {
218+
instance_id = var.prometheus_id
219+
alert_rule {
220+
name = "alert_rule-update"
221+
rules {
222+
name = "rules-update"
223+
rule = "(count(kube_node_status_allocatable_cpu_cores) by (cluster) -1) / count(kube_node_status_allocatable_cpu_cores) by (cluster)"
224+
template = "集群{{ $labels.cluster }}内Pod申请的CPU过载,当前CPU申请占比{{ $value | humanizePercentage }}"
225+
for = "3m"
226+
labels {
227+
name = "severity"
228+
value = "warning"
229+
}
230+
}
231+
notification {
232+
type = "amp"
233+
enabled = true
234+
alert_manager {
235+
url = "xxx"
236+
}
237+
}
238+
}
239+
}`

0 commit comments

Comments
 (0)