diff --git a/.changelog/3649.txt b/.changelog/3649.txt new file mode 100644 index 0000000000..3fd67aa799 --- /dev/null +++ b/.changelog/3649.txt @@ -0,0 +1,3 @@ +```release-note:new-resource +tencentcloud_apm_prometheus_rule +``` \ No newline at end of file diff --git a/go.mod b/go.mod index 450eed24ea..f0513816fc 100644 --- a/go.mod +++ b/go.mod @@ -29,7 +29,7 @@ require ( github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/antiddos v1.0.799 github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/api v1.0.285 github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/apigateway v1.0.763 - github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/apm v1.2.2 + github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/apm v1.3.8 github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/as v1.3.4 github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/bi v1.0.824 github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cam v1.1.27 @@ -46,7 +46,7 @@ require ( github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/clb v1.0.1107 github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cloudaudit v1.0.1033 github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cls v1.0.1148 - github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.3.7 + github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.3.8 github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cvm v1.0.1206 github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cwp v1.0.762 github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cynosdb v1.3.7 diff --git a/go.sum b/go.sum index e5e383902b..7813beb62d 100644 --- a/go.sum +++ b/go.sum @@ -825,8 +825,8 @@ github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/api v1.0.285 h1:gFmukRG github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/api v1.0.285/go.mod h1:aGlXSWjtSnE6kuqcaRy/NKj1CLiB8NlMSHGsDn+k7Ag= github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/apigateway v1.0.763 h1:jNjM/+MU2HplNZpCxDBIBKXZzumIDFJ4ztYHob0thow= github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/apigateway v1.0.763/go.mod h1:OlRreot089Ec7bEYMUovUBSuCD/x0D0ONPZ0g3YA5hg= -github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/apm v1.2.2 h1:/BlN5LD/rLm0WMM4Yyp2m4XQ8EVzlSV0uKt3SuT5+SE= -github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/apm v1.2.2/go.mod h1:XIggn8QQknIzb+1lnsOiKbTQ0fjRe1uV6P+1N0L2ccI= +github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/apm v1.3.8 h1:v/G/D3bqUKMR2QTlyPVBcLIJBBy4MXs/0JDCcZFUK50= +github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/apm v1.3.8/go.mod h1:DarTPk6LPu4LtKwDRbF2V2Af4KKXVXnzyteNhAifWm8= github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/as v1.3.4 h1:hTOxj4qB+UX6MflqmIYT4cPbfEqtiJEfi9tR58DxkIw= github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/as v1.3.4/go.mod h1:q7fZk23gy6BkZGcNw9NXFSj26R44tiJZKGxLFfOrMo0= github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/bi v1.0.824 h1:DVKvZ6h+qd7tadUrCjVAkCCmE3TsbK2ZmwGd3AJcpWc= @@ -934,11 +934,11 @@ github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.1.46/go.mod h github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.1.49/go.mod h1:r5r4xbfxSaeR04b166HGsBa/R4U3SueirEUpXGuw+Q0= github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.1.50/go.mod h1:r5r4xbfxSaeR04b166HGsBa/R4U3SueirEUpXGuw+Q0= github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.1.51/go.mod h1:r5r4xbfxSaeR04b166HGsBa/R4U3SueirEUpXGuw+Q0= -github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.2.2/go.mod h1:r5r4xbfxSaeR04b166HGsBa/R4U3SueirEUpXGuw+Q0= github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.3.3/go.mod h1:r5r4xbfxSaeR04b166HGsBa/R4U3SueirEUpXGuw+Q0= github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.3.4/go.mod h1:r5r4xbfxSaeR04b166HGsBa/R4U3SueirEUpXGuw+Q0= -github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.3.7 h1:MCrz0g4LDxoCdCgz6Dtas8b7HHLMfgTpIIF/tlor0Hw= github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.3.7/go.mod h1:r5r4xbfxSaeR04b166HGsBa/R4U3SueirEUpXGuw+Q0= +github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.3.8 h1:DdeB0VtEs8UmJSPdNCTeqMW5ifX3Phlvu0o0WJXZ6yE= +github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.3.8/go.mod h1:r5r4xbfxSaeR04b166HGsBa/R4U3SueirEUpXGuw+Q0= github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/controlcenter v1.1.51 h1:pGwrfCBBCt1u+EDHwfNj9NLQpvk5MVKVMcsE7SvwqM4= github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/controlcenter v1.1.51/go.mod h1:aTEdZDUTIOTS0CMDMMpKIkoc0HqtQ5+dRlaZO1KF/gg= github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/csip v1.0.860 h1:F3esKBIT3HW9+7Gt8cVgf8X06VdGIczpgLBUECzSEzU= diff --git a/tencentcloud/provider.go b/tencentcloud/provider.go index 0cac0e5305..3e0fdc6a84 100644 --- a/tencentcloud/provider.go +++ b/tencentcloud/provider.go @@ -2250,6 +2250,7 @@ func Provider() *schema.Provider { "tencentcloud_apm_sample_config": apm.ResourceTencentCloudApmSampleConfig(), "tencentcloud_apm_application_config": apm.ResourceTencentCloudApmApplicationConfig(), "tencentcloud_apm_association_config": apm.ResourceTencentCloudApmAssociationConfig(), + "tencentcloud_apm_prometheus_rule": apm.ResourceTencentCloudApmPrometheusRule(), "tencentcloud_lighthouse_firewall_rule": lighthouse.ResourceTencentCloudLighthouseFirewallRule(), "tencentcloud_lighthouse_disk_backup": lighthouse.ResourceTencentCloudLighthouseDiskBackup(), "tencentcloud_lighthouse_apply_disk_backup": lighthouse.ResourceTencentCloudLighthouseApplyDiskBackup(), diff --git a/tencentcloud/provider.md b/tencentcloud/provider.md index 4a169580b9..d5ab0d088c 100644 --- a/tencentcloud/provider.md +++ b/tencentcloud/provider.md @@ -2065,6 +2065,7 @@ tencentcloud_apm_instance tencentcloud_apm_sample_config tencentcloud_apm_application_config tencentcloud_apm_association_config +tencentcloud_apm_prometheus_rule Tencent Cloud Service Engine(TSE) Data Source diff --git a/tencentcloud/services/apm/resource_tc_apm_prometheus_rule.go b/tencentcloud/services/apm/resource_tc_apm_prometheus_rule.go new file mode 100644 index 0000000000..4c7ff445cb --- /dev/null +++ b/tencentcloud/services/apm/resource_tc_apm_prometheus_rule.go @@ -0,0 +1,348 @@ +package apm + +import ( + "context" + "fmt" + "log" + "strings" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" + apmv20210622 "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/apm/v20210622" + + tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common" + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" +) + +func ResourceTencentCloudApmPrometheusRule() *schema.Resource { + return &schema.Resource{ + Create: resourceTencentCloudApmPrometheusRuleCreate, + Read: resourceTencentCloudApmPrometheusRuleRead, + Update: resourceTencentCloudApmPrometheusRuleUpdate, + Delete: resourceTencentCloudApmPrometheusRuleDelete, + Importer: &schema.ResourceImporter{ + State: schema.ImportStatePassthrough, + }, + Schema: map[string]*schema.Schema{ + "name": { + Type: schema.TypeString, + Required: true, + Description: "Metric match rule name.", + }, + + "service_name": { + Type: schema.TypeString, + Required: true, + Description: "Applications where the rule takes effect. input an empty string for all applications.", + }, + + "metric_match_type": { + Type: schema.TypeInt, + Required: true, + Description: "Match type: 0 - precision match, 1 - prefix match, 2 - suffix match.", + }, + + "metric_name_rule": { + Type: schema.TypeString, + Required: true, + Description: "Specifies the rule for customer-defined metric names with cache hit.", + }, + + "instance_id": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + Description: "Business system ID.", + }, + + "status": { + Type: schema.TypeInt, + Optional: true, + Computed: true, + ValidateFunc: tccommon.ValidateAllowedIntValue([]int{1, 2}), + Description: "Rule status. 1 - enabled, 2 - disabled. Default value: 1.", + }, + + // computed + "rule_id": { + Type: schema.TypeInt, + Computed: true, + Description: "ID of the indicator matching rule.", + }, + }, + } +} + +func resourceTencentCloudApmPrometheusRuleCreate(d *schema.ResourceData, meta interface{}) error { + defer tccommon.LogElapsed("resource.tencentcloud_apm_prometheus_rule.create")() + defer tccommon.InconsistentCheck(d, meta)() + + var ( + logId = tccommon.GetLogId(tccommon.ContextNil) + ctx = tccommon.NewResourceLifeCycleHandleFuncContext(context.Background(), logId, d, meta) + request = apmv20210622.NewCreateApmPrometheusRuleRequest() + response = apmv20210622.NewCreateApmPrometheusRuleResponse() + instanceId string + ruleId string + ) + + if v, ok := d.GetOk("name"); ok { + request.Name = helper.String(v.(string)) + } + + if v, ok := d.GetOk("service_name"); ok { + request.ServiceName = helper.String(v.(string)) + } + + if v, ok := d.GetOkExists("metric_match_type"); ok { + request.MetricMatchType = helper.IntInt64(v.(int)) + } + + if v, ok := d.GetOk("metric_name_rule"); ok { + request.MetricNameRule = helper.String(v.(string)) + } + + if v, ok := d.GetOk("instance_id"); ok { + request.InstanceId = helper.String(v.(string)) + instanceId = v.(string) + } + + reqErr := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError { + result, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseApmClient().CreateApmPrometheusRuleWithContext(ctx, request) + if e != nil { + return tccommon.RetryError(e) + } else { + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) + } + + if result == nil || result.Response == nil { + return resource.NonRetryableError(fmt.Errorf("Create apm prometheus rule failed, Response is nil.")) + } + + response = result + return nil + }) + + if reqErr != nil { + log.Printf("[CRITAL]%s create apm prometheus rule failed, reason:%+v", logId, reqErr) + return reqErr + } + + if response.Response.RuleId == nil { + return fmt.Errorf("RuleId is nil.") + } + + ruleId = helper.Int64ToStr(*response.Response.RuleId) + d.SetId(strings.Join([]string{instanceId, ruleId}, tccommon.FILED_SP)) + + // set status + if v, ok := d.GetOkExists("status"); ok { + if v.(int) == 2 { + request := apmv20210622.NewModifyApmPrometheusRuleRequest() + if v, ok := d.GetOk("name"); ok { + request.Name = helper.String(v.(string)) + } + + if v, ok := d.GetOk("service_name"); ok { + request.ServiceName = helper.String(v.(string)) + } + + if v, ok := d.GetOkExists("metric_match_type"); ok { + request.MetricMatchType = helper.IntInt64(v.(int)) + } + + if v, ok := d.GetOk("metric_name_rule"); ok { + request.MetricNameRule = helper.String(v.(string)) + } + + request.InstanceId = &instanceId + request.Id = helper.StrToInt64Point(ruleId) + request.Status = helper.IntUint64(2) + reqErr := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError { + result, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseApmClient().ModifyApmPrometheusRuleWithContext(ctx, request) + if e != nil { + return tccommon.RetryError(e) + } else { + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) + } + + return nil + }) + + if reqErr != nil { + log.Printf("[CRITAL]%s update apm prometheus rule failed, reason:%+v", logId, reqErr) + return reqErr + } + } + } + + return resourceTencentCloudApmPrometheusRuleRead(d, meta) +} + +func resourceTencentCloudApmPrometheusRuleRead(d *schema.ResourceData, meta interface{}) error { + defer tccommon.LogElapsed("resource.tencentcloud_apm_prometheus_rule.read")() + defer tccommon.InconsistentCheck(d, meta)() + + var ( + logId = tccommon.GetLogId(tccommon.ContextNil) + ctx = tccommon.NewResourceLifeCycleHandleFuncContext(context.Background(), logId, d, meta) + service = ApmService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()} + ) + + idSplit := strings.Split(d.Id(), tccommon.FILED_SP) + if len(idSplit) != 2 { + return fmt.Errorf("id is broken,%s", d.Id()) + } + + instanceId := idSplit[0] + ruleId := idSplit[1] + + respData, err := service.DescribeApmPrometheusRuleById(ctx, instanceId, ruleId) + if err != nil { + return err + } + + if respData == nil { + log.Printf("[WARN]%s resource `tencentcloud_apm_prometheus_rule` [%s] not found, please check if it has been deleted.\n", logId, d.Id()) + d.SetId("") + return nil + } + + _ = d.Set("instance_id", instanceId) + + if respData.Name != nil { + _ = d.Set("name", *respData.Name) + } + + if respData.ServiceName != nil { + _ = d.Set("service_name", *respData.ServiceName) + } + + if respData.MetricMatchType != nil { + _ = d.Set("metric_match_type", *respData.MetricMatchType) + } + + if respData.MetricNameRule != nil { + _ = d.Set("metric_name_rule", *respData.MetricNameRule) + } + + if respData.Status != nil { + _ = d.Set("status", *respData.Status) + } + + if respData.Id != nil { + _ = d.Set("rule_id", *respData.Id) + } + + return nil +} + +func resourceTencentCloudApmPrometheusRuleUpdate(d *schema.ResourceData, meta interface{}) error { + defer tccommon.LogElapsed("resource.tencentcloud_apm_prometheus_rule.update")() + defer tccommon.InconsistentCheck(d, meta)() + + var ( + logId = tccommon.GetLogId(tccommon.ContextNil) + ctx = tccommon.NewResourceLifeCycleHandleFuncContext(context.Background(), logId, d, meta) + ) + + idSplit := strings.Split(d.Id(), tccommon.FILED_SP) + if len(idSplit) != 2 { + return fmt.Errorf("id is broken,%s", d.Id()) + } + + instanceId := idSplit[0] + ruleId := idSplit[1] + + needChange := false + mutableArgs := []string{"name", "service_name", "metric_match_type", "metric_name_rule", "status"} + for _, v := range mutableArgs { + if d.HasChange(v) { + needChange = true + break + } + } + + if needChange { + request := apmv20210622.NewModifyApmPrometheusRuleRequest() + if v, ok := d.GetOk("name"); ok { + request.Name = helper.String(v.(string)) + } + + if v, ok := d.GetOk("service_name"); ok { + request.ServiceName = helper.String(v.(string)) + } + + if v, ok := d.GetOkExists("metric_match_type"); ok { + request.MetricMatchType = helper.IntInt64(v.(int)) + } + + if v, ok := d.GetOk("metric_name_rule"); ok { + request.MetricNameRule = helper.String(v.(string)) + } + + if v, ok := d.GetOkExists("status"); ok { + request.Status = helper.IntUint64(v.(int)) + } + + request.InstanceId = &instanceId + request.Id = helper.StrToInt64Point(ruleId) + reqErr := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError { + result, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseApmClient().ModifyApmPrometheusRuleWithContext(ctx, request) + if e != nil { + return tccommon.RetryError(e) + } else { + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) + } + + return nil + }) + + if reqErr != nil { + log.Printf("[CRITAL]%s update apm prometheus rule failed, reason:%+v", logId, reqErr) + return reqErr + } + } + + return resourceTencentCloudApmPrometheusRuleRead(d, meta) +} + +func resourceTencentCloudApmPrometheusRuleDelete(d *schema.ResourceData, meta interface{}) error { + defer tccommon.LogElapsed("resource.tencentcloud_apm_prometheus_rule.delete")() + defer tccommon.InconsistentCheck(d, meta)() + + var ( + logId = tccommon.GetLogId(tccommon.ContextNil) + ctx = tccommon.NewResourceLifeCycleHandleFuncContext(context.Background(), logId, d, meta) + request = apmv20210622.NewModifyApmPrometheusRuleRequest() + ) + + idSplit := strings.Split(d.Id(), tccommon.FILED_SP) + if len(idSplit) != 2 { + return fmt.Errorf("id is broken,%s", d.Id()) + } + + instanceId := idSplit[0] + ruleId := idSplit[1] + + request.InstanceId = &instanceId + request.Id = helper.StrToInt64Point(ruleId) + request.Status = helper.IntUint64(3) + reqErr := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError { + result, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseApmClient().ModifyApmPrometheusRuleWithContext(ctx, request) + if e != nil { + return tccommon.RetryError(e) + } else { + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) + } + + return nil + }) + + if reqErr != nil { + log.Printf("[CRITAL]%s delete apm prometheus rule failed, reason:%+v", logId, reqErr) + return reqErr + } + + return nil +} diff --git a/tencentcloud/services/apm/resource_tc_apm_prometheus_rule.md b/tencentcloud/services/apm/resource_tc_apm_prometheus_rule.md new file mode 100644 index 0000000000..2494aecadd --- /dev/null +++ b/tencentcloud/services/apm/resource_tc_apm_prometheus_rule.md @@ -0,0 +1,22 @@ +Provides a resource to create a APM prometheus rule + +Example Usage + +```hcl +resource "tencentcloud_apm_prometheus_rule" "example" { + instance_id = "apm-lhqHyRBuA" + name = "tf-example" + service_name = "java-market-service" + metric_match_type = 0 + metric_name_rule = "task.duration" + status = 1 +} +``` + +Import + +APM prometheus rule can be imported using the instanceId#ruleId, e.g. + +``` +terraform import tencentcloud_apm_prometheus_rule.example apm-lhqHyRBuA#140 +``` diff --git a/tencentcloud/services/apm/resource_tc_apm_prometheus_rule_test.go b/tencentcloud/services/apm/resource_tc_apm_prometheus_rule_test.go new file mode 100644 index 0000000000..5198b678b5 --- /dev/null +++ b/tencentcloud/services/apm/resource_tc_apm_prometheus_rule_test.go @@ -0,0 +1,72 @@ +package apm_test + +import ( + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" + + tcacctest "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/acctest" +) + +func TestAccTencentCloudApmPrometheusRuleResource_basic(t *testing.T) { + t.Parallel() + resource.Test(t, resource.TestCase{ + PreCheck: func() { + tcacctest.AccPreCheck(t) + }, + Providers: tcacctest.AccProviders, + Steps: []resource.TestStep{ + { + Config: testAccApmPrometheusRule, + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttrSet("tencentcloud_apm_prometheus_rule.example", "id"), + resource.TestCheckResourceAttrSet("tencentcloud_apm_prometheus_rule.example", "instance_id"), + resource.TestCheckResourceAttrSet("tencentcloud_apm_prometheus_rule.example", "name"), + resource.TestCheckResourceAttrSet("tencentcloud_apm_prometheus_rule.example", "service_name"), + resource.TestCheckResourceAttrSet("tencentcloud_apm_prometheus_rule.example", "metric_match_type"), + resource.TestCheckResourceAttrSet("tencentcloud_apm_prometheus_rule.example", "metric_name_rule"), + resource.TestCheckResourceAttrSet("tencentcloud_apm_prometheus_rule.example", "status"), + ), + }, + { + Config: testAccApmPrometheusRuleUpdate, + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttrSet("tencentcloud_apm_prometheus_rule.example", "id"), + resource.TestCheckResourceAttrSet("tencentcloud_apm_prometheus_rule.example", "instance_id"), + resource.TestCheckResourceAttrSet("tencentcloud_apm_prometheus_rule.example", "name"), + resource.TestCheckResourceAttrSet("tencentcloud_apm_prometheus_rule.example", "service_name"), + resource.TestCheckResourceAttrSet("tencentcloud_apm_prometheus_rule.example", "metric_match_type"), + resource.TestCheckResourceAttrSet("tencentcloud_apm_prometheus_rule.example", "metric_name_rule"), + resource.TestCheckResourceAttrSet("tencentcloud_apm_prometheus_rule.example", "status"), + ), + }, + { + ResourceName: "tencentcloud_apm_prometheus_rule.example", + ImportState: true, + ImportStateVerify: true, + }, + }, + }) +} + +const testAccApmPrometheusRule = ` +resource "tencentcloud_apm_prometheus_rule" "example" { + instance_id = "apm-lhqHyRBuA" + name = "tf-example" + service_name = "java-market-service" + metric_match_type = 0 + metric_name_rule = "task.duration" + status = 1 +} +` + +const testAccApmPrometheusRuleUpdate = ` +resource "tencentcloud_apm_prometheus_rule" "example" { + instance_id = "apm-lhqHyRBuA" + name = "tf-example-update" + service_name = "java-market-service" + metric_match_type = 0 + metric_name_rule = "task.duration" + status = 2 +} +` diff --git a/tencentcloud/services/apm/service_tencentcloud_apm.go b/tencentcloud/services/apm/service_tencentcloud_apm.go index 83bd6c62cc..dd49869ab8 100644 --- a/tencentcloud/services/apm/service_tencentcloud_apm.go +++ b/tencentcloud/services/apm/service_tencentcloud_apm.go @@ -7,6 +7,7 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common" + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" apm "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/apm/v20210622" @@ -246,3 +247,48 @@ func (me *ApmService) DescribeApmAssociationById(ctx context.Context, instanceId ret = response.Response.ApmAssociation return } + +func (me *ApmService) DescribeApmPrometheusRuleById(ctx context.Context, instanceId, ruleId string) (ret *apm.ApmPrometheusRules, errRet error) { + logId := tccommon.GetLogId(ctx) + + request := apm.NewDescribeApmPrometheusRuleRequest() + response := apm.NewDescribeApmPrometheusRuleResponse() + request.InstanceId = &instanceId + + defer func() { + if errRet != nil { + log.Printf("[CRITAL]%s api[%s] fail, request body [%s], reason[%s]\n", logId, request.GetAction(), request.ToJsonString(), errRet.Error()) + } + }() + + err := resource.Retry(tccommon.ReadRetryTimeout, func() *resource.RetryError { + ratelimit.Check(request.GetAction()) + result, err := me.client.UseApmClient().DescribeApmPrometheusRule(request) + if err != nil { + return tccommon.RetryError(err) + } else { + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) + } + + if result == nil || result.Response == nil || result.Response.ApmPrometheusRules == nil || len(result.Response.ApmPrometheusRules) == 0 { + return resource.NonRetryableError(fmt.Errorf("Describe apm prometheus rule failed, Response is nil.")) + } + + response = result + return nil + }) + + if err != nil { + errRet = err + return + } + + for _, item := range response.Response.ApmPrometheusRules { + if item != nil && item.Id != nil && helper.Int64ToStr(*item.Id) == ruleId { + ret = item + return + } + } + + return +} diff --git a/tencentcloud/services/igtm/resource_tc_igtm_strategy.md b/tencentcloud/services/igtm/resource_tc_igtm_strategy.md index 64c762eb56..b6c7c50ca7 100644 --- a/tencentcloud/services/igtm/resource_tc_igtm_strategy.md +++ b/tencentcloud/services/igtm/resource_tc_igtm_strategy.md @@ -117,5 +117,5 @@ Import IGTM strategy can be imported using the instanceId#strategyId, e.g. ``` -terraform import tencentcloud_igtm_strategy.igtm_strategy gtm-uukztqtoaru#7556 +terraform import tencentcloud_igtm_strategy.example gtm-uukztqtoaru#7556 ``` diff --git a/vendor/github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/apm/v20210622/models.go b/vendor/github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/apm/v20210622/models.go index 10faba8d6e..6b8b7967bf 100644 --- a/vendor/github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/apm/v20210622/models.go +++ b/vendor/github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/apm/v20210622/models.go @@ -249,6 +249,12 @@ type ApmAppConfig struct { // 探针熔断CPU阈值 DisableCpuUsed *int64 `json:"DisableCpuUsed,omitnil,omitempty" name:"DisableCpuUsed"` + + // 是否开启SQL参数获取 + DbStatementParametersEnabled *bool `json:"DbStatementParametersEnabled,omitnil,omitempty" name:"DbStatementParametersEnabled"` + + // 慢SQL阈值 + SlowSQLThresholds []*ApmTag `json:"SlowSQLThresholds,omitnil,omitempty" name:"SlowSQLThresholds"` } type ApmApplicationConfigView struct { @@ -314,6 +320,12 @@ type ApmApplicationConfigView struct { // 探针熔断CPU阈值 DisableCpuUsed *int64 `json:"DisableCpuUsed,omitnil,omitempty" name:"DisableCpuUsed"` + + // 是否开启SQL参数获取 + DbStatementParametersEnabled *bool `json:"DbStatementParametersEnabled,omitnil,omitempty" name:"DbStatementParametersEnabled"` + + // 慢SQL阈值 + SlowSQLThresholds []*ApmTag `json:"SlowSQLThresholds,omitnil,omitempty" name:"SlowSQLThresholds"` } type ApmAssociation struct { @@ -784,6 +796,9 @@ func (r *CreateApmPrometheusRuleRequest) FromJsonString(s string) error { // Predefined struct for user type CreateApmPrometheusRuleResponseParams struct { + // 指标匹配规则的ID + RuleId *int64 `json:"RuleId,omitnil,omitempty" name:"RuleId"` + // 唯一请求 ID,由服务端生成,每次请求都会返回(若请求因其他原因未能抵达服务端,则该次请求不会获得 RequestId)。定位问题时需要提供该次请求的 RequestId。 RequestId *string `json:"RequestId,omitnil,omitempty" name:"RequestId"` } @@ -2700,6 +2715,12 @@ type ModifyApmApplicationConfigRequestParams struct { // 探针熔断CPU阈值 DisableCpuUsed *int64 `json:"DisableCpuUsed,omitnil,omitempty" name:"DisableCpuUsed"` + + // 是否开启SQL参数获取 + DbStatementParametersEnabled *bool `json:"DbStatementParametersEnabled,omitnil,omitempty" name:"DbStatementParametersEnabled"` + + // 慢SQL阈值 + SlowSQLThresholds []*ApmTag `json:"SlowSQLThresholds,omitnil,omitempty" name:"SlowSQLThresholds"` } type ModifyApmApplicationConfigRequest struct { @@ -2851,6 +2872,12 @@ type ModifyApmApplicationConfigRequest struct { // 探针熔断CPU阈值 DisableCpuUsed *int64 `json:"DisableCpuUsed,omitnil,omitempty" name:"DisableCpuUsed"` + + // 是否开启SQL参数获取 + DbStatementParametersEnabled *bool `json:"DbStatementParametersEnabled,omitnil,omitempty" name:"DbStatementParametersEnabled"` + + // 慢SQL阈值 + SlowSQLThresholds []*ApmTag `json:"SlowSQLThresholds,omitnil,omitempty" name:"SlowSQLThresholds"` } func (r *ModifyApmApplicationConfigRequest) ToJsonString() string { @@ -2914,6 +2941,8 @@ func (r *ModifyApmApplicationConfigRequest) FromJsonString(s string) error { delete(f, "UrlNumberSegmentThreshold") delete(f, "DisableMemoryUsed") delete(f, "DisableCpuUsed") + delete(f, "DbStatementParametersEnabled") + delete(f, "SlowSQLThresholds") if len(f) > 0 { return tcerr.NewTencentCloudSDKError("ClientError.BuildRequestError", "ModifyApmApplicationConfigRequest has unknown keys!", "") } @@ -2944,10 +2973,10 @@ func (r *ModifyApmApplicationConfigResponse) FromJsonString(s string) error { // Predefined struct for user type ModifyApmAssociationRequestParams struct { - // 关联的产品名,当前只支持Prometheus + // 关联的产品名,当前只支持Prometheus、CKafka ProductName *string `json:"ProductName,omitnil,omitempty" name:"ProductName"` - // 关联关系的状态:// 关联关系状态:1(启用)、2(不启用)、4(已删除) + // 关联关系的状态:// 关联关系状态:1(启用)、2(不启用) Status *uint64 `json:"Status,omitnil,omitempty" name:"Status"` // 业务系统ID @@ -2963,10 +2992,10 @@ type ModifyApmAssociationRequestParams struct { type ModifyApmAssociationRequest struct { *tchttp.BaseRequest - // 关联的产品名,当前只支持Prometheus + // 关联的产品名,当前只支持Prometheus、CKafka ProductName *string `json:"ProductName,omitnil,omitempty" name:"ProductName"` - // 关联关系的状态:// 关联关系状态:1(启用)、2(不启用)、4(已删除) + // 关联关系的状态:// 关联关系状态:1(启用)、2(不启用) Status *uint64 `json:"Status,omitnil,omitempty" name:"Status"` // 业务系统ID diff --git a/vendor/github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/http/request.go b/vendor/github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/http/request.go index 1c7674a7ce..f7856f10e3 100644 --- a/vendor/github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/http/request.go +++ b/vendor/github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/http/request.go @@ -265,7 +265,7 @@ func CompleteCommonParams(request Request, region string, requestClient string) params["Action"] = request.GetAction() params["Timestamp"] = strconv.FormatInt(time.Now().Unix(), 10) params["Nonce"] = strconv.Itoa(rand.Int()) - params["RequestClient"] = "SDK_GO_1.3.7" + params["RequestClient"] = "SDK_GO_1.3.8" if requestClient != "" { params["RequestClient"] += ": " + requestClient } diff --git a/vendor/modules.txt b/vendor/modules.txt index 2a7cf40306..e82271b6b1 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -1107,7 +1107,7 @@ github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/api/v20201106 # github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/apigateway v1.0.763 ## explicit; go 1.14 github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/apigateway/v20180808 -# github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/apm v1.2.2 +# github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/apm v1.3.8 ## explicit; go 1.14 github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/apm/v20210622 # github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/as v1.3.4 @@ -1170,7 +1170,7 @@ github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cloudaudit/v20190319 # github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cls v1.0.1148 ## explicit; go 1.14 github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cls/v20201016 -# github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.3.7 +# github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.3.8 ## explicit; go 1.11 github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/errors diff --git a/website/docs/r/apm_prometheus_rule.html.markdown b/website/docs/r/apm_prometheus_rule.html.markdown new file mode 100644 index 0000000000..650d45cb48 --- /dev/null +++ b/website/docs/r/apm_prometheus_rule.html.markdown @@ -0,0 +1,53 @@ +--- +subcategory: "Application Performance Management(APM)" +layout: "tencentcloud" +page_title: "TencentCloud: tencentcloud_apm_prometheus_rule" +sidebar_current: "docs-tencentcloud-resource-apm_prometheus_rule" +description: |- + Provides a resource to create a APM prometheus rule +--- + +# tencentcloud_apm_prometheus_rule + +Provides a resource to create a APM prometheus rule + +## Example Usage + +```hcl +resource "tencentcloud_apm_prometheus_rule" "example" { + instance_id = "apm-lhqHyRBuA" + name = "tf-example" + service_name = "java-market-service" + metric_match_type = 0 + metric_name_rule = "task.duration" + status = 1 +} +``` + +## Argument Reference + +The following arguments are supported: + +* `instance_id` - (Required, String, ForceNew) Business system ID. +* `metric_match_type` - (Required, Int) Match type: 0 - precision match, 1 - prefix match, 2 - suffix match. +* `metric_name_rule` - (Required, String) Specifies the rule for customer-defined metric names with cache hit. +* `name` - (Required, String) Metric match rule name. +* `service_name` - (Required, String) Applications where the rule takes effect. input an empty string for all applications. +* `status` - (Optional, Int) Rule status. 1 - enabled, 2 - disabled. Default value: 1. + +## Attributes Reference + +In addition to all arguments above, the following attributes are exported: + +* `id` - ID of the resource. +* `rule_id` - ID of the indicator matching rule. + + +## Import + +APM prometheus rule can be imported using the instanceId#ruleId, e.g. + +``` +terraform import tencentcloud_apm_prometheus_rule.example apm-lhqHyRBuA#140 +``` + diff --git a/website/docs/r/igtm_strategy.html.markdown b/website/docs/r/igtm_strategy.html.markdown index f10b5fbf28..f3ab4ada4e 100644 --- a/website/docs/r/igtm_strategy.html.markdown +++ b/website/docs/r/igtm_strategy.html.markdown @@ -177,6 +177,6 @@ In addition to all arguments above, the following attributes are exported: IGTM strategy can be imported using the instanceId#strategyId, e.g. ``` -terraform import tencentcloud_igtm_strategy.igtm_strategy gtm-uukztqtoaru#7556 +terraform import tencentcloud_igtm_strategy.example gtm-uukztqtoaru#7556 ``` diff --git a/website/tencentcloud.erb b/website/tencentcloud.erb index 853d019fee..1df0bab713 100644 --- a/website/tencentcloud.erb +++ b/website/tencentcloud.erb @@ -343,6 +343,9 @@