Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit c88dad8

Browse files
committedAug 19, 2024·
add
1 parent 3c4de5a commit c88dad8

9 files changed

+602
-0
lines changed
 

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

0 commit comments

Comments
 (0)
Please sign in to comment.