Skip to content

Commit bfaba9f

Browse files
authored
fix(cdb): [125187689] tencentcloud_mysql_dr_instance optmize create PREPAID instance (#3426)
* add * add * add * add
1 parent 5aed632 commit bfaba9f

File tree

3 files changed

+147
-28
lines changed

3 files changed

+147
-28
lines changed

tencentcloud/services/cdb/resource_tc_mysql_dr_instance.go

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,6 @@ func ResourceTencentCloudMysqlDrInstance() *schema.Resource {
144144
ValidateFunc: tccommon.ValidateStringLengthInRange(1, 100),
145145
Description: "Private network ID. If `vpc_id` is set, this value is required.",
146146
},
147-
148147
"security_groups": {
149148
Type: schema.TypeSet,
150149
Optional: true,
@@ -435,8 +434,55 @@ func resourceTencentCloudMysqlDrInstanceUpdate(d *schema.ResourceData, meta inte
435434
logId := tccommon.GetLogId(tccommon.ContextNil)
436435
ctx := context.WithValue(context.TODO(), tccommon.LogIdKey, logId)
437436

437+
mysqlID := d.Id()
438438
payType := getPayType(d).(int)
439439

440+
if d.HasChange("prepaid_period") {
441+
if v, ok := d.GetOk("charge_type"); ok {
442+
if v.(string) != MYSQL_CHARGE_TYPE_PREPAID {
443+
return fmt.Errorf("`prepaid_period` only support prepaid instance.")
444+
}
445+
}
446+
}
447+
448+
if d.HasChange("charge_type") {
449+
oldChargeTypeInterface, newChargeTypeInterface := d.GetChange("charge_type")
450+
oldChargeType := oldChargeTypeInterface.(string)
451+
newChargeType := newChargeTypeInterface.(string)
452+
453+
if oldChargeType == MYSQL_CHARGE_TYPE_PREPAID && newChargeType == MYSQL_CHARGE_TYPE_POSTPAID {
454+
return fmt.Errorf("`charge_type` only supports modification from `POSTPAID` to `PREPAID`.")
455+
}
456+
457+
var period int = 1
458+
if v, ok := d.GetOkExists("prepaid_period"); ok {
459+
period = v.(int)
460+
}
461+
462+
request := cdb.NewRenewDBInstanceRequest()
463+
request.InstanceId = &mysqlID
464+
request.ModifyPayType = helper.String(newChargeType)
465+
request.TimeSpan = helper.IntInt64(period)
466+
err := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError {
467+
result, err := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseMysqlClient().RenewDBInstance(request)
468+
if err != nil {
469+
return tccommon.RetryError(err, tccommon.InternalError)
470+
} else {
471+
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString())
472+
}
473+
474+
if result == nil || result.Response == nil {
475+
return resource.NonRetryableError(fmt.Errorf("Renew DB instance failed, Response is nil."))
476+
}
477+
478+
return nil
479+
})
480+
481+
if err != nil {
482+
return err
483+
}
484+
}
485+
440486
d.Partial(true)
441487

442488
if payType == MysqlPayByMonth {
@@ -563,13 +609,15 @@ func mysqlDrInstanceSet(ctx context.Context, requestInter interface{}, d *schema
563609
}
564610
}
565611

566-
if payType, ok := d.GetOk("pay_type"); ok && okByMonth {
612+
payType, ok := d.GetOkExists("pay_type")
613+
if (!ok || payType == -1) && okByMonth {
567614
var period int
568615
if !ok || payType == -1 {
569616
period = d.Get("prepaid_period").(int)
570617
} else {
571618
period = d.Get("period").(int)
572619
}
620+
573621
requestByMonth.Period = helper.IntInt64(period)
574622
}
575623

tencentcloud/services/cdb/resource_tc_mysql_dr_instance.md

Lines changed: 49 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,74 @@
1-
Provides a mysql instance resource to create read-only database instances.
1+
Provides a mysql instance resource to create CDB dr(disaster recovery) instance.
2+
3+
~> **NOTE:** Field `charge_type` only supports modification from `POSTPAID` to `PREPAID`. And the default renewal period is 1 month. and you can also use the `prepaid_period` field to customize the renewal period.
24

35
Example Usage
46

7+
Create POSTPAID dr instance
8+
59
```hcl
6-
resource "tencentcloud_mysql_dr_instance" "mysql_dr" {
7-
master_instance_id = "cdb-adjdu3t5"
10+
resource "tencentcloud_mysql_dr_instance" "example" {
11+
master_instance_id = "cdb-3kwa3gfj"
812
master_region = "ap-guangzhou"
913
auto_renew_flag = 0
10-
availability_zone = "ap-shanghai-3"
14+
availability_zone = "ap-guangzhou-6"
1115
charge_type = "POSTPAID"
1216
cpu = 4
1317
device_type = "UNIVERSAL"
14-
first_slave_zone = "ap-shanghai-4"
15-
instance_name = "mysql-dr-test-up"
18+
first_slave_zone = "ap-guangzhou-7"
19+
instance_name = "tf-example"
1620
mem_size = 8000
21+
project_id = 0
22+
security_groups = [
23+
"sg-e6a8xxib",
24+
]
25+
slave_deploy_mode = 1
26+
slave_sync_mode = 0
27+
subnet_id = "subnet-hhi88a58"
28+
volume_size = 100
29+
vpc_id = "vpc-i5yyodl9"
30+
intranet_port = 3360
31+
tags = {
32+
createBy = "Terraform"
33+
}
34+
}
35+
```
36+
37+
Create PREPAID dr instance
38+
39+
```hcl
40+
resource "tencentcloud_mysql_dr_instance" "example" {
41+
master_instance_id = "cdb-3kwa3gfj"
42+
master_region = "ap-guangzhou"
43+
availability_zone = "ap-guangzhou-6"
44+
charge_type = "PREPAID"
1745
prepaid_period = 1
46+
auto_renew_flag = 1
47+
cpu = 4
48+
device_type = "UNIVERSAL"
49+
first_slave_zone = "ap-guangzhou-7"
50+
instance_name = "tf-example"
51+
mem_size = 8000
1852
project_id = 0
1953
security_groups = [
20-
"sg-q4d821qk",
54+
"sg-e6a8xxib",
2155
]
2256
slave_deploy_mode = 1
2357
slave_sync_mode = 0
24-
subnet_id = "subnet-5vfntba5"
58+
subnet_id = "subnet-hhi88a58"
2559
volume_size = 100
26-
vpc_id = "vpc-h6s1s3aa"
60+
vpc_id = "vpc-i5yyodl9"
2761
intranet_port = 3360
2862
tags = {
29-
test = "test-tf"
63+
createBy = "Terraform"
3064
}
3165
}
3266
```
67+
3368
Import
3469

35-
mysql dr database instances can be imported using the id, e.g.
70+
CDB dr(disaster recovery) instancecan be imported using the id, e.g.
71+
72+
```
73+
terraform import tencentcloud_mysql_dr_instance.example cdb-bcet7sdb
3674
```
37-
terraform import tencentcloud_mysql_dr_instance.mysql_dr cdb-bcet7sdb
38-
```

website/docs/r/mysql_dr_instance.html.markdown

Lines changed: 48 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,40 +4,74 @@ layout: "tencentcloud"
44
page_title: "TencentCloud: tencentcloud_mysql_dr_instance"
55
sidebar_current: "docs-tencentcloud-resource-mysql_dr_instance"
66
description: |-
7-
Provides a mysql instance resource to create read-only database instances.
7+
Provides a mysql instance resource to create CDB dr(disaster recovery) instance.
88
---
99

1010
# tencentcloud_mysql_dr_instance
1111

12-
Provides a mysql instance resource to create read-only database instances.
12+
Provides a mysql instance resource to create CDB dr(disaster recovery) instance.
13+
14+
~> **NOTE:** Field `charge_type` only supports modification from `POSTPAID` to `PREPAID`. And the default renewal period is 1 month. and you can also use the `prepaid_period` field to customize the renewal period.
1315

1416
## Example Usage
1517

18+
### Create POSTPAID dr instance
19+
1620
```hcl
17-
resource "tencentcloud_mysql_dr_instance" "mysql_dr" {
18-
master_instance_id = "cdb-adjdu3t5"
21+
resource "tencentcloud_mysql_dr_instance" "example" {
22+
master_instance_id = "cdb-3kwa3gfj"
1923
master_region = "ap-guangzhou"
2024
auto_renew_flag = 0
21-
availability_zone = "ap-shanghai-3"
25+
availability_zone = "ap-guangzhou-6"
2226
charge_type = "POSTPAID"
2327
cpu = 4
2428
device_type = "UNIVERSAL"
25-
first_slave_zone = "ap-shanghai-4"
26-
instance_name = "mysql-dr-test-up"
29+
first_slave_zone = "ap-guangzhou-7"
30+
instance_name = "tf-example"
2731
mem_size = 8000
32+
project_id = 0
33+
security_groups = [
34+
"sg-e6a8xxib",
35+
]
36+
slave_deploy_mode = 1
37+
slave_sync_mode = 0
38+
subnet_id = "subnet-hhi88a58"
39+
volume_size = 100
40+
vpc_id = "vpc-i5yyodl9"
41+
intranet_port = 3360
42+
tags = {
43+
createBy = "Terraform"
44+
}
45+
}
46+
```
47+
48+
### Create PREPAID dr instance
49+
50+
```hcl
51+
resource "tencentcloud_mysql_dr_instance" "example" {
52+
master_instance_id = "cdb-3kwa3gfj"
53+
master_region = "ap-guangzhou"
54+
availability_zone = "ap-guangzhou-6"
55+
charge_type = "PREPAID"
2856
prepaid_period = 1
57+
auto_renew_flag = 1
58+
cpu = 4
59+
device_type = "UNIVERSAL"
60+
first_slave_zone = "ap-guangzhou-7"
61+
instance_name = "tf-example"
62+
mem_size = 8000
2963
project_id = 0
3064
security_groups = [
31-
"sg-q4d821qk",
65+
"sg-e6a8xxib",
3266
]
3367
slave_deploy_mode = 1
3468
slave_sync_mode = 0
35-
subnet_id = "subnet-5vfntba5"
69+
subnet_id = "subnet-hhi88a58"
3670
volume_size = 100
37-
vpc_id = "vpc-h6s1s3aa"
71+
vpc_id = "vpc-i5yyodl9"
3872
intranet_port = 3360
3973
tags = {
40-
test = "test-tf"
74+
createBy = "Terraform"
4175
}
4276
}
4377
```
@@ -81,8 +115,9 @@ In addition to all arguments above, the following attributes are exported:
81115

82116
## Import
83117

84-
mysql dr database instances can be imported using the id, e.g.
118+
CDB dr(disaster recovery) instancecan be imported using the id, e.g.
119+
85120
```
86-
terraform import tencentcloud_mysql_dr_instance.mysql_dr cdb-bcet7sdb
121+
terraform import tencentcloud_mysql_dr_instance.example cdb-bcet7sdb
87122
```
88123

0 commit comments

Comments
 (0)