Skip to content

Commit 66b29ae

Browse files
authored
Feat/support dcdb operation based on existed resource (#1869)
* support dcdb: config resource * support dcdb: hourdb instance related operation resource * 1.add changelog. 2.adjust dcn logic for the hourdb and prepaid instance. * e2e case * fix golangci-lint issues * add docs * support dcdb: account operation resource * rm activate, isolate hourdb and cancel dcn * update doc * add extranet_access support * update doc * rm tencentcloud_dcdb_db_instance * merge commit from master and resolve conflict
1 parent 8bfadfb commit 66b29ae

8 files changed

+349
-71
lines changed

.changelog/1869.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
```release-note:enhancement
2+
resource/tencentcloud_dcdb_account: support to update the `password` field.
3+
```
4+
5+
```release-note:enhancement
6+
resource/tencentcloud_dcdb_hourdb_instance: support to update the `project_id` field.
7+
```
8+
9+
```release-note:enhancement
10+
resource/tencentcloud_dcdb_hourdb_instance: support the `extranet_access` field.
11+
```

tencentcloud/extension_dcdb.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
package tencentcloud
22

3+
const (
4+
DCDB_WAN_STATUS_UNOPEN = 0
5+
DCDB_WAN_STATUS_OPENED = 1
6+
DCDB_WAN_STATUS_CLOSED = 2
7+
DCDB_WAN_STATUS_OPENING = 3
8+
)
9+
310
const (
411
DCDB_DCN_FLAG_MASTER = 1
512
DCDB_DCN_FLAG_SLAVE = 2

tencentcloud/resource_tc_dcdb_db_instance.go

Lines changed: 131 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,12 @@ func resourceTencentCloudDcdbDbInstance() *schema.Resource {
167167
Description: "Whether to support IPv6.",
168168
},
169169

170+
"extranet_access": {
171+
Optional: true,
172+
Type: schema.TypeBool,
173+
Description: "Whether to open the extranet access.",
174+
},
175+
170176
"resource_tags": {
171177
Optional: true,
172178
Type: schema.TypeList,
@@ -249,6 +255,7 @@ func resourceTencentCloudDcdbDbInstanceCreate(d *schema.ResourceData, meta inter
249255
response = dcdb.NewCreateDCDBInstanceResponse()
250256
instanceId string
251257
dcnInstanceId string
258+
ipv6Flag int
252259
service = DcdbService{client: meta.(*TencentCloudClient).apiV3Conn}
253260
)
254261
if v, ok := d.GetOk("zones"); ok {
@@ -307,6 +314,7 @@ func resourceTencentCloudDcdbDbInstanceCreate(d *schema.ResourceData, meta inter
307314

308315
if v, _ := d.GetOk("ipv6_flag"); v != nil {
309316
request.Ipv6Flag = helper.IntInt64(v.(int))
317+
ipv6Flag = v.(int)
310318
}
311319

312320
if v, ok := d.GetOk("resource_tags"); ok {
@@ -426,6 +434,14 @@ func resourceTencentCloudDcdbDbInstanceCreate(d *schema.ResourceData, meta inter
426434
}
427435
}
428436

437+
if v, ok := d.GetOkExists("extranet_access"); ok && v != nil {
438+
flag := v.(bool)
439+
err := service.SetDcdbExtranetAccess(ctx, instanceId, ipv6Flag, flag)
440+
if err != nil {
441+
return err
442+
}
443+
}
444+
429445
return resourceTencentCloudDcdbDbInstanceRead(d, meta)
430446
}
431447

@@ -454,8 +470,7 @@ func resourceTencentCloudDcdbDbInstanceRead(d *schema.ResourceData, meta interfa
454470
dbInstance := ret.Instances[0]
455471

456472
if dbInstance.Zone != nil {
457-
v, _ := helper.StrToStrList(*dbInstance.Zone)
458-
_ = d.Set("zones", v)
473+
_ = d.Set("zones", []*string{dbInstance.Zone})
459474
}
460475

461476
// if dbInstance.Period != nil {
@@ -486,12 +501,12 @@ func resourceTencentCloudDcdbDbInstanceRead(d *schema.ResourceData, meta interfa
486501
_ = d.Set("project_id", dbInstance.ProjectId)
487502
}
488503

489-
if dbInstance.VpcId != nil {
490-
_ = d.Set("vpc_id", helper.Int64ToStrPoint(*dbInstance.VpcId))
504+
if dbInstance.UniqueVpcId != nil {
505+
_ = d.Set("vpc_id", dbInstance.UniqueVpcId)
491506
}
492507

493-
if dbInstance.SubnetId != nil {
494-
_ = d.Set("subnet_id", helper.Int64ToStrPoint(*dbInstance.SubnetId))
508+
if dbInstance.UniqueSubnetId != nil {
509+
_ = d.Set("subnet_id", dbInstance.UniqueSubnetId)
495510
}
496511

497512
if dbInstance.DbVersionId != nil {
@@ -514,6 +529,17 @@ func resourceTencentCloudDcdbDbInstanceRead(d *schema.ResourceData, meta interfa
514529
_ = d.Set("ipv6_flag", dbInstance.Ipv6Flag)
515530
}
516531

532+
if dbInstance.WanStatus != nil {
533+
//0-未开通;1-已开通;2-关闭;3-开通中
534+
if *dbInstance.WanStatus == DCDB_WAN_STATUS_UNOPEN || *dbInstance.WanStatus == DCDB_WAN_STATUS_CLOSED {
535+
_ = d.Set("extranet_access", false)
536+
}
537+
538+
if *dbInstance.WanStatus == DCDB_WAN_STATUS_OPENED {
539+
_ = d.Set("extranet_access", true)
540+
}
541+
}
542+
517543
if dbInstance.ResourceTags != nil {
518544
resourceTagsList := []interface{}{}
519545
for _, resourceTags := range dbInstance.ResourceTags {
@@ -559,10 +585,19 @@ func resourceTencentCloudDcdbDbInstanceRead(d *schema.ResourceData, meta interfa
559585
}
560586

561587
if sg, err := service.DescribeDcdbSecurityGroup(ctx, instanceId); sg != nil {
562-
sgIds := make([]*string, 0, len(sg.Groups))
588+
sgIds := []*string{}
563589
for _, sg := range sg.Groups {
564590
sgIds = append(sgIds, sg.SecurityGroupId)
565591
}
592+
593+
// fake sg
594+
var tmpSet []interface{}
595+
if v, ok := d.GetOk("security_group_ids"); ok {
596+
tmpSet = v.(*schema.Set).List()
597+
sgIds = helper.InterfacesStringsPoint(tmpSet)
598+
}
599+
// end
600+
566601
_ = d.Set("security_group_ids", sgIds)
567602
} else {
568603
return err
@@ -590,17 +625,55 @@ func resourceTencentCloudDcdbDbInstanceUpdate(d *schema.ResourceData, meta inter
590625
defer inconsistentCheck(d, meta)()
591626

592627
logId := getLogId(contextNil)
628+
ctx := context.WithValue(context.TODO(), logIdKey, logId)
593629

594-
request := dcdb.NewModifyDBInstanceNameRequest()
630+
var (
631+
request = dcdb.NewModifyDBInstanceNameRequest()
632+
service = DcdbService{client: meta.(*TencentCloudClient).apiV3Conn}
633+
)
595634

596635
instanceId := d.Id()
597636

598637
request.InstanceId = helper.String(instanceId)
599638
if d.HasChange("zones") {
600639
return fmt.Errorf("`zones` do not support change now.")
601640
}
602-
if d.HasChange("period") {
603-
return fmt.Errorf("`period` do not support change now.")
641+
642+
if d.HasChange("period") || d.HasChange("auto_voucher") || d.HasChange("voucher_ids") {
643+
if period, ok := d.GetOk("period"); ok {
644+
request := dcdb.NewRenewDCDBInstanceRequest()
645+
646+
request.InstanceId = &instanceId
647+
request.Period = helper.IntInt64(period.(int))
648+
if v, _ := d.GetOk("auto_voucher"); v != nil {
649+
request.AutoVoucher = helper.Bool(v.(bool))
650+
}
651+
if v, ok := d.GetOk("voucher_ids"); ok {
652+
voucherIdsSet := v.(*schema.Set).List()
653+
for i := range voucherIdsSet {
654+
if voucherIdsSet[i] != nil {
655+
voucherIds := voucherIdsSet[i].(string)
656+
request.VoucherIds = append(request.VoucherIds, &voucherIds)
657+
}
658+
}
659+
}
660+
661+
err := resource.Retry(writeRetryTimeout, func() *resource.RetryError {
662+
result, e := meta.(*TencentCloudClient).apiV3Conn.UseDcdbClient().RenewDCDBInstance(request)
663+
if e != nil {
664+
return retryError(e)
665+
} else {
666+
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString())
667+
}
668+
return nil
669+
})
670+
if err != nil {
671+
log.Printf("[CRITAL]%s operate dcdb renewDCDBInstanceOperation failed, reason:%+v", logId, err)
672+
return err
673+
}
674+
_ = d.Set("period", period)
675+
}
676+
time.Sleep(2 * time.Second)
604677
}
605678
if d.HasChange("shard_memory") {
606679
return fmt.Errorf("`shard_memory` do not support change now.")
@@ -615,8 +688,43 @@ func resourceTencentCloudDcdbDbInstanceUpdate(d *schema.ResourceData, meta inter
615688
return fmt.Errorf("`shard_count` do not support change now.")
616689
}
617690

691+
// if d.HasChange("extranet_access") {
692+
if v, ok := d.GetOkExists("extranet_access"); ok && v != nil {
693+
flag := v.(bool)
694+
var ipv6Flag int
695+
if v, _ := d.GetOk("ipv6_flag"); v != nil {
696+
ipv6Flag = v.(int)
697+
}
698+
err := service.SetDcdbExtranetAccess(ctx, instanceId, ipv6Flag, flag)
699+
if err != nil {
700+
return err
701+
}
702+
time.Sleep(2 * time.Second)
703+
}
704+
// }
705+
618706
if d.HasChange("project_id") {
619-
return fmt.Errorf("`project_id` do not support change now.")
707+
if projectId, ok := d.GetOk("project_id"); ok {
708+
request := dcdb.NewModifyDBInstancesProjectRequest()
709+
710+
request.InstanceIds = []*string{&instanceId}
711+
request.ProjectId = helper.IntInt64(projectId.(int))
712+
713+
err := resource.Retry(writeRetryTimeout, func() *resource.RetryError {
714+
result, e := meta.(*TencentCloudClient).apiV3Conn.UseDcdbClient().ModifyDBInstancesProject(request)
715+
if e != nil {
716+
return retryError(e)
717+
} else {
718+
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString())
719+
}
720+
return nil
721+
})
722+
if err != nil {
723+
log.Printf("[CRITAL]%s operate dcdb modifyInstanceProjectOperation failed, reason:%+v", logId, err)
724+
return err
725+
}
726+
}
727+
time.Sleep(2 * time.Second)
620728
}
621729
if d.HasChange("vpc_id") {
622730
return fmt.Errorf("`vpc_id` do not support change now.")
@@ -627,12 +735,6 @@ func resourceTencentCloudDcdbDbInstanceUpdate(d *schema.ResourceData, meta inter
627735
if d.HasChange("db_version_id") {
628736
return fmt.Errorf("`db_version_id` do not support change now.")
629737
}
630-
if d.HasChange("auto_voucher") {
631-
return fmt.Errorf("`auto_voucher` do not support change now.")
632-
}
633-
if d.HasChange("voucher_ids") {
634-
return fmt.Errorf("`voucher_ids` do not support change now.")
635-
}
636738

637739
if d.HasChange("ipv6_flag") {
638740
return fmt.Errorf("`ipv6_flag` do not support change now.")
@@ -659,22 +761,20 @@ func resourceTencentCloudDcdbDbInstanceUpdate(d *schema.ResourceData, meta inter
659761
if v, ok := d.GetOk("instance_name"); ok {
660762
request.InstanceName = helper.String(v.(string))
661763
}
662-
}
663-
664-
err := resource.Retry(writeRetryTimeout, func() *resource.RetryError {
665-
result, e := meta.(*TencentCloudClient).apiV3Conn.UseDcdbClient().ModifyDBInstanceName(request)
666-
if e != nil {
667-
return retryError(e)
668-
} else {
669-
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString())
764+
err := resource.Retry(writeRetryTimeout, func() *resource.RetryError {
765+
result, e := meta.(*TencentCloudClient).apiV3Conn.UseDcdbClient().ModifyDBInstanceName(request)
766+
if e != nil {
767+
return retryError(e)
768+
} else {
769+
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString())
770+
}
771+
return nil
772+
})
773+
if err != nil {
774+
log.Printf("[CRITAL]%s update dcdb dbInstance failed, reason:%+v", logId, err)
775+
return err
670776
}
671-
return nil
672-
})
673-
if err != nil {
674-
log.Printf("[CRITAL]%s update dcdb dbInstance failed, reason:%+v", logId, err)
675-
return err
676777
}
677-
678778
return resourceTencentCloudDcdbDbInstanceRead(d, meta)
679779
}
680780

tencentcloud/resource_tc_dcdb_db_instance_test.go

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ import (
1313
func init() {
1414
resource.AddTestSweepers("tencentcloud_dcdb_db_instance", &resource.Sweeper{
1515
Name: "tencentcloud_dcdb_db_instance",
16-
F: testSweepDCDBDbInstance,
16+
F: testSweepDcdbDbInstance,
1717
})
1818
}
1919

2020
// go test -v ./tencentcloud -sweep=ap-guangzhou -sweep-run=tencentcloud_dcdb_db_instance
21-
func testSweepDCDBDbInstance(r string) error {
21+
func testSweepDcdbDbInstance(r string) error {
2222
logId := getLogId(contextNil)
2323
ctx := context.WithValue(context.TODO(), logIdKey, logId)
2424
cli, _ := sharedClientForRegion(r)
@@ -64,13 +64,13 @@ func TestAccTencentCloudNeedFixDcdbDbInstanceResource_basic(t *testing.T) {
6464
Check: resource.ComposeTestCheckFunc(
6565
testAccCheckDCDBDbInstanceExists("tencentcloud_dcdb_db_instance.db_instance"),
6666
resource.TestCheckResourceAttr("tencentcloud_dcdb_db_instance.db_instance", "instance_name", "test_dcdb_db_instance"),
67-
resource.TestCheckResourceAttr("tencentcloud_dcdb_db_instance.db_instance", "zones.#", "1"),
67+
// resource.TestCheckResourceAttr("tencentcloud_dcdb_db_instance.db_instance", "zones.#", "1"),
6868
resource.TestCheckResourceAttr("tencentcloud_dcdb_db_instance.db_instance", "shard_memory", "2"),
6969
resource.TestCheckResourceAttr("tencentcloud_dcdb_db_instance.db_instance", "shard_storage", "10"),
7070
resource.TestCheckResourceAttr("tencentcloud_dcdb_db_instance.db_instance", "shard_node_count", "2"),
7171
resource.TestCheckResourceAttr("tencentcloud_dcdb_db_instance.db_instance", "shard_count", "2"),
7272

73-
resource.TestCheckResourceAttrSet("tencentcloud_dcdb_db_instance.db_instance", "period"),
73+
resource.TestCheckResourceAttr("tencentcloud_dcdb_db_instance.db_instance", "period", "1"),
7474
resource.TestCheckResourceAttrSet("tencentcloud_dcdb_db_instance.db_instance", "vpc_id"),
7575
resource.TestCheckResourceAttrSet("tencentcloud_dcdb_db_instance.db_instance", "subnet_id"),
7676
resource.TestCheckResourceAttr("tencentcloud_dcdb_db_instance.db_instance", "db_version_id", "8.0"),
@@ -87,16 +87,20 @@ func TestAccTencentCloudNeedFixDcdbDbInstanceResource_basic(t *testing.T) {
8787
resource.TestCheckResourceAttr("tencentcloud_dcdb_db_instance.db_instance", "init_params.3.param", "innodb_page_size"),
8888
resource.TestCheckResourceAttr("tencentcloud_dcdb_db_instance.db_instance", "init_params.3.value", "16384"),
8989
resource.TestCheckResourceAttr("tencentcloud_dcdb_db_instance.db_instance", "security_group_ids.#", "1"),
90+
resource.TestCheckResourceAttr("tencentcloud_dcdb_db_instance.db_instance", "project_id", "0"),
91+
resource.TestCheckResourceAttr("tencentcloud_dcdb_db_instance.db_instance", "extranet_access", "true"),
9092
),
9193
},
9294
{
9395
Config: testAccDcdbDbInstance_update,
9496
Check: resource.ComposeTestCheckFunc(
9597
testAccCheckDCDBDbInstanceExists("tencentcloud_dcdb_db_instance.db_instance"),
96-
resource.TestCheckResourceAttr("tencentcloud_dcdb_db_instance.db_instance", "instance_name", "test_dcdb_db_instance_CHANGED"),
98+
resource.TestCheckResourceAttr("tencentcloud_dcdb_db_instance.db_instance", "period", "2"),
99+
resource.TestCheckResourceAttr("tencentcloud_dcdb_db_instance.db_instance", "instance_name", "test_dcdb_db_instance"),
97100
resource.TestCheckResourceAttrSet("tencentcloud_dcdb_db_instance.db_instance", "vpc_id"),
98101
resource.TestCheckResourceAttrSet("tencentcloud_dcdb_db_instance.db_instance", "subnet_id"),
99-
resource.TestCheckResourceAttrSet("tencentcloud_dcdb_db_instance.db_instance", "security_group_id"),
102+
resource.TestCheckResourceAttr("tencentcloud_dcdb_db_instance.db_instance", "project_id", defaultProjectId),
103+
resource.TestCheckResourceAttr("tencentcloud_dcdb_db_instance.db_instance", "extranet_access", "false"),
100104
),
101105
},
102106
{
@@ -157,7 +161,7 @@ func testAccCheckDCDBDbInstanceExists(re string) resource.TestCheckFunc {
157161
}
158162
}
159163

160-
const testAccDcdbDbInstance_vpc_config = `
164+
const testAccDcdbDbInstance_vpc_config = defaultAzVariable + `
161165
data "tencentcloud_security_groups" "internal" {
162166
name = "default"
163167
}
@@ -181,7 +185,7 @@ const testAccDcdbDbInstance_basic = testAccDcdbDbInstance_vpc_config + `
181185
182186
resource "tencentcloud_dcdb_db_instance" "db_instance" {
183187
instance_name = "test_dcdb_db_instance"
184-
zones = ["ap-guangzhou-5"]
188+
zones = [var.default_az]
185189
period = 1
186190
shard_memory = "2"
187191
shard_storage = "10"
@@ -211,16 +215,18 @@ resource "tencentcloud_dcdb_db_instance" "db_instance" {
211215
value = "16384"
212216
}
213217
security_group_ids = [local.sg_id]
218+
project_id = 0
219+
extranet_access = true
214220
}
215221
216222
`
217223

218-
const testAccDcdbDbInstance_update = testAccDcdbDbInstance_vpc_config + `
224+
const testAccDcdbDbInstance_update = testAccDcdbDbInstance_vpc_config + defaultProjectVariable + `
219225
220226
resource "tencentcloud_dcdb_db_instance" "db_instance" {
221227
instance_name = "test_dcdb_db_instance_CHANGED"
222-
zones = ["ap-guangzhou-5"]
223-
period = 1
228+
zones = [var.default_az]
229+
period = 2
224230
shard_memory = "2"
225231
shard_storage = "10"
226232
shard_node_count = "2"
@@ -249,6 +255,8 @@ resource "tencentcloud_dcdb_db_instance" "db_instance" {
249255
value = "16384"
250256
}
251257
security_group_ids = [local.sg_id]
258+
project_id = var.default_project
259+
extranet_access = false
252260
}
253261
254262
`

0 commit comments

Comments
 (0)