Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/support dcdb feature #1865

Merged
merged 10 commits into from
Jun 7, 2023
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
19 changes: 19 additions & 0 deletions .changelog/1865.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
```release-note:new-resource
tencentcloud_dcdb_activate_hour_instance_operation
```

```release-note:new-resource
tencentcloud_dcdb_isolate_hour_instance_operation
```

```release-note:new-resource
tencentcloud_dcdb_cancel_dcn_job_operation
```

```release-note:enhancement
resource/tencentcloud_dcdb_hourdb_instance: support `dcn_region` and `dcn_instance_id` fields.
```

```release-note:enhancement
resource/tencentcloud_dcdb_db_instance: optimize the processing logic of dcn.
```
20 changes: 20 additions & 0 deletions tencentcloud/basic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -838,6 +838,26 @@ const (
defaultDcdbSGName = "default"
)

// ref with `local.dcdb_id`
const CommonPresetDcdb = `

variable "availability_zone" {
default = "` + defaultAZone + `"
}
variable "region" {
default = "` + defaultRegion + `"
}

data "tencentcloud_dcdb_instances" "dcdb" {
search_name = "instancename"
search_key = "` + defaultDcdbInstanceName + `"
}

locals {
dcdb_id = data.tencentcloud_dcdb_instances.dcdb.list.0.instance_id
}
`

// End of DCDB
// SES
const (
Expand Down
6 changes: 6 additions & 0 deletions tencentcloud/extension_dcdb.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package tencentcloud

const (
DCDB_DCN_FLAG_MASTER = 1
DCDB_DCN_FLAG_SLAVE = 2
)
6 changes: 6 additions & 0 deletions tencentcloud/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -1106,6 +1106,9 @@ TDSQL for MySQL(DCDB)
tencentcloud_dcdb_db_parameters
tencentcloud_dcdb_db_sync_mode_config
tencentcloud_dcdb_encrypt_attributes_config
tencentcloud_dcdb_cancel_dcn_job_operation
tencentcloud_dcdb_activate_hour_instance_operation
tencentcloud_dcdb_isolate_hour_instance_operation

Short Message Service(SMS)
Resource
Expand Down Expand Up @@ -2403,6 +2406,9 @@ func Provider() *schema.Provider {
"tencentcloud_dcdb_db_parameters": resourceTencentCloudDcdbDbParameters(),
"tencentcloud_dcdb_encrypt_attributes_config": resourceTencentCloudDcdbEncryptAttributesConfig(),
"tencentcloud_dcdb_db_sync_mode_config": resourceTencentCloudDcdbDbSyncModeConfig(),
"tencentcloud_dcdb_activate_hour_instance_operation": resourceTencentCloudDcdbActivateHourInstanceOperation(),
"tencentcloud_dcdb_isolate_hour_instance_operation": resourceTencentCloudDcdbIsolateHourInstanceOperation(),
"tencentcloud_dcdb_cancel_dcn_job_operation": resourceTencentCloudDcdbCancelDcnJobOperation(),
"tencentcloud_cat_task_set": resourceTencentCloudCatTaskSet(),
"tencentcloud_mariadb_dedicatedcluster_db_instance": resourceTencentCloudMariadbDedicatedclusterDbInstance(),
"tencentcloud_mariadb_instance": resourceTencentCloudMariadbInstance(),
Expand Down
31 changes: 30 additions & 1 deletion tencentcloud/resource_tc_dcdb_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"fmt"
"log"
"strings"
"time"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
Expand Down Expand Up @@ -241,7 +242,30 @@ func resourceTencentCloudDcdbAccountUpdate(d *schema.ResourceData, meta interfac
}

if d.HasChange("password") {
return fmt.Errorf("`password` do not support change now.")
// return fmt.Errorf("`password` do not support change now.")
if v, ok := d.GetOk("password"); ok {
request := dcdb.NewResetAccountPasswordRequest()
request.InstanceId = &instanceId
request.UserName = &userName
if v, ok := d.GetOk("host"); ok {
request.Host = helper.String(v.(string))
}
request.Password = helper.String(v.(string))

err := resource.Retry(writeRetryTimeout, func() *resource.RetryError {
result, e := meta.(*TencentCloudClient).apiV3Conn.UseDcdbClient().ResetAccountPassword(request)
if e != nil {
return retryError(e)
} else {
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString())
}
return nil
})
if err != nil {
log.Printf("[CRITAL]%s operate dcdb resetAccountPasswordOperation failed, reason:%+v", logId, err)
return err
}
}
}

if d.HasChange("read_only") {
Expand Down Expand Up @@ -307,5 +331,10 @@ func resourceTencentCloudDcdbAccountDelete(d *schema.ResourceData, meta interfac
return err
}

conf := BuildStateChangeConf([]string{}, []string{"deleted"}, readRetryTimeout, time.Second, service.DcdbAccountRefreshFunc(instanceId, userName, []string{}))
if _, e := conf.WaitForState(); e != nil {
return e
}

return nil
}
22 changes: 12 additions & 10 deletions tencentcloud/resource_tc_dcdb_account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"strings"
"testing"
"time"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
Expand Down Expand Up @@ -47,20 +48,20 @@ func testSweepDCDBAccount(r string) error {
return nil
}

func TestAcc_NOT_Ready_TencentCloudDCDBAccountResource(t *testing.T) {
func TestAccTencentCloudDcdbAccountResource_basic(t *testing.T) {
t.Parallel()

timestamp := time.Now().Nanosecond()
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckDcdbAccountDestroy,
Steps: []resource.TestStep{
{
Config: fmt.Sprintf(testAccDcdbAccount_basic, defaultDcdbInstanceId),
Config: fmt.Sprintf(testAccDcdbAccount_basic, defaultDcdbInstanceId, timestamp),
Check: resource.ComposeTestCheckFunc(
testAccCheckDcdbAccountExists("tencentcloud_dcdb_account.basic"),
resource.TestCheckResourceAttrSet("tencentcloud_dcdb_account.basic", "instance_id"),
resource.TestCheckResourceAttr("tencentcloud_dcdb_account.basic", "user_name", "mysql"),
resource.TestCheckResourceAttrSet("tencentcloud_dcdb_account.basic", "user_name"),
resource.TestCheckResourceAttr("tencentcloud_dcdb_account.basic", "host", "127.0.0.1"),
resource.TestCheckResourceAttr("tencentcloud_dcdb_account.basic", "password", "===password==="),
resource.TestCheckResourceAttr("tencentcloud_dcdb_account.basic", "description", "this is a test account"),
Expand All @@ -69,12 +70,13 @@ func TestAcc_NOT_Ready_TencentCloudDCDBAccountResource(t *testing.T) {
),
},
{
Config: fmt.Sprintf(testAccDcdbAccount_update, defaultDcdbInstanceId),
Config: fmt.Sprintf(testAccDcdbAccount_update, defaultDcdbInstanceId, timestamp),
Check: resource.ComposeTestCheckFunc(
testAccCheckDcdbAccountExists("tencentcloud_dcdb_account.basic"),
resource.TestCheckResourceAttrSet("tencentcloud_dcdb_account.basic", "instance_id"),
resource.TestCheckResourceAttr("tencentcloud_dcdb_account.basic", "user_name", "mysql"),
resource.TestCheckResourceAttrSet("tencentcloud_dcdb_account.basic", "user_name"),
resource.TestCheckResourceAttr("tencentcloud_dcdb_account.basic", "host", "127.0.0.1"),
resource.TestCheckResourceAttr("tencentcloud_dcdb_account.basic", "password", "===password===updated==="),
resource.TestCheckResourceAttr("tencentcloud_dcdb_account.basic", "description", "this is a changed test account"),
resource.TestCheckResourceAttr("tencentcloud_dcdb_account.basic", "read_only", "0"),
),
Expand Down Expand Up @@ -104,7 +106,7 @@ func testAccCheckDcdbAccountDestroy(s *terraform.State) error {
if err != nil {
return err
}
if account.Users != nil && len(account.Users) > 0 {
if account != nil && len(account.Users) > 0 {
return fmt.Errorf("dcdb account still exists: %s", rs.Primary.ID)
}
}
Expand Down Expand Up @@ -143,7 +145,7 @@ const testAccDcdbAccount_basic = `

resource "tencentcloud_dcdb_account" "basic" {
instance_id = "%s"
user_name = "mysql"
user_name = "mysql_%d"
host = "127.0.0.1"
password = "===password==="
read_only = 0
Expand All @@ -156,9 +158,9 @@ const testAccDcdbAccount_update = `

resource "tencentcloud_dcdb_account" "basic" {
instance_id = "%s"
user_name = "mysql"
user_name = "mysql_%d"
host = "127.0.0.1"
password = "===password==="
password = "===password===updated==="
read_only = 0
description = "this is a changed test account"
max_user_connections = 10
Expand Down
85 changes: 85 additions & 0 deletions tencentcloud/resource_tc_dcdb_activate_hour_instance_operation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
Provides a resource to create a dcdb activate_hour_instance_operation

Example Usage

```hcl
resource "tencentcloud_dcdb_activate_hour_instance_operation" "activate_hour_instance_operation" {
instance_id = local.dcdb_id
}
```
*/
package tencentcloud

import (
"log"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
dcdb "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/dcdb/v20180411"
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
)

func resourceTencentCloudDcdbActivateHourInstanceOperation() *schema.Resource {
return &schema.Resource{
Create: resourceTencentCloudDcdbActivateHourInstanceOperationCreate,
Read: resourceTencentCloudDcdbActivateHourInstanceOperationRead,
Delete: resourceTencentCloudDcdbActivateHourInstanceOperationDelete,
Schema: map[string]*schema.Schema{
"instance_id": {
Required: true,
ForceNew: true,
Type: schema.TypeString,
Description: "instance ID in the format of dcdbt-ow728lmc, which can be obtained through the `DescribeDCDBInstances` API.",
},
},
}
}

func resourceTencentCloudDcdbActivateHourInstanceOperationCreate(d *schema.ResourceData, meta interface{}) error {
defer logElapsed("resource.tencentcloud_dcdb_activate_hour_instance_operation.create")()
defer inconsistentCheck(d, meta)()

logId := getLogId(contextNil)

var (
request = dcdb.NewActiveHourDCDBInstanceRequest()
instanceId string
)
if v, ok := d.GetOk("instance_id"); ok {
instanceId = v.(string)
request.InstanceIds = []*string{helper.String(instanceId)}
}

err := resource.Retry(writeRetryTimeout, func() *resource.RetryError {
result, e := meta.(*TencentCloudClient).apiV3Conn.UseDcdbClient().ActiveHourDCDBInstance(request)
if e != nil {
return retryError(e)
} else {
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString())
}
return nil
})
if err != nil {
log.Printf("[CRITAL]%s operate dcdb activateHourInstanceOperation failed, reason:%+v", logId, err)
return err
}

d.SetId(instanceId)

return resourceTencentCloudDcdbActivateHourInstanceOperationRead(d, meta)
}

func resourceTencentCloudDcdbActivateHourInstanceOperationRead(d *schema.ResourceData, meta interface{}) error {
defer logElapsed("resource.tencentcloud_dcdb_activate_hour_instance_operation.read")()
defer inconsistentCheck(d, meta)()

return nil
}

func resourceTencentCloudDcdbActivateHourInstanceOperationDelete(d *schema.ResourceData, meta interface{}) error {
defer logElapsed("resource.tencentcloud_dcdb_activate_hour_instance_operation.delete")()
defer inconsistentCheck(d, meta)()

return nil
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package tencentcloud

import (
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)

func TestAccTencentCloudDcdbActivateHourInstanceOperationResource_basic(t *testing.T) {
t.Parallel()
resource.Test(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
},
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: fmt.Sprintf(testAccDcdbIsolateHourInstanceOperation, "oper_isolate"),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet("tencentcloud_dcdb_isolate_hour_instance_operation.isolate_operation", "id"),
resource.TestCheckResourceAttrSet("tencentcloud_dcdb_isolate_hour_instance_operation.isolate_operation", "instance_id"),
),
},
{
Config: fmt.Sprintf(testAccDcdbActivateHourInstanceOperation, "oper_isolate"),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet("tencentcloud_dcdb_activate_hour_instance_operation.activate_operation", "id"),
resource.TestCheckResourceAttrSet("tencentcloud_dcdb_activate_hour_instance_operation.activate_operation", "instance_id"),
),
},
},
})
}

const testAccDcdbHourInsOperation = defaultAzVariable + `
data "tencentcloud_security_groups" "internal" {
name = "default"
}

data "tencentcloud_vpc_instances" "vpc" {
name ="Default-VPC"
}

data "tencentcloud_vpc_subnets" "subnet" {
vpc_id = data.tencentcloud_vpc_instances.vpc.instance_list.0.vpc_id
}

locals {
vpc_id = data.tencentcloud_vpc_subnets.subnet.instance_list.0.vpc_id
subnet_id = data.tencentcloud_vpc_subnets.subnet.instance_list.0.subnet_id
sg_id = data.tencentcloud_security_groups.internal.security_groups.0.security_group_id
}

resource "tencentcloud_dcdb_hourdb_instance" "hourdb_instance" {
instance_name = "test_dcdb_db_hourdb_instance_%s"
zones = [var.default_az]
shard_memory = "2"
shard_storage = "10"
shard_node_count = "2"
shard_count = "2"
vpc_id = local.vpc_id
subnet_id = local.subnet_id
security_group_id = local.sg_id
db_version_id = "8.0"
resource_tags {
tag_key = "aaa"
tag_value = "bbb"
}
}

locals {
dcdb_id = tencentcloud_dcdb_hourdb_instance.hourdb_instance.id
}
`

const testAccDcdbActivateHourInstanceOperation = testAccDcdbHourInsOperation + `

resource "tencentcloud_dcdb_activate_hour_instance_operation" "activate_operation" {
instance_id = local.dcdb_id
}

`

const testAccDcdbIsolateHourInstanceOperation = testAccDcdbHourInsOperation + `

resource "tencentcloud_dcdb_isolate_hour_instance_operation" "isolate_operation" {
instance_id = local.dcdb_id
}

`
Loading