Skip to content

Commit 402c2e2

Browse files
committed
feat: supprot datasource cloud
1 parent c45d0fe commit 402c2e2

9 files changed

+246
-153
lines changed

.changelog/2239.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ tencentcloud_bi_project_user_role
2222
tencentcloud_bi_datasource
2323
```
2424

25+
```release-note:new-resource
26+
tencentcloud_bi_datasource_cloud
27+
```
28+
2529
```release-note:new-resource
2630
tencentcloud_bi_embed_token
2731
```

tencentcloud/provider.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1905,6 +1905,7 @@ Business Intelligence(BI)
19051905
tencentcloud_bi_user_role
19061906
tencentcloud_bi_project_user_role
19071907
tencentcloud_bi_datasource
1908+
tencentcloud_bi_datasource_cloud
19081909
tencentcloud_bi_embed_token
19091910
tencentcloud_bi_embed_interval
19101911
*/

tencentcloud/resource_tc_bi_datasource.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ func resourceTencentCloudBiDatasource() *schema.Resource {
6565
"db_type": {
6666
Required: true,
6767
Type: schema.TypeString,
68-
Description: "MYSQL.",
68+
Description: "`MYSQL`, `MSSQL`, `POSTGRE`, `ORACLE`, `CLICKHOUSE`, `TIDB`, `HIVE`, `PRESTO`.",
6969
},
7070

7171
"charset": {

tencentcloud/resource_tc_bi_datasource_cloud.go

Lines changed: 115 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -5,40 +5,33 @@ Example Usage
55
66
```hcl
77
resource "tencentcloud_bi_datasource_cloud" "datasource_cloud" {
8-
service_type = "Cloud"
9-
db_type = "Database type."
10-
charset = "utf8"
11-
db_user = "root"
12-
db_pwd = "abc"
13-
db_name = "abc"
14-
source_name = "abc"
15-
project_id = "123"
16-
vip = "1.2.3.4"
17-
vport = "3306"
18-
vpc_id = ""
19-
uniq_vpc_id = ""
20-
region_id = ""
21-
extra_param = ""
22-
data_origin = "abc"
23-
data_origin_project_id = "abc"
24-
data_origin_datasource_id = "abc"
8+
charset = "utf8"
9+
db_name = "bi_dev"
10+
db_type = "MYSQL"
11+
db_user = "root"
12+
project_id = "11015056"
13+
db_pwd = "xxxxxx"
14+
service_type {
15+
instance_id = "cdb-12viotu5"
16+
region = "ap-guangzhou"
17+
type = "Cloud"
18+
}
19+
source_name = "tf-test1"
20+
vip = "10.0.0.4"
21+
vport = "3306"
22+
region_id = "gz"
23+
vpc_id = 5292713
2524
}
2625
```
27-
28-
Import
29-
30-
bi datasource_cloud can be imported using the id, e.g.
31-
32-
```
33-
terraform import tencentcloud_bi_datasource_cloud.datasource_cloud datasource_cloud_id
34-
```
3526
*/
3627
package tencentcloud
3728

3829
import (
3930
"context"
31+
"fmt"
4032
"log"
4133
"strconv"
34+
"strings"
4235

4336
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
4437
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
@@ -52,20 +45,38 @@ func resourceTencentCloudBiDatasourceCloud() *schema.Resource {
5245
Read: resourceTencentCloudBiDatasourceCloudRead,
5346
Update: resourceTencentCloudBiDatasourceCloudUpdate,
5447
Delete: resourceTencentCloudBiDatasourceCloudDelete,
55-
Importer: &schema.ResourceImporter{
56-
State: schema.ImportStatePassthrough,
57-
},
48+
5849
Schema: map[string]*schema.Schema{
5950
"service_type": {
6051
Required: true,
61-
Type: schema.TypeString,
62-
Description: "Own or Cloud.",
52+
Type: schema.TypeList,
53+
MaxItems: 1,
54+
Description: "Service type, Own or Cloud.",
55+
Elem: &schema.Resource{
56+
Schema: map[string]*schema.Schema{
57+
"type": {
58+
Type: schema.TypeString,
59+
Required: true,
60+
Description: "Service type, Cloud.",
61+
},
62+
"instance_id": {
63+
Type: schema.TypeString,
64+
Required: true,
65+
Description: "Instance Id.",
66+
},
67+
"region": {
68+
Type: schema.TypeString,
69+
Required: true,
70+
Description: "Region.",
71+
},
72+
},
73+
},
6374
},
6475

6576
"db_type": {
6677
Required: true,
6778
Type: schema.TypeString,
68-
Description: "MYSQL.",
79+
Description: "`MYSQL`, `TDSQL-C_MYSQL`, `TDSQL_MYSQL`, `MSSQL`, `POSTGRESQL`, `MARIADB`.",
6980
},
7081

7182
"charset": {
@@ -118,7 +129,7 @@ func resourceTencentCloudBiDatasourceCloud() *schema.Resource {
118129
},
119130

120131
"vpc_id": {
121-
Optional: true,
132+
Required: true,
122133
Type: schema.TypeString,
123134
Description: "Vpc identification.",
124135
},
@@ -137,6 +148,7 @@ func resourceTencentCloudBiDatasourceCloud() *schema.Resource {
137148

138149
"extra_param": {
139150
Optional: true,
151+
Computed: true,
140152
Type: schema.TypeString,
141153
Description: "Extended parameters.",
142154
},
@@ -169,12 +181,18 @@ func resourceTencentCloudBiDatasourceCloudCreate(d *schema.ResourceData, meta in
169181
logId := getLogId(contextNil)
170182

171183
var (
172-
request = bi.NewCreateDatasourceCloudRequest()
173-
response = bi.NewCreateDatasourceCloudResponse()
174-
id int64
184+
request = bi.NewCreateDatasourceCloudRequest()
185+
response = bi.NewCreateDatasourceCloudResponse()
186+
projectId string
187+
id int64
175188
)
176-
if v, ok := d.GetOk("service_type"); ok {
177-
request.ServiceType = helper.String(v.(string))
189+
190+
if dMap, ok := helper.InterfacesHeadMap(d, "service_type"); ok {
191+
v, o := helper.MapToString(dMap)
192+
if !o {
193+
return fmt.Errorf("ServiceType `%s` format error", dMap)
194+
}
195+
request.ServiceType = &v
178196
}
179197

180198
if v, ok := d.GetOk("db_type"); ok {
@@ -202,6 +220,7 @@ func resourceTencentCloudBiDatasourceCloudCreate(d *schema.ResourceData, meta in
202220
}
203221

204222
if v, ok := d.GetOk("project_id"); ok {
223+
projectId = v.(string)
205224
request.ProjectId = helper.String(v.(string))
206225
}
207226

@@ -257,7 +276,7 @@ func resourceTencentCloudBiDatasourceCloudCreate(d *schema.ResourceData, meta in
257276
}
258277

259278
id = *response.Response.Data.Id
260-
d.SetId(strconv.FormatInt(id, 10))
279+
d.SetId(strings.Join([]string{projectId, strconv.FormatInt(id, 10)}, FILED_SP))
261280

262281
return resourceTencentCloudBiDatasourceCloudRead(d, meta)
263282
}
@@ -272,10 +291,16 @@ func resourceTencentCloudBiDatasourceCloudRead(d *schema.ResourceData, meta inte
272291

273292
service := BiService{client: meta.(*TencentCloudClient).apiV3Conn}
274293

275-
id := d.Id()
276-
idint, _ := strconv.Atoi(id)
294+
idSplit := strings.Split(d.Id(), FILED_SP)
295+
if len(idSplit) != 2 {
296+
return fmt.Errorf("id is broken,%s", d.Id())
297+
}
298+
projectId := idSplit[0]
299+
projectIdInt, _ := strconv.ParseInt(projectId, 10, 64)
300+
id := idSplit[1]
301+
idInt, _ := strconv.ParseInt(id, 10, 64)
277302

278-
datasourceCloud, err := service.DescribeBiDatasourceCloudById(ctx, uint64(idint))
303+
datasourceCloud, err := service.DescribeBiDatasourceCloudById(ctx, uint64(projectIdInt), uint64(idInt))
279304
if err != nil {
280305
return err
281306
}
@@ -287,7 +312,12 @@ func resourceTencentCloudBiDatasourceCloudRead(d *schema.ResourceData, meta inte
287312
}
288313

289314
if datasourceCloud.ServiceType != nil {
290-
_ = d.Set("service_type", datasourceCloud.ServiceType)
315+
v, err := helper.JsonToMap(*datasourceCloud.ServiceType)
316+
if err != nil {
317+
return fmt.Errorf("ServiceType `%v` format error", *datasourceCloud.ServiceType)
318+
}
319+
320+
_ = d.Set("service_type", []interface{}{v})
291321
}
292322

293323
if datasourceCloud.DbType != nil {
@@ -361,76 +391,64 @@ func resourceTencentCloudBiDatasourceCloudUpdate(d *schema.ResourceData, meta in
361391

362392
request := bi.NewModifyDatasourceCloudRequest()
363393

364-
id := d.Id()
365-
idint, _ := strconv.Atoi(id)
366-
idUint64 := uint64(idint)
394+
idSplit := strings.Split(d.Id(), FILED_SP)
395+
if len(idSplit) != 2 {
396+
return fmt.Errorf("id is broken,%s", d.Id())
397+
}
398+
projectId := idSplit[0]
399+
id := idSplit[1]
400+
idInt, _ := strconv.ParseInt(id, 10, 64)
401+
idUint64 := uint64(idInt)
367402

368403
request.Id = &idUint64
404+
request.ProjectId = &projectId
369405

370-
if d.HasChange("service_type") {
371-
if v, ok := d.GetOk("service_type"); ok {
372-
request.ServiceType = helper.String(v.(string))
406+
if dMap, ok := helper.InterfacesHeadMap(d, "service_type"); ok {
407+
v, o := helper.MapToString(dMap)
408+
if !o {
409+
return fmt.Errorf("ServiceType `%s` format error", dMap)
373410
}
411+
request.ServiceType = &v
374412
}
375413

376-
if d.HasChange("db_type") {
377-
if v, ok := d.GetOk("db_type"); ok {
378-
request.DbType = helper.String(v.(string))
379-
}
414+
if v, ok := d.GetOk("db_type"); ok {
415+
request.DbType = helper.String(v.(string))
380416
}
381417

382-
if d.HasChange("charset") {
383-
if v, ok := d.GetOk("charset"); ok {
384-
request.Charset = helper.String(v.(string))
385-
}
418+
if v, ok := d.GetOk("charset"); ok {
419+
request.Charset = helper.String(v.(string))
386420
}
387421

388-
if d.HasChange("db_user") {
389-
if v, ok := d.GetOk("db_user"); ok {
390-
request.DbUser = helper.String(v.(string))
391-
}
422+
if v, ok := d.GetOk("db_user"); ok {
423+
request.DbUser = helper.String(v.(string))
392424
}
393425

394-
if d.HasChange("db_pwd") {
395-
if v, ok := d.GetOk("db_pwd"); ok {
396-
request.DbPwd = helper.String(v.(string))
397-
}
426+
if v, ok := d.GetOk("db_pwd"); ok {
427+
request.DbPwd = helper.String(v.(string))
398428
}
399429

400-
if d.HasChange("db_name") {
401-
if v, ok := d.GetOk("db_name"); ok {
402-
request.DbName = helper.String(v.(string))
403-
}
430+
if v, ok := d.GetOk("db_name"); ok {
431+
request.DbName = helper.String(v.(string))
404432
}
405433

406-
if d.HasChange("source_name") {
407-
if v, ok := d.GetOk("source_name"); ok {
408-
request.SourceName = helper.String(v.(string))
409-
}
434+
if v, ok := d.GetOk("source_name"); ok {
435+
request.SourceName = helper.String(v.(string))
410436
}
411437

412-
if d.HasChange("project_id") {
413-
if v, ok := d.GetOk("project_id"); ok {
414-
request.ProjectId = helper.String(v.(string))
415-
}
438+
if v, ok := d.GetOk("vip"); ok {
439+
request.Vip = helper.String(v.(string))
416440
}
417441

418-
if d.HasChange("vip") {
419-
if v, ok := d.GetOk("vip"); ok {
420-
request.Vip = helper.String(v.(string))
421-
}
442+
if v, ok := d.GetOk("vport"); ok {
443+
request.Vport = helper.String(v.(string))
422444
}
423445

424-
if d.HasChange("vport") {
425-
if v, ok := d.GetOk("vport"); ok {
426-
request.Vport = helper.String(v.(string))
427-
}
446+
if v, ok := d.GetOk("region_id"); ok {
447+
request.RegionId = helper.String(v.(string))
428448
}
429449

430-
if d.HasChange("vpc_id") {
431-
if v, ok := d.GetOk("vpc_id"); ok {
432-
request.VpcId = helper.String(v.(string))
433-
}
450+
if v, ok := d.GetOk("vpc_id"); ok {
451+
request.VpcId = helper.String(v.(string))
434452
}
435453

436454
if d.HasChange("uniq_vpc_id") {
@@ -439,12 +457,6 @@ func resourceTencentCloudBiDatasourceCloudUpdate(d *schema.ResourceData, meta in
439457
}
440458
}
441459

442-
if d.HasChange("region_id") {
443-
if v, ok := d.GetOk("region_id"); ok {
444-
request.RegionId = helper.String(v.(string))
445-
}
446-
}
447-
448460
if d.HasChange("extra_param") {
449461
if v, ok := d.GetOk("extra_param"); ok {
450462
request.ExtraParam = helper.String(v.(string))
@@ -494,10 +506,16 @@ func resourceTencentCloudBiDatasourceCloudDelete(d *schema.ResourceData, meta in
494506
ctx := context.WithValue(context.TODO(), logIdKey, logId)
495507

496508
service := BiService{client: meta.(*TencentCloudClient).apiV3Conn}
497-
id := d.Id()
498-
idint, _ := strconv.Atoi(id)
509+
idSplit := strings.Split(d.Id(), FILED_SP)
510+
if len(idSplit) != 2 {
511+
return fmt.Errorf("id is broken,%s", d.Id())
512+
}
513+
projectId := idSplit[0]
514+
projectIdInt, _ := strconv.ParseInt(projectId, 10, 64)
515+
id := idSplit[1]
516+
idInt, _ := strconv.ParseInt(id, 10, 64)
499517

500-
if err := service.DeleteBiDatasourceCloudById(ctx, uint64(idint)); err != nil {
518+
if err := service.DeleteBiDatasourceCloudById(ctx, uint64(projectIdInt), uint64(idInt)); err != nil {
501519
return err
502520
}
503521

0 commit comments

Comments
 (0)