Skip to content

Commit

Permalink
fix: alert_rule optimization (#1407)
Browse files Browse the repository at this point in the history
* fix: alert_rule optimization

* feat: add changelog'

Co-authored-by: anonymous <anonymous@mail.org>
  • Loading branch information
gitmkn and anonymous authored Nov 21, 2022
1 parent 39c3774 commit 0daf098
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 34 deletions.
3 changes: 3 additions & 0 deletions .changelog/1407.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
resource/tencentcloud_monitor_tmp_alert_rule: Parameter check
```
77 changes: 44 additions & 33 deletions tencentcloud/resource_tc_monitor_tmp_alert_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import (

"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
sdkErrors "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/errors"
monitor "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/monitor/v20180724"
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
)
Expand Down Expand Up @@ -172,6 +173,9 @@ func resourceTencentCloudMonitorTmpAlertRuleCreate(d *schema.ResourceData, meta
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")
}
label := labels.(map[string]interface{})
var kv monitor.PrometheusRuleKV
kv.Key = helper.String(label["key"].(string))
Expand All @@ -184,6 +188,9 @@ func resourceTencentCloudMonitorTmpAlertRuleCreate(d *schema.ResourceData, meta
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")
}
annotation := annotations.(map[string]interface{})
var kv monitor.PrometheusRuleKV
kv.Key = helper.String(annotation["key"].(string))
Expand All @@ -199,6 +206,10 @@ func resourceTencentCloudMonitorTmpAlertRuleCreate(d *schema.ResourceData, meta
err := resource.Retry(writeRetryTimeout, func() *resource.RetryError {
result, e := meta.(*TencentCloudClient).apiV3Conn.UseMonitorClient().CreateAlertRule(request)
if e != nil {
ee, ok := e.(*sdkErrors.TencentCloudSDKError)
if ok && IsContains("FailedOperation", ee.Code) {
return resource.NonRetryableError(ee)
}
return retryError(e)
} else {
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n",
Expand Down Expand Up @@ -279,7 +290,7 @@ func resourceTencentCloudMonitorTmpAlertRuleRead(d *schema.ResourceData, meta in
_ = d.Set("labels", result)
}
if tmpAlertRule.Annotations != nil {
annotationsList := tmpAlertRule.Labels
annotationsList := tmpAlertRule.Annotations
result := make([]map[string]interface{}, 0, len(annotationsList))
for _, v := range annotationsList {
mapping := map[string]interface{}{
Expand Down Expand Up @@ -334,47 +345,47 @@ func resourceTencentCloudMonitorTmpAlertRuleUpdate(d *schema.ResourceData, meta
request.RuleState = helper.IntInt64(v.(int))
}

if d.HasChange("duration") {
if v, ok := d.GetOk("duration"); ok {
request.Duration = helper.String(v.(string))
}
if v, ok := d.GetOk("duration"); ok {
request.Duration = helper.String(v.(string))
}
if d.HasChange("labels") {
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)
}
request.Labels = prometheusRuleKV

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)
}
request.Labels = prometheusRuleKV
}
if d.HasChange("annotations") {
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)
}
request.Annotations = 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)
}
request.Annotations = prometheusRuleKV
}
if d.HasChange("type") {
if v, ok := d.GetOk("type"); ok {
request.Type = helper.String(v.(string))
}

if v, ok := d.GetOk("type"); ok {
request.Type = helper.String(v.(string))
}

err := resource.Retry(writeRetryTimeout, func() *resource.RetryError {
result, e := meta.(*TencentCloudClient).apiV3Conn.UseMonitorClient().UpdateAlertRule(request)
if e != nil {
ee, ok := e.(*sdkErrors.TencentCloudSDKError)
if ok && IsContains("FailedOperation", ee.Code) {
return resource.NonRetryableError(ee)
}
return retryError(e)
} else {
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n",
Expand Down
31 changes: 30 additions & 1 deletion tencentcloud/resource_tc_monitor_tmp_alert_rule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/terraform"
)

func TestAccTencentCloudMonitorAlertRule_basic(t *testing.T) {
// go test -i; go test -test.run TestAccTencentCloudMonitorAlertRuleResource_basic -v
func TestAccTencentCloudMonitorAlertRuleResource_basic(t *testing.T) {
t.Parallel()
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheckCommon(t, ACCOUNT_TYPE_COMMON) },
Expand All @@ -25,6 +26,12 @@ func TestAccTencentCloudMonitorAlertRule_basic(t *testing.T) {
resource.TestCheckResourceAttr("tencentcloud_monitor_tmp_alert_rule.basic", "receivers.#", "1"),
resource.TestCheckResourceAttr("tencentcloud_monitor_tmp_alert_rule.basic", "expr", "increase(mysql_global_status_slow_queries[1m]) > 0"),
resource.TestCheckResourceAttr("tencentcloud_monitor_tmp_alert_rule.basic", "duration", "4m"),
resource.TestCheckResourceAttr("tencentcloud_monitor_tmp_alert_rule.basic", "labels.#", "1"),
resource.TestCheckResourceAttr("tencentcloud_monitor_tmp_alert_rule.basic", "labels.0.key", "hello1"),
resource.TestCheckResourceAttr("tencentcloud_monitor_tmp_alert_rule.basic", "labels.0.value", "world1"),
resource.TestCheckResourceAttr("tencentcloud_monitor_tmp_alert_rule.basic", "annotations.#", "1"),
resource.TestCheckResourceAttr("tencentcloud_monitor_tmp_alert_rule.basic", "annotations.0.key", "hello2"),
resource.TestCheckResourceAttr("tencentcloud_monitor_tmp_alert_rule.basic", "annotations.0.value", "world2"),
),
},
{
Expand All @@ -35,6 +42,12 @@ func TestAccTencentCloudMonitorAlertRule_basic(t *testing.T) {
resource.TestCheckResourceAttr("tencentcloud_monitor_tmp_alert_rule.basic", "receivers.#", "1"),
resource.TestCheckResourceAttr("tencentcloud_monitor_tmp_alert_rule.basic", "expr", "increase(mysql_global_status_slow_queries[1m]) > 1"),
resource.TestCheckResourceAttr("tencentcloud_monitor_tmp_alert_rule.basic", "duration", "2m"),
resource.TestCheckResourceAttr("tencentcloud_monitor_tmp_alert_rule.basic", "labels.#", "1"),
resource.TestCheckResourceAttr("tencentcloud_monitor_tmp_alert_rule.basic", "labels.0.key", "hello3"),
resource.TestCheckResourceAttr("tencentcloud_monitor_tmp_alert_rule.basic", "labels.0.value", "world3"),
resource.TestCheckResourceAttr("tencentcloud_monitor_tmp_alert_rule.basic", "annotations.#", "1"),
resource.TestCheckResourceAttr("tencentcloud_monitor_tmp_alert_rule.basic", "annotations.0.key", "hello4"),
resource.TestCheckResourceAttr("tencentcloud_monitor_tmp_alert_rule.basic", "annotations.0.value", "world4"),
),
},
{
Expand Down Expand Up @@ -119,6 +132,14 @@ resource "tencentcloud_monitor_tmp_alert_rule" "basic" {
expr = "increase(mysql_global_status_slow_queries[1m]) > 0"
duration = "4m"
rule_state = 2
labels {
key = "hello1"
value = "world1"
}
annotations {
key = "hello2"
value = "world2"
}
}`

const testAlertRule_update = testAlertRuleVar + `
Expand All @@ -129,4 +150,12 @@ resource "tencentcloud_monitor_tmp_alert_rule" "basic" {
expr = "increase(mysql_global_status_slow_queries[1m]) > 1"
duration = "2m"
rule_state = 2
labels {
key = "hello3"
value = "world3"
}
annotations {
key = "hello4"
value = "world4"
}
}`

0 comments on commit 0daf098

Please sign in to comment.