@@ -3,6 +3,7 @@ package tencentcloud
33import (
44 "context"
55 "fmt"
6+ "strings"
67 "testing"
78
89 "github.com/hashicorp/terraform-plugin-sdk/helper/resource"
@@ -12,6 +13,67 @@ import (
1213var testPostgresqlInstanceResourceName = "tencentcloud_postgresql_instance"
1314var testPostgresqlInstanceResourceKey = testPostgresqlInstanceResourceName + ".test"
1415
16+ func init () {
17+ resource .AddTestSweepers (testPostgresqlInstanceResourceName , & resource.Sweeper {
18+ Name : testPostgresqlInstanceResourceName ,
19+ F : func (r string ) error {
20+ logId := getLogId (contextNil )
21+ ctx := context .WithValue (context .TODO (), logIdKey , logId )
22+ cli , _ := sharedClientForRegion (r )
23+ client := cli .(* TencentCloudClient ).apiV3Conn
24+ postgresqlService := PostgresqlService {client : client }
25+ vpcService := VpcService {client : client }
26+
27+ instances , err := postgresqlService .DescribePostgresqlInstances (ctx , nil )
28+ if err != nil {
29+ return err
30+ }
31+
32+ var vpcs []string
33+
34+ for _ , v := range instances {
35+ id := * v .DBInstanceId
36+ name := * v .DBInstanceName
37+ vpcId := * v .VpcId
38+ if strings .HasPrefix (name , keepResource ) || strings .HasPrefix (name , defaultResource ) {
39+ continue
40+ }
41+ err := postgresqlService .IsolatePostgresqlInstance (ctx , id )
42+ if err != nil {
43+ continue
44+ }
45+ err = resource .Retry (readRetryTimeout , func () * resource.RetryError {
46+ instance , has , err := postgresqlService .DescribePostgresqlInstanceById (ctx , id )
47+ if err != nil {
48+ return retryError (err )
49+ }
50+ if ! has {
51+ return resource .NonRetryableError (fmt .Errorf ("instance %s removed" , id ))
52+ }
53+ if * instance .DBInstanceStatus != "isolated" {
54+ return resource .RetryableError (fmt .Errorf ("waiting for instance isolated, now is %s" , * instance .DBInstanceStatus ))
55+ }
56+ return nil
57+ })
58+ if err != nil {
59+ continue
60+ }
61+ err = postgresqlService .DeletePostgresqlInstance (ctx , id )
62+ if err != nil {
63+ continue
64+ }
65+ vpcs = append (vpcs , vpcId )
66+ }
67+
68+ for _ , v := range vpcs {
69+ _ = vpcService .DeleteVpc (ctx , v )
70+ }
71+
72+ return nil
73+ },
74+ })
75+ }
76+
1577func TestAccTencentCloudPostgresqlInstanceResource (t * testing.T ) {
1678 t .Parallel ()
1779 resource .Test (t , resource.TestCase {
@@ -37,7 +99,7 @@ func TestAccTencentCloudPostgresqlInstanceResource(t *testing.T) {
3799 resource .TestCheckResourceAttrSet (testPostgresqlInstanceResourceKey , "availability_zone" ),
38100 resource .TestCheckResourceAttrSet (testPostgresqlInstanceResourceKey , "private_access_ip" ),
39101 resource .TestCheckResourceAttrSet (testPostgresqlInstanceResourceKey , "private_access_port" ),
40- resource .TestCheckResourceAttr (testPostgresqlInstanceResourceKey , "tags.tf" , "test" ),
102+ // resource.TestCheckResourceAttr(testPostgresqlInstanceResourceKey, "tags.tf", "test"),
41103 ),
42104 },
43105 {
@@ -68,7 +130,7 @@ func TestAccTencentCloudPostgresqlInstanceResource(t *testing.T) {
68130 resource .TestCheckResourceAttrSet (testPostgresqlInstanceResourceKey , "private_access_port" ),
69131 resource .TestCheckResourceAttrSet (testPostgresqlInstanceResourceKey , "public_access_host" ),
70132 resource .TestCheckResourceAttrSet (testPostgresqlInstanceResourceKey , "public_access_port" ),
71- resource .TestCheckResourceAttr (testPostgresqlInstanceResourceKey , "tags.tf" , "teest" ),
133+ // resource.TestCheckResourceAttr(testPostgresqlInstanceResourceKey, "tags.tf", "teest"),
72134 ),
73135 },
74136 },
@@ -105,7 +167,7 @@ func TestAccTencentCloudPostgresqlMAZInstanceResource(t *testing.T) {
105167 testAccCheckPostgresqlInstanceExists (testPostgresqlInstanceResourceKey ),
106168 resource .TestCheckResourceAttrSet (testPostgresqlInstanceResourceKey , "id" ),
107169 resource .TestCheckResourceAttr (testPostgresqlInstanceResourceKey , "db_node_set.#" , "2" ),
108- resource .TestCheckResourceAttr (testPostgresqlInstanceResourceKey , "availability_zone" , "ap-guangzhou-7 " ),
170+ resource .TestCheckResourceAttr (testPostgresqlInstanceResourceKey , "availability_zone" , "ap-guangzhou-6 " ),
109171 ),
110172 },
111173 },
@@ -240,10 +302,6 @@ resource "tencentcloud_postgresql_instance" "test" {
240302 db_node_set {
241303 zone = "ap-guangzhou-7"
242304 }
243-
244- tags = {
245- tf = "test"
246- }
247305}
248306`
249307
@@ -254,7 +312,7 @@ resource "tencentcloud_vpc" "vpc" {
254312}
255313
256314resource "tencentcloud_subnet" "subnet" {
257- availability_zone = "ap-guangzhou-7 "
315+ availability_zone = "ap-guangzhou-6 "
258316 cidr_block = "10.0.0.0/24"
259317 name = "pg-sub1"
260318 vpc_id = tencentcloud_vpc.vpc.id
@@ -269,19 +327,14 @@ resource "tencentcloud_postgresql_instance" "test" {
269327 engine_version = "10.4"
270328 root_password = "t1qaA2k1wgvfa3?ZZZ"
271329 charset = "LATIN1"
272- public_access_switch = true
273330 memory = 4
274331 storage = 250
275332 db_node_set {
333+ role = "Primary"
276334 zone = "ap-guangzhou-6"
277335 }
278336 db_node_set {
279- role = "Primary"
280- zone = "ap-guangzhou-7"
337+ zone = "ap-guangzhou-6"
281338 }
282-
283- tags = {
284- tf = "teest"
285- }
286339}
287340`
0 commit comments