Skip to content

Commit 96418d9

Browse files
authored
support postgresql config resource (#1806)
* support postgresql config resource * passed the e2e case * add changelog and update doc * adjust example
1 parent 9ac4aa6 commit 96418d9

16 files changed

+1664
-352
lines changed

.changelog/1806.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_postgresql_security_group_config
3+
```
4+
5+
```release-note:new-resource
6+
tencentcloud_postgresql_backup_download_restriction_config
7+
```

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ require (
6060
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/monitor v1.0.616
6161
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/mps v1.0.584
6262
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/organization v1.0.540
63-
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/postgres v1.0.625
63+
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/postgres v1.0.661
6464
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/privatedns v1.0.290
6565
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/pts v1.0.533
6666
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/redis v1.0.657

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -890,6 +890,8 @@ github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/organization v1.0.540 h
890890
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/organization v1.0.540/go.mod h1:jHcHwlsdNL9ycLwNtqYZVjLKJILA1xuODMeMDMOxCrA=
891891
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/postgres v1.0.625 h1:kdRKVdupmT5Ai2FdmmSXZ48zI6TCBgQTNvT0+74oCuM=
892892
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/postgres v1.0.625/go.mod h1:oITteEF2Tol99FFbQkkUIvoVjEjnod0SYP/y+2JVjuY=
893+
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/postgres v1.0.661 h1:rSnn8bRRlWGhIkMdaberPIHcHrB30isdD2AclnW3aHA=
894+
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/postgres v1.0.661/go.mod h1:ZUWwncGwpSOw0KCEkAwTRIxBoTi+HisKLLZ58easDyI=
893895
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/privatedns v1.0.290 h1:osYZxh9ApEc8UpvIMwjAUfdl7ytRcWUpcnnqLIpiJ/U=
894896
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/privatedns v1.0.290/go.mod h1:En+pdagcHkAASorHT1l8R6tUtieRNNxaQ7nfyqWPefk=
895897
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/pts v1.0.533 h1:BJDJlHkuoPsSUJ/ZHnmtX3gmA2PVfmKyhB7uV2d9QlE=

tencentcloud/provider.go

Lines changed: 353 additions & 349 deletions
Large diffs are not rendered by default.
Lines changed: 240 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,240 @@
1+
/*
2+
Provides a resource to create a postgresql backup_download_restriction_config
3+
4+
Example Usage
5+
6+
Unlimit the restriction of the backup file download.
7+
```hcl
8+
resource "tencentcloud_postgresql_backup_download_restriction_config" "backup_download_restriction_config" {
9+
restriction_type = "NONE"
10+
}
11+
```
12+
13+
Set the download only to allow the intranet downloads.
14+
```hcl
15+
resource "tencentcloud_postgresql_backup_download_restriction_config" "backup_download_restriction_config" {
16+
restriction_type = "INTRANET"
17+
}
18+
```
19+
20+
Restrict the backup file download by customizing.
21+
```hcl
22+
resource "tencentcloud_vpc" "pg_vpc" {
23+
name = var.instance_name
24+
cidr_block = var.vpc_cidr
25+
}
26+
27+
resource "tencentcloud_postgresql_backup_download_restriction_config" "backup_download_restriction_config" {
28+
restriction_type = "CUSTOMIZE"
29+
vpc_restriction_effect = "DENY"
30+
vpc_id_set = [tencentcloud_vpc.pg_vpc2.id]
31+
ip_restriction_effect = "DENY"
32+
ip_set = ["192.168.0.0"]
33+
}
34+
```
35+
36+
Import
37+
38+
postgresql backup_download_restriction_config can be imported using the id, e.g.
39+
40+
```
41+
terraform import tencentcloud_postgresql_backup_download_restriction_config.backup_download_restriction_config backup_download_restriction_config_id
42+
```
43+
*/
44+
package tencentcloud
45+
46+
import (
47+
"context"
48+
"log"
49+
50+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
51+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
52+
postgresql "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/postgres/v20170312"
53+
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
54+
)
55+
56+
func resourceTencentCloudPostgresqlBackupDownloadRestrictionConfig() *schema.Resource {
57+
return &schema.Resource{
58+
Create: resourceTencentCloudPostgresqlBackupDownloadRestrictionConfigCreate,
59+
Read: resourceTencentCloudPostgresqlBackupDownloadRestrictionConfigRead,
60+
Update: resourceTencentCloudPostgresqlBackupDownloadRestrictionConfigUpdate,
61+
Delete: resourceTencentCloudPostgresqlBackupDownloadRestrictionConfigDelete,
62+
Importer: &schema.ResourceImporter{
63+
State: schema.ImportStatePassthrough,
64+
},
65+
Schema: map[string]*schema.Schema{
66+
"restriction_type": {
67+
Required: true,
68+
Type: schema.TypeString,
69+
Description: "Backup file download restriction type: NONE:Unlimited, both internal and external networks can be downloaded. INTRANET:Only intranet downloads are allowed. CUSTOMIZE:Customize the vpc or ip that limits downloads.",
70+
},
71+
72+
"vpc_restriction_effect": {
73+
Optional: true,
74+
Type: schema.TypeString,
75+
Description: "vpc limit Strategy: ALLOW, DENY.",
76+
},
77+
78+
"vpc_id_set": {
79+
Optional: true,
80+
Type: schema.TypeSet,
81+
Elem: &schema.Schema{
82+
Type: schema.TypeString,
83+
},
84+
Description: "The list of vpcIds that allow or deny downloading of backup files.",
85+
},
86+
87+
"ip_restriction_effect": {
88+
Optional: true,
89+
Type: schema.TypeString,
90+
Description: "ip limit Strategy: ALLOW, DENY.",
91+
},
92+
93+
"ip_set": {
94+
Optional: true,
95+
Type: schema.TypeSet,
96+
Elem: &schema.Schema{
97+
Type: schema.TypeString,
98+
},
99+
Description: "The list of ips that are allowed or denied to download backup files.",
100+
},
101+
},
102+
}
103+
}
104+
105+
func resourceTencentCloudPostgresqlBackupDownloadRestrictionConfigCreate(d *schema.ResourceData, meta interface{}) error {
106+
defer logElapsed("resource.tencentcloud_postgresql_backup_download_restriction_config.create")()
107+
defer inconsistentCheck(d, meta)()
108+
109+
var resType string
110+
if v, ok := d.GetOk("restriction_type"); ok {
111+
resType = v.(string)
112+
}
113+
114+
d.SetId(resType)
115+
116+
return resourceTencentCloudPostgresqlBackupDownloadRestrictionConfigUpdate(d, meta)
117+
}
118+
119+
func resourceTencentCloudPostgresqlBackupDownloadRestrictionConfigRead(d *schema.ResourceData, meta interface{}) error {
120+
defer logElapsed("resource.tencentcloud_postgresql_backup_download_restriction_config.read")()
121+
defer inconsistentCheck(d, meta)()
122+
123+
logId := getLogId(contextNil)
124+
125+
ctx := context.WithValue(context.TODO(), logIdKey, logId)
126+
127+
service := PostgresqlService{client: meta.(*TencentCloudClient).apiV3Conn}
128+
129+
resType := d.Id()
130+
131+
BackupDownloadRestrictionConfig, err := service.DescribePostgresqlBackupDownloadRestrictionConfigById(ctx, resType)
132+
if err != nil {
133+
return err
134+
}
135+
136+
if BackupDownloadRestrictionConfig == nil {
137+
d.SetId("")
138+
log.Printf("[WARN]%s resource `PostgresqlBackupDownloadRestrictionConfig` [%s] not found, please check if it has been deleted.\n", logId, d.Id())
139+
return nil
140+
}
141+
142+
if BackupDownloadRestrictionConfig.RestrictionType != nil {
143+
_ = d.Set("restriction_type", BackupDownloadRestrictionConfig.RestrictionType)
144+
}
145+
146+
if BackupDownloadRestrictionConfig.VpcRestrictionEffect != nil {
147+
_ = d.Set("vpc_restriction_effect", BackupDownloadRestrictionConfig.VpcRestrictionEffect)
148+
}
149+
150+
if BackupDownloadRestrictionConfig.VpcIdSet != nil {
151+
_ = d.Set("vpc_id_set", BackupDownloadRestrictionConfig.VpcIdSet)
152+
}
153+
154+
if BackupDownloadRestrictionConfig.IpRestrictionEffect != nil {
155+
_ = d.Set("ip_restriction_effect", BackupDownloadRestrictionConfig.IpRestrictionEffect)
156+
}
157+
158+
if BackupDownloadRestrictionConfig.IpSet != nil {
159+
_ = d.Set("ip_set", BackupDownloadRestrictionConfig.IpSet)
160+
}
161+
162+
return nil
163+
}
164+
165+
func resourceTencentCloudPostgresqlBackupDownloadRestrictionConfigUpdate(d *schema.ResourceData, meta interface{}) error {
166+
defer logElapsed("resource.tencentcloud_postgresql_backup_download_restriction_config.update")()
167+
defer inconsistentCheck(d, meta)()
168+
169+
logId := getLogId(contextNil)
170+
171+
request := postgresql.NewModifyBackupDownloadRestrictionRequest()
172+
173+
resType := d.Id()
174+
175+
if d.HasChange("restriction_type") {
176+
if v, ok := d.GetOk("restriction_type"); ok {
177+
resType = v.(string)
178+
}
179+
}
180+
request.RestrictionType = &resType
181+
182+
if d.HasChange("vpc_restriction_effect") {
183+
if v, ok := d.GetOk("vpc_restriction_effect"); ok {
184+
request.VpcRestrictionEffect = helper.String(v.(string))
185+
}
186+
}
187+
188+
if d.HasChange("vpc_id_set") {
189+
if v, ok := d.GetOk("vpc_id_set"); ok {
190+
vpcIdSetSet := v.(*schema.Set).List()
191+
for i := range vpcIdSetSet {
192+
if vpcIdSetSet[i] != nil {
193+
vpcIdSet := vpcIdSetSet[i].(string)
194+
request.VpcIdSet = append(request.VpcIdSet, &vpcIdSet)
195+
}
196+
}
197+
}
198+
}
199+
200+
if d.HasChange("ip_restriction_effect") {
201+
if v, ok := d.GetOk("ip_restriction_effect"); ok {
202+
request.IpRestrictionEffect = helper.String(v.(string))
203+
}
204+
}
205+
206+
if d.HasChange("ip_set") {
207+
if v, ok := d.GetOk("ip_set"); ok {
208+
ipSetSet := v.(*schema.Set).List()
209+
for i := range ipSetSet {
210+
if ipSetSet[i] != nil {
211+
ipSet := ipSetSet[i].(string)
212+
request.IpSet = append(request.IpSet, &ipSet)
213+
}
214+
}
215+
}
216+
}
217+
218+
err := resource.Retry(writeRetryTimeout, func() *resource.RetryError {
219+
result, e := meta.(*TencentCloudClient).apiV3Conn.UsePostgresqlClient().ModifyBackupDownloadRestriction(request)
220+
if e != nil {
221+
return retryError(e)
222+
} else {
223+
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString())
224+
}
225+
return nil
226+
})
227+
if err != nil {
228+
log.Printf("[CRITAL]%s update postgresql BackupDownloadRestrictionConfig failed, reason:%+v", logId, err)
229+
return err
230+
}
231+
232+
return resourceTencentCloudPostgresqlBackupDownloadRestrictionConfigRead(d, meta)
233+
}
234+
235+
func resourceTencentCloudPostgresqlBackupDownloadRestrictionConfigDelete(d *schema.ResourceData, meta interface{}) error {
236+
defer logElapsed("resource.tencentcloud_postgresql_backup_download_restriction_config.delete")()
237+
defer inconsistentCheck(d, meta)()
238+
239+
return nil
240+
}

0 commit comments

Comments
 (0)