Skip to content

fix(tmp): [117224022]Fix array order issue #2620

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/2620.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
resource/tencentcloud_monitor_tmp_alert_rule: Fix array order issue
```
2 changes: 1 addition & 1 deletion tencentcloud/acctest/basic.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ const (
// monitor
const (
DefaultTmpVpcId = "vpc-axrsmmrv"
DefaultTmpSubnetId = "subnet-b23ua6gk"
DefaultTmpSubnetId = "subnet-j5vja918"
)

/*
Expand Down
89 changes: 47 additions & 42 deletions tencentcloud/services/tmp/resource_tc_monitor_tmp_alert_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -78,7 +78,7 @@ func ResourceTencentCloudMonitorTmpAlertRule() *schema.Resource {
},
},
"annotations": {
Type: schema.TypeList,
Type: schema.TypeSet,
Optional: true,
Description: "Rule alarm duration.",
Elem: &schema.Resource{
Expand Down Expand Up @@ -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))
}
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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"
Expand All @@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
4 changes: 2 additions & 2 deletions website/docs/r/monitor_tmp_alert_rule.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
Loading