Skip to content

feat(cdwpg): [122485020]add resource #3236

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

Merged
merged 7 commits into from
Mar 28, 2025
Merged
Show file tree
Hide file tree
Changes from 6 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
11 changes: 11 additions & 0 deletions .changelog/3236.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
```release-note:new-resource
tencentcloud_cdwpg_reset_account_password
```

```release-note:new-resource
tencentcloud_cdwpg_restart_instance
```

```release-note:new-resource
tencentcloud_cdwpg_userhba
```
3 changes: 3 additions & 0 deletions tencentcloud/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -2224,6 +2224,9 @@ func Provider() *schema.Provider {
"tencentcloud_bi_embed_interval_apply": bi.ResourceTencentCloudBiEmbedIntervalApply(),
"tencentcloud_cdwpg_instance": cdwpg.ResourceTencentCloudCdwpgInstance(),
"tencentcloud_cdwpg_dbconfig": cdwpg.ResourceTencentCloudCdwpgDbconfig(),
"tencentcloud_cdwpg_reset_account_password": cdwpg.ResourceTencentCloudCdwpgResetAccountPassword(),
"tencentcloud_cdwpg_restart_instance": cdwpg.ResourceTencentCloudCdwpgRestartInstance(),
"tencentcloud_cdwpg_userhba": cdwpg.ResourceTencentCloudCdwpgUserhba(),
"tencentcloud_clickhouse_keyval_config": cdwch.ResourceTencentCloudClickhouseKeyvalConfig(),
"tencentcloud_clickhouse_xml_config": cdwch.ResourceTencentCloudClickhouseXmlConfig(),
"tencentcloud_clb_target_group_attachments": clb.ResourceTencentCloudClbTargetGroupAttachments(),
Expand Down
3 changes: 3 additions & 0 deletions tencentcloud/provider.md
Original file line number Diff line number Diff line change
Expand Up @@ -2253,6 +2253,9 @@ tencentcloud_cdwpg_nodes
Resource
tencentcloud_cdwpg_instance
tencentcloud_cdwpg_dbconfig
tencentcloud_cdwpg_reset_account_password
tencentcloud_cdwpg_restart_instance
tencentcloud_cdwpg_userhba

CSIP
Resource
Expand Down
40 changes: 40 additions & 0 deletions tencentcloud/services/cdwpg/resource_tc_cdwpg_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,11 @@ func ResourceTencentCloudCdwpgInstance() *schema.Resource {
Optional: true,
Description: "Tag description list.",
},
"product_version": {
Optional: true,
Type: schema.TypeString,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

computed

Description: "Version.",
},
},
}
}
Expand Down Expand Up @@ -224,6 +229,9 @@ func resourceTencentCloudCdwpgInstanceCreate(d *schema.ResourceData, meta interf
request.Resources = append(request.Resources, &resourceSpecNew)
}
}
if v, ok := d.GetOk("product_version"); ok {
request.ProductVersion = helper.String(v.(string))
}

err := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError {
result, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseCdwpgClient().CreateInstanceByApi(request)
Expand Down Expand Up @@ -366,6 +374,10 @@ func resourceTencentCloudCdwpgInstanceRead(d *schema.ResourceData, meta interfac

}

if instance.Version != nil {
_ = d.Set("product_version", instance.Version)
}

tcClient := meta.(tccommon.ProviderMeta).GetAPIV3Conn()
tagService := svctag.NewTagService(tcClient)
tags, err := tagService.DescribeResourceTags(ctx, "cdwpg", "cdwpgInstance", tcClient.Region, d.Id())
Expand Down Expand Up @@ -489,6 +501,34 @@ func resourceTencentCloudCdwpgInstanceUpdate(d *schema.ResourceData, meta interf
}
}
}
if d.HasChange("product_version") {
if v, ok := d.GetOk("product_version"); ok {
request := cdwpg.NewUpgradeInstanceRequest()
request.InstanceId = helper.String(instanceId)
request.PackageVersion = helper.String(v.(string))
err := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError {
result, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseCdwpgV20201230Client().UpgradeInstance(request)
if e != nil {
return tccommon.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 create cdwpg upgrade instance failed, reason:%+v", logId, err)
return err
}

service := CdwpgService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()}
conf := tccommon.BuildStateChangeConf([]string{}, []string{"Serving"}, 10*tccommon.ReadRetryTimeout, time.Second, service.InstanceStateRefreshFunc(instanceId, []string{}))

if _, e := conf.WaitForState(); e != nil {
return e
}
}
}

return resourceTencentCloudCdwpgInstanceRead(d, meta)
}

Expand Down
116 changes: 116 additions & 0 deletions tencentcloud/services/cdwpg/resource_tc_cdwpg_instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,36 @@ func TestAccTencentCloudCdwpgInstanceResource_basic(t *testing.T) {
})
}

func TestAccTencentCloudCdwpgInstanceResource_withVersion(t *testing.T) {
t.Parallel()
resource.Test(t, resource.TestCase{
PreCheck: func() { tcacctest.AccPreCheckCommon(t, tcacctest.ACCOUNT_TYPE_PREPAY) },
Providers: tcacctest.AccProviders,
Steps: []resource.TestStep{
{
Config: testAccCdwpgInstanceWithVersion,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet("tencentcloud_cdwpg_instance.instance", "id"),
resource.TestCheckResourceAttr("tencentcloud_cdwpg_instance.instance", "product_version", "3.16.9.3"),
),
},
{
Config: testAccCdwpgInstanceWithVersionUpdate,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet("tencentcloud_cdwpg_instance.instance", "id"),
resource.TestCheckResourceAttr("tencentcloud_cdwpg_instance.instance", "product_version", "3.16.9.4"),
),
},
{
ResourceName: "tencentcloud_cdwpg_instance.instance",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"admin_password"},
},
},
})
}

const testAccCdwpgInstance = `
resource "tencentcloud_cdwpg_instance" "instance" {
instance_name = "test_pg"
Expand Down Expand Up @@ -177,3 +207,89 @@ resource "tencentcloud_cdwpg_instance" "instance" {
}
}
`

const testAccCdwpgInstanceWithVersion = `
resource "tencentcloud_cdwpg_instance" "instance" {
instance_name = "test_pg"
zone = "ap-guangzhou-6"
user_vpc_id = "vpc-axrsmmrv"
user_subnet_id = "subnet-kxaxknmg"
charge_properties {
renew_flag = 0
time_span = 1
time_unit = "h"
charge_type = "POSTPAID_BY_HOUR"

}
admin_password = "bWJSZDVtVmZkNExJ"
resources {
spec_name = "S_4_16_H_CN"
count = 2
disk_spec {
disk_type = "CLOUD_HSSD"
disk_size = 200
disk_count = 1
}
type = "cn"

}
resources {
spec_name = "S_4_16_H_CN"
count = 2
disk_spec {
disk_type = "CLOUD_HSSD"
disk_size = 20
disk_count = 10
}
type = "dn"

}
tags = {
"tagKey" = "tagValue"
}
product_version = "3.16.9.3"
}
`

const testAccCdwpgInstanceWithVersionUpdate = `
resource "tencentcloud_cdwpg_instance" "instance" {
instance_name = "test_pg"
zone = "ap-guangzhou-6"
user_vpc_id = "vpc-axrsmmrv"
user_subnet_id = "subnet-kxaxknmg"
charge_properties {
renew_flag = 0
time_span = 1
time_unit = "h"
charge_type = "POSTPAID_BY_HOUR"

}
admin_password = "bWJSZDVtVmZkNExJ"
resources {
spec_name = "S_4_16_H_CN"
count = 2
disk_spec {
disk_type = "CLOUD_HSSD"
disk_size = 200
disk_count = 1
}
type = "cn"

}
resources {
spec_name = "S_4_16_H_CN"
count = 2
disk_spec {
disk_type = "CLOUD_HSSD"
disk_size = 20
disk_count = 10
}
type = "dn"

}
tags = {
"tagKey" = "tagValue"
}
product_version = "3.16.9.4"
}
`
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
package cdwpg

import (
"context"
"fmt"
"log"
"strings"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
cdwpgv20201230 "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cdwpg/v20201230"

tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common"
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
)

func ResourceTencentCloudCdwpgResetAccountPassword() *schema.Resource {
return &schema.Resource{
Create: resourceTencentCloudCdwpgResetAccountPasswordCreate,
Read: resourceTencentCloudCdwpgResetAccountPasswordRead,
Update: resourceTencentCloudCdwpgResetAccountPasswordUpdate,
Delete: resourceTencentCloudCdwpgResetAccountPasswordDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"instance_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "Instance id.",
},

"user_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "Username.",
},

"new_password": {
Type: schema.TypeString,
Required: true,
Sensitive: true,
Description: "New password.",
},
},
}
}

func resourceTencentCloudCdwpgResetAccountPasswordCreate(d *schema.ResourceData, meta interface{}) error {
defer tccommon.LogElapsed("resource.tencentcloud_cdwpg_reset_account_password.create")()
defer tccommon.InconsistentCheck(d, meta)()

instanceId := d.Get("instance_id").(string)
userName := d.Get("user_name").(string)

d.SetId(strings.Join([]string{instanceId, userName}, tccommon.FILED_SP))

return resourceTencentCloudCdwpgResetAccountPasswordUpdate(d, meta)
}

func resourceTencentCloudCdwpgResetAccountPasswordRead(d *schema.ResourceData, meta interface{}) error {
defer tccommon.LogElapsed("resource.tencentcloud_cdwpg_reset_account_password.read")()
defer tccommon.InconsistentCheck(d, meta)()

logId := tccommon.GetLogId(tccommon.ContextNil)

ctx := tccommon.NewResourceLifeCycleHandleFuncContext(context.Background(), logId, d, meta)

service := CdwpgService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()}

idSplit := strings.Split(d.Id(), tccommon.FILED_SP)
if len(idSplit) != 2 {
return fmt.Errorf("id is broken,%s", d.Id())
}
instanceId := idSplit[0]
userName := idSplit[1]

respData, err := service.DescribeCdwpgAccountById(ctx, instanceId)
if err != nil {
return err
}

if respData == nil {
d.SetId("")
log.Printf("[WARN]%s resource `cdwpg_account` [%s] not found, please check if it has been deleted.\n", logId, d.Id())
return nil
}
if respData.InstanceId != nil {
_ = d.Set("instance_id", respData.InstanceId)
}

if respData.UserName != nil {
_ = d.Set("user_name", respData.UserName)
}

_ = userName
return nil
}

func resourceTencentCloudCdwpgResetAccountPasswordUpdate(d *schema.ResourceData, meta interface{}) error {
defer tccommon.LogElapsed("resource.tencentcloud_cdwpg_reset_account_password.update")()
defer tccommon.InconsistentCheck(d, meta)()

logId := tccommon.GetLogId(tccommon.ContextNil)

ctx := tccommon.NewResourceLifeCycleHandleFuncContext(context.Background(), logId, d, meta)

idSplit := strings.Split(d.Id(), tccommon.FILED_SP)
if len(idSplit) != 2 {
return fmt.Errorf("id is broken,%s", d.Id())
}
instanceId := idSplit[0]
userName := idSplit[1]

needChange := false
mutableArgs := []string{"new_password"}
for _, v := range mutableArgs {
if d.HasChange(v) {
needChange = true
break
}
}

if needChange {
request := cdwpgv20201230.NewResetAccountPasswordRequest()

request.InstanceId = helper.String(instanceId)

request.UserName = helper.String(userName)

if v, ok := d.GetOk("new_password"); ok {
request.NewPassword = helper.String(v.(string))
}

err := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError {
result, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseCdwpgV20201230Client().ResetAccountPasswordWithContext(ctx, request)
if e != nil {
return tccommon.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 update cdwpg account failed, reason:%+v", logId, err)
return err
}
}

return resourceTencentCloudCdwpgResetAccountPasswordRead(d, meta)
}

func resourceTencentCloudCdwpgResetAccountPasswordDelete(d *schema.ResourceData, meta interface{}) error {
defer tccommon.LogElapsed("resource.tencentcloud_cdwpg_reset_account_password.delete")()
defer tccommon.InconsistentCheck(d, meta)()

return nil
}
Loading
Loading