|
| 1 | +package tencentcloud |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "strings" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/hashicorp/terraform-plugin-sdk/helper/resource" |
| 10 | + "github.com/hashicorp/terraform-plugin-sdk/terraform" |
| 11 | +) |
| 12 | + |
| 13 | +func TestAccTencentCloudMonitorAlertRule_basic(t *testing.T) { |
| 14 | + t.Parallel() |
| 15 | + resource.Test(t, resource.TestCase{ |
| 16 | + PreCheck: func() { testAccPreCheckCommon(t, ACCOUNT_TYPE_COMMON) }, |
| 17 | + Providers: testAccProviders, |
| 18 | + CheckDestroy: testAccCheckAlertRuleDestroy, |
| 19 | + Steps: []resource.TestStep{ |
| 20 | + { |
| 21 | + Config: testAlertRule_basic, |
| 22 | + Check: resource.ComposeTestCheckFunc( |
| 23 | + testAccCheckAlertRuleExists("tencentcloud_monitor_tmp_alert_rule.basic"), |
| 24 | + resource.TestCheckResourceAttr("tencentcloud_monitor_tmp_alert_rule.basic", "rule_name", "test-rule_name"), |
| 25 | + resource.TestCheckResourceAttr("tencentcloud_monitor_tmp_alert_rule.basic", "receivers.#", "1"), |
| 26 | + resource.TestCheckResourceAttr("tencentcloud_monitor_tmp_alert_rule.basic", "expr", "increase(mysql_global_status_slow_queries[1m]) > 0"), |
| 27 | + resource.TestCheckResourceAttr("tencentcloud_monitor_tmp_alert_rule.basic", "duration", "4m"), |
| 28 | + ), |
| 29 | + }, |
| 30 | + { |
| 31 | + Config: testAlertRule_update, |
| 32 | + Check: resource.ComposeTestCheckFunc( |
| 33 | + testAccCheckAlertRuleExists("tencentcloud_monitor_tmp_alert_rule.basic"), |
| 34 | + resource.TestCheckResourceAttr("tencentcloud_monitor_tmp_alert_rule.basic", "rule_name", "test-rule_name_update"), |
| 35 | + resource.TestCheckResourceAttr("tencentcloud_monitor_tmp_alert_rule.basic", "receivers.#", "1"), |
| 36 | + resource.TestCheckResourceAttr("tencentcloud_monitor_tmp_alert_rule.basic", "expr", "increase(mysql_global_status_slow_queries[1m]) > 1"), |
| 37 | + resource.TestCheckResourceAttr("tencentcloud_monitor_tmp_alert_rule.basic", "duration", "2m"), |
| 38 | + ), |
| 39 | + }, |
| 40 | + { |
| 41 | + ResourceName: "tencentcloud_monitor_tmp_alert_rule.basic", |
| 42 | + ImportState: true, |
| 43 | + ImportStateVerify: true, |
| 44 | + }, |
| 45 | + }, |
| 46 | + }) |
| 47 | +} |
| 48 | + |
| 49 | +func testAccCheckAlertRuleDestroy(s *terraform.State) error { |
| 50 | + logId := getLogId(contextNil) |
| 51 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 52 | + service := MonitorService{client: testAccProvider.Meta().(*TencentCloudClient).apiV3Conn} |
| 53 | + for _, rs := range s.RootModule().Resources { |
| 54 | + if rs.Type != "tencentcloud_monitor_tmp_tke_alert_rule" { |
| 55 | + continue |
| 56 | + } |
| 57 | + if rs.Primary.ID == "" { |
| 58 | + return fmt.Errorf("resource id is not set") |
| 59 | + } |
| 60 | + ids := strings.Split(rs.Primary.ID, FILED_SP) |
| 61 | + if len(ids) != 2 { |
| 62 | + return fmt.Errorf("id is broken, id is %s", rs.Primary.ID) |
| 63 | + } |
| 64 | + |
| 65 | + instance, err := service.DescribeMonitorTmpAlertRuleById(ctx, ids[0], ids[1]) |
| 66 | + if err != nil { |
| 67 | + return err |
| 68 | + } |
| 69 | + |
| 70 | + if instance != nil { |
| 71 | + return fmt.Errorf("instance %s still exists", rs.Primary.ID) |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + return nil |
| 76 | +} |
| 77 | + |
| 78 | +func testAccCheckAlertRuleExists(r string) resource.TestCheckFunc { |
| 79 | + return func(s *terraform.State) error { |
| 80 | + logId := getLogId(contextNil) |
| 81 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 82 | + |
| 83 | + rs, ok := s.RootModule().Resources[r] |
| 84 | + if !ok { |
| 85 | + return fmt.Errorf("resource %s is not found", r) |
| 86 | + } |
| 87 | + if rs.Primary.ID == "" { |
| 88 | + return fmt.Errorf("resource id is not set") |
| 89 | + } |
| 90 | + ids := strings.Split(rs.Primary.ID, FILED_SP) |
| 91 | + if len(ids) != 2 { |
| 92 | + return fmt.Errorf("id is broken, id is %s", rs.Primary.ID) |
| 93 | + } |
| 94 | + |
| 95 | + service := MonitorService{client: testAccProvider.Meta().(*TencentCloudClient).apiV3Conn} |
| 96 | + instance, err := service.DescribeMonitorTmpAlertRuleById(ctx, ids[0], ids[1]) |
| 97 | + if err != nil { |
| 98 | + return err |
| 99 | + } |
| 100 | + |
| 101 | + if instance == nil { |
| 102 | + return fmt.Errorf("instance %s is not found", rs.Primary.ID) |
| 103 | + } |
| 104 | + |
| 105 | + return nil |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +const testAlertRuleVar = ` |
| 110 | +variable "prometheus_id" { |
| 111 | + default = "` + defaultPrometheusId + `" |
| 112 | +} |
| 113 | +` |
| 114 | +const testAlertRule_basic = testAlertRuleVar + ` |
| 115 | +resource "tencentcloud_monitor_tmp_alert_rule" "basic" { |
| 116 | + instance_id = var.prometheus_id |
| 117 | + rule_name = "test-rule_name" |
| 118 | + receivers = ["Consumer-6vkna7pevq"] |
| 119 | + expr = "increase(mysql_global_status_slow_queries[1m]) > 0" |
| 120 | + duration = "4m" |
| 121 | + rule_state = 2 |
| 122 | +}` |
| 123 | + |
| 124 | +const testAlertRule_update = testAlertRuleVar + ` |
| 125 | +resource "tencentcloud_monitor_tmp_alert_rule" "basic" { |
| 126 | + instance_id = var.prometheus_id |
| 127 | + rule_name = "test-rule_name_update" |
| 128 | + receivers = ["Consumer-6vkna7pevq"] |
| 129 | + expr = "increase(mysql_global_status_slow_queries[1m]) > 1" |
| 130 | + duration = "2m" |
| 131 | + rule_state = 2 |
| 132 | +}` |
0 commit comments