Skip to content

Commit 93e4f16

Browse files
authored
fix: postgresql - support backup plans and xlog datasource (#988)
* fix: postgresql - support backup plans and xlog datasource * fix: pgsql backup plan assignment * fix: add open/close extranet retry * fix: isolate retry and datasource testcase
1 parent 0811ada commit 93e4f16

11 files changed

+559
-83
lines changed

tencentcloud/basic_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,21 @@ resource "tencentcloud_instance" "default" {
280280

281281
// End of SQLServer
282282

283+
// PostgreSQL
284+
285+
const defaultPGSQLName = "keep-postgresql"
286+
const CommonPresetPGSQL = `
287+
data "tencentcloud_postgresql_instances" "foo" {
288+
name = "` + defaultPGSQLName + `"
289+
}
290+
291+
locals {
292+
pgsql_id = data.tencentcloud_postgresql_instances.foo.instance_list.0.id
293+
}
294+
`
295+
296+
// End of PostgreSQL
297+
283298
const defaultCVMName = "keep-cvm"
284299
const presetCVM = `
285300
data "tencentcloud_instances" "instance" {

tencentcloud/data_source_tc_postgresql_instances_test.go

Lines changed: 7 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -18,69 +18,27 @@ func TestAccTencentCloudDataPostgresqlInstances(t *testing.T) {
1818
{
1919
Config: testAccTencentCloudDataPostgresqlInstanceBasic,
2020
Check: resource.ComposeTestCheckFunc(
21-
testAccCheckPostgresqlInstanceExists("tencentcloud_postgresql_instance.test"),
2221
resource.TestCheckResourceAttr(testDataPostgresqlInstancesName, "instance_list.#", "1"),
2322
resource.TestCheckResourceAttrSet(testDataPostgresqlInstancesName, "instance_list.0.id"),
2423
resource.TestCheckResourceAttrSet(testDataPostgresqlInstancesName, "instance_list.0.create_time"),
25-
resource.TestCheckResourceAttr(testDataPostgresqlInstancesName, "instance_list.0.charge_type", "POSTPAID_BY_HOUR"),
26-
resource.TestCheckResourceAttr(testDataPostgresqlInstancesName, "instance_list.0.engine_version", "10.4"),
27-
resource.TestCheckResourceAttr(testDataPostgresqlInstancesName, "instance_list.0.project_id", "0"),
28-
resource.TestCheckResourceAttr(testDataPostgresqlInstancesName, "instance_list.0.memory", "2"),
29-
resource.TestCheckResourceAttr(testDataPostgresqlInstancesName, "instance_list.0.storage", "10"),
24+
resource.TestCheckResourceAttrSet(testDataPostgresqlInstancesName, "instance_list.0.charge_type"),
25+
resource.TestCheckResourceAttrSet(testDataPostgresqlInstancesName, "instance_list.0.engine_version"),
26+
resource.TestCheckResourceAttrSet(testDataPostgresqlInstancesName, "instance_list.0.project_id"),
27+
resource.TestCheckResourceAttrSet(testDataPostgresqlInstancesName, "instance_list.0.memory"),
28+
resource.TestCheckResourceAttrSet(testDataPostgresqlInstancesName, "instance_list.0.storage"),
3029
resource.TestCheckResourceAttrSet(testDataPostgresqlInstancesName, "instance_list.0.private_access_ip"),
3130
resource.TestCheckResourceAttrSet(testDataPostgresqlInstancesName, "instance_list.0.private_access_port"),
3231
resource.TestCheckResourceAttrSet(testDataPostgresqlInstancesName, "instance_list.0.public_access_switch"),
3332
resource.TestCheckResourceAttrSet(testDataPostgresqlInstancesName, "instance_list.0.charset"),
34-
resource.TestCheckResourceAttr(testDataPostgresqlInstancesName, "instance_list.0.tags.tf", "test"),
3533
),
3634
},
3735
},
3836
})
3937
}
4038

41-
const testAccTencentCloudDataPostgresqlInstanceBasic = `
42-
data "tencentcloud_availability_zones_by_product" "pg" {
43-
product = "postgres"
44-
}
45-
46-
resource "tencentcloud_vpc" "vpc" {
47-
cidr_block = "10.0.0.0/24"
48-
name = "test-pg-vpc"
49-
}
50-
51-
resource "tencentcloud_subnet" "subnet" {
52-
availability_zone = local.az
53-
cidr_block = "10.0.0.0/24"
54-
name = "sub1"
55-
vpc_id = tencentcloud_vpc.vpc.id
56-
}
57-
58-
locals {
59-
az = data.tencentcloud_availability_zones_by_product.pg.zones.0.name
60-
vpc_id = tencentcloud_vpc.vpc.id
61-
subnet_id = tencentcloud_subnet.subnet.id
62-
}
63-
64-
65-
resource "tencentcloud_postgresql_instance" "test" {
66-
name = "tf_postsql_instance"
67-
availability_zone = local.az
68-
charge_type = "POSTPAID_BY_HOUR"
69-
engine_version = "10.4"
70-
root_password = "1qaA2k1wgvfa!_3ZZZ"
71-
charset = "UTF8"
72-
project_id = 0
73-
memory = 2
74-
storage = 10
75-
vpc_id = local.vpc_id
76-
subnet_id = local.subnet_id
77-
78-
tags = {
79-
tf = "test"
80-
}
81-
}
39+
const testAccTencentCloudDataPostgresqlInstanceBasic = CommonPresetPGSQL + `
8240
8341
data "tencentcloud_postgresql_instances" "id_test"{
84-
id = tencentcloud_postgresql_instance.test.id
42+
id = local.pgsql_id
8543
}
8644
`
Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
/*
2+
Provide a datasource to query PostgreSQL Xlogs.
3+
4+
Example Usage
5+
6+
```hcl
7+
data "tencentcloud_postgresql_xlogs" "foo" {
8+
instance_id = "postgres-xxxxxxxx"
9+
start_time = "2022-01-01 00:00:00"
10+
end_time = "2022-01-07 01:02:03"
11+
}
12+
```
13+
14+
15+
*/
16+
package tencentcloud
17+
18+
import (
19+
"context"
20+
"time"
21+
22+
postgresql "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/postgres/v20170312"
23+
24+
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
25+
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
26+
)
27+
28+
func datasourceTencentCloudPostgresqlXlogs() *schema.Resource {
29+
return &schema.Resource{
30+
Read: datasourceTencentCloudPostgresqlXlogsRead,
31+
Importer: &schema.ResourceImporter{
32+
State: schema.ImportStatePassthrough,
33+
},
34+
Schema: map[string]*schema.Schema{
35+
"instance_id": {
36+
Type: schema.TypeString,
37+
Required: true,
38+
Description: "PostgreSQL instance id.",
39+
},
40+
"start_time": {
41+
Type: schema.TypeString,
42+
Optional: true,
43+
Description: "Xlog start time, format `yyyy-MM-dd hh:mm:ss`, start time cannot before 7 days ago.",
44+
},
45+
"end_time": {
46+
Type: schema.TypeString,
47+
Optional: true,
48+
Description: "Xlog end time, format `yyyy-MM-dd hh:mm:ss`.",
49+
},
50+
"result_output_file": {
51+
Type: schema.TypeString,
52+
Optional: true,
53+
Description: "Used for save results.",
54+
},
55+
"list": {
56+
Type: schema.TypeList,
57+
Computed: true,
58+
Description: "List of Xlog query result.",
59+
Elem: &schema.Resource{
60+
Schema: map[string]*schema.Schema{
61+
"id": {
62+
Type: schema.TypeInt,
63+
Computed: true,
64+
Description: "Xlog id.",
65+
},
66+
"start_time": {
67+
Type: schema.TypeString,
68+
Computed: true,
69+
Description: "Xlog file created start time.",
70+
},
71+
"end_time": {
72+
Type: schema.TypeString,
73+
Computed: true,
74+
Description: "Xlog file created end time.",
75+
},
76+
"internal_addr": {
77+
Type: schema.TypeString,
78+
Computed: true,
79+
Description: "Xlog internal download address.",
80+
},
81+
"external_addr": {
82+
Type: schema.TypeString,
83+
Computed: true,
84+
Description: "Xlog external download address.",
85+
},
86+
"size": {
87+
Type: schema.TypeInt,
88+
Computed: true,
89+
Description: "Xlog file size.",
90+
},
91+
},
92+
},
93+
},
94+
},
95+
}
96+
}
97+
98+
func datasourceTencentCloudPostgresqlXlogsRead(d *schema.ResourceData, meta interface{}) error {
99+
defer logElapsed("datasource.tencentcloud_postgresql_xlogs.read")()
100+
defer inconsistentCheck(d, meta)()
101+
102+
logId := getLogId(contextNil)
103+
ctx := context.WithValue(context.TODO(), logIdKey, logId)
104+
client := meta.(*TencentCloudClient).apiV3Conn
105+
service := PostgresqlService{client}
106+
107+
request := postgresql.NewDescribeDBXlogsRequest()
108+
109+
id := d.Get("instance_id").(string)
110+
defaultEndTime := time.Now()
111+
112+
if endTime, ok := d.GetOk("end_time"); ok && endTime != "" {
113+
request.EndTime = helper.String(endTime.(string))
114+
} else {
115+
endTime := defaultEndTime.Format("2006-01-02 15:04:05")
116+
request.EndTime = &endTime
117+
}
118+
119+
if startTime, ok := d.GetOk("start_time"); ok && startTime != "" {
120+
request.StartTime = helper.String(startTime.(string))
121+
} else {
122+
defaultStartTime := defaultEndTime.AddDate(0, 0, -7)
123+
startTime := defaultStartTime.Format("2006-01-02 15:04:05")
124+
request.StartTime = &startTime
125+
}
126+
127+
request.DBInstanceId = &id
128+
129+
result, err := service.DescribeDBXlogs(ctx, request)
130+
131+
if err != nil {
132+
d.SetId("")
133+
return err
134+
}
135+
136+
list := make([]interface{}, 0, len(result))
137+
138+
for i := range result {
139+
item := result[i]
140+
xlog := map[string]interface{}{
141+
"id": item.Id,
142+
"start_time": item.StartTime,
143+
"end_time": item.EndTime,
144+
"internal_addr": item.InternalAddr,
145+
"external_addr": item.ExternalAddr,
146+
"size": item.Size,
147+
}
148+
149+
list = append(list, xlog)
150+
}
151+
152+
d.SetId("postgres-xlog-" + id)
153+
154+
if err := d.Set("list", list); err != nil {
155+
return err
156+
}
157+
158+
if output, ok := d.GetOk("result_output_file"); ok {
159+
return writeToFile(output.(string), list)
160+
}
161+
162+
return nil
163+
}
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+
"fmt"
5+
"testing"
6+
"time"
7+
8+
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
9+
)
10+
11+
func TestAccTencentCloudDataSourcePostgresqlXlogs(t *testing.T) {
12+
t.Parallel()
13+
14+
startTime := time.Now().AddDate(0, 0, -7).Format("2006-01-02 15:04:05")
15+
endTime := time.Now().Format("2006-01-02 15:04:05")
16+
resource.Test(t, resource.TestCase{
17+
PreCheck: func() { testAccPreCheck(t) },
18+
Providers: testAccProviders,
19+
Steps: []resource.TestStep{
20+
{
21+
Config: testAccDataSourcePostgresqlXlogsBasic(startTime, endTime),
22+
Check: resource.ComposeAggregateTestCheckFunc(
23+
resource.TestCheckResourceAttr("data.tencentcloud_postgresql_xlogs.foo", "start_time", startTime),
24+
resource.TestCheckResourceAttr("data.tencentcloud_postgresql_xlogs.foo", "end_time", endTime),
25+
resource.TestCheckResourceAttrSet("data.tencentcloud_postgresql_xlogs.foo", "list.#"),
26+
resource.TestCheckResourceAttrSet("data.tencentcloud_postgresql_xlogs.foo", "list.0.id"),
27+
resource.TestCheckResourceAttrSet("data.tencentcloud_postgresql_xlogs.foo", "list.0.start_time"),
28+
resource.TestCheckResourceAttrSet("data.tencentcloud_postgresql_xlogs.foo", "list.0.end_time"),
29+
resource.TestCheckResourceAttrSet("data.tencentcloud_postgresql_xlogs.foo", "list.0.internal_addr"),
30+
resource.TestCheckResourceAttrSet("data.tencentcloud_postgresql_xlogs.foo", "list.0.external_addr"),
31+
resource.TestCheckResourceAttrSet("data.tencentcloud_postgresql_xlogs.foo", "list.0.size"),
32+
resource.TestCheckResourceAttrSet("data.tencentcloud_postgresql_xlogs.bar", "list.#"),
33+
),
34+
},
35+
},
36+
})
37+
}
38+
39+
func testAccDataSourcePostgresqlXlogsBasic(startTime, endTime string) string {
40+
return fmt.Sprintf(`
41+
%s
42+
data "tencentcloud_postgresql_xlogs" "foo" {
43+
instance_id = local.pgsql_id
44+
start_time = "%s"
45+
end_time = "%s"
46+
}
47+
48+
data "tencentcloud_postgresql_xlogs" "bar" {
49+
instance_id = local.pgsql_id
50+
}
51+
`, CommonPresetPGSQL, startTime, endTime)
52+
}

tencentcloud/provider.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,7 @@ PostgreSQL
412412
Data Source
413413
tencentcloud_postgresql_instances
414414
tencentcloud_postgresql_specinfos
415+
tencentcloud_postgresql_xlogs
415416
416417
Resource
417418
tencentcloud_postgresql_instance
@@ -844,6 +845,7 @@ func Provider() terraform.ResourceProvider {
844845
"tencentcloud_elasticsearch_instances": dataSourceTencentCloudElasticsearchInstances(),
845846
"tencentcloud_postgresql_instances": dataSourceTencentCloudPostgresqlInstances(),
846847
"tencentcloud_postgresql_specinfos": dataSourceTencentCloudPostgresqlSpecinfos(),
848+
"tencentcloud_postgresql_xlogs": datasourceTencentCloudPostgresqlXlogs(),
847849
"tencentcloud_sqlserver_zone_config": dataSourceTencentSqlserverZoneConfig(),
848850
"tencentcloud_sqlserver_instances": dataSourceTencentCloudSqlserverInstances(),
849851
"tencentcloud_sqlserver_backups": dataSourceTencentCloudSqlserverBackups(),

0 commit comments

Comments
 (0)