Skip to content

Commit 6997c1f

Browse files
authored
support postgresql: data source (#1839)
* support postgresql: data source * add backup data source * add other ds * add e2e case * add changelog and update doc * adjust e2e case
1 parent 0feeb9e commit 6997c1f

31 files changed

+3338
-0
lines changed

.changelog/1839.txt

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
```release-note:new-data-source
2+
tencentcloud_postgresql_base_backups
3+
```
4+
5+
```release-note:new-data-source
6+
tencentcloud_postgresql_log_backups
7+
```
8+
9+
```release-note:new-data-source
10+
tencentcloud_postgresql_backup_download_urls
11+
```
12+
13+
```release-note:new-data-source
14+
tencentcloud_postgresql_db_instance_classes
15+
```
16+
17+
```release-note:new-data-source
18+
tencentcloud_postgresql_default_parameters
19+
```
20+
21+
```release-note:new-data-source
22+
tencentcloud_postgresql_recovery_time
23+
```
24+
25+
```release-note:new-data-source
26+
tencentcloud_postgresql_regions
27+
```
28+
29+
```release-note:new-data-source
30+
tencentcloud_postgresql_db_instance_versions
31+
```
32+
33+
```release-note:new-data-source
34+
tencentcloud_postgresql_zones
35+
```
Lines changed: 206 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,206 @@
1+
/*
2+
Use this data source to query detailed information of postgresql backup_download_urls
3+
4+
Example Usage
5+
6+
```hcl
7+
data "tencentcloud_postgresql_log_backups" "log_backups" {
8+
min_finish_time = "%s"
9+
max_finish_time = "%s"
10+
filters {
11+
name = "db-instance-id"
12+
values = [local.pgsql_id]
13+
}
14+
order_by = "StartTime"
15+
order_by_type = "desc"
16+
17+
}
18+
19+
data "tencentcloud_postgresql_backup_download_urls" "backup_download_urls" {
20+
db_instance_id = local.pgsql_id
21+
backup_type = "LogBackup"
22+
backup_id = data.tencentcloud_postgresql_log_backups.log_backups.log_backup_set.0.id
23+
url_expire_time = 12
24+
backup_download_restriction {
25+
restriction_type = "NONE"
26+
vpc_restriction_effect = "ALLOW"
27+
vpc_id_set = [local.vpc_id]
28+
ip_restriction_effect = "ALLOW"
29+
ip_set = ["0.0.0.0"]
30+
}
31+
}
32+
```
33+
*/
34+
package tencentcloud
35+
36+
import (
37+
"context"
38+
39+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
40+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
41+
postgresql "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/postgres/v20170312"
42+
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
43+
)
44+
45+
func dataSourceTencentCloudPostgresqlBackupDownloadUrls() *schema.Resource {
46+
return &schema.Resource{
47+
Read: dataSourceTencentCloudPostgresqlBackupDownloadUrlsRead,
48+
Schema: map[string]*schema.Schema{
49+
"db_instance_id": {
50+
Required: true,
51+
Type: schema.TypeString,
52+
Description: "Instance ID.",
53+
},
54+
55+
"backup_type": {
56+
Required: true,
57+
Type: schema.TypeString,
58+
Description: "Backup type. Valid values: `LogBackup`, `BaseBackup`.",
59+
},
60+
61+
"backup_id": {
62+
Required: true,
63+
Type: schema.TypeString,
64+
Description: "Unique backup ID.",
65+
},
66+
67+
"url_expire_time": {
68+
Optional: true,
69+
Type: schema.TypeInt,
70+
Description: "Validity period of a URL, which is 12 hours by default.",
71+
},
72+
73+
"backup_download_restriction": {
74+
Optional: true,
75+
Type: schema.TypeList,
76+
MaxItems: 1,
77+
Description: "Backup download restriction.",
78+
Elem: &schema.Resource{
79+
Schema: map[string]*schema.Schema{
80+
"restriction_type": {
81+
Type: schema.TypeString,
82+
Optional: true,
83+
Description: "Type of the network restrictions for downloading backup files. Valid values: `NONE` (backups can be downloaded over both private and public networks), `INTRANET` (backups can only be downloaded over the private network), `CUSTOMIZE` (backups can be downloaded over specified VPCs or at specified IPs).",
84+
},
85+
"vpc_restriction_effect": {
86+
Type: schema.TypeString,
87+
Optional: true,
88+
Description: "Whether VPC is allowed. Valid values: `ALLOW` (allow), `DENY` (deny).",
89+
},
90+
"vpc_id_set": {
91+
Type: schema.TypeSet,
92+
Elem: &schema.Schema{
93+
Type: schema.TypeString,
94+
},
95+
Optional: true,
96+
Description: "Whether it is allowed to download the VPC ID list of the backup files.",
97+
},
98+
"ip_restriction_effect": {
99+
Type: schema.TypeString,
100+
Optional: true,
101+
Description: "Whether IP is allowed. Valid values: `ALLOW` (allow), `DENY` (deny).",
102+
},
103+
"ip_set": {
104+
Type: schema.TypeSet,
105+
Elem: &schema.Schema{
106+
Type: schema.TypeString,
107+
},
108+
Optional: true,
109+
Description: "Whether it is allowed to download IP list of the backup files.",
110+
},
111+
},
112+
},
113+
},
114+
115+
"backup_download_url": {
116+
Computed: true,
117+
Type: schema.TypeString,
118+
Description: "Backup download URL.",
119+
},
120+
121+
"result_output_file": {
122+
Type: schema.TypeString,
123+
Optional: true,
124+
Description: "Used to save results.",
125+
},
126+
},
127+
}
128+
}
129+
130+
func dataSourceTencentCloudPostgresqlBackupDownloadUrlsRead(d *schema.ResourceData, meta interface{}) error {
131+
defer logElapsed("data_source.tencentcloud_postgresql_backup_download_urls.read")()
132+
defer inconsistentCheck(d, meta)()
133+
134+
logId := getLogId(contextNil)
135+
var id string
136+
137+
ctx := context.WithValue(context.TODO(), logIdKey, logId)
138+
139+
paramMap := make(map[string]interface{})
140+
if v, ok := d.GetOk("db_instance_id"); ok {
141+
paramMap["DBInstanceId"] = helper.String(v.(string))
142+
id = v.(string)
143+
}
144+
145+
if v, ok := d.GetOk("backup_type"); ok {
146+
paramMap["BackupType"] = helper.String(v.(string))
147+
}
148+
149+
if v, ok := d.GetOk("backup_id"); ok {
150+
paramMap["BackupId"] = helper.String(v.(string))
151+
}
152+
153+
if v, _ := d.GetOk("url_expire_time"); v != nil {
154+
paramMap["URLExpireTime"] = helper.IntUint64(v.(int))
155+
}
156+
157+
if dMap, ok := helper.InterfacesHeadMap(d, "backup_download_restriction"); ok {
158+
backupDownloadRestriction := postgresql.BackupDownloadRestriction{}
159+
if v, ok := dMap["restriction_type"]; ok {
160+
backupDownloadRestriction.RestrictionType = helper.String(v.(string))
161+
}
162+
if v, ok := dMap["vpc_restriction_effect"]; ok {
163+
backupDownloadRestriction.VpcRestrictionEffect = helper.String(v.(string))
164+
}
165+
if v, ok := dMap["vpc_id_set"]; ok {
166+
vpcIdSetSet := v.(*schema.Set).List()
167+
backupDownloadRestriction.VpcIdSet = helper.InterfacesStringsPoint(vpcIdSetSet)
168+
}
169+
if v, ok := dMap["ip_restriction_effect"]; ok {
170+
backupDownloadRestriction.IpRestrictionEffect = helper.String(v.(string))
171+
}
172+
if v, ok := dMap["ip_set"]; ok {
173+
ipSetSet := v.(*schema.Set).List()
174+
backupDownloadRestriction.IpSet = helper.InterfacesStringsPoint(ipSetSet)
175+
}
176+
paramMap["BackupDownloadRestriction"] = &backupDownloadRestriction
177+
}
178+
179+
service := PostgresqlService{client: meta.(*TencentCloudClient).apiV3Conn}
180+
181+
var backupDownloadURL *string
182+
err := resource.Retry(readRetryTimeout, func() *resource.RetryError {
183+
result, e := service.DescribePostgresqlBackupDownloadUrlsByFilter(ctx, paramMap)
184+
if e != nil {
185+
return retryError(e)
186+
}
187+
backupDownloadURL = result
188+
return nil
189+
})
190+
if err != nil {
191+
return err
192+
}
193+
194+
if backupDownloadURL != nil {
195+
_ = d.Set("backup_download_url", backupDownloadURL)
196+
}
197+
198+
d.SetId(helper.DataResourceIdHash(id))
199+
output, ok := d.GetOk("result_output_file")
200+
if ok && output.(string) != "" {
201+
if e := writeToFile(output.(string), backupDownloadURL); e != nil {
202+
return e
203+
}
204+
}
205+
return nil
206+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package tencentcloud
2+
3+
import (
4+
"testing"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
7+
)
8+
9+
func TestAccTencentCloudPostgresqlBackupDownloadUrlsDataSource_basic(t *testing.T) {
10+
t.Parallel()
11+
// loc, _ := time.LoadLocation("Asia/Chongqing")
12+
// startTime := time.Now().AddDate(0, 0, -7).In(loc).Format("2006-01-02 15:04:05")
13+
// endTime := time.Now().AddDate(0, 0, 1).In(loc).Format("2006-01-02 15:04:05")
14+
15+
resource.Test(t, resource.TestCase{
16+
PreCheck: func() {
17+
testAccPreCheck(t)
18+
},
19+
Providers: testAccProviders,
20+
Steps: []resource.TestStep{
21+
{
22+
// Config: fmt.Sprintf(testAccPostgresqlBackupDownloadUrlsDataSource, startTime, endTime),
23+
Config: testAccPostgresqlBackupDownloadUrlsDataSource,
24+
Check: resource.ComposeTestCheckFunc(
25+
testAccCheckTencentCloudDataSourceID("data.tencentcloud_postgresql_backup_download_urls.backup_download_urls"),
26+
resource.TestCheckResourceAttrSet("data.tencentcloud_postgresql_backup_download_urls.backup_download_urls", "db_instance_id"),
27+
resource.TestCheckResourceAttr("data.tencentcloud_postgresql_backup_download_urls.backup_download_urls", "backup_type", "LogBackup"),
28+
resource.TestCheckResourceAttrSet("data.tencentcloud_postgresql_backup_download_urls.backup_download_urls", "backup_id"),
29+
resource.TestCheckResourceAttr("data.tencentcloud_postgresql_backup_download_urls.backup_download_urls", "url_expire_time", "12"),
30+
resource.TestCheckResourceAttrSet("data.tencentcloud_postgresql_backup_download_urls.backup_download_urls", "backup_download_restriction.#"),
31+
resource.TestCheckResourceAttr("data.tencentcloud_postgresql_backup_download_urls.backup_download_urls", "backup_download_restriction.0.restriction_type", "NONE"),
32+
resource.TestCheckResourceAttr("data.tencentcloud_postgresql_backup_download_urls.backup_download_urls", "backup_download_restriction.0.vpc_restriction_effect", "ALLOW"),
33+
resource.TestCheckResourceAttr("data.tencentcloud_postgresql_backup_download_urls.backup_download_urls", "backup_download_restriction.0.vpc_id_set.#", "1"),
34+
resource.TestCheckResourceAttr("data.tencentcloud_postgresql_backup_download_urls.backup_download_urls", "backup_download_restriction.0.ip_restriction_effect", "ALLOW"),
35+
resource.TestCheckResourceAttr("data.tencentcloud_postgresql_backup_download_urls.backup_download_urls", "backup_download_restriction.0.ip_set.#", "1"),
36+
resource.TestCheckResourceAttrSet("data.tencentcloud_postgresql_backup_download_urls.backup_download_urls", "backup_download_url"),
37+
),
38+
},
39+
},
40+
})
41+
}
42+
43+
const testAccPostgresqlBackupDownloadUrlsDataSource = CommonPresetPGSQL + defaultVpcSubnets + `
44+
data "tencentcloud_postgresql_log_backups" "log_backups" {
45+
min_finish_time = ""
46+
max_finish_time = ""
47+
filters {
48+
name = "db-instance-id"
49+
values = [local.pgsql_id]
50+
}
51+
// order_by = "StartTime"
52+
order_by_type = "desc"
53+
}
54+
55+
data "tencentcloud_postgresql_backup_download_urls" "backup_download_urls" {
56+
db_instance_id = local.pgsql_id
57+
backup_type = "LogBackup"
58+
// backup_id = data.tencentcloud_postgresql_log_backups.log_backups.log_backup_set.0.id
59+
backup_id = "01a57d08-b7f5-584e-b64a-dc2236bb0438"
60+
url_expire_time = 12
61+
backup_download_restriction {
62+
restriction_type = "NONE"
63+
vpc_restriction_effect = "ALLOW"
64+
vpc_id_set = [local.vpc_id]
65+
ip_restriction_effect = "ALLOW"
66+
ip_set = ["0.0.0.0"]
67+
}
68+
}
69+
70+
`

0 commit comments

Comments
 (0)