Skip to content

Commit 3634d8a

Browse files
authored
Feat/mariadb support (#1370)
* feat: mariadb support * fix: modify read * docs: update * feat: dedicatedcluster delete * feat: dedicatedcluster delete * fix: merge master * fix: make fmt * fix: update unit * fix: golangci-lint * fix: golangci-lint * fix: golangci-lint * fix: update unit * feat: add changelog * fix: unit needFix * fix: unit needFix Co-authored-by: arunma <arunma@tencent.com>
1 parent 10f90e3 commit 3634d8a

23 files changed

+12706
-14
lines changed

.changelog/1370.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
```release-note:new-resource
2+
tencentcloud_mariadb_db_instances
3+
```
4+
5+
```release-note:new-resource
6+
tencentcloud_mariadb_hour_db_instance
7+
```
8+
9+
```release-note:new-resource
10+
tencentcloud_mariadb_dedicatedcluster_db_instance
11+
```

go.mod

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ require (
3535
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/clb v1.0.445
3636
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cloudaudit v1.0.199
3737
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cls v1.0.412
38-
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.529
38+
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.532
3939
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cvm v1.0.445
4040
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cynosdb v1.0.488
4141
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/dayu v1.0.335
@@ -48,6 +48,7 @@ require (
4848
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/gaap v1.0.514
4949
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/kms v1.0.199
5050
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/lighthouse v1.0.413
51+
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/mariadb v1.0.532
5152
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/mongodb v1.0.199
5253
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/monitor v1.0.518
5354
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/postgres v1.0.391

go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -506,6 +506,8 @@ github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.524/go.mod
506506
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.527/go.mod h1:7sCQWVkxcsR38nffDW057DRGk8mUjK1Ing/EFOK8s8Y=
507507
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.529 h1:B26Pn9N5O3Mmbh8icZSpZ8kkhcfnapUdU/TBxPDWggA=
508508
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.529/go.mod h1:7sCQWVkxcsR38nffDW057DRGk8mUjK1Ing/EFOK8s8Y=
509+
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.532 h1:DjnGMxuX49TNNyXp6V18VO+oY9Y5Gs4/+vL0LeZ8YIo=
510+
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.532/go.mod h1:7sCQWVkxcsR38nffDW057DRGk8mUjK1Ing/EFOK8s8Y=
509511
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cvm v1.0.445 h1:Bh7XD0ypNMHYyBOM8hhKsSu+y0VVKUnJVS+YKKhfpGg=
510512
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cvm v1.0.445/go.mod h1:jMDD351efCFpT1+KVFbcpu6SbmP4TYmp4qkoCfr63nQ=
511513
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cynosdb v1.0.488 h1:A1seXWtMf2atBjSNYvcwxyDoFzCMgqyVnsxnWzhqJEA=
@@ -531,6 +533,8 @@ github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/kms v1.0.199 h1:rSDQeqv
531533
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/kms v1.0.199/go.mod h1:yrBKWhChnDqNz1xuXdSbWXG56XawEq0G5j1lg4VwBD4=
532534
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/lighthouse v1.0.413 h1:9v95vMBfqvBciwagN+ZqP6Ue3iBFYZRVQga4c1yL5Uc=
533535
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/lighthouse v1.0.413/go.mod h1:nG/8Iz/MtV4zBxz9ToLJ90O4ocdME9WUKwims/bitr4=
536+
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/mariadb v1.0.532 h1:3PCI1M2dvfAWVx+5xTqap+8qIsmG8cQ4lpvGfwmO2NM=
537+
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/mariadb v1.0.532/go.mod h1:lb1pdGQn9dzyXmq6+NkpN4avTVRvG+ziqFtOeL5D7y4=
534538
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/mongodb v1.0.199 h1:2jsGprrewRIP3smcTsY5GpEuOuJ+qyV/BN3Py0Ivf1o=
535539
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/mongodb v1.0.199/go.mod h1:f5GhbJyuYQBzRaDHGe3I4U9utb0Pmx2NlI+XcqSQRp0=
536540
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/monitor v1.0.518 h1:XClo3Xa5F9pko91olHT85aZXXcRkQiOYBCMqm8+tHG8=

tencentcloud/basic_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -778,3 +778,10 @@ const (
778778
)
779779

780780
// End of SES
781+
// MARIADB
782+
const (
783+
defaultMariadbSubnetId = "subnet-ob6clqwk"
784+
defaultMariadbVpcId = "vpc-68vi2d3h"
785+
)
786+
787+
// End of MARIADB

tencentcloud/connectivity/client.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ import (
4747
gaap "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/gaap/v20180529"
4848
kms "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/kms/v20190118"
4949
lighthouse "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/lighthouse/v20200324"
50+
mariadb "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/mariadb/v20170312"
5051
mongodb "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/mongodb/v20190725"
5152
monitor "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/monitor/v20180724"
5253
postgre "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/postgres/v20170312"
@@ -133,6 +134,7 @@ type TencentCloudClient struct {
133134
dcdbConn *dcdb.Client
134135
smsConn *sms.Client
135136
catConn *cat.Client
137+
mariadbConn *mariadb.Client
136138
}
137139

138140
// NewClientProfile returns a new ClientProfile
@@ -838,6 +840,19 @@ func (me *TencentCloudClient) UseCatClient() *cat.Client {
838840
return me.catConn
839841
}
840842

843+
// UseMariadbClient returns mariadb client for service
844+
func (me *TencentCloudClient) UseMariadbClient() *mariadb.Client {
845+
if me.mariadbConn != nil {
846+
return me.mariadbConn
847+
}
848+
849+
cpf := me.NewClientProfile(300)
850+
me.mariadbConn, _ = mariadb.NewClient(me.Credential, me.Region, cpf)
851+
me.mariadbConn.WithHttpTransport(&LogRoundTripper{})
852+
853+
return me.mariadbConn
854+
}
855+
841856
func getEnvDefault(key string, defVal int) int {
842857
val, ex := os.LookupEnv(key)
843858
if !ex {
Lines changed: 281 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,281 @@
1+
/*
2+
Use this data source to query detailed information of mariadb dbInstances
3+
4+
Example Usage
5+
6+
```hcl
7+
data "tencentcloud_mariadb_db_instances" "dbInstances" {
8+
instance_ids = ["tdsql-ijxtqk5p"]
9+
project_ids = ["0"]
10+
vpc_id = "5556791"
11+
subnet_id = "3454730"
12+
}
13+
```
14+
*/
15+
package tencentcloud
16+
17+
import (
18+
"context"
19+
"log"
20+
"strconv"
21+
22+
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
23+
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
24+
mariadb "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/mariadb/v20170312"
25+
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
26+
)
27+
28+
func dataSourceTencentCloudMariadbDbInstances() *schema.Resource {
29+
return &schema.Resource{
30+
Read: dataSourceTencentCloudMariadbDbInstancesRead,
31+
Schema: map[string]*schema.Schema{
32+
"instance_ids": {
33+
Type: schema.TypeSet,
34+
Elem: &schema.Schema{
35+
Type: schema.TypeString,
36+
},
37+
Optional: true,
38+
Description: "instance ids.",
39+
},
40+
41+
"project_ids": {
42+
Type: schema.TypeSet,
43+
Elem: &schema.Schema{
44+
Type: schema.TypeInt,
45+
},
46+
Optional: true,
47+
Description: "project ids.",
48+
},
49+
50+
"search_name": {
51+
Type: schema.TypeString,
52+
Optional: true,
53+
Description: "instance name or vip.",
54+
},
55+
56+
"vpc_id": {
57+
Type: schema.TypeString,
58+
Optional: true,
59+
Description: "vpc id.",
60+
},
61+
62+
"subnet_id": {
63+
Type: schema.TypeString,
64+
Optional: true,
65+
Description: "subnet id.",
66+
},
67+
68+
"instances": {
69+
Type: schema.TypeList,
70+
Computed: true,
71+
Description: "instances info.",
72+
Elem: &schema.Resource{
73+
Schema: map[string]*schema.Schema{
74+
"instance_id": {
75+
Type: schema.TypeString,
76+
Computed: true,
77+
Description: "instance id.",
78+
},
79+
"instance_name": {
80+
Type: schema.TypeString,
81+
Computed: true,
82+
Description: "instance name.",
83+
},
84+
"project_id": {
85+
Type: schema.TypeInt,
86+
Computed: true,
87+
Description: "project id.",
88+
},
89+
"region": {
90+
Type: schema.TypeString,
91+
Computed: true,
92+
Description: "region.",
93+
},
94+
"zone": {
95+
Type: schema.TypeString,
96+
Computed: true,
97+
Description: "available zone.",
98+
},
99+
"memory": {
100+
Type: schema.TypeInt,
101+
Computed: true,
102+
Description: "meory of instance.",
103+
},
104+
"storage": {
105+
Type: schema.TypeInt,
106+
Computed: true,
107+
Description: "storage of instance.",
108+
},
109+
"vpc_id": {
110+
Type: schema.TypeString,
111+
Computed: true,
112+
Description: "vpc id.",
113+
},
114+
"subnet_id": {
115+
Type: schema.TypeString,
116+
Computed: true,
117+
Description: "subnet id.",
118+
},
119+
"db_version_id": {
120+
Type: schema.TypeString,
121+
Computed: true,
122+
Description: "db version id.",
123+
},
124+
"resource_tags": {
125+
Type: schema.TypeList,
126+
Computed: true,
127+
Description: "resource tags.",
128+
Elem: &schema.Resource{
129+
Schema: map[string]*schema.Schema{
130+
"tag_key": {
131+
Type: schema.TypeString,
132+
Computed: true,
133+
Description: "tag key.",
134+
},
135+
"tag_value": {
136+
Type: schema.TypeString,
137+
Computed: true,
138+
Description: "tag value.",
139+
},
140+
},
141+
},
142+
},
143+
},
144+
},
145+
},
146+
147+
"result_output_file": {
148+
Type: schema.TypeString,
149+
Optional: true,
150+
Description: "Used to save results.",
151+
},
152+
},
153+
}
154+
}
155+
156+
func dataSourceTencentCloudMariadbDbInstancesRead(d *schema.ResourceData, meta interface{}) error {
157+
defer logElapsed("data_source.tencentcloud_mariadb_db_instances.read")()
158+
defer inconsistentCheck(d, meta)()
159+
160+
logId := getLogId(contextNil)
161+
ctx := context.WithValue(context.TODO(), logIdKey, logId)
162+
ids := make([]string, 0)
163+
164+
paramMap := make(map[string]interface{})
165+
if v, ok := d.GetOk("instance_ids"); ok {
166+
instanceIdsSet := v.(*schema.Set).List()
167+
var instanceIds []*string
168+
for i := range instanceIdsSet {
169+
instanceId := instanceIdsSet[i].(string)
170+
instanceIds = append(instanceIds, &instanceId)
171+
}
172+
paramMap["instance_ids"] = instanceIds
173+
}
174+
175+
if v, ok := d.GetOk("project_ids"); ok {
176+
projectIdsSet := v.(*schema.Set).List()
177+
var projectIds []*int64
178+
for i := range projectIdsSet {
179+
projectIds = append(projectIds, helper.IntInt64(projectIdsSet[i].(int)))
180+
}
181+
paramMap["project_ids"] = projectIds
182+
}
183+
184+
if v, ok := d.GetOk("search_name"); ok {
185+
paramMap["search_name"] = helper.String(v.(string))
186+
}
187+
188+
if v, ok := d.GetOk("vpc_id"); ok {
189+
paramMap["vpc_id"] = helper.String(v.(string))
190+
}
191+
192+
if v, ok := d.GetOk("subnet_id"); ok {
193+
paramMap["subnet_id"] = helper.String(v.(string))
194+
}
195+
196+
var instances []*mariadb.DBInstance
197+
mariadbService := MariadbService{client: meta.(*TencentCloudClient).apiV3Conn}
198+
err := resource.Retry(readRetryTimeout, func() *resource.RetryError {
199+
results, e := mariadbService.DescribeMariadbDbInstancesByFilter(ctx, paramMap)
200+
if e != nil {
201+
return retryError(e)
202+
}
203+
instances = results
204+
return nil
205+
})
206+
if err != nil {
207+
log.Printf("[CRITAL]%s read Mariadb instances failed, reason:%+v", logId, err)
208+
return err
209+
}
210+
211+
instanceList := make([]map[string]interface{}, 0, len(instances))
212+
if instances != nil {
213+
for _, instance := range instances {
214+
instanceMap := map[string]interface{}{}
215+
if instance.InstanceId != nil {
216+
instanceMap["instance_id"] = instance.InstanceId
217+
}
218+
if instance.InstanceName != nil {
219+
instanceMap["instance_name"] = instance.InstanceName
220+
}
221+
if instance.ProjectId != nil {
222+
instanceMap["project_id"] = *instance.ProjectId
223+
}
224+
if instance.Region != nil {
225+
instanceMap["region"] = instance.Region
226+
}
227+
if instance.Zone != nil {
228+
instanceMap["zone"] = instance.Zone
229+
}
230+
if instance.Memory != nil {
231+
instanceMap["memory"] = instance.Memory
232+
}
233+
if instance.Storage != nil {
234+
instanceMap["storage"] = instance.Storage
235+
}
236+
if instance.VpcId != nil {
237+
instanceMap["vpc_id"] = strconv.Itoa(int(*instance.VpcId))
238+
}
239+
if instance.SubnetId != nil {
240+
instanceMap["subnet_id"] = strconv.Itoa(int(*instance.SubnetId))
241+
}
242+
if instance.DbVersionId != nil {
243+
instanceMap["db_version_id"] = instance.DbVersionId
244+
}
245+
if instance.ResourceTags != nil {
246+
resourceTagsList := []interface{}{}
247+
for _, resourceTags := range instance.ResourceTags {
248+
resourceTagsMap := map[string]interface{}{}
249+
if resourceTags.TagKey != nil {
250+
resourceTagsMap["tag_key"] = resourceTags.TagKey
251+
}
252+
if resourceTags.TagValue != nil {
253+
resourceTagsMap["tag_value"] = resourceTags.TagValue
254+
}
255+
256+
resourceTagsList = append(resourceTagsList, resourceTagsMap)
257+
}
258+
instanceMap["resource_tags"] = resourceTagsList
259+
}
260+
261+
instanceList = append(instanceList, instanceMap)
262+
ids = append(ids, *instance.InstanceId)
263+
}
264+
err = d.Set("instances", instanceList)
265+
if err != nil {
266+
log.Printf("[CRITAL]%s provider set instances list fail, reason:%s\n ", logId, err.Error())
267+
return err
268+
}
269+
}
270+
271+
d.SetId(helper.DataResourceIdsHash(ids))
272+
273+
output, ok := d.GetOk("result_output_file")
274+
if ok && output.(string) != "" {
275+
if e := writeToFile(output.(string), instanceList); e != nil {
276+
return e
277+
}
278+
}
279+
280+
return nil
281+
}

0 commit comments

Comments
 (0)