Skip to content

Commit 3b4b251

Browse files
authored
feat(apm): [128680339] add new resource (#3649)
* add * add * add
1 parent 10fe263 commit 3b4b251

File tree

15 files changed

+588
-11
lines changed

15 files changed

+588
-11
lines changed

.changelog/3649.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:new-resource
2+
tencentcloud_apm_prometheus_rule
3+
```

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ require (
2929
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/antiddos v1.0.799
3030
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/api v1.0.285
3131
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/apigateway v1.0.763
32-
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/apm v1.2.2
32+
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/apm v1.3.8
3333
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/as v1.3.4
3434
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/bi v1.0.824
3535
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cam v1.1.27

go.sum

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -825,8 +825,8 @@ github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/api v1.0.285 h1:gFmukRG
825825
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/api v1.0.285/go.mod h1:aGlXSWjtSnE6kuqcaRy/NKj1CLiB8NlMSHGsDn+k7Ag=
826826
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/apigateway v1.0.763 h1:jNjM/+MU2HplNZpCxDBIBKXZzumIDFJ4ztYHob0thow=
827827
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/apigateway v1.0.763/go.mod h1:OlRreot089Ec7bEYMUovUBSuCD/x0D0ONPZ0g3YA5hg=
828-
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/apm v1.2.2 h1:/BlN5LD/rLm0WMM4Yyp2m4XQ8EVzlSV0uKt3SuT5+SE=
829-
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/apm v1.2.2/go.mod h1:XIggn8QQknIzb+1lnsOiKbTQ0fjRe1uV6P+1N0L2ccI=
828+
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/apm v1.3.8 h1:v/G/D3bqUKMR2QTlyPVBcLIJBBy4MXs/0JDCcZFUK50=
829+
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/apm v1.3.8/go.mod h1:DarTPk6LPu4LtKwDRbF2V2Af4KKXVXnzyteNhAifWm8=
830830
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/as v1.3.4 h1:hTOxj4qB+UX6MflqmIYT4cPbfEqtiJEfi9tR58DxkIw=
831831
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/as v1.3.4/go.mod h1:q7fZk23gy6BkZGcNw9NXFSj26R44tiJZKGxLFfOrMo0=
832832
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/bi v1.0.824 h1:DVKvZ6h+qd7tadUrCjVAkCCmE3TsbK2ZmwGd3AJcpWc=
@@ -933,7 +933,6 @@ github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.1.46/go.mod h
933933
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.1.49/go.mod h1:r5r4xbfxSaeR04b166HGsBa/R4U3SueirEUpXGuw+Q0=
934934
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.1.50/go.mod h1:r5r4xbfxSaeR04b166HGsBa/R4U3SueirEUpXGuw+Q0=
935935
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.1.51/go.mod h1:r5r4xbfxSaeR04b166HGsBa/R4U3SueirEUpXGuw+Q0=
936-
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.2.2/go.mod h1:r5r4xbfxSaeR04b166HGsBa/R4U3SueirEUpXGuw+Q0=
937936
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.3.3/go.mod h1:r5r4xbfxSaeR04b166HGsBa/R4U3SueirEUpXGuw+Q0=
938937
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.3.4/go.mod h1:r5r4xbfxSaeR04b166HGsBa/R4U3SueirEUpXGuw+Q0=
939938
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.3.7/go.mod h1:r5r4xbfxSaeR04b166HGsBa/R4U3SueirEUpXGuw+Q0=

tencentcloud/provider.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2250,6 +2250,7 @@ func Provider() *schema.Provider {
22502250
"tencentcloud_apm_sample_config": apm.ResourceTencentCloudApmSampleConfig(),
22512251
"tencentcloud_apm_application_config": apm.ResourceTencentCloudApmApplicationConfig(),
22522252
"tencentcloud_apm_association_config": apm.ResourceTencentCloudApmAssociationConfig(),
2253+
"tencentcloud_apm_prometheus_rule": apm.ResourceTencentCloudApmPrometheusRule(),
22532254
"tencentcloud_lighthouse_firewall_rule": lighthouse.ResourceTencentCloudLighthouseFirewallRule(),
22542255
"tencentcloud_lighthouse_disk_backup": lighthouse.ResourceTencentCloudLighthouseDiskBackup(),
22552256
"tencentcloud_lighthouse_apply_disk_backup": lighthouse.ResourceTencentCloudLighthouseApplyDiskBackup(),

tencentcloud/provider.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2065,6 +2065,7 @@ tencentcloud_apm_instance
20652065
tencentcloud_apm_sample_config
20662066
tencentcloud_apm_application_config
20672067
tencentcloud_apm_association_config
2068+
tencentcloud_apm_prometheus_rule
20682069

20692070
Tencent Cloud Service Engine(TSE)
20702071
Data Source
Lines changed: 348 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,348 @@
1+
package apm
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"log"
7+
"strings"
8+
9+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
10+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
11+
apmv20210622 "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/apm/v20210622"
12+
13+
tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common"
14+
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
15+
)
16+
17+
func ResourceTencentCloudApmPrometheusRule() *schema.Resource {
18+
return &schema.Resource{
19+
Create: resourceTencentCloudApmPrometheusRuleCreate,
20+
Read: resourceTencentCloudApmPrometheusRuleRead,
21+
Update: resourceTencentCloudApmPrometheusRuleUpdate,
22+
Delete: resourceTencentCloudApmPrometheusRuleDelete,
23+
Importer: &schema.ResourceImporter{
24+
State: schema.ImportStatePassthrough,
25+
},
26+
Schema: map[string]*schema.Schema{
27+
"name": {
28+
Type: schema.TypeString,
29+
Required: true,
30+
Description: "Metric match rule name.",
31+
},
32+
33+
"service_name": {
34+
Type: schema.TypeString,
35+
Required: true,
36+
Description: "Applications where the rule takes effect. input an empty string for all applications.",
37+
},
38+
39+
"metric_match_type": {
40+
Type: schema.TypeInt,
41+
Required: true,
42+
Description: "Match type: 0 - precision match, 1 - prefix match, 2 - suffix match.",
43+
},
44+
45+
"metric_name_rule": {
46+
Type: schema.TypeString,
47+
Required: true,
48+
Description: "Specifies the rule for customer-defined metric names with cache hit.",
49+
},
50+
51+
"instance_id": {
52+
Type: schema.TypeString,
53+
Required: true,
54+
ForceNew: true,
55+
Description: "Business system ID.",
56+
},
57+
58+
"status": {
59+
Type: schema.TypeInt,
60+
Optional: true,
61+
Computed: true,
62+
ValidateFunc: tccommon.ValidateAllowedIntValue([]int{1, 2}),
63+
Description: "Rule status. 1 - enabled, 2 - disabled. Default value: 1.",
64+
},
65+
66+
// computed
67+
"rule_id": {
68+
Type: schema.TypeInt,
69+
Computed: true,
70+
Description: "ID of the indicator matching rule.",
71+
},
72+
},
73+
}
74+
}
75+
76+
func resourceTencentCloudApmPrometheusRuleCreate(d *schema.ResourceData, meta interface{}) error {
77+
defer tccommon.LogElapsed("resource.tencentcloud_apm_prometheus_rule.create")()
78+
defer tccommon.InconsistentCheck(d, meta)()
79+
80+
var (
81+
logId = tccommon.GetLogId(tccommon.ContextNil)
82+
ctx = tccommon.NewResourceLifeCycleHandleFuncContext(context.Background(), logId, d, meta)
83+
request = apmv20210622.NewCreateApmPrometheusRuleRequest()
84+
response = apmv20210622.NewCreateApmPrometheusRuleResponse()
85+
instanceId string
86+
ruleId string
87+
)
88+
89+
if v, ok := d.GetOk("name"); ok {
90+
request.Name = helper.String(v.(string))
91+
}
92+
93+
if v, ok := d.GetOk("service_name"); ok {
94+
request.ServiceName = helper.String(v.(string))
95+
}
96+
97+
if v, ok := d.GetOkExists("metric_match_type"); ok {
98+
request.MetricMatchType = helper.IntInt64(v.(int))
99+
}
100+
101+
if v, ok := d.GetOk("metric_name_rule"); ok {
102+
request.MetricNameRule = helper.String(v.(string))
103+
}
104+
105+
if v, ok := d.GetOk("instance_id"); ok {
106+
request.InstanceId = helper.String(v.(string))
107+
instanceId = v.(string)
108+
}
109+
110+
reqErr := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError {
111+
result, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseApmClient().CreateApmPrometheusRuleWithContext(ctx, request)
112+
if e != nil {
113+
return tccommon.RetryError(e)
114+
} else {
115+
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString())
116+
}
117+
118+
if result == nil || result.Response == nil {
119+
return resource.NonRetryableError(fmt.Errorf("Create apm prometheus rule failed, Response is nil."))
120+
}
121+
122+
response = result
123+
return nil
124+
})
125+
126+
if reqErr != nil {
127+
log.Printf("[CRITAL]%s create apm prometheus rule failed, reason:%+v", logId, reqErr)
128+
return reqErr
129+
}
130+
131+
if response.Response.RuleId == nil {
132+
return fmt.Errorf("RuleId is nil.")
133+
}
134+
135+
ruleId = helper.Int64ToStr(*response.Response.RuleId)
136+
d.SetId(strings.Join([]string{instanceId, ruleId}, tccommon.FILED_SP))
137+
138+
// set status
139+
if v, ok := d.GetOkExists("status"); ok {
140+
if v.(int) == 2 {
141+
request := apmv20210622.NewModifyApmPrometheusRuleRequest()
142+
if v, ok := d.GetOk("name"); ok {
143+
request.Name = helper.String(v.(string))
144+
}
145+
146+
if v, ok := d.GetOk("service_name"); ok {
147+
request.ServiceName = helper.String(v.(string))
148+
}
149+
150+
if v, ok := d.GetOkExists("metric_match_type"); ok {
151+
request.MetricMatchType = helper.IntInt64(v.(int))
152+
}
153+
154+
if v, ok := d.GetOk("metric_name_rule"); ok {
155+
request.MetricNameRule = helper.String(v.(string))
156+
}
157+
158+
request.InstanceId = &instanceId
159+
request.Id = helper.StrToInt64Point(ruleId)
160+
request.Status = helper.IntUint64(2)
161+
reqErr := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError {
162+
result, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseApmClient().ModifyApmPrometheusRuleWithContext(ctx, request)
163+
if e != nil {
164+
return tccommon.RetryError(e)
165+
} else {
166+
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString())
167+
}
168+
169+
return nil
170+
})
171+
172+
if reqErr != nil {
173+
log.Printf("[CRITAL]%s update apm prometheus rule failed, reason:%+v", logId, reqErr)
174+
return reqErr
175+
}
176+
}
177+
}
178+
179+
return resourceTencentCloudApmPrometheusRuleRead(d, meta)
180+
}
181+
182+
func resourceTencentCloudApmPrometheusRuleRead(d *schema.ResourceData, meta interface{}) error {
183+
defer tccommon.LogElapsed("resource.tencentcloud_apm_prometheus_rule.read")()
184+
defer tccommon.InconsistentCheck(d, meta)()
185+
186+
var (
187+
logId = tccommon.GetLogId(tccommon.ContextNil)
188+
ctx = tccommon.NewResourceLifeCycleHandleFuncContext(context.Background(), logId, d, meta)
189+
service = ApmService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()}
190+
)
191+
192+
idSplit := strings.Split(d.Id(), tccommon.FILED_SP)
193+
if len(idSplit) != 2 {
194+
return fmt.Errorf("id is broken,%s", d.Id())
195+
}
196+
197+
instanceId := idSplit[0]
198+
ruleId := idSplit[1]
199+
200+
respData, err := service.DescribeApmPrometheusRuleById(ctx, instanceId, ruleId)
201+
if err != nil {
202+
return err
203+
}
204+
205+
if respData == nil {
206+
log.Printf("[WARN]%s resource `tencentcloud_apm_prometheus_rule` [%s] not found, please check if it has been deleted.\n", logId, d.Id())
207+
d.SetId("")
208+
return nil
209+
}
210+
211+
_ = d.Set("instance_id", instanceId)
212+
213+
if respData.Name != nil {
214+
_ = d.Set("name", *respData.Name)
215+
}
216+
217+
if respData.ServiceName != nil {
218+
_ = d.Set("service_name", *respData.ServiceName)
219+
}
220+
221+
if respData.MetricMatchType != nil {
222+
_ = d.Set("metric_match_type", *respData.MetricMatchType)
223+
}
224+
225+
if respData.MetricNameRule != nil {
226+
_ = d.Set("metric_name_rule", *respData.MetricNameRule)
227+
}
228+
229+
if respData.Status != nil {
230+
_ = d.Set("status", *respData.Status)
231+
}
232+
233+
if respData.Id != nil {
234+
_ = d.Set("rule_id", *respData.Id)
235+
}
236+
237+
return nil
238+
}
239+
240+
func resourceTencentCloudApmPrometheusRuleUpdate(d *schema.ResourceData, meta interface{}) error {
241+
defer tccommon.LogElapsed("resource.tencentcloud_apm_prometheus_rule.update")()
242+
defer tccommon.InconsistentCheck(d, meta)()
243+
244+
var (
245+
logId = tccommon.GetLogId(tccommon.ContextNil)
246+
ctx = tccommon.NewResourceLifeCycleHandleFuncContext(context.Background(), logId, d, meta)
247+
)
248+
249+
idSplit := strings.Split(d.Id(), tccommon.FILED_SP)
250+
if len(idSplit) != 2 {
251+
return fmt.Errorf("id is broken,%s", d.Id())
252+
}
253+
254+
instanceId := idSplit[0]
255+
ruleId := idSplit[1]
256+
257+
needChange := false
258+
mutableArgs := []string{"name", "service_name", "metric_match_type", "metric_name_rule", "status"}
259+
for _, v := range mutableArgs {
260+
if d.HasChange(v) {
261+
needChange = true
262+
break
263+
}
264+
}
265+
266+
if needChange {
267+
request := apmv20210622.NewModifyApmPrometheusRuleRequest()
268+
if v, ok := d.GetOk("name"); ok {
269+
request.Name = helper.String(v.(string))
270+
}
271+
272+
if v, ok := d.GetOk("service_name"); ok {
273+
request.ServiceName = helper.String(v.(string))
274+
}
275+
276+
if v, ok := d.GetOkExists("metric_match_type"); ok {
277+
request.MetricMatchType = helper.IntInt64(v.(int))
278+
}
279+
280+
if v, ok := d.GetOk("metric_name_rule"); ok {
281+
request.MetricNameRule = helper.String(v.(string))
282+
}
283+
284+
if v, ok := d.GetOkExists("status"); ok {
285+
request.Status = helper.IntUint64(v.(int))
286+
}
287+
288+
request.InstanceId = &instanceId
289+
request.Id = helper.StrToInt64Point(ruleId)
290+
reqErr := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError {
291+
result, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseApmClient().ModifyApmPrometheusRuleWithContext(ctx, request)
292+
if e != nil {
293+
return tccommon.RetryError(e)
294+
} else {
295+
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString())
296+
}
297+
298+
return nil
299+
})
300+
301+
if reqErr != nil {
302+
log.Printf("[CRITAL]%s update apm prometheus rule failed, reason:%+v", logId, reqErr)
303+
return reqErr
304+
}
305+
}
306+
307+
return resourceTencentCloudApmPrometheusRuleRead(d, meta)
308+
}
309+
310+
func resourceTencentCloudApmPrometheusRuleDelete(d *schema.ResourceData, meta interface{}) error {
311+
defer tccommon.LogElapsed("resource.tencentcloud_apm_prometheus_rule.delete")()
312+
defer tccommon.InconsistentCheck(d, meta)()
313+
314+
var (
315+
logId = tccommon.GetLogId(tccommon.ContextNil)
316+
ctx = tccommon.NewResourceLifeCycleHandleFuncContext(context.Background(), logId, d, meta)
317+
request = apmv20210622.NewModifyApmPrometheusRuleRequest()
318+
)
319+
320+
idSplit := strings.Split(d.Id(), tccommon.FILED_SP)
321+
if len(idSplit) != 2 {
322+
return fmt.Errorf("id is broken,%s", d.Id())
323+
}
324+
325+
instanceId := idSplit[0]
326+
ruleId := idSplit[1]
327+
328+
request.InstanceId = &instanceId
329+
request.Id = helper.StrToInt64Point(ruleId)
330+
request.Status = helper.IntUint64(3)
331+
reqErr := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError {
332+
result, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseApmClient().ModifyApmPrometheusRuleWithContext(ctx, request)
333+
if e != nil {
334+
return tccommon.RetryError(e)
335+
} else {
336+
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString())
337+
}
338+
339+
return nil
340+
})
341+
342+
if reqErr != nil {
343+
log.Printf("[CRITAL]%s delete apm prometheus rule failed, reason:%+v", logId, reqErr)
344+
return reqErr
345+
}
346+
347+
return nil
348+
}

0 commit comments

Comments
 (0)