From bf6fa7a8419b79bc5fade34840bdfca61042d43c Mon Sep 17 00:00:00 2001 From: arunma Date: Mon, 6 May 2024 17:15:28 +0800 Subject: [PATCH 1/3] fix(tmp): [117224022]Fix array order issue --- tencentcloud/acctest/basic.go | 2 +- .../tmp/resource_tc_monitor_tmp_alert_rule.go | 89 ++++++++++--------- ...resource_tc_monitor_tmp_alert_rule_test.go | 15 ++-- .../resource_tc_monitor_tmp_instance_test.go | 2 +- 4 files changed, 54 insertions(+), 54 deletions(-) diff --git a/tencentcloud/acctest/basic.go b/tencentcloud/acctest/basic.go index 90f716a68a..8e252fd3d4 100644 --- a/tencentcloud/acctest/basic.go +++ b/tencentcloud/acctest/basic.go @@ -240,7 +240,7 @@ const ( // monitor const ( DefaultTmpVpcId = "vpc-axrsmmrv" - DefaultTmpSubnetId = "subnet-b23ua6gk" + DefaultTmpSubnetId = "subnet-j5vja918" ) /* diff --git a/tencentcloud/services/tmp/resource_tc_monitor_tmp_alert_rule.go b/tencentcloud/services/tmp/resource_tc_monitor_tmp_alert_rule.go index 3debeb057d..0411927f22 100644 --- a/tencentcloud/services/tmp/resource_tc_monitor_tmp_alert_rule.go +++ b/tencentcloud/services/tmp/resource_tc_monitor_tmp_alert_rule.go @@ -59,7 +59,7 @@ func ResourceTencentCloudMonitorTmpAlertRule() *schema.Resource { Description: "Rule alarm duration.", }, "labels": { - Type: schema.TypeList, + Type: schema.TypeSet, Optional: true, Description: "Rule alarm duration.", Elem: &schema.Resource{ @@ -78,7 +78,7 @@ func ResourceTencentCloudMonitorTmpAlertRule() *schema.Resource { }, }, "annotations": { - Type: schema.TypeList, + Type: schema.TypeSet, Optional: true, Description: "Rule alarm duration.", Elem: &schema.Resource{ @@ -142,36 +142,37 @@ func resourceTencentCloudMonitorTmpAlertRuleCreate(d *schema.ResourceData, meta if v, ok := d.GetOk("duration"); ok { request.Duration = helper.String(v.(string)) } + if v, ok := d.GetOk("labels"); ok { - labelsList := v.([]interface{}) - prometheusRuleKV := make([]*monitor.PrometheusRuleKV, 0, len(labelsList)) - for _, labels := range labelsList { - if labels == nil { - return fmt.Errorf("Invalid `labels` parameter, must not be empty") + for _, item := range v.(*schema.Set).List() { + dMap := item.(map[string]interface{}) + prometheusRuleKV := monitor.PrometheusRuleKV{} + if v, ok := dMap["key"]; ok { + prometheusRuleKV.Key = helper.String(v.(string)) + } + + if v, ok := dMap["value"]; ok { + prometheusRuleKV.Value = helper.String(v.(string)) } - label := labels.(map[string]interface{}) - var kv monitor.PrometheusRuleKV - kv.Key = helper.String(label["key"].(string)) - kv.Value = helper.String(label["value"].(string)) - prometheusRuleKV = append(prometheusRuleKV, &kv) + request.Labels = append(request.Labels, &prometheusRuleKV) } - request.Labels = prometheusRuleKV } + if v, ok := d.GetOk("annotations"); ok { - annotationsList := v.([]interface{}) - prometheusRuleKV := make([]*monitor.PrometheusRuleKV, 0, len(annotationsList)) - for _, annotations := range annotationsList { - if annotations == nil { - return fmt.Errorf("Invalid `annotation` parameter, must not be empty") + for _, item := range v.(*schema.Set).List() { + dMap := item.(map[string]interface{}) + prometheusRuleKV := monitor.PrometheusRuleKV{} + if v, ok := dMap["key"]; ok { + prometheusRuleKV.Key = helper.String(v.(string)) + } + + if v, ok := dMap["value"]; ok { + prometheusRuleKV.Value = helper.String(v.(string)) } - annotation := annotations.(map[string]interface{}) - var kv monitor.PrometheusRuleKV - kv.Key = helper.String(annotation["key"].(string)) - kv.Value = helper.String(annotation["value"].(string)) - prometheusRuleKV = append(prometheusRuleKV, &kv) + request.Annotations = append(request.Annotations, &prometheusRuleKV) } - request.Annotations = prometheusRuleKV } + if v, ok := d.GetOk("type"); ok { request.Type = helper.String(v.(string)) } @@ -323,29 +324,33 @@ func resourceTencentCloudMonitorTmpAlertRuleUpdate(d *schema.ResourceData, meta } if v, ok := d.GetOk("labels"); ok { - labelsList := v.([]interface{}) - prometheusRuleKV := make([]*monitor.PrometheusRuleKV, 0, len(labelsList)) - for _, labels := range labelsList { - label := labels.(map[string]interface{}) - var kv monitor.PrometheusRuleKV - kv.Key = helper.String(label["key"].(string)) - kv.Value = helper.String(label["value"].(string)) - prometheusRuleKV = append(prometheusRuleKV, &kv) + for _, item := range v.(*schema.Set).List() { + dMap := item.(map[string]interface{}) + prometheusRuleKV := monitor.PrometheusRuleKV{} + if v, ok := dMap["key"]; ok { + prometheusRuleKV.Key = helper.String(v.(string)) + } + + if v, ok := dMap["value"]; ok { + prometheusRuleKV.Value = helper.String(v.(string)) + } + request.Labels = append(request.Labels, &prometheusRuleKV) } - request.Labels = prometheusRuleKV } if v, ok := d.GetOk("annotations"); ok { - annotationsList := v.([]interface{}) - prometheusRuleKV := make([]*monitor.PrometheusRuleKV, 0, len(annotationsList)) - for _, annotations := range annotationsList { - annotation := annotations.(map[string]interface{}) - var kv monitor.PrometheusRuleKV - kv.Key = helper.String(annotation["key"].(string)) - kv.Value = helper.String(annotation["value"].(string)) - prometheusRuleKV = append(prometheusRuleKV, &kv) + for _, item := range v.(*schema.Set).List() { + dMap := item.(map[string]interface{}) + prometheusRuleKV := monitor.PrometheusRuleKV{} + if v, ok := dMap["key"]; ok { + prometheusRuleKV.Key = helper.String(v.(string)) + } + + if v, ok := dMap["value"]; ok { + prometheusRuleKV.Value = helper.String(v.(string)) + } + request.Annotations = append(request.Annotations, &prometheusRuleKV) } - request.Annotations = prometheusRuleKV } if v, ok := d.GetOk("type"); ok { diff --git a/tencentcloud/services/tmp/resource_tc_monitor_tmp_alert_rule_test.go b/tencentcloud/services/tmp/resource_tc_monitor_tmp_alert_rule_test.go index 2b3ab8f774..49736e3a04 100644 --- a/tencentcloud/services/tmp/resource_tc_monitor_tmp_alert_rule_test.go +++ b/tencentcloud/services/tmp/resource_tc_monitor_tmp_alert_rule_test.go @@ -18,7 +18,7 @@ import ( func TestAccTencentCloudMonitorAlertRuleResource_basic(t *testing.T) { t.Parallel() resource.Test(t, resource.TestCase{ - PreCheck: func() { tcacctest.AccPreCheckCommon(t, tcacctest.ACCOUNT_TYPE_COMMON) }, + PreCheck: func() { tcacctest.AccPreCheck(t) }, Providers: tcacctest.AccProviders, CheckDestroy: testAccCheckAlertRuleDestroy, Steps: []resource.TestStep{ @@ -123,14 +123,9 @@ func testAccCheckAlertRuleExists(r string) resource.TestCheckFunc { } } -const testAlertRuleVar = ` -variable "prometheus_id" { - default = "` + tcacctest.DefaultPrometheusId + `" -} -` -const testAlertRule_basic = testAlertRuleVar + ` +const testAlertRule_basic = testInstance_basic + ` resource "tencentcloud_monitor_tmp_alert_rule" "basic" { - instance_id = var.prometheus_id + instance_id = tencentcloud_monitor_tmp_instance.basic.id rule_name = "test-rule_name" receivers = ["notice-tj75hgqj"] expr = "increase(mysql_global_status_slow_queries[1m]) > 0" @@ -146,9 +141,9 @@ resource "tencentcloud_monitor_tmp_alert_rule" "basic" { } }` -const testAlertRule_update = testAlertRuleVar + ` +const testAlertRule_update = testInstance_basic + ` resource "tencentcloud_monitor_tmp_alert_rule" "basic" { - instance_id = var.prometheus_id + instance_id = tencentcloud_monitor_tmp_instance.basic.id rule_name = "test-rule_name_update" receivers = ["notice-tj75hgqj"] expr = "increase(mysql_global_status_slow_queries[1m]) > 1" diff --git a/tencentcloud/services/tmp/resource_tc_monitor_tmp_instance_test.go b/tencentcloud/services/tmp/resource_tc_monitor_tmp_instance_test.go index c6c59272a9..286d41a77e 100644 --- a/tencentcloud/services/tmp/resource_tc_monitor_tmp_instance_test.go +++ b/tencentcloud/services/tmp/resource_tc_monitor_tmp_instance_test.go @@ -18,7 +18,7 @@ import ( func TestAccTencentCloudMonitorInstance_basic(t *testing.T) { t.Parallel() resource.Test(t, resource.TestCase{ - PreCheck: func() { tcacctest.AccPreCheckCommon(t, tcacctest.ACCOUNT_TYPE_COMMON) }, + PreCheck: func() { tcacctest.AccPreCheck(t) }, Providers: tcacctest.AccProviders, CheckDestroy: testAccCheckMonInstanceDestroy, Steps: []resource.TestStep{ From a46d1c41c5dad051f14b2dd382e757607c597439 Mon Sep 17 00:00:00 2001 From: arunma Date: Mon, 6 May 2024 18:02:24 +0800 Subject: [PATCH 2/3] fix: modify doc --- website/docs/r/monitor_tmp_alert_rule.html.markdown | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/website/docs/r/monitor_tmp_alert_rule.html.markdown b/website/docs/r/monitor_tmp_alert_rule.html.markdown index 64d30721cb..dc69b6d1fa 100644 --- a/website/docs/r/monitor_tmp_alert_rule.html.markdown +++ b/website/docs/r/monitor_tmp_alert_rule.html.markdown @@ -81,9 +81,9 @@ The following arguments are supported: * `instance_id` - (Required, String) Instance id. * `receivers` - (Required, Set: [`String`]) Alarm notification template id list. * `rule_name` - (Required, String) Rule name. -* `annotations` - (Optional, List) Rule alarm duration. +* `annotations` - (Optional, Set) Rule alarm duration. * `duration` - (Optional, String) Rule alarm duration. -* `labels` - (Optional, List) Rule alarm duration. +* `labels` - (Optional, Set) Rule alarm duration. * `rule_state` - (Optional, Int) Rule state code. * `type` - (Optional, String) Alarm Policy Template Classification. From e562b000c3902922912437baccc4410aacb9ec61 Mon Sep 17 00:00:00 2001 From: arunma Date: Mon, 6 May 2024 21:51:25 +0800 Subject: [PATCH 3/3] feat: add changelog --- .changelog/2620.txt | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 .changelog/2620.txt diff --git a/.changelog/2620.txt b/.changelog/2620.txt new file mode 100644 index 0000000000..19d61b8a93 --- /dev/null +++ b/.changelog/2620.txt @@ -0,0 +1,3 @@ +```release-note:enhancement +resource/tencentcloud_monitor_tmp_alert_rule: Fix array order issue +```