Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion tencentcloud/basic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ var appid string = os.Getenv("TENCENTCLOUD_APPID")
var ownerUin string = os.Getenv("TENCENTCLOUD_OWNER_UIN")

const (
keepResource = "keep"
keepResource = "keep"
defaultResource = "Default"
)

Expand Down
4 changes: 2 additions & 2 deletions tencentcloud/extension_postgresql.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ const (
POSTGRESQL_DB_CHARSET_LATIN1 = "LATIN1"
)

var POSTSQL_DB_CHARSET = []string{POSTGRESQL_DB_CHARSET_UTF8, POSTGRESQL_DB_CHARSET_LATIN1}
var POSTGRESQL_DB_CHARSET = []string{POSTGRESQL_DB_CHARSET_UTF8, POSTGRESQL_DB_CHARSET_LATIN1}

const (
POSTGRESQL_STAUTS_RUNNING = "running"
)

var POSTGRES_RETRYABLE_STATUS = []string{
var POSTGRESQL_RETRYABLE_STATUS = []string{
"initing",
"expanding",
"switching",
Expand Down
31 changes: 26 additions & 5 deletions tencentcloud/resource_tc_postgresql_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ func resourceTencentCloudPostgresqlInstance() *schema.Resource {
Optional: true,
Default: POSTGRESQL_DB_CHARSET_UTF8,
ForceNew: true,
ValidateFunc: validateAllowedStringValue(POSTSQL_DB_CHARSET),
ValidateFunc: validateAllowedStringValue(POSTGRESQL_DB_CHARSET),
Description: "Charset of the root account. Valid values are `UTF8`,`LATIN1`.",
},
"public_access_switch": {
Expand Down Expand Up @@ -337,7 +337,7 @@ func resourceTencentCloudPostgresqlInstanceCreate(d *schema.ResourceData, meta i
securityGroups = d.Get("security_groups").(*schema.Set).List()
zone = d.Get("availability_zone").(string)
storage = d.Get("storage").(int)
memory = d.Get("memory").(int)
memory = d.Get("memory").(int) // Memory only used for query specCode which contains memory info
username = d.Get("root_user").(string)
password = d.Get("root_password").(string)
charset = d.Get("charset").(string)
Expand Down Expand Up @@ -706,11 +706,32 @@ func resourceTencentCloudPostgresqlInstanceUpdate(d *schema.ResourceData, meta i
})
}

if err := postgresqlService.ModifyDBInstanceDeployment(ctx, request); err != nil {
err := resource.Retry(writeRetryTimeout, func() *resource.RetryError {
if err := postgresqlService.ModifyDBInstanceDeployment(ctx, request); err != nil {
return retryError(err, postgresql.OPERATIONDENIED_INSTANCESTATUSLIMITOPERROR)
}
return nil
})

if err != nil {
return err
}

err = resource.Retry(readRetryTimeout*10, func() *resource.RetryError {
instance, _, err := postgresqlService.DescribePostgresqlInstanceById(ctx, d.Id())
if err != nil {
return retryError(err)
}
if IsContains(POSTGRESQL_RETRYABLE_STATUS, *instance.DBInstanceStatus) {
return resource.RetryableError(fmt.Errorf("instance status is %s, retrying", *instance.DBInstanceStatus))
}
return nil
})

if err != nil {
return err
}

d.SetPartial("db_node_set")
}

if d.HasChange("zone") {
Expand Down Expand Up @@ -789,7 +810,7 @@ func resourceTencentCloudPostgresqlInstanceRead(d *schema.ResourceData, meta int
}
return retryError(inErr)
}
if IsContains(POSTGRES_RETRYABLE_STATUS, *instance.DBInstanceStatus) {
if IsContains(POSTGRESQL_RETRYABLE_STATUS, *instance.DBInstanceStatus) {
return resource.RetryableError(fmt.Errorf("instance %s is %s, retrying", *instance.DBInstanceId, *instance.DBInstanceStatus))
}
return nil
Expand Down
83 changes: 68 additions & 15 deletions tencentcloud/resource_tc_postgresql_instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package tencentcloud
import (
"context"
"fmt"
"strings"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
Expand All @@ -12,6 +13,67 @@ import (
var testPostgresqlInstanceResourceName = "tencentcloud_postgresql_instance"
var testPostgresqlInstanceResourceKey = testPostgresqlInstanceResourceName + ".test"

func init() {
resource.AddTestSweepers(testPostgresqlInstanceResourceName, &resource.Sweeper{
Name: testPostgresqlInstanceResourceName,
F: func(r string) error {
logId := getLogId(contextNil)
ctx := context.WithValue(context.TODO(), logIdKey, logId)
cli, _ := sharedClientForRegion(r)
client := cli.(*TencentCloudClient).apiV3Conn
postgresqlService := PostgresqlService{client: client}
vpcService := VpcService{client: client}

instances, err := postgresqlService.DescribePostgresqlInstances(ctx, nil)
if err != nil {
return err
}

var vpcs []string

for _, v := range instances {
id := *v.DBInstanceId
name := *v.DBInstanceName
vpcId := *v.VpcId
if strings.HasPrefix(name, keepResource) || strings.HasPrefix(name, defaultResource) {
continue
}
err := postgresqlService.IsolatePostgresqlInstance(ctx, id)
if err != nil {
continue
}
err = resource.Retry(readRetryTimeout, func() *resource.RetryError {
instance, has, err := postgresqlService.DescribePostgresqlInstanceById(ctx, id)
if err != nil {
return retryError(err)
}
if !has {
return resource.NonRetryableError(fmt.Errorf("instance %s removed", id))
}
if *instance.DBInstanceStatus != "isolated" {
return resource.RetryableError(fmt.Errorf("waiting for instance isolated, now is %s", *instance.DBInstanceStatus))
}
return nil
})
if err != nil {
continue
}
err = postgresqlService.DeletePostgresqlInstance(ctx, id)
if err != nil {
continue
}
vpcs = append(vpcs, vpcId)
}

for _, v := range vpcs {
_ = vpcService.DeleteVpc(ctx, v)
}

return nil
},
})
}

func TestAccTencentCloudPostgresqlInstanceResource(t *testing.T) {
t.Parallel()
resource.Test(t, resource.TestCase{
Expand All @@ -37,7 +99,7 @@ func TestAccTencentCloudPostgresqlInstanceResource(t *testing.T) {
resource.TestCheckResourceAttrSet(testPostgresqlInstanceResourceKey, "availability_zone"),
resource.TestCheckResourceAttrSet(testPostgresqlInstanceResourceKey, "private_access_ip"),
resource.TestCheckResourceAttrSet(testPostgresqlInstanceResourceKey, "private_access_port"),
resource.TestCheckResourceAttr(testPostgresqlInstanceResourceKey, "tags.tf", "test"),
//resource.TestCheckResourceAttr(testPostgresqlInstanceResourceKey, "tags.tf", "test"),
),
},
{
Expand Down Expand Up @@ -68,7 +130,7 @@ func TestAccTencentCloudPostgresqlInstanceResource(t *testing.T) {
resource.TestCheckResourceAttrSet(testPostgresqlInstanceResourceKey, "private_access_port"),
resource.TestCheckResourceAttrSet(testPostgresqlInstanceResourceKey, "public_access_host"),
resource.TestCheckResourceAttrSet(testPostgresqlInstanceResourceKey, "public_access_port"),
resource.TestCheckResourceAttr(testPostgresqlInstanceResourceKey, "tags.tf", "teest"),
//resource.TestCheckResourceAttr(testPostgresqlInstanceResourceKey, "tags.tf", "teest"),
),
},
},
Expand Down Expand Up @@ -105,7 +167,7 @@ func TestAccTencentCloudPostgresqlMAZInstanceResource(t *testing.T) {
testAccCheckPostgresqlInstanceExists(testPostgresqlInstanceResourceKey),
resource.TestCheckResourceAttrSet(testPostgresqlInstanceResourceKey, "id"),
resource.TestCheckResourceAttr(testPostgresqlInstanceResourceKey, "db_node_set.#", "2"),
resource.TestCheckResourceAttr(testPostgresqlInstanceResourceKey, "availability_zone", "ap-guangzhou-7"),
resource.TestCheckResourceAttr(testPostgresqlInstanceResourceKey, "availability_zone", "ap-guangzhou-6"),
),
},
},
Expand Down Expand Up @@ -240,10 +302,6 @@ resource "tencentcloud_postgresql_instance" "test" {
db_node_set {
zone = "ap-guangzhou-7"
}

tags = {
tf = "test"
}
}
`

Expand All @@ -254,7 +312,7 @@ resource "tencentcloud_vpc" "vpc" {
}

resource "tencentcloud_subnet" "subnet" {
availability_zone = "ap-guangzhou-7"
availability_zone = "ap-guangzhou-6"
cidr_block = "10.0.0.0/24"
name = "pg-sub1"
vpc_id = tencentcloud_vpc.vpc.id
Expand All @@ -269,19 +327,14 @@ resource "tencentcloud_postgresql_instance" "test" {
engine_version = "10.4"
root_password = "t1qaA2k1wgvfa3?ZZZ"
charset = "LATIN1"
public_access_switch = true
memory = 4
storage = 250
db_node_set {
role = "Primary"
zone = "ap-guangzhou-6"
}
db_node_set {
role = "Primary"
zone = "ap-guangzhou-7"
zone = "ap-guangzhou-6"
}

tags = {
tf = "teest"
}
}
`