Skip to content

Commit 5e701aa

Browse files
authored
feat(cdb): [119213393] add new resource tencentcloud_mysql_cls_log_attachment (#2780)
* add * add * add
1 parent 42bc86a commit 5e701aa

10 files changed

+599
-0
lines changed

.changelog/2780.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_mysql_cls_log_attachment
3+
```

tencentcloud/provider.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1275,6 +1275,7 @@ func Provider() *schema.Provider {
12751275
"tencentcloud_mysql_ro_stop_replication": cdb.ResourceTencentCloudMysqlRoStopReplication(),
12761276
"tencentcloud_mysql_switch_proxy": cdb.ResourceTencentCloudMysqlSwitchProxy(),
12771277
"tencentcloud_mysql_ssl": cdb.ResourceTencentCloudMysqlSsl(),
1278+
"tencentcloud_mysql_cls_log_attachment": cdb.ResourceTencentCloudMysqlClsLogAttachment(),
12781279
"tencentcloud_cos_bucket": cos.ResourceTencentCloudCosBucket(),
12791280
"tencentcloud_cos_bucket_object": cos.ResourceTencentCloudCosBucketObject(),
12801281
"tencentcloud_cos_bucket_referer": cos.ResourceTencentCloudCosBucketReferer(),

tencentcloud/provider.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -785,6 +785,7 @@ TencentDB for MySQL(cdb)
785785
tencentcloud_mysql_isolate_instance
786786
tencentcloud_mysql_dr_instance
787787
tencentcloud_mysql_ssl
788+
tencentcloud_mysql_cls_log_attachment
788789

789790
Cloud Monitor(Monitor)
790791
Data Source

tencentcloud/services/cdb/extension_mysql.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,3 +127,13 @@ var MYSQL_TASK_TYPES = map[string]int64{
127127
"DROP TABLES": 12,
128128
"SWITCH DR TO MASTER": 13,
129129
}
130+
131+
const (
132+
MYSQL_LOG_TO_CLS_TYPE_ERROR = "error"
133+
MYSQL_LOG_TO_CLS_TYPE_SLOW = "slowlog"
134+
)
135+
136+
var MYSQL_LOG_TO_CLS_TYPE = []string{
137+
MYSQL_LOG_TO_CLS_TYPE_ERROR,
138+
MYSQL_LOG_TO_CLS_TYPE_SLOW,
139+
}
Lines changed: 262 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,262 @@
1+
package cdb
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"log"
7+
"strings"
8+
9+
tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common"
10+
11+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
12+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
13+
mysql "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cdb/v20170320"
14+
15+
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
16+
)
17+
18+
func ResourceTencentCloudMysqlClsLogAttachment() *schema.Resource {
19+
return &schema.Resource{
20+
Create: resourceTencentCloudMysqlClsLogAttachmentCreate,
21+
Read: resourceTencentCloudMysqlClsLogAttachmentRead,
22+
Delete: resourceTencentCloudMysqlClsLogAttachmentDelete,
23+
24+
Schema: map[string]*schema.Schema{
25+
"instance_id": {
26+
Required: true,
27+
ForceNew: true,
28+
Type: schema.TypeString,
29+
Description: "The id of instance.",
30+
},
31+
"log_type": {
32+
Required: true,
33+
ForceNew: true,
34+
Type: schema.TypeString,
35+
ValidateFunc: tccommon.ValidateAllowedStringValue(MYSQL_LOG_TO_CLS_TYPE),
36+
Description: "Log type. Support `error` or `slowlog`.",
37+
},
38+
"create_log_set": {
39+
Optional: true,
40+
ForceNew: true,
41+
Type: schema.TypeBool,
42+
Description: "Whether to create log set.",
43+
},
44+
"create_log_topic": {
45+
Optional: true,
46+
ForceNew: true,
47+
Type: schema.TypeBool,
48+
Description: "Whether to create log topic.",
49+
},
50+
"log_set": {
51+
Required: true,
52+
ForceNew: true,
53+
Type: schema.TypeString,
54+
Description: "If `create_log_set` is `true`, use log set name, Else use log set Id.",
55+
},
56+
"log_topic": {
57+
Required: true,
58+
ForceNew: true,
59+
Type: schema.TypeString,
60+
Description: "If `create_log_topic` is `true`, use log topic name, Else use log topic Id.",
61+
},
62+
"period": {
63+
Optional: true,
64+
ForceNew: true,
65+
Type: schema.TypeInt,
66+
Description: "The validity period of the log theme is 30 days by default when not filled in.",
67+
},
68+
"create_index": {
69+
Optional: true,
70+
ForceNew: true,
71+
Type: schema.TypeBool,
72+
Description: "Whether to create index.",
73+
},
74+
"cls_region": {
75+
Optional: true,
76+
Computed: true,
77+
Type: schema.TypeString,
78+
Description: "Cls region.",
79+
},
80+
"log_set_id": {
81+
Computed: true,
82+
Type: schema.TypeString,
83+
Description: "Log set Id.",
84+
},
85+
"log_topic_id": {
86+
Computed: true,
87+
Type: schema.TypeString,
88+
Description: "Log topic Id.",
89+
},
90+
"status": {
91+
Computed: true,
92+
Type: schema.TypeString,
93+
Description: "Log Status.",
94+
},
95+
},
96+
}
97+
}
98+
99+
func resourceTencentCloudMysqlClsLogAttachmentCreate(d *schema.ResourceData, meta interface{}) error {
100+
defer tccommon.LogElapsed("resource.tencentcloud_mysql_cls_log_attachment.create")()
101+
defer tccommon.InconsistentCheck(d, meta)()
102+
103+
var (
104+
logId = tccommon.GetLogId(tccommon.ContextNil)
105+
request = mysql.NewModifyDBInstanceLogToCLSRequest()
106+
instanceId string
107+
logType string
108+
)
109+
110+
if v, ok := d.GetOk("instance_id"); ok {
111+
instanceId = v.(string)
112+
request.InstanceId = helper.String(v.(string))
113+
}
114+
115+
if v, ok := d.GetOk("log_type"); ok {
116+
logType = v.(string)
117+
request.LogType = helper.String(v.(string))
118+
}
119+
120+
if v, ok := d.GetOkExists("create_log_set"); ok {
121+
request.CreateLogset = helper.Bool(v.(bool))
122+
}
123+
124+
if v, ok := d.GetOkExists("create_log_topic"); ok {
125+
request.CreateLogTopic = helper.Bool(v.(bool))
126+
}
127+
128+
if v, ok := d.GetOk("log_set"); ok {
129+
request.Logset = helper.String(v.(string))
130+
}
131+
132+
if v, ok := d.GetOk("log_topic"); ok {
133+
request.LogTopic = helper.String(v.(string))
134+
}
135+
136+
if v, ok := d.GetOkExists("period"); ok {
137+
request.Period = helper.IntInt64(v.(int))
138+
}
139+
140+
if v, ok := d.GetOkExists("create_index"); ok {
141+
request.CreateIndex = helper.Bool(v.(bool))
142+
}
143+
144+
if v, ok := d.GetOk("cls_region"); ok {
145+
request.ClsRegion = helper.String(v.(string))
146+
}
147+
148+
request.Status = helper.String("ON")
149+
err := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError {
150+
result, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseMysqlClient().ModifyDBInstanceLogToCLS(request)
151+
if e != nil {
152+
return tccommon.RetryError(e)
153+
} else {
154+
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString())
155+
}
156+
157+
return nil
158+
})
159+
160+
if err != nil {
161+
log.Printf("[CRITAL]%s create mysql ModifyDBInstanceLogToCLS failed, reason:%+v", logId, err)
162+
return err
163+
}
164+
165+
d.SetId(strings.Join([]string{instanceId, logType}, tccommon.FILED_SP))
166+
167+
return resourceTencentCloudMysqlClsLogAttachmentRead(d, meta)
168+
}
169+
170+
func resourceTencentCloudMysqlClsLogAttachmentRead(d *schema.ResourceData, meta interface{}) error {
171+
defer tccommon.LogElapsed("resource.tencentcloud_mysql_cls_log_attachment.read")()
172+
defer tccommon.InconsistentCheck(d, meta)()
173+
174+
var (
175+
logId = tccommon.GetLogId(tccommon.ContextNil)
176+
ctx = context.WithValue(context.TODO(), tccommon.LogIdKey, logId)
177+
service = MysqlService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()}
178+
)
179+
180+
idSplit := strings.Split(d.Id(), tccommon.FILED_SP)
181+
if len(idSplit) != 2 {
182+
return fmt.Errorf("id is broken,%s", d.Id())
183+
}
184+
185+
instanceId := idSplit[0]
186+
logType := idSplit[1]
187+
188+
logToCLSResponseParam, err := service.DescribeMysqlInstanceLogToCLSById(ctx, instanceId)
189+
if err != nil {
190+
return err
191+
}
192+
193+
if logToCLSResponseParam == nil {
194+
d.SetId("")
195+
log.Printf("[WARN]%s resource `DescribeDBInstanceLogToCLS` [%s] not found, please check if it has been deleted.\n", logId, d.Id())
196+
return nil
197+
}
198+
199+
_ = d.Set("instance_id", instanceId)
200+
_ = d.Set("log_type", logType)
201+
202+
if logType == MYSQL_LOG_TO_CLS_TYPE_ERROR {
203+
if logToCLSResponseParam.ErrorLog.LogSetId != nil {
204+
_ = d.Set("log_set_id", *logToCLSResponseParam.ErrorLog.LogSetId)
205+
}
206+
207+
if logToCLSResponseParam.ErrorLog.LogTopicId != nil {
208+
_ = d.Set("log_topic_id", *logToCLSResponseParam.ErrorLog.LogTopicId)
209+
}
210+
211+
if logToCLSResponseParam.ErrorLog.Status != nil {
212+
_ = d.Set("status", *logToCLSResponseParam.ErrorLog.Status)
213+
}
214+
215+
if logToCLSResponseParam.ErrorLog.ClsRegion != nil {
216+
_ = d.Set("cls_region", *logToCLSResponseParam.ErrorLog.ClsRegion)
217+
}
218+
} else {
219+
if logToCLSResponseParam.SlowLog.LogSetId != nil {
220+
_ = d.Set("log_set_id", *logToCLSResponseParam.SlowLog.LogSetId)
221+
}
222+
223+
if logToCLSResponseParam.SlowLog.LogTopicId != nil {
224+
_ = d.Set("log_topic_id", *logToCLSResponseParam.SlowLog.LogTopicId)
225+
}
226+
227+
if logToCLSResponseParam.SlowLog.Status != nil {
228+
_ = d.Set("status", *logToCLSResponseParam.SlowLog.Status)
229+
}
230+
231+
if logToCLSResponseParam.SlowLog.ClsRegion != nil {
232+
_ = d.Set("cls_region", *logToCLSResponseParam.SlowLog.ClsRegion)
233+
}
234+
}
235+
236+
return nil
237+
}
238+
239+
func resourceTencentCloudMysqlClsLogAttachmentDelete(d *schema.ResourceData, meta interface{}) error {
240+
defer tccommon.LogElapsed("resource.tencentcloud_mysql_cls_log_attachment.delete")()
241+
defer tccommon.InconsistentCheck(d, meta)()
242+
243+
var (
244+
logId = tccommon.GetLogId(tccommon.ContextNil)
245+
ctx = context.WithValue(context.TODO(), tccommon.LogIdKey, logId)
246+
service = MysqlService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()}
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+
logType := idSplit[1]
256+
257+
if err := service.DeleteMysqlInstanceLogToCLSById(ctx, instanceId, logType); err != nil {
258+
return err
259+
}
260+
261+
return nil
262+
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
Provides a resource to create a mysql log to cls
2+
3+
~> **NOTE:** The CLS resource bound to resource `tencentcloud_mysql_cls_log_attachment` needs to be manually deleted.
4+
5+
Example Usage
6+
7+
Create Error Log to ClS
8+
9+
```hcl
10+
# create vpc
11+
resource "tencentcloud_vpc" "vpc" {
12+
name = "vpc-mysql"
13+
cidr_block = "10.0.0.0/16"
14+
}
15+
16+
# create subnet
17+
resource "tencentcloud_subnet" "subnet" {
18+
availability_zone = "ap-guangzhou-6"
19+
name = "subnet-mysql"
20+
vpc_id = tencentcloud_vpc.vpc.id
21+
cidr_block = "10.0.0.0/16"
22+
is_multicast = false
23+
}
24+
25+
# create security group
26+
resource "tencentcloud_security_group" "security_group" {
27+
name = "sg-mysql"
28+
description = "mysql test"
29+
}
30+
31+
# create mysql instance
32+
resource "tencentcloud_mysql_instance" "example" {
33+
internet_service = 1
34+
engine_version = "5.7"
35+
charge_type = "POSTPAID"
36+
root_password = "PassWord123"
37+
slave_deploy_mode = 0
38+
availability_zone = "ap-guangzhou-6"
39+
slave_sync_mode = 1
40+
instance_name = "tf-example-mysql"
41+
mem_size = 4000
42+
volume_size = 200
43+
vpc_id = tencentcloud_vpc.vpc.id
44+
subnet_id = tencentcloud_subnet.subnet.id
45+
intranet_port = 3306
46+
security_groups = [tencentcloud_security_group.security_group.id]
47+
48+
tags = {
49+
name = "test"
50+
}
51+
52+
parameters = {
53+
character_set_server = "utf8"
54+
max_connections = "1000"
55+
}
56+
}
57+
58+
# attachment cls log
59+
resource "tencentcloud_mysql_cls_log_attachment" "example" {
60+
instance_id = tencentcloud_mysql_instance.example.id
61+
log_type = "error"
62+
create_log_set = true
63+
create_log_topic = true
64+
log_set = "tf_log_set"
65+
log_topic = "tf_log_topic"
66+
period = 30
67+
create_index = true
68+
cls_region = "ap-guangzhou"
69+
}
70+
```
71+
72+
Create Slow Log to ClS
73+
74+
```hcl
75+
resource "tencentcloud_mysql_cls_log_attachment" "example" {
76+
instance_id = tencentcloud_mysql_instance.example.id
77+
log_type = "slowlog"
78+
log_set = "50d499a8-c4c0-4442-aa04-e8aa8a02437d"
79+
log_topic = "140d4d39-4307-45a8-9655-290f679b063d"
80+
}
81+
```
82+
83+
Import
84+
85+
mysql log to cls can be imported using the id, e.g.
86+
87+
```
88+
terraform import tencentcloud_mysql_cls_log_attachment.example cdb-8fk7id2l#slowlog
89+
```

0 commit comments

Comments
 (0)