Skip to content

Commit 2d32b22

Browse files
tongyimingmikatong
and
mikatong
authored
feat(cdwpg): [122485020]add resource (#3236)
* add resource * add changelog * update resource * update * update * update * update --------- Co-authored-by: mikatong <[email protected]>
1 parent 4b6f714 commit 2d32b22

22 files changed

+1151
-0
lines changed

.changelog/3236.txt

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
```release-note:new-resource
2+
tencentcloud_cdwpg_reset_account_password
3+
```
4+
5+
```release-note:new-resource
6+
tencentcloud_cdwpg_restart_instance
7+
```
8+
9+
```release-note:new-resource
10+
tencentcloud_cdwpg_userhba
11+
```
12+
13+
```release-note:enhancement
14+
resource/tencentcloud_cdwpg_instance: support product_version
15+
```

tencentcloud/provider.go

+3
Original file line numberDiff line numberDiff line change
@@ -2228,6 +2228,9 @@ func Provider() *schema.Provider {
22282228
"tencentcloud_bi_embed_interval_apply": bi.ResourceTencentCloudBiEmbedIntervalApply(),
22292229
"tencentcloud_cdwpg_instance": cdwpg.ResourceTencentCloudCdwpgInstance(),
22302230
"tencentcloud_cdwpg_dbconfig": cdwpg.ResourceTencentCloudCdwpgDbconfig(),
2231+
"tencentcloud_cdwpg_reset_account_password": cdwpg.ResourceTencentCloudCdwpgResetAccountPassword(),
2232+
"tencentcloud_cdwpg_restart_instance": cdwpg.ResourceTencentCloudCdwpgRestartInstance(),
2233+
"tencentcloud_cdwpg_userhba": cdwpg.ResourceTencentCloudCdwpgUserhba(),
22312234
"tencentcloud_clickhouse_keyval_config": cdwch.ResourceTencentCloudClickhouseKeyvalConfig(),
22322235
"tencentcloud_clickhouse_xml_config": cdwch.ResourceTencentCloudClickhouseXmlConfig(),
22332236
"tencentcloud_clb_target_group_attachments": clb.ResourceTencentCloudClbTargetGroupAttachments(),

tencentcloud/provider.md

+3
Original file line numberDiff line numberDiff line change
@@ -2256,6 +2256,9 @@ tencentcloud_cdwpg_nodes
22562256
Resource
22572257
tencentcloud_cdwpg_instance
22582258
tencentcloud_cdwpg_dbconfig
2259+
tencentcloud_cdwpg_reset_account_password
2260+
tencentcloud_cdwpg_restart_instance
2261+
tencentcloud_cdwpg_userhba
22592262

22602263
CSIP
22612264
Resource

tencentcloud/services/cdwpg/resource_tc_cdwpg_instance.go

+41
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,12 @@ func ResourceTencentCloudCdwpgInstance() *schema.Resource {
143143
Optional: true,
144144
Description: "Tag description list.",
145145
},
146+
"product_version": {
147+
Optional: true,
148+
Computed: true,
149+
Type: schema.TypeString,
150+
Description: "Version.",
151+
},
146152
},
147153
}
148154
}
@@ -224,6 +230,9 @@ func resourceTencentCloudCdwpgInstanceCreate(d *schema.ResourceData, meta interf
224230
request.Resources = append(request.Resources, &resourceSpecNew)
225231
}
226232
}
233+
if v, ok := d.GetOk("product_version"); ok {
234+
request.ProductVersion = helper.String(v.(string))
235+
}
227236

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

367376
}
368377

378+
if instance.Version != nil {
379+
_ = d.Set("product_version", instance.Version)
380+
}
381+
369382
tcClient := meta.(tccommon.ProviderMeta).GetAPIV3Conn()
370383
tagService := svctag.NewTagService(tcClient)
371384
tags, err := tagService.DescribeResourceTags(ctx, "cdwpg", "cdwpgInstance", tcClient.Region, d.Id())
@@ -489,6 +502,34 @@ func resourceTencentCloudCdwpgInstanceUpdate(d *schema.ResourceData, meta interf
489502
}
490503
}
491504
}
505+
if d.HasChange("product_version") {
506+
if v, ok := d.GetOk("product_version"); ok {
507+
request := cdwpg.NewUpgradeInstanceRequest()
508+
request.InstanceId = helper.String(instanceId)
509+
request.PackageVersion = helper.String(v.(string))
510+
err := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError {
511+
result, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseCdwpgV20201230Client().UpgradeInstance(request)
512+
if e != nil {
513+
return tccommon.RetryError(e)
514+
} else {
515+
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString())
516+
}
517+
return nil
518+
})
519+
if err != nil {
520+
log.Printf("[CRITAL]%s create cdwpg upgrade instance failed, reason:%+v", logId, err)
521+
return err
522+
}
523+
524+
service := CdwpgService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()}
525+
conf := tccommon.BuildStateChangeConf([]string{}, []string{"Serving"}, 10*tccommon.ReadRetryTimeout, time.Second, service.InstanceStateRefreshFunc(instanceId, []string{}))
526+
527+
if _, e := conf.WaitForState(); e != nil {
528+
return e
529+
}
530+
}
531+
}
532+
492533
return resourceTencentCloudCdwpgInstanceRead(d, meta)
493534
}
494535

tencentcloud/services/cdwpg/resource_tc_cdwpg_instance_test.go

+116
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,36 @@ func TestAccTencentCloudCdwpgInstanceResource_basic(t *testing.T) {
5252
})
5353
}
5454

55+
func TestAccTencentCloudCdwpgInstanceResource_withVersion(t *testing.T) {
56+
t.Parallel()
57+
resource.Test(t, resource.TestCase{
58+
PreCheck: func() { tcacctest.AccPreCheckCommon(t, tcacctest.ACCOUNT_TYPE_PREPAY) },
59+
Providers: tcacctest.AccProviders,
60+
Steps: []resource.TestStep{
61+
{
62+
Config: testAccCdwpgInstanceWithVersion,
63+
Check: resource.ComposeTestCheckFunc(
64+
resource.TestCheckResourceAttrSet("tencentcloud_cdwpg_instance.instance", "id"),
65+
resource.TestCheckResourceAttr("tencentcloud_cdwpg_instance.instance", "product_version", "3.16.9.3"),
66+
),
67+
},
68+
{
69+
Config: testAccCdwpgInstanceWithVersionUpdate,
70+
Check: resource.ComposeTestCheckFunc(
71+
resource.TestCheckResourceAttrSet("tencentcloud_cdwpg_instance.instance", "id"),
72+
resource.TestCheckResourceAttr("tencentcloud_cdwpg_instance.instance", "product_version", "3.16.9.4"),
73+
),
74+
},
75+
{
76+
ResourceName: "tencentcloud_cdwpg_instance.instance",
77+
ImportState: true,
78+
ImportStateVerify: true,
79+
ImportStateVerifyIgnore: []string{"admin_password"},
80+
},
81+
},
82+
})
83+
}
84+
5585
const testAccCdwpgInstance = `
5686
resource "tencentcloud_cdwpg_instance" "instance" {
5787
instance_name = "test_pg"
@@ -177,3 +207,89 @@ resource "tencentcloud_cdwpg_instance" "instance" {
177207
}
178208
}
179209
`
210+
211+
const testAccCdwpgInstanceWithVersion = `
212+
resource "tencentcloud_cdwpg_instance" "instance" {
213+
instance_name = "test_pg"
214+
zone = "ap-guangzhou-6"
215+
user_vpc_id = "vpc-axrsmmrv"
216+
user_subnet_id = "subnet-kxaxknmg"
217+
charge_properties {
218+
renew_flag = 0
219+
time_span = 1
220+
time_unit = "h"
221+
charge_type = "POSTPAID_BY_HOUR"
222+
223+
}
224+
admin_password = "bWJSZDVtVmZkNExJ"
225+
resources {
226+
spec_name = "S_4_16_H_CN"
227+
count = 2
228+
disk_spec {
229+
disk_type = "CLOUD_HSSD"
230+
disk_size = 200
231+
disk_count = 1
232+
}
233+
type = "cn"
234+
235+
}
236+
resources {
237+
spec_name = "S_4_16_H_CN"
238+
count = 2
239+
disk_spec {
240+
disk_type = "CLOUD_HSSD"
241+
disk_size = 20
242+
disk_count = 10
243+
}
244+
type = "dn"
245+
246+
}
247+
tags = {
248+
"tagKey" = "tagValue"
249+
}
250+
product_version = "3.16.9.3"
251+
}
252+
`
253+
254+
const testAccCdwpgInstanceWithVersionUpdate = `
255+
resource "tencentcloud_cdwpg_instance" "instance" {
256+
instance_name = "test_pg"
257+
zone = "ap-guangzhou-6"
258+
user_vpc_id = "vpc-axrsmmrv"
259+
user_subnet_id = "subnet-kxaxknmg"
260+
charge_properties {
261+
renew_flag = 0
262+
time_span = 1
263+
time_unit = "h"
264+
charge_type = "POSTPAID_BY_HOUR"
265+
266+
}
267+
admin_password = "bWJSZDVtVmZkNExJ"
268+
resources {
269+
spec_name = "S_4_16_H_CN"
270+
count = 2
271+
disk_spec {
272+
disk_type = "CLOUD_HSSD"
273+
disk_size = 200
274+
disk_count = 1
275+
}
276+
type = "cn"
277+
278+
}
279+
resources {
280+
spec_name = "S_4_16_H_CN"
281+
count = 2
282+
disk_spec {
283+
disk_type = "CLOUD_HSSD"
284+
disk_size = 20
285+
disk_count = 10
286+
}
287+
type = "dn"
288+
289+
}
290+
tags = {
291+
"tagKey" = "tagValue"
292+
}
293+
product_version = "3.16.9.4"
294+
}
295+
`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
package cdwpg
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"log"
7+
"strings"
8+
9+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
10+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
11+
cdwpgv20201230 "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cdwpg/v20201230"
12+
13+
tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common"
14+
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
15+
)
16+
17+
func ResourceTencentCloudCdwpgResetAccountPassword() *schema.Resource {
18+
return &schema.Resource{
19+
Create: resourceTencentCloudCdwpgResetAccountPasswordCreate,
20+
Read: resourceTencentCloudCdwpgResetAccountPasswordRead,
21+
Update: resourceTencentCloudCdwpgResetAccountPasswordUpdate,
22+
Delete: resourceTencentCloudCdwpgResetAccountPasswordDelete,
23+
Importer: &schema.ResourceImporter{
24+
State: schema.ImportStatePassthrough,
25+
},
26+
Schema: map[string]*schema.Schema{
27+
"instance_id": {
28+
Type: schema.TypeString,
29+
Required: true,
30+
ForceNew: true,
31+
Description: "Instance id.",
32+
},
33+
34+
"user_name": {
35+
Type: schema.TypeString,
36+
Required: true,
37+
ForceNew: true,
38+
Description: "Username.",
39+
},
40+
41+
"new_password": {
42+
Type: schema.TypeString,
43+
Required: true,
44+
Sensitive: true,
45+
Description: "New password.",
46+
},
47+
},
48+
}
49+
}
50+
51+
func resourceTencentCloudCdwpgResetAccountPasswordCreate(d *schema.ResourceData, meta interface{}) error {
52+
defer tccommon.LogElapsed("resource.tencentcloud_cdwpg_reset_account_password.create")()
53+
defer tccommon.InconsistentCheck(d, meta)()
54+
55+
instanceId := d.Get("instance_id").(string)
56+
userName := d.Get("user_name").(string)
57+
58+
d.SetId(strings.Join([]string{instanceId, userName}, tccommon.FILED_SP))
59+
60+
return resourceTencentCloudCdwpgResetAccountPasswordUpdate(d, meta)
61+
}
62+
63+
func resourceTencentCloudCdwpgResetAccountPasswordRead(d *schema.ResourceData, meta interface{}) error {
64+
defer tccommon.LogElapsed("resource.tencentcloud_cdwpg_reset_account_password.read")()
65+
defer tccommon.InconsistentCheck(d, meta)()
66+
67+
logId := tccommon.GetLogId(tccommon.ContextNil)
68+
69+
ctx := tccommon.NewResourceLifeCycleHandleFuncContext(context.Background(), logId, d, meta)
70+
71+
service := CdwpgService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()}
72+
73+
idSplit := strings.Split(d.Id(), tccommon.FILED_SP)
74+
if len(idSplit) != 2 {
75+
return fmt.Errorf("id is broken,%s", d.Id())
76+
}
77+
instanceId := idSplit[0]
78+
userName := idSplit[1]
79+
80+
respData, err := service.DescribeCdwpgAccountById(ctx, instanceId)
81+
if err != nil {
82+
return err
83+
}
84+
85+
if respData == nil {
86+
d.SetId("")
87+
log.Printf("[WARN]%s resource `cdwpg_account` [%s] not found, please check if it has been deleted.\n", logId, d.Id())
88+
return nil
89+
}
90+
if respData.InstanceId != nil {
91+
_ = d.Set("instance_id", respData.InstanceId)
92+
}
93+
94+
if respData.UserName != nil {
95+
_ = d.Set("user_name", respData.UserName)
96+
}
97+
98+
_ = userName
99+
return nil
100+
}
101+
102+
func resourceTencentCloudCdwpgResetAccountPasswordUpdate(d *schema.ResourceData, meta interface{}) error {
103+
defer tccommon.LogElapsed("resource.tencentcloud_cdwpg_reset_account_password.update")()
104+
defer tccommon.InconsistentCheck(d, meta)()
105+
106+
logId := tccommon.GetLogId(tccommon.ContextNil)
107+
108+
ctx := tccommon.NewResourceLifeCycleHandleFuncContext(context.Background(), logId, d, meta)
109+
110+
idSplit := strings.Split(d.Id(), tccommon.FILED_SP)
111+
if len(idSplit) != 2 {
112+
return fmt.Errorf("id is broken,%s", d.Id())
113+
}
114+
instanceId := idSplit[0]
115+
userName := idSplit[1]
116+
117+
needChange := false
118+
mutableArgs := []string{"new_password"}
119+
for _, v := range mutableArgs {
120+
if d.HasChange(v) {
121+
needChange = true
122+
break
123+
}
124+
}
125+
126+
if needChange {
127+
request := cdwpgv20201230.NewResetAccountPasswordRequest()
128+
129+
request.InstanceId = helper.String(instanceId)
130+
131+
request.UserName = helper.String(userName)
132+
133+
if v, ok := d.GetOk("new_password"); ok {
134+
request.NewPassword = helper.String(v.(string))
135+
}
136+
137+
err := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError {
138+
result, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseCdwpgV20201230Client().ResetAccountPasswordWithContext(ctx, request)
139+
if e != nil {
140+
return tccommon.RetryError(e)
141+
} else {
142+
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString())
143+
}
144+
return nil
145+
})
146+
if err != nil {
147+
log.Printf("[CRITAL]%s update cdwpg account failed, reason:%+v", logId, err)
148+
return err
149+
}
150+
}
151+
152+
return resourceTencentCloudCdwpgResetAccountPasswordRead(d, meta)
153+
}
154+
155+
func resourceTencentCloudCdwpgResetAccountPasswordDelete(d *schema.ResourceData, meta interface{}) error {
156+
defer tccommon.LogElapsed("resource.tencentcloud_cdwpg_reset_account_password.delete")()
157+
defer tccommon.InconsistentCheck(d, meta)()
158+
159+
return nil
160+
}

0 commit comments

Comments
 (0)