Skip to content
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
1 change: 1 addition & 0 deletions tencentcloud/basic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ const (
defaultTemplateId = "temp-gqunlvo1"
tkeClusterIdAgent = "cls-87o4klby"
tkeClusterTypeAgent = "eks"
defaultAgentId = "agent-q3zy8gt8"
)

/*
Expand Down
15 changes: 15 additions & 0 deletions tencentcloud/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package tencentcloud

import (
"context"
"encoding/base64"
"encoding/json"
"fmt"
"io/ioutil"
Expand Down Expand Up @@ -349,3 +350,17 @@ func YamlParser(config string) (map[interface{}]interface{}, error) {
}
return m, nil
}

func YamlToBase64(config string) string {
m := []byte(config)
encodedStr := base64.StdEncoding.EncodeToString(m)
return encodedStr
}

func Base64ToYaml(config string) (string, error) {
yamlConfig, err := base64.StdEncoding.DecodeString(config)
if err != nil {
return "", err
}
return string(yamlConfig), nil
}
3 changes: 2 additions & 1 deletion tencentcloud/resource_tc_kubernetes_addon_attachment.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,11 @@ package tencentcloud
import (
"context"
"fmt"
tke "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tke/v20180525"
"log"
"strings"

tke "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tke/v20180525"

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
)
Expand Down
12 changes: 9 additions & 3 deletions tencentcloud/resource_tc_monitor_tmp_alert_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,15 +245,21 @@ func resourceTencentCloudMonitorTmpAlertRuleRead(d *schema.ResourceData, meta in
return fmt.Errorf("resource `tmpAlertRule` %s does not exist", ids[1])
}

_ = d.Set("instance_id", ids[0])
if tmpAlertRule.RuleName != nil {
_ = d.Set("rule_name", tmpAlertRule.RuleName)
}
if tmpAlertRule.Expr != nil {
_ = d.Set("expr", tmpAlertRule.Expr)
}
//if tmpAlertRule.Receivers != nil {
// _ = d.Set("receivers", tmpAlertRule.Receivers)
//}
if tmpAlertRule.Receivers != nil {
list := tmpAlertRule.Receivers
result := make([]string, 0, len(list))
for _, v := range list {
result = append(result, *v)
}
_ = d.Set("receivers", result)
}
if tmpAlertRule.RuleState != nil {
_ = d.Set("rule_state", tmpAlertRule.RuleState)
}
Expand Down
132 changes: 132 additions & 0 deletions tencentcloud/resource_tc_monitor_tmp_alert_rule_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
package tencentcloud

import (
"context"
"fmt"
"strings"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/terraform"
)

func TestAccTencentCloudMonitorAlertRule_basic(t *testing.T) {
t.Parallel()
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheckCommon(t, ACCOUNT_TYPE_COMMON) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAlertRuleDestroy,
Steps: []resource.TestStep{
{
Config: testAlertRule_basic,
Check: resource.ComposeTestCheckFunc(
testAccCheckAlertRuleExists("tencentcloud_monitor_tmp_alert_rule.basic"),
resource.TestCheckResourceAttr("tencentcloud_monitor_tmp_alert_rule.basic", "rule_name", "test-rule_name"),
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"),
),
},
{
Config: testAlertRule_update,
Check: resource.ComposeTestCheckFunc(
testAccCheckAlertRuleExists("tencentcloud_monitor_tmp_alert_rule.basic"),
resource.TestCheckResourceAttr("tencentcloud_monitor_tmp_alert_rule.basic", "rule_name", "test-rule_name_update"),
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"),
),
},
{
ResourceName: "tencentcloud_monitor_tmp_alert_rule.basic",
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func testAccCheckAlertRuleDestroy(s *terraform.State) error {
logId := getLogId(contextNil)
ctx := context.WithValue(context.TODO(), logIdKey, logId)
service := MonitorService{client: testAccProvider.Meta().(*TencentCloudClient).apiV3Conn}
for _, rs := range s.RootModule().Resources {
if rs.Type != "tencentcloud_monitor_tmp_tke_alert_rule" {
continue
}
if rs.Primary.ID == "" {
return fmt.Errorf("resource id is not set")
}
ids := strings.Split(rs.Primary.ID, FILED_SP)
if len(ids) != 2 {
return fmt.Errorf("id is broken, id is %s", rs.Primary.ID)
}

instance, err := service.DescribeMonitorTmpAlertRuleById(ctx, ids[0], ids[1])
if err != nil {
return err
}

if instance != nil {
return fmt.Errorf("instance %s still exists", rs.Primary.ID)
}
}

return nil
}

func testAccCheckAlertRuleExists(r string) resource.TestCheckFunc {
return func(s *terraform.State) error {
logId := getLogId(contextNil)
ctx := context.WithValue(context.TODO(), logIdKey, logId)

rs, ok := s.RootModule().Resources[r]
if !ok {
return fmt.Errorf("resource %s is not found", r)
}
if rs.Primary.ID == "" {
return fmt.Errorf("resource id is not set")
}
ids := strings.Split(rs.Primary.ID, FILED_SP)
if len(ids) != 2 {
return fmt.Errorf("id is broken, id is %s", rs.Primary.ID)
}

service := MonitorService{client: testAccProvider.Meta().(*TencentCloudClient).apiV3Conn}
instance, err := service.DescribeMonitorTmpAlertRuleById(ctx, ids[0], ids[1])
if err != nil {
return err
}

if instance == nil {
return fmt.Errorf("instance %s is not found", rs.Primary.ID)
}

return nil
}
}

const testAlertRuleVar = `
variable "prometheus_id" {
default = "` + defaultPrometheusId + `"
}
`
const testAlertRule_basic = testAlertRuleVar + `
resource "tencentcloud_monitor_tmp_alert_rule" "basic" {
instance_id = var.prometheus_id
rule_name = "test-rule_name"
receivers = ["Consumer-6vkna7pevq"]
expr = "increase(mysql_global_status_slow_queries[1m]) > 0"
duration = "4m"
rule_state = 2
}`

const testAlertRule_update = testAlertRuleVar + `
resource "tencentcloud_monitor_tmp_alert_rule" "basic" {
instance_id = var.prometheus_id
rule_name = "test-rule_name_update"
receivers = ["Consumer-6vkna7pevq"]
expr = "increase(mysql_global_status_slow_queries[1m]) > 1"
duration = "2m"
rule_state = 2
}`
18 changes: 17 additions & 1 deletion tencentcloud/resource_tc_monitor_tmp_exporter_integration.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ func resourceTencentCloudMonitorTmpExporterIntegrationCreate(d *schema.ResourceD
instanceId string
kubeType int
clusterId string
kind string
)

var (
Expand All @@ -91,6 +92,7 @@ func resourceTencentCloudMonitorTmpExporterIntegrationCreate(d *schema.ResourceD
}

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

Expand Down Expand Up @@ -127,7 +129,7 @@ func resourceTencentCloudMonitorTmpExporterIntegrationCreate(d *schema.ResourceD

tmpExporterIntegrationId := *response.Response.Names[0]

d.SetId(strings.Join([]string{tmpExporterIntegrationId, instanceId, strconv.Itoa(kubeType), clusterId}, FILED_SP))
d.SetId(strings.Join([]string{tmpExporterIntegrationId, instanceId, strconv.Itoa(kubeType), clusterId, kind}, FILED_SP))

return resourceTencentCloudMonitorTmpExporterIntegrationRead(d, meta)
}
Expand Down Expand Up @@ -225,5 +227,19 @@ func resourceTencentCloudMonitorTmpExporterIntegrationDelete(d *schema.ResourceD
return err
}

err := resource.Retry(2*readRetryTimeout, func() *resource.RetryError {
tmpExporterIntegration, errRet := service.DescribeMonitorTmpExporterIntegration(ctx, tmpExporterIntegrationId)
if errRet != nil {
return retryError(errRet, InternalError)
}
if tmpExporterIntegration == nil {
return nil
}
return resource.RetryableError(fmt.Errorf("exporter integration status is %v, retry...", *tmpExporterIntegration.Status))
})
if err != nil {
return err
}

return nil
}
120 changes: 120 additions & 0 deletions tencentcloud/resource_tc_monitor_tmp_exporter_integration_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
package tencentcloud

import (
"context"
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/terraform"
)

func TestAccTencentCloudMonitorExporterIntegration_basic(t *testing.T) {
t.Parallel()
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheckCommon(t, ACCOUNT_TYPE_COMMON) },
Providers: testAccProviders,
CheckDestroy: testAccCheckExporterIntegrationDestroy,
Steps: []resource.TestStep{
{
Config: testExporterIntegration_basic,
Check: resource.ComposeTestCheckFunc(
testAccCheckExporterIntegrationExists("tencentcloud_monitor_tmp_exporter_integration.basic"),
resource.TestCheckResourceAttr("tencentcloud_monitor_tmp_exporter_integration.basic", "kind", "cvm-http-sd-exporter"),
resource.TestCheckResourceAttr("tencentcloud_monitor_tmp_exporter_integration.basic", "kube_type", "1"),
resource.TestCheckResourceAttr("tencentcloud_monitor_tmp_exporter_integration.basic", "cluster_id", "cls-ely08ic4"),
),
},
{
Config: testExporterIntegration_update,
Check: resource.ComposeTestCheckFunc(
testAccCheckExporterIntegrationExists("tencentcloud_monitor_tmp_exporter_integration.basic"),
resource.TestCheckResourceAttr("tencentcloud_monitor_tmp_exporter_integration.basic", "kind", "cvm-http-sd-exporter"),
resource.TestCheckResourceAttr("tencentcloud_monitor_tmp_exporter_integration.basic", "kube_type", "1"),
resource.TestCheckResourceAttr("tencentcloud_monitor_tmp_exporter_integration.basic", "cluster_id", "cls-87o4klby"),
),
},
},
})
}

func testAccCheckExporterIntegrationDestroy(s *terraform.State) error {
logId := getLogId(contextNil)
ctx := context.WithValue(context.TODO(), logIdKey, logId)
service := MonitorService{client: testAccProvider.Meta().(*TencentCloudClient).apiV3Conn}
for _, rs := range s.RootModule().Resources {
if rs.Type != "tencentcloud_monitor_tmp_exporter_integration" {
continue
}
if rs.Primary.ID == "" {
return fmt.Errorf("resource id is not set")
}

instance, err := service.DescribeMonitorTmpExporterIntegration(ctx, rs.Primary.ID)
if err != nil {
return err
}

if instance != nil {
return fmt.Errorf("ExporterIntegration %s still exists", rs.Primary.ID)
}
}

return nil
}

func testAccCheckExporterIntegrationExists(r string) resource.TestCheckFunc {
return func(s *terraform.State) error {
logId := getLogId(contextNil)
ctx := context.WithValue(context.TODO(), logIdKey, logId)

rs, ok := s.RootModule().Resources[r]
if !ok {
return fmt.Errorf("resource %s is not found", r)
}
if rs.Primary.ID == "" {
return fmt.Errorf("resource id is not set")
}

service := MonitorService{client: testAccProvider.Meta().(*TencentCloudClient).apiV3Conn}
tmpExporterIntegration, err := service.DescribeMonitorTmpExporterIntegration(ctx, rs.Primary.ID)
if err != nil {
return err
}

if tmpExporterIntegration == nil {
return fmt.Errorf("ExporterIntegration %s is not found", rs.Primary.ID)
}

return nil
}
}

const testExporterIntegrationVar = `
variable "prometheus_id" {
default = "` + defaultPrometheusId + `"
}
variable "default_cluster" {
default = "` + defaultTkeClusterId + `"
}
variable "cluster_id" {
default = "` + tkeClusterIdAgent + `"
}
`
const testExporterIntegration_basic = testExporterIntegrationVar + `
resource "tencentcloud_monitor_tmp_exporter_integration" "basic" {
instance_id = var.prometheus_id
kind = "cvm-http-sd-exporter"
content = "{\"kind\":\"cvm-http-sd-exporter\",\"spec\":{\"job\":\"job_name: example-job-name\\nmetrics_path: /metrics\\ncvm_sd_configs:\\n- region: ap-guangzhou\\n ports:\\n - 9100\\n filters: \\n - name: tag:示例标签键\\n values: \\n - 示例标签值\\nrelabel_configs: \\n- source_labels: [__meta_cvm_instance_state]\\n regex: RUNNING\\n action: keep\\n- regex: __meta_cvm_tag_(.*)\\n replacement: $1\\n action: labelmap\\n- source_labels: [__meta_cvm_region]\\n target_label: region\\n action: replace\"}}"
kube_type = 1
cluster_id = var.default_cluster
}`

const testExporterIntegration_update = testExporterIntegrationVar + `
resource "tencentcloud_monitor_tmp_exporter_integration" "basic" {
instance_id = var.prometheus_id
kind = "cvm-http-sd-exporter"
content = "{\"kind\":\"cvm-http-sd-exporter\",\"spec\":{\"job\":\"job_name: example-job-name\\nmetrics_path: /metrics\\ncvm_sd_configs:\\n- region: ap-guangzhou\\n ports:\\n - 9100\\n filters: \\n - name: tag:示例标签键\\n values: \\n - 示例标签值\\nrelabel_configs: \\n- source_labels: [__meta_cvm_instance_state]\\n regex: RUNNING\\n action: keep\\n- regex: __meta_cvm_tag_(.*)\\n replacement: $1\\n action: labelmap\\n- source_labels: [__meta_cvm_region]\\n target_label: region\\n action: replace\"}}"
kube_type = 1
cluster_id = var.cluster_id
}`
Loading