Skip to content

Commit 8357142

Browse files
authored
Feat/support dcdb resource (#1500)
* update dts mode * support tencentcloud_dcdb_db_instance * update doc * update e2e case * update misspell * support tencentcloud_dcdb_account_privileges * update tencentcloud_dcdb_account_privileges e2e * update e2e case * update provider * feat/support tencentcloud_dcdb_db_parameters * add changelog * add data_source
1 parent 7317811 commit 8357142

17 files changed

+1061
-19
lines changed

.changelog/1500.txt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
2+
```release-note:new-resource
3+
tencentcloud_dcdb_db_parameters
4+
```
5+
6+
```release-note:new-data-source
7+
tencentcloud_dcdb_database_objects
8+
```
9+
10+
```release-note:new-data-source
11+
tencentcloud_dcdb_database_tables
12+
```
13+
14+
```release-note:enhancement
15+
resource/tencentcloud_dcdb_account_privileges: optimize the logic after modifing the AccountPrivileges
16+
```
Lines changed: 217 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,217 @@
1+
/*
2+
Use this data source to query detailed information of dcdb database_objects
3+
4+
Example Usage
5+
6+
```hcl
7+
data "tencentcloud_dcdb_database_objects" "database_objects" {
8+
instance_id = "dcdbt-ow7t8lmc"
9+
db_name = <nil>
10+
}
11+
```
12+
*/
13+
package tencentcloud
14+
15+
import (
16+
"context"
17+
"strings"
18+
19+
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
20+
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
21+
dcdb "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/dcdb/v20180411"
22+
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
23+
)
24+
25+
func dataSourceTencentCloudDcdbDatabaseObjects() *schema.Resource {
26+
return &schema.Resource{
27+
Read: dataSourceTencentCloudDcdbDatabaseObjectsRead,
28+
Schema: map[string]*schema.Schema{
29+
"instance_id": {
30+
Required: true,
31+
Type: schema.TypeString,
32+
Description: "The ID of instance.",
33+
},
34+
35+
"db_name": {
36+
Required: true,
37+
Type: schema.TypeString,
38+
Description: "Database name, obtained through the DescribeDatabases api.",
39+
},
40+
41+
"tables": {
42+
Computed: true,
43+
Type: schema.TypeList,
44+
Description: "Table list.",
45+
Elem: &schema.Resource{
46+
Schema: map[string]*schema.Schema{
47+
"table": {
48+
Type: schema.TypeString,
49+
Computed: true,
50+
Description: "The name of table.",
51+
},
52+
},
53+
},
54+
},
55+
56+
"views": {
57+
Computed: true,
58+
Type: schema.TypeList,
59+
Description: "View list.",
60+
Elem: &schema.Resource{
61+
Schema: map[string]*schema.Schema{
62+
"view": {
63+
Type: schema.TypeString,
64+
Computed: true,
65+
Description: "The name of view.",
66+
},
67+
},
68+
},
69+
},
70+
71+
"procs": {
72+
Computed: true,
73+
Type: schema.TypeList,
74+
Description: "Procedure list.",
75+
Elem: &schema.Resource{
76+
Schema: map[string]*schema.Schema{
77+
"proc": {
78+
Type: schema.TypeString,
79+
Computed: true,
80+
Description: "The name of procedure.",
81+
},
82+
},
83+
},
84+
},
85+
86+
"funcs": {
87+
Computed: true,
88+
Type: schema.TypeList,
89+
Description: "Function list.",
90+
Elem: &schema.Resource{
91+
Schema: map[string]*schema.Schema{
92+
"func": {
93+
Type: schema.TypeString,
94+
Computed: true,
95+
Description: "The name of function.",
96+
},
97+
},
98+
},
99+
},
100+
101+
"result_output_file": {
102+
Type: schema.TypeString,
103+
Optional: true,
104+
Description: "Used to save results.",
105+
},
106+
},
107+
}
108+
}
109+
110+
func dataSourceTencentCloudDcdbDatabaseObjectsRead(d *schema.ResourceData, meta interface{}) error {
111+
defer logElapsed("data_source.tencentcloud_dcdb_database_objects.read")()
112+
defer inconsistentCheck(d, meta)()
113+
114+
logId := getLogId(contextNil)
115+
116+
ctx := context.WithValue(context.TODO(), logIdKey, logId)
117+
118+
paramMap := make(map[string]interface{})
119+
if v, ok := d.GetOk("instance_id"); ok {
120+
paramMap["instance_id"] = helper.String(v.(string))
121+
}
122+
123+
if v, ok := d.GetOk("db_name"); ok {
124+
paramMap["db_name"] = helper.String(v.(string))
125+
}
126+
127+
service := DcdbService{client: meta.(*TencentCloudClient).apiV3Conn}
128+
var result *dcdb.DescribeDatabaseObjectsResponseParams
129+
130+
err := resource.Retry(readRetryTimeout, func() *resource.RetryError {
131+
var e error
132+
result, e = service.DescribeDcdbDBObjectsByFilter(ctx, paramMap)
133+
if e != nil {
134+
return retryError(e)
135+
}
136+
return nil
137+
})
138+
if err != nil {
139+
return err
140+
}
141+
142+
ids := make([]string, 0)
143+
data := make(map[string]interface{})
144+
145+
if result != nil {
146+
tables := result.Tables
147+
tabList := make([]map[string]interface{}, 0, len(tables))
148+
if tables != nil {
149+
for _, databaseTable := range tables {
150+
databaseTableMap := map[string]interface{}{}
151+
152+
if databaseTable.Table != nil {
153+
databaseTableMap["table"] = databaseTable.Table
154+
}
155+
tabList = append(tabList, databaseTableMap)
156+
}
157+
_ = d.Set("tables", tabList)
158+
data["tables"] = tabList
159+
}
160+
161+
views := result.Views
162+
viewList := make([]map[string]interface{}, 0, len(views))
163+
if views != nil {
164+
for _, databaseView := range views {
165+
databaseViewMap := map[string]interface{}{}
166+
167+
if databaseView.View != nil {
168+
databaseViewMap["view"] = databaseView.View
169+
}
170+
viewList = append(viewList, databaseViewMap)
171+
}
172+
_ = d.Set("views", viewList)
173+
data["views"] = viewList
174+
}
175+
176+
procs := result.Procs
177+
procList := make([]map[string]interface{}, 0, len(procs))
178+
if procs != nil {
179+
for _, databaseProcedure := range procs {
180+
databaseProcedureMap := map[string]interface{}{}
181+
182+
if databaseProcedure.Proc != nil {
183+
databaseProcedureMap["proc"] = databaseProcedure.Proc
184+
}
185+
procList = append(procList, databaseProcedureMap)
186+
}
187+
_ = d.Set("procs", procList)
188+
data["procs"] = procList
189+
}
190+
191+
funcs := result.Funcs
192+
funcList := make([]map[string]interface{}, 0, len(funcs))
193+
if funcs != nil {
194+
for _, databaseFunction := range funcs {
195+
databaseFunctionMap := map[string]interface{}{}
196+
197+
if databaseFunction.Func != nil {
198+
databaseFunctionMap["func"] = databaseFunction.Func
199+
}
200+
funcList = append(funcList, databaseFunctionMap)
201+
}
202+
_ = d.Set("funcs", funcList)
203+
data["funcs"] = funcList
204+
}
205+
}
206+
207+
ids = append(ids, strings.Join([]string{*result.InstanceId, *result.DbName}, FILED_SP))
208+
209+
d.SetId(helper.DataResourceIdsHash(ids))
210+
output, ok := d.GetOk("result_output_file")
211+
if ok && output.(string) != "" {
212+
if e := writeToFile(output.(string), data); e != nil {
213+
return e
214+
}
215+
}
216+
return nil
217+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package tencentcloud
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
8+
)
9+
10+
func TestAccTencentCloudDCDBDatabaseObjectsDataSource_basic(t *testing.T) {
11+
t.Parallel()
12+
resource.Test(t, resource.TestCase{
13+
PreCheck: func() {
14+
testAccPreCheck(t)
15+
},
16+
Providers: testAccProviders,
17+
Steps: []resource.TestStep{
18+
{
19+
Config: fmt.Sprintf(testAccDCDBDatabaseObjectsDataSource, defaultDcdbInstanceId),
20+
Check: resource.ComposeTestCheckFunc(
21+
testAccCheckTencentCloudDataSourceID("data.tencentcloud_dcdb_database_objects.database_objects"),
22+
resource.TestCheckResourceAttrSet("data.tencentcloud_dcdb_database_objects.database_objects", "tables.#"),
23+
resource.TestCheckResourceAttrSet("data.tencentcloud_dcdb_database_objects.database_objects", "views.#"),
24+
resource.TestCheckResourceAttrSet("data.tencentcloud_dcdb_database_objects.database_objects", "procs.#"),
25+
resource.TestCheckResourceAttrSet("data.tencentcloud_dcdb_database_objects.database_objects", "funcs.#"),
26+
resource.TestCheckResourceAttr("data.tencentcloud_dcdb_database_objects.database_objects", "db_name", "tf_test_db"),
27+
),
28+
},
29+
},
30+
})
31+
}
32+
33+
const testAccDCDBDatabaseObjectsDataSource = `
34+
35+
data "tencentcloud_dcdb_database_objects" "database_objects" {
36+
instance_id = "%s"
37+
db_name = "tf_test_db"
38+
}
39+
40+
`

0 commit comments

Comments
 (0)