Skip to content

Commit 0daf098

Browse files
gitmknanonymous
andauthored
fix: alert_rule optimization (#1407)
* fix: alert_rule optimization * feat: add changelog' Co-authored-by: anonymous <anonymous@mail.org>
1 parent 39c3774 commit 0daf098

File tree

3 files changed

+77
-34
lines changed

3 files changed

+77
-34
lines changed

.changelog/1407.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_monitor_tmp_alert_rule: Parameter check
3+
```

tencentcloud/resource_tc_monitor_tmp_alert_rule.go

Lines changed: 44 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import (
3939

4040
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
4141
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
42+
sdkErrors "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/errors"
4243
monitor "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/monitor/v20180724"
4344
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
4445
)
@@ -172,6 +173,9 @@ func resourceTencentCloudMonitorTmpAlertRuleCreate(d *schema.ResourceData, meta
172173
labelsList := v.([]interface{})
173174
prometheusRuleKV := make([]*monitor.PrometheusRuleKV, 0, len(labelsList))
174175
for _, labels := range labelsList {
176+
if labels == nil {
177+
return fmt.Errorf("Invalid `labels` parameter, must not be empty")
178+
}
175179
label := labels.(map[string]interface{})
176180
var kv monitor.PrometheusRuleKV
177181
kv.Key = helper.String(label["key"].(string))
@@ -184,6 +188,9 @@ func resourceTencentCloudMonitorTmpAlertRuleCreate(d *schema.ResourceData, meta
184188
annotationsList := v.([]interface{})
185189
prometheusRuleKV := make([]*monitor.PrometheusRuleKV, 0, len(annotationsList))
186190
for _, annotations := range annotationsList {
191+
if annotations == nil {
192+
return fmt.Errorf("Invalid `annotation` parameter, must not be empty")
193+
}
187194
annotation := annotations.(map[string]interface{})
188195
var kv monitor.PrometheusRuleKV
189196
kv.Key = helper.String(annotation["key"].(string))
@@ -199,6 +206,10 @@ func resourceTencentCloudMonitorTmpAlertRuleCreate(d *schema.ResourceData, meta
199206
err := resource.Retry(writeRetryTimeout, func() *resource.RetryError {
200207
result, e := meta.(*TencentCloudClient).apiV3Conn.UseMonitorClient().CreateAlertRule(request)
201208
if e != nil {
209+
ee, ok := e.(*sdkErrors.TencentCloudSDKError)
210+
if ok && IsContains("FailedOperation", ee.Code) {
211+
return resource.NonRetryableError(ee)
212+
}
202213
return retryError(e)
203214
} else {
204215
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n",
@@ -279,7 +290,7 @@ func resourceTencentCloudMonitorTmpAlertRuleRead(d *schema.ResourceData, meta in
279290
_ = d.Set("labels", result)
280291
}
281292
if tmpAlertRule.Annotations != nil {
282-
annotationsList := tmpAlertRule.Labels
293+
annotationsList := tmpAlertRule.Annotations
283294
result := make([]map[string]interface{}, 0, len(annotationsList))
284295
for _, v := range annotationsList {
285296
mapping := map[string]interface{}{
@@ -334,47 +345,47 @@ func resourceTencentCloudMonitorTmpAlertRuleUpdate(d *schema.ResourceData, meta
334345
request.RuleState = helper.IntInt64(v.(int))
335346
}
336347

337-
if d.HasChange("duration") {
338-
if v, ok := d.GetOk("duration"); ok {
339-
request.Duration = helper.String(v.(string))
340-
}
348+
if v, ok := d.GetOk("duration"); ok {
349+
request.Duration = helper.String(v.(string))
341350
}
342-
if d.HasChange("labels") {
343-
if v, ok := d.GetOk("labels"); ok {
344-
labelsList := v.([]interface{})
345-
prometheusRuleKV := make([]*monitor.PrometheusRuleKV, 0, len(labelsList))
346-
for _, labels := range labelsList {
347-
label := labels.(map[string]interface{})
348-
var kv monitor.PrometheusRuleKV
349-
kv.Key = helper.String(label["key"].(string))
350-
kv.Value = helper.String(label["value"].(string))
351-
prometheusRuleKV = append(prometheusRuleKV, &kv)
352-
}
353-
request.Labels = prometheusRuleKV
351+
352+
if v, ok := d.GetOk("labels"); ok {
353+
labelsList := v.([]interface{})
354+
prometheusRuleKV := make([]*monitor.PrometheusRuleKV, 0, len(labelsList))
355+
for _, labels := range labelsList {
356+
label := labels.(map[string]interface{})
357+
var kv monitor.PrometheusRuleKV
358+
kv.Key = helper.String(label["key"].(string))
359+
kv.Value = helper.String(label["value"].(string))
360+
prometheusRuleKV = append(prometheusRuleKV, &kv)
354361
}
362+
request.Labels = prometheusRuleKV
355363
}
356-
if d.HasChange("annotations") {
357-
if v, ok := d.GetOk("annotations"); ok {
358-
annotationsList := v.([]interface{})
359-
prometheusRuleKV := make([]*monitor.PrometheusRuleKV, 0, len(annotationsList))
360-
for _, annotations := range annotationsList {
361-
annotation := annotations.(map[string]interface{})
362-
var kv monitor.PrometheusRuleKV
363-
kv.Key = helper.String(annotation["key"].(string))
364-
kv.Value = helper.String(annotation["value"].(string))
365-
prometheusRuleKV = append(prometheusRuleKV, &kv)
366-
}
367-
request.Annotations = prometheusRuleKV
364+
365+
if v, ok := d.GetOk("annotations"); ok {
366+
annotationsList := v.([]interface{})
367+
prometheusRuleKV := make([]*monitor.PrometheusRuleKV, 0, len(annotationsList))
368+
for _, annotations := range annotationsList {
369+
annotation := annotations.(map[string]interface{})
370+
var kv monitor.PrometheusRuleKV
371+
kv.Key = helper.String(annotation["key"].(string))
372+
kv.Value = helper.String(annotation["value"].(string))
373+
prometheusRuleKV = append(prometheusRuleKV, &kv)
368374
}
375+
request.Annotations = prometheusRuleKV
369376
}
370-
if d.HasChange("type") {
371-
if v, ok := d.GetOk("type"); ok {
372-
request.Type = helper.String(v.(string))
373-
}
377+
378+
if v, ok := d.GetOk("type"); ok {
379+
request.Type = helper.String(v.(string))
374380
}
381+
375382
err := resource.Retry(writeRetryTimeout, func() *resource.RetryError {
376383
result, e := meta.(*TencentCloudClient).apiV3Conn.UseMonitorClient().UpdateAlertRule(request)
377384
if e != nil {
385+
ee, ok := e.(*sdkErrors.TencentCloudSDKError)
386+
if ok && IsContains("FailedOperation", ee.Code) {
387+
return resource.NonRetryableError(ee)
388+
}
378389
return retryError(e)
379390
} else {
380391
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n",

tencentcloud/resource_tc_monitor_tmp_alert_rule_test.go

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ import (
1010
"github.com/hashicorp/terraform-plugin-sdk/terraform"
1111
)
1212

13-
func TestAccTencentCloudMonitorAlertRule_basic(t *testing.T) {
13+
// go test -i; go test -test.run TestAccTencentCloudMonitorAlertRuleResource_basic -v
14+
func TestAccTencentCloudMonitorAlertRuleResource_basic(t *testing.T) {
1415
t.Parallel()
1516
resource.Test(t, resource.TestCase{
1617
PreCheck: func() { testAccPreCheckCommon(t, ACCOUNT_TYPE_COMMON) },
@@ -25,6 +26,12 @@ func TestAccTencentCloudMonitorAlertRule_basic(t *testing.T) {
2526
resource.TestCheckResourceAttr("tencentcloud_monitor_tmp_alert_rule.basic", "receivers.#", "1"),
2627
resource.TestCheckResourceAttr("tencentcloud_monitor_tmp_alert_rule.basic", "expr", "increase(mysql_global_status_slow_queries[1m]) > 0"),
2728
resource.TestCheckResourceAttr("tencentcloud_monitor_tmp_alert_rule.basic", "duration", "4m"),
29+
resource.TestCheckResourceAttr("tencentcloud_monitor_tmp_alert_rule.basic", "labels.#", "1"),
30+
resource.TestCheckResourceAttr("tencentcloud_monitor_tmp_alert_rule.basic", "labels.0.key", "hello1"),
31+
resource.TestCheckResourceAttr("tencentcloud_monitor_tmp_alert_rule.basic", "labels.0.value", "world1"),
32+
resource.TestCheckResourceAttr("tencentcloud_monitor_tmp_alert_rule.basic", "annotations.#", "1"),
33+
resource.TestCheckResourceAttr("tencentcloud_monitor_tmp_alert_rule.basic", "annotations.0.key", "hello2"),
34+
resource.TestCheckResourceAttr("tencentcloud_monitor_tmp_alert_rule.basic", "annotations.0.value", "world2"),
2835
),
2936
},
3037
{
@@ -35,6 +42,12 @@ func TestAccTencentCloudMonitorAlertRule_basic(t *testing.T) {
3542
resource.TestCheckResourceAttr("tencentcloud_monitor_tmp_alert_rule.basic", "receivers.#", "1"),
3643
resource.TestCheckResourceAttr("tencentcloud_monitor_tmp_alert_rule.basic", "expr", "increase(mysql_global_status_slow_queries[1m]) > 1"),
3744
resource.TestCheckResourceAttr("tencentcloud_monitor_tmp_alert_rule.basic", "duration", "2m"),
45+
resource.TestCheckResourceAttr("tencentcloud_monitor_tmp_alert_rule.basic", "labels.#", "1"),
46+
resource.TestCheckResourceAttr("tencentcloud_monitor_tmp_alert_rule.basic", "labels.0.key", "hello3"),
47+
resource.TestCheckResourceAttr("tencentcloud_monitor_tmp_alert_rule.basic", "labels.0.value", "world3"),
48+
resource.TestCheckResourceAttr("tencentcloud_monitor_tmp_alert_rule.basic", "annotations.#", "1"),
49+
resource.TestCheckResourceAttr("tencentcloud_monitor_tmp_alert_rule.basic", "annotations.0.key", "hello4"),
50+
resource.TestCheckResourceAttr("tencentcloud_monitor_tmp_alert_rule.basic", "annotations.0.value", "world4"),
3851
),
3952
},
4053
{
@@ -119,6 +132,14 @@ resource "tencentcloud_monitor_tmp_alert_rule" "basic" {
119132
expr = "increase(mysql_global_status_slow_queries[1m]) > 0"
120133
duration = "4m"
121134
rule_state = 2
135+
labels {
136+
key = "hello1"
137+
value = "world1"
138+
}
139+
annotations {
140+
key = "hello2"
141+
value = "world2"
142+
}
122143
}`
123144

124145
const testAlertRule_update = testAlertRuleVar + `
@@ -129,4 +150,12 @@ resource "tencentcloud_monitor_tmp_alert_rule" "basic" {
129150
expr = "increase(mysql_global_status_slow_queries[1m]) > 1"
130151
duration = "2m"
131152
rule_state = 2
153+
labels {
154+
key = "hello3"
155+
value = "world3"
156+
}
157+
annotations {
158+
key = "hello4"
159+
value = "world4"
160+
}
132161
}`

0 commit comments

Comments
 (0)