Skip to content

Commit fa2365b

Browse files
gitmknanonymous
andauthored
Feat/mysql resource (#1812)
* feat: support cdb resource * feat: support mysql resource * feat: support mysql resource * fix:go fmt * fix: modify doc --------- Co-authored-by: anonymous <anonymous@mail.org>
1 parent 2798b27 commit fa2365b

10 files changed

+624
-0
lines changed

.changelog/1812.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
```release-note:new-resource
2+
tencentcloud_mysql_backup_download_restriction
3+
```
4+
5+
```release-note:new-resource
6+
tencentcloud_mysql_renew_db_instance_operation
7+
```

tencentcloud/provider.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,8 @@ TencentDB for MySQL(cdb)
510510
tencentcloud_mysql_security_groups_attachment
511511
tencentcloud_mysql_local_binlog_config
512512
tencentcloud_mysql_audit_log_file
513+
tencentcloud_mysql_backup_download_restriction
514+
tencentcloud_mysql_renew_db_instance_operation
513515
514516
Cloud Monitor(Monitor)
515517
Data Source
@@ -1834,6 +1836,8 @@ func Provider() *schema.Provider {
18341836
"tencentcloud_mysql_deploy_group": resourceTencentCloudMysqlDeployGroup(),
18351837
"tencentcloud_mysql_local_binlog_config": resourceTencentCloudMysqlLocalBinlogConfig(),
18361838
"tencentcloud_mysql_audit_log_file": resourceTencentCloudMysqlAuditLogFile(),
1839+
"tencentcloud_mysql_backup_download_restriction": resourceTencentCloudMysqlBackupDownloadRestriction(),
1840+
"tencentcloud_mysql_renew_db_instance_operation": resourceTencentCloudMysqlRenewDbInstanceOperation(),
18371841
"tencentcloud_cos_bucket": resourceTencentCloudCosBucket(),
18381842
"tencentcloud_cos_bucket_object": resourceTencentCloudCosBucketObject(),
18391843
"tencentcloud_cfs_file_system": resourceTencentCloudCfsFileSystem(),
Lines changed: 248 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,248 @@
1+
/*
2+
Provides a resource to create a mysql backup_download_restriction
3+
4+
Example Usage
5+
6+
```hcl
7+
resource "tencentcloud_mysql_backup_download_restriction" "backup_download_restriction" {
8+
limit_type = "Customize"
9+
vpc_comparison_symbol = "In"
10+
ip_comparison_symbol = "In"
11+
limit_vpc {
12+
region = "ap-guangzhou"
13+
vpc_list = ["vpc-4owdpnwr"]
14+
}
15+
limit_ip = ["127.0.0.1"]
16+
}
17+
```
18+
19+
Import
20+
21+
mysql backup_download_restriction can be imported using the "BackupDownloadRestriction", as follows.
22+
23+
```
24+
terraform import tencentcloud_mysql_backup_download_restriction.backup_download_restriction BackupDownloadRestriction
25+
```
26+
*/
27+
package tencentcloud
28+
29+
import (
30+
"context"
31+
"log"
32+
"strings"
33+
34+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
35+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
36+
mysql "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cdb/v20170320"
37+
sdkErrors "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/errors"
38+
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
39+
)
40+
41+
func resourceTencentCloudMysqlBackupDownloadRestriction() *schema.Resource {
42+
return &schema.Resource{
43+
Create: resourceTencentCloudMysqlBackupDownloadRestrictionCreate,
44+
Read: resourceTencentCloudMysqlBackupDownloadRestrictionRead,
45+
Update: resourceTencentCloudMysqlBackupDownloadRestrictionUpdate,
46+
Delete: resourceTencentCloudMysqlBackupDownloadRestrictionDelete,
47+
Importer: &schema.ResourceImporter{
48+
State: schema.ImportStatePassthrough,
49+
},
50+
Schema: map[string]*schema.Schema{
51+
"limit_type": {
52+
Required: true,
53+
Type: schema.TypeString,
54+
Description: "NoLimit No limit, both internal and external networks can be downloaded; LimitOnlyIntranet Only intranet can be downloaded; Customize user-defined vpc:ip can be downloaded. LimitVpc and LimitIp can be set only when the value is Customize.",
55+
},
56+
57+
"vpc_comparison_symbol": {
58+
Optional: true,
59+
Type: schema.TypeString,
60+
Description: "This parameter only supports In, which means that the vpc specified by LimitVpc can be downloaded. The default is In.",
61+
},
62+
63+
"ip_comparison_symbol": {
64+
Optional: true,
65+
Type: schema.TypeString,
66+
Description: "In: The specified ip can be downloaded; NotIn: The specified ip cannot be downloaded. The default is In.",
67+
},
68+
69+
"limit_vpc": {
70+
Optional: true,
71+
Type: schema.TypeList,
72+
Description: "vpc settings to limit downloads.",
73+
Elem: &schema.Resource{
74+
Schema: map[string]*schema.Schema{
75+
"region": {
76+
Type: schema.TypeString,
77+
Required: true,
78+
Description: "Restrict downloads from regions. Currently only the current region is supported.",
79+
},
80+
"vpc_list": {
81+
Type: schema.TypeSet,
82+
Elem: &schema.Schema{
83+
Type: schema.TypeString,
84+
},
85+
Required: true,
86+
Description: "List of vpcs to limit downloads.",
87+
},
88+
},
89+
},
90+
},
91+
92+
"limit_ip": {
93+
Optional: true,
94+
Type: schema.TypeSet,
95+
Elem: &schema.Schema{
96+
Type: schema.TypeString,
97+
},
98+
Description: "ip settings to limit downloads.",
99+
},
100+
},
101+
}
102+
}
103+
104+
func resourceTencentCloudMysqlBackupDownloadRestrictionCreate(d *schema.ResourceData, meta interface{}) error {
105+
defer logElapsed("resource.tencentcloud_mysql_backup_download_restriction.create")()
106+
defer inconsistentCheck(d, meta)()
107+
108+
d.SetId("BackupDownloadRestriction")
109+
110+
return resourceTencentCloudMysqlBackupDownloadRestrictionUpdate(d, meta)
111+
}
112+
113+
func resourceTencentCloudMysqlBackupDownloadRestrictionRead(d *schema.ResourceData, meta interface{}) error {
114+
defer logElapsed("resource.tencentcloud_mysql_backup_download_restriction.read")()
115+
defer inconsistentCheck(d, meta)()
116+
117+
logId := getLogId(contextNil)
118+
119+
ctx := context.WithValue(context.TODO(), logIdKey, logId)
120+
121+
service := MysqlService{client: meta.(*TencentCloudClient).apiV3Conn}
122+
123+
backupDownloadRestriction, err := service.DescribeMysqlBackupDownloadRestrictionById(ctx)
124+
if err != nil {
125+
return err
126+
}
127+
128+
if backupDownloadRestriction == nil {
129+
d.SetId("")
130+
log.Printf("[WARN]%s resource `MysqlBackupDownloadRestriction` [%s] not found, please check if it has been deleted.\n", logId, d.Id())
131+
return nil
132+
}
133+
134+
if backupDownloadRestriction.LimitType != nil {
135+
_ = d.Set("limit_type", backupDownloadRestriction.LimitType)
136+
}
137+
138+
if backupDownloadRestriction.VpcComparisonSymbol != nil {
139+
_ = d.Set("vpc_comparison_symbol", backupDownloadRestriction.VpcComparisonSymbol)
140+
}
141+
142+
if backupDownloadRestriction.IpComparisonSymbol != nil {
143+
_ = d.Set("ip_comparison_symbol", backupDownloadRestriction.IpComparisonSymbol)
144+
}
145+
146+
if backupDownloadRestriction.LimitVpc != nil {
147+
limitVpcList := []interface{}{}
148+
for _, limitVpc := range backupDownloadRestriction.LimitVpc {
149+
limitVpcMap := map[string]interface{}{}
150+
151+
if limitVpc.Region != nil {
152+
limitVpcMap["region"] = limitVpc.Region
153+
}
154+
155+
if limitVpc.VpcList != nil {
156+
limitVpcMap["vpc_list"] = limitVpc.VpcList
157+
}
158+
159+
limitVpcList = append(limitVpcList, limitVpcMap)
160+
}
161+
162+
_ = d.Set("limit_vpc", limitVpcList)
163+
164+
}
165+
166+
if backupDownloadRestriction.LimitIp != nil {
167+
_ = d.Set("limit_ip", backupDownloadRestriction.LimitIp)
168+
}
169+
170+
return nil
171+
}
172+
173+
func resourceTencentCloudMysqlBackupDownloadRestrictionUpdate(d *schema.ResourceData, meta interface{}) error {
174+
defer logElapsed("resource.tencentcloud_mysql_backup_download_restriction.update")()
175+
defer inconsistentCheck(d, meta)()
176+
177+
logId := getLogId(contextNil)
178+
179+
request := mysql.NewModifyBackupDownloadRestrictionRequest()
180+
181+
if v, ok := d.GetOk("limit_type"); ok {
182+
request.LimitType = helper.String(v.(string))
183+
}
184+
185+
if d.HasChange("vpc_comparison_symbol") {
186+
if v, ok := d.GetOk("vpc_comparison_symbol"); ok {
187+
request.VpcComparisonSymbol = helper.String(v.(string))
188+
}
189+
}
190+
191+
if d.HasChange("ip_comparison_symbol") {
192+
if v, ok := d.GetOk("ip_comparison_symbol"); ok {
193+
request.IpComparisonSymbol = helper.String(v.(string))
194+
}
195+
}
196+
197+
if d.HasChange("limit_vpc") {
198+
if v, ok := d.GetOk("limit_vpc"); ok {
199+
for _, item := range v.([]interface{}) {
200+
dMap := item.(map[string]interface{})
201+
limitVpcItem := mysql.BackupLimitVpcItem{}
202+
if v, ok := dMap["region"]; ok {
203+
limitVpcItem.Region = helper.String(v.(string))
204+
}
205+
if v, ok := dMap["vpc_list"]; ok {
206+
vpcListSet := v.(*schema.Set).List()
207+
limitVpcItem.VpcList = helper.InterfacesStringsPoint(vpcListSet)
208+
}
209+
request.LimitVpc = append(request.LimitVpc, &limitVpcItem)
210+
}
211+
}
212+
}
213+
214+
if d.HasChange("limit_ip") {
215+
if v, ok := d.GetOk("limit_ip"); ok {
216+
limitIpSet := v.(*schema.Set).List()
217+
request.LimitIp = helper.InterfacesStringsPoint(limitIpSet)
218+
}
219+
}
220+
221+
err := resource.Retry(writeRetryTimeout, func() *resource.RetryError {
222+
result, e := meta.(*TencentCloudClient).apiV3Conn.UseMysqlClient().ModifyBackupDownloadRestriction(request)
223+
if e != nil {
224+
if sdkerr, ok := e.(*sdkErrors.TencentCloudSDKError); ok {
225+
if strings.Contains(sdkerr.Code, "FailedOperation") {
226+
return resource.NonRetryableError(e)
227+
}
228+
}
229+
return retryError(e)
230+
} else {
231+
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString())
232+
}
233+
return nil
234+
})
235+
if err != nil {
236+
log.Printf("[CRITAL]%s update mysql backupDownloadRestriction failed, reason:%+v", logId, err)
237+
return err
238+
}
239+
240+
return resourceTencentCloudMysqlBackupDownloadRestrictionRead(d, meta)
241+
}
242+
243+
func resourceTencentCloudMysqlBackupDownloadRestrictionDelete(d *schema.ResourceData, meta interface{}) error {
244+
defer logElapsed("resource.tencentcloud_mysql_backup_download_restriction.delete")()
245+
defer inconsistentCheck(d, meta)()
246+
247+
return nil
248+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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 TestAccTencentCloudMysqlBackupDownloadRestrictionResource_basic -v
10+
func TestAccTencentCloudMysqlBackupDownloadRestrictionResource_basic(t *testing.T) {
11+
t.Parallel()
12+
resource.Test(t, resource.TestCase{
13+
PreCheck: func() { testAccPreCheck(t) },
14+
Providers: testAccProviders,
15+
Steps: []resource.TestStep{
16+
{
17+
Config: testAccMysqlBackupDownloadRestriction,
18+
Check: resource.ComposeAggregateTestCheckFunc(
19+
resource.TestCheckResourceAttr("tencentcloud_mysql_backup_download_restriction.backup_download_restriction", "limit_type", "Customize"),
20+
resource.TestCheckResourceAttr("tencentcloud_mysql_backup_download_restriction.backup_download_restriction", "vpc_comparison_symbol", "In"),
21+
resource.TestCheckResourceAttr("tencentcloud_mysql_backup_download_restriction.backup_download_restriction", "ip_comparison_symbol", "In"),
22+
resource.TestCheckResourceAttr("tencentcloud_mysql_backup_download_restriction.backup_download_restriction", "limit_vpc.#", "1"),
23+
resource.TestCheckResourceAttr("tencentcloud_mysql_backup_download_restriction.backup_download_restriction", "limit_vpc.0.region", "ap-guangzhou"),
24+
resource.TestCheckResourceAttr("tencentcloud_mysql_backup_download_restriction.backup_download_restriction", "limit_vpc.0.vpc_list.#", "1"),
25+
resource.TestCheckResourceAttr("tencentcloud_mysql_backup_download_restriction.backup_download_restriction", "limit_vpc.0.vpc_list.0", "vpc-4owdpnwr"),
26+
resource.TestCheckResourceAttr("tencentcloud_mysql_backup_download_restriction.backup_download_restriction", "limit_ip.#", "1"),
27+
resource.TestCheckResourceAttr("tencentcloud_mysql_backup_download_restriction.backup_download_restriction", "limit_ip.0", "127.0.0.1"),
28+
),
29+
},
30+
{
31+
ResourceName: "tencentcloud_mysql_backup_download_restriction.backup_download_restriction",
32+
ImportState: true,
33+
ImportStateVerify: true,
34+
},
35+
},
36+
})
37+
}
38+
39+
const testAccMysqlBackupDownloadRestriction = `
40+
41+
resource "tencentcloud_mysql_backup_download_restriction" "backup_download_restriction" {
42+
limit_type = "Customize"
43+
vpc_comparison_symbol = "In"
44+
ip_comparison_symbol = "In"
45+
limit_vpc {
46+
region = "ap-guangzhou"
47+
vpc_list = ["vpc-4owdpnwr"]
48+
}
49+
limit_ip = ["127.0.0.1"]
50+
}
51+
52+
`

0 commit comments

Comments
 (0)