Skip to content

Commit 8bfadfb

Browse files
gitmknanonymous
andauthored
feat: support database (#1871)
* feat: support database * feat: add changelog --------- Co-authored-by: anonymous <anonymous@mail.org>
1 parent 57d2064 commit 8bfadfb

18 files changed

+1306
-2
lines changed

.changelog/1871.txt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
```release-note:new-data-source
2+
tencentcloud_mysql_databases
3+
```
4+
5+
```release-note:new-data-source
6+
tencentcloud_mysql_error_log
7+
```
8+
9+
```release-note:new-data-source
10+
tencentcloud_mysql_project_security_group
11+
```
12+
13+
```release-note:new-data-source
14+
tencentcloud_mysql_ro_min_scale
15+
```
Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
/*
2+
Use this data source to query detailed information of mysql databases
3+
4+
Example Usage
5+
6+
```hcl
7+
data "tencentcloud_mysql_databases" "databases" {
8+
instance_id = "cdb-c1nl9rpv"
9+
database_regexp = ""
10+
}
11+
```
12+
*/
13+
package tencentcloud
14+
15+
import (
16+
"context"
17+
18+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
19+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
20+
cdb "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cdb/v20170320"
21+
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
22+
)
23+
24+
func dataSourceTencentCloudMysqlDatabases() *schema.Resource {
25+
return &schema.Resource{
26+
Read: dataSourceTencentCloudMysqlDatabasesRead,
27+
Schema: map[string]*schema.Schema{
28+
"instance_id": {
29+
Required: true,
30+
Type: schema.TypeString,
31+
Description: "The ID of instance.",
32+
},
33+
34+
"offset": {
35+
Optional: true,
36+
Type: schema.TypeInt,
37+
Description: "Page offset.",
38+
},
39+
40+
"limit": {
41+
Optional: true,
42+
Type: schema.TypeInt,
43+
Description: "The number of single requests, the default value is 20, the minimum value is 1, and the maximum value is 100.",
44+
},
45+
46+
"database_regexp": {
47+
Optional: true,
48+
Type: schema.TypeString,
49+
Description: "Regular expression to match database library names.",
50+
},
51+
52+
"items": {
53+
Computed: true,
54+
Type: schema.TypeSet,
55+
Elem: &schema.Schema{
56+
Type: schema.TypeString,
57+
},
58+
Description: "Returned instance information.",
59+
},
60+
61+
"database_list": {
62+
Computed: true,
63+
Type: schema.TypeList,
64+
Description: "Database name and character set.",
65+
Elem: &schema.Resource{
66+
Schema: map[string]*schema.Schema{
67+
"database_name": {
68+
Computed: true,
69+
Type: schema.TypeString,
70+
Description: "The name of database.",
71+
},
72+
"character_set": {
73+
Computed: true,
74+
Type: schema.TypeString,
75+
Description: "character set type.",
76+
},
77+
},
78+
},
79+
},
80+
81+
"result_output_file": {
82+
Type: schema.TypeString,
83+
Optional: true,
84+
Description: "Used to save results.",
85+
},
86+
},
87+
}
88+
}
89+
90+
func dataSourceTencentCloudMysqlDatabasesRead(d *schema.ResourceData, meta interface{}) error {
91+
defer logElapsed("data_source.tencentcloud_mysql_databases.read")()
92+
defer inconsistentCheck(d, meta)()
93+
94+
logId := getLogId(contextNil)
95+
96+
ctx := context.WithValue(context.TODO(), logIdKey, logId)
97+
98+
paramMap := make(map[string]interface{})
99+
if v, ok := d.GetOk("instance_id"); ok {
100+
paramMap["InstanceId"] = helper.String(v.(string))
101+
}
102+
103+
if v, ok := d.GetOk("database_regexp"); ok {
104+
paramMap["DatabaseRegexp"] = helper.String(v.(string))
105+
}
106+
107+
service := MysqlService{client: meta.(*TencentCloudClient).apiV3Conn}
108+
109+
var databases *cdb.DescribeDatabasesResponseParams
110+
err := resource.Retry(readRetryTimeout, func() *resource.RetryError {
111+
result, e := service.DescribeMysqlDatabasesByFilter(ctx, paramMap)
112+
if e != nil {
113+
return retryError(e)
114+
}
115+
databases = result
116+
return nil
117+
})
118+
if err != nil {
119+
return err
120+
}
121+
122+
if databases.Items != nil {
123+
_ = d.Set("items", databases.Items)
124+
}
125+
126+
ids := make([]string, 0, len(databases.DatabaseList))
127+
tmpList := make([]map[string]interface{}, 0, len(databases.DatabaseList))
128+
if databases.DatabaseList != nil {
129+
for _, databasesWithCharacterLists := range databases.DatabaseList {
130+
databasesWithCharacterListsMap := map[string]interface{}{}
131+
132+
if databasesWithCharacterLists.DatabaseName != nil {
133+
databasesWithCharacterListsMap["database_name"] = databasesWithCharacterLists.DatabaseName
134+
}
135+
136+
if databasesWithCharacterLists.CharacterSet != nil {
137+
databasesWithCharacterListsMap["character_set"] = databasesWithCharacterLists.CharacterSet
138+
}
139+
140+
ids = append(ids, *databasesWithCharacterLists.DatabaseName)
141+
tmpList = append(tmpList, databasesWithCharacterListsMap)
142+
}
143+
144+
_ = d.Set("database_list", tmpList)
145+
}
146+
147+
d.SetId(helper.DataResourceIdsHash(ids))
148+
output, ok := d.GetOk("result_output_file")
149+
if ok && output.(string) != "" {
150+
if e := writeToFile(output.(string), map[string]interface{}{
151+
"items": databases.Items,
152+
"database_list": tmpList,
153+
}); e != nil {
154+
return e
155+
}
156+
}
157+
return nil
158+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package tencentcloud
2+
3+
import (
4+
"testing"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
7+
)
8+
9+
// go test -i; go test -test.run TestAccTencentCloudMysqlDatabasesDataSource_basic -v
10+
func TestAccTencentCloudMysqlDatabasesDataSource_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: testAccMysqlDatabasesDataSource,
20+
Check: resource.ComposeTestCheckFunc(
21+
testAccCheckTencentCloudDataSourceID("data.tencentcloud_mysql_databases.databases"),
22+
resource.TestCheckResourceAttrSet("data.tencentcloud_mysql_databases.databases", "database_list.#"),
23+
resource.TestCheckResourceAttrSet("data.tencentcloud_mysql_databases.databases", "database_list.0.character_set"),
24+
resource.TestCheckResourceAttrSet("data.tencentcloud_mysql_databases.databases", "database_list.0.database_name"),
25+
resource.TestCheckResourceAttrSet("data.tencentcloud_mysql_databases.databases", "items.#"),
26+
),
27+
},
28+
},
29+
})
30+
}
31+
32+
const testAccMysqlDatabasesDataSourceVar = `
33+
variable "instance_id" {
34+
default = "` + defaultDbBrainInstanceId + `"
35+
}
36+
`
37+
38+
const testAccMysqlDatabasesDataSource = testAccMysqlDatabasesDataSourceVar + `
39+
40+
data "tencentcloud_mysql_databases" "databases" {
41+
instance_id = var.instance_id
42+
database_regexp = "^tf_ci_test"
43+
}
44+
45+
`
Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
/*
2+
Use this data source to query detailed information of mysql error_log
3+
4+
Example Usage
5+
6+
```hcl
7+
data "tencentcloud_mysql_error_log" "error_log" {
8+
instance_id = "cdb-fitq5t9h"
9+
start_time = 1683538307
10+
end_time = 1686043908
11+
key_words = ["Shutting"]
12+
inst_type = "slave"
13+
}
14+
```
15+
*/
16+
package tencentcloud
17+
18+
import (
19+
"context"
20+
"strconv"
21+
22+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
23+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
24+
cdb "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cdb/v20170320"
25+
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
26+
)
27+
28+
func dataSourceTencentCloudMysqlErrorLog() *schema.Resource {
29+
return &schema.Resource{
30+
Read: dataSourceTencentCloudMysqlErrorLogRead,
31+
Schema: map[string]*schema.Schema{
32+
"instance_id": {
33+
Required: true,
34+
Type: schema.TypeString,
35+
Description: "instance id.",
36+
},
37+
38+
"start_time": {
39+
Required: true,
40+
Type: schema.TypeInt,
41+
Description: "Start timestamp. For example 1585142640.",
42+
},
43+
44+
"end_time": {
45+
Required: true,
46+
Type: schema.TypeInt,
47+
Description: "End timestamp. For example 1585142640.",
48+
},
49+
50+
"key_words": {
51+
Optional: true,
52+
Type: schema.TypeSet,
53+
Elem: &schema.Schema{
54+
Type: schema.TypeString,
55+
},
56+
Description: "A list of keywords to match, up to 15 keywords are supported.",
57+
},
58+
59+
"inst_type": {
60+
Optional: true,
61+
Type: schema.TypeString,
62+
Description: "Only valid when the instance is the master instance or disaster recovery instance, the optional value: slave, which means to pull the log of the slave machine.",
63+
},
64+
65+
"items": {
66+
Computed: true,
67+
Type: schema.TypeList,
68+
Description: "The records returned.",
69+
Elem: &schema.Resource{
70+
Schema: map[string]*schema.Schema{
71+
"timestamp": {
72+
Type: schema.TypeInt,
73+
Computed: true,
74+
Description: "The time the error occurred.",
75+
},
76+
"content": {
77+
Type: schema.TypeString,
78+
Computed: true,
79+
Description: "error details.",
80+
},
81+
},
82+
},
83+
},
84+
85+
"result_output_file": {
86+
Type: schema.TypeString,
87+
Optional: true,
88+
Description: "Used to save results.",
89+
},
90+
},
91+
}
92+
}
93+
94+
func dataSourceTencentCloudMysqlErrorLogRead(d *schema.ResourceData, meta interface{}) error {
95+
defer logElapsed("data_source.tencentcloud_mysql_error_log.read")()
96+
defer inconsistentCheck(d, meta)()
97+
98+
logId := getLogId(contextNil)
99+
100+
ctx := context.WithValue(context.TODO(), logIdKey, logId)
101+
102+
paramMap := make(map[string]interface{})
103+
if v, ok := d.GetOk("instance_id"); ok {
104+
paramMap["InstanceId"] = helper.String(v.(string))
105+
}
106+
107+
if v, _ := d.GetOk("start_time"); v != nil {
108+
paramMap["StartTime"] = helper.IntUint64(v.(int))
109+
}
110+
111+
if v, _ := d.GetOk("end_time"); v != nil {
112+
paramMap["EndTime"] = helper.IntUint64(v.(int))
113+
}
114+
115+
if v, ok := d.GetOk("key_words"); ok {
116+
keyWordsSet := v.(*schema.Set).List()
117+
paramMap["KeyWords"] = helper.InterfacesStringsPoint(keyWordsSet)
118+
}
119+
120+
if v, ok := d.GetOk("inst_type"); ok {
121+
paramMap["InstType"] = helper.String(v.(string))
122+
}
123+
124+
service := MysqlService{client: meta.(*TencentCloudClient).apiV3Conn}
125+
var result []*cdb.ErrlogItem
126+
err := resource.Retry(readRetryTimeout, func() *resource.RetryError {
127+
response, e := service.DescribeMysqlErrorLogByFilter(ctx, paramMap)
128+
if e != nil {
129+
return retryError(e)
130+
}
131+
result = response
132+
return nil
133+
})
134+
if err != nil {
135+
return err
136+
}
137+
138+
ids := make([]string, 0, len(result))
139+
tmpList := make([]map[string]interface{}, 0, len(result))
140+
if result != nil {
141+
for _, errlogItem := range result {
142+
errlogItemMap := map[string]interface{}{}
143+
144+
if errlogItem.Timestamp != nil {
145+
errlogItemMap["timestamp"] = errlogItem.Timestamp
146+
}
147+
148+
if errlogItem.Content != nil {
149+
errlogItemMap["content"] = errlogItem.Content
150+
}
151+
152+
ids = append(ids, strconv.FormatUint(*errlogItem.Timestamp, 10))
153+
tmpList = append(tmpList, errlogItemMap)
154+
}
155+
156+
_ = d.Set("items", tmpList)
157+
}
158+
159+
d.SetId(helper.DataResourceIdsHash(ids))
160+
output, ok := d.GetOk("result_output_file")
161+
if ok && output.(string) != "" {
162+
if e := writeToFile(output.(string), tmpList); e != nil {
163+
return e
164+
}
165+
}
166+
return nil
167+
}

0 commit comments

Comments
 (0)