Skip to content

Commit 54c4ca6

Browse files
authored
Merge pull request #460 from Sesede/master
Fix auto-forcenew upgrade tencentcloud-terraform SDK with resource `tencentcloud_mysql_instance`
2 parents 86b1cd8 + f138201 commit 54c4ca6

File tree

4 files changed

+45
-30
lines changed

4 files changed

+45
-30
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
BUG FIXES:
44

55
* Resource: `tencentcloud_instance` fix `allocate_public_ip` inconsistency when eip is attached to the cvm.
6+
* Resource: `tencentcloud_mysql_instance` fix auto-forcenew on `charge_type` and `pay_type` when upgrading terraform version. ([#459](https://github.com/terraform-providers/terraform-provider-tencentcloud/pull/459)).
67

78
## 1.38.1 (June 30, 2020)
89

tencentcloud/resource_tc_mysql_instance.go

Lines changed: 39 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ Example Usage
99
resource "tencentcloud_mysql_instance" "default" {
1010
internet_service = 1
1111
engine_version = "5.7"
12-
12+
charge_type = "POSTPAID"
1313
root_password = "********"
1414
slave_deploy_mode = 0
1515
first_slave_zone = "ap-guangzhou-4"
@@ -62,11 +62,13 @@ func TencentMsyqlBasicInfo() map[string]*schema.Schema {
6262
"pay_type": {
6363
Type: schema.TypeInt,
6464
Deprecated: "It has been deprecated from version 1.36.0.",
65-
ForceNew: true,
6665
Optional: true,
6766
ValidateFunc: validateAllowedIntValue([]int{MysqlPayByMonth, MysqlPayByUse}),
6867
ConflictsWith: []string{"charge_type", "prepaid_period"},
69-
//Default: MysqlPayByUse,
68+
DiffSuppressFunc: func(k, olds, news string, d *schema.ResourceData) bool {
69+
return true
70+
},
71+
Default: -1,
7072
Description: "Pay type of instance, 0: prepaid, 1: postpaid.",
7173
},
7274
"charge_type": {
@@ -76,24 +78,44 @@ func TencentMsyqlBasicInfo() map[string]*schema.Schema {
7678
ValidateFunc: validateAllowedStringValue([]string{MYSQL_CHARGE_TYPE_PREPAID, MYSQL_CHARGE_TYPE_POSTPAID}),
7779
ConflictsWith: []string{"pay_type", "period"},
7880
Default: MYSQL_CHARGE_TYPE_POSTPAID,
79-
Description: "Pay type of instance, valid values are `PREPAID`, `POSTPAID`. Default is `POSTPAID`.",
81+
DiffSuppressFunc: func(k, olds, news string, d *schema.ResourceData) bool {
82+
if (olds == "" && news == MYSQL_CHARGE_TYPE_POSTPAID) ||
83+
(olds == MYSQL_CHARGE_TYPE_POSTPAID && news == "") {
84+
if v, ok := d.GetOkExists("pay_type"); ok && v.(int) == MysqlPayByUse {
85+
return true
86+
}
87+
} else if (olds == "" && news == MYSQL_CHARGE_TYPE_PREPAID) ||
88+
(olds == MYSQL_CHARGE_TYPE_PREPAID && news == "") {
89+
if v, ok := d.GetOkExists("pay_type"); ok && v.(int) == MysqlPayByMonth {
90+
return true
91+
}
92+
}
93+
return olds == news
94+
},
95+
Description: "Pay type of instance, valid values are `PREPAID`, `POSTPAID`. Default is `POSTPAID`.",
8096
},
8197
"period": {
8298
Type: schema.TypeInt,
8399
Deprecated: "It has been deprecated from version 1.36.0.",
84100
Optional: true,
85-
Default: 1,
101+
Default: -1,
86102
ConflictsWith: []string{"charge_type", "prepaid_period"},
87103
ValidateFunc: validateAllowedIntValue(MYSQL_AVAILABLE_PERIOD),
88-
Description: "Period of instance. NOTES: Only supported prepaid instance.",
104+
DiffSuppressFunc: func(k, olds, news string, d *schema.ResourceData) bool {
105+
return true
106+
},
107+
Description: "Period of instance. NOTES: Only supported prepaid instance.",
89108
},
90109
"prepaid_period": {
91110
Type: schema.TypeInt,
92111
Optional: true,
93112
Default: 1,
94113
ConflictsWith: []string{"pay_type", "period"},
95-
ValidateFunc: validateAllowedIntValue(MYSQL_AVAILABLE_PERIOD),
96-
Description: "Period of instance. NOTES: Only supported prepaid instance.",
114+
DiffSuppressFunc: func(k, olds, news string, d *schema.ResourceData) bool {
115+
return true
116+
},
117+
ValidateFunc: validateAllowedIntValue(MYSQL_AVAILABLE_PERIOD),
118+
Description: "Period of instance. NOTES: Only supported prepaid instance.",
97119
},
98120
"auto_renew_flag": {
99121
Type: schema.TypeInt,
@@ -488,9 +510,9 @@ func mysqlCreateInstancePayByMonth(ctx context.Context, d *schema.ResourceData,
488510

489511
request := cdb.NewCreateDBInstanceRequest()
490512

491-
_, oldOk := d.GetOkExists("pay_type")
513+
payType, oldOk := d.GetOkExists("pay_type")
492514
var period int
493-
if !oldOk {
515+
if !oldOk || payType == -1 {
494516
period = d.Get("prepaid_period").(int)
495517
} else {
496518
period = d.Get("period").(int)
@@ -663,24 +685,16 @@ func tencentMsyqlBasicInfoRead(ctx context.Context, d *schema.ResourceData, meta
663685
d.SetId("")
664686
return
665687
}
666-
_ = d.Set("instance_name", *mysqlInfo.InstanceName)
667688

668-
_, oldOk := d.GetOkExists("pay_type")
669-
var periodKey string
670-
if oldOk {
671-
_ = d.Set("pay_type", int(*mysqlInfo.PayType))
672-
periodKey = "period"
673-
} else {
674-
periodKey = "prepaid_period"
675-
676-
_ = d.Set("charge_type", MYSQL_CHARGE_TYPE[int(*mysqlInfo.PayType)])
677-
678-
}
689+
_ = d.Set("instance_name", *mysqlInfo.InstanceName)
679690

691+
_ = d.Set("charge_type", MYSQL_CHARGE_TYPE[int(*mysqlInfo.PayType)])
692+
_ = d.Set("pay_type", -1)
693+
_ = d.Set("period", -1)
680694
if int(*mysqlInfo.PayType) == MysqlPayByMonth {
681-
tempInt, _ := d.Get(periodKey).(int)
695+
tempInt, _ := d.Get("prepaid_period").(int)
682696
if tempInt == 0 {
683-
_ = d.Set(periodKey, 1)
697+
_ = d.Set("prepaid_period", 1)
684698
}
685699
}
686700

@@ -1339,7 +1353,7 @@ func getPayType(d *schema.ResourceData) (payType interface{}) {
13391353
chargeType := d.Get("charge_type")
13401354
payType, oldOk := d.GetOkExists("pay_type")
13411355

1342-
if !oldOk {
1356+
if !oldOk || payType == -1 {
13431357
if chargeType == MYSQL_CHARGE_TYPE_PREPAID {
13441358
payType = MysqlPayByMonth
13451359
} else {

website/docs/r/mysql_instance.html.markdown

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ Provides a mysql instance resource to create master database instances.
1616

1717
```hcl
1818
resource "tencentcloud_mysql_instance" "default" {
19-
internet_service = 1
20-
engine_version = "5.7"
21-
19+
internet_service = 1
20+
engine_version = "5.7"
21+
charge_type = "POSTPAID"
2222
root_password = "********"
2323
slave_deploy_mode = 0
2424
first_slave_zone = "ap-guangzhou-4"
@@ -61,7 +61,7 @@ The following arguments are supported:
6161
* `internet_service` - (Optional) Indicates whether to enable the access to an instance from public network: 0 - No, 1 - Yes.
6262
* `intranet_port` - (Optional) Public access port, rang form 1024 to 65535 and default value is 3306.
6363
* `parameters` - (Optional) List of parameters to use.
64-
* `pay_type` - (Optional, ForceNew, **Deprecated**) It has been deprecated from version 1.36.0. Pay type of instance, 0: prepaid, 1: postpaid.
64+
* `pay_type` - (Optional, **Deprecated**) It has been deprecated from version 1.36.0. Pay type of instance, 0: prepaid, 1: postpaid.
6565
* `period` - (Optional, **Deprecated**) It has been deprecated from version 1.36.0. Period of instance. NOTES: Only supported prepaid instance.
6666
* `prepaid_period` - (Optional) Period of instance. NOTES: Only supported prepaid instance.
6767
* `project_id` - (Optional) Project ID, default value is 0.

website/docs/r/mysql_readonly_instance.html.markdown

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ The following arguments are supported:
4343
* `charge_type` - (Optional, ForceNew) Pay type of instance, valid values are `PREPAID`, `POSTPAID`. Default is `POSTPAID`.
4444
* `force_delete` - (Optional) Indicate whether to delete instance directly or not. Default is false. If set true, the instance will be deleted instead of staying recycle bin. Note: only works for `PREPAID` instance. When the main mysql instance set true, this para of the readonly mysql instance will not take effect.
4545
* `intranet_port` - (Optional) Public access port, rang form 1024 to 65535 and default value is 3306.
46-
* `pay_type` - (Optional, ForceNew, **Deprecated**) It has been deprecated from version 1.36.0. Pay type of instance, 0: prepaid, 1: postpaid.
46+
* `pay_type` - (Optional, **Deprecated**) It has been deprecated from version 1.36.0. Pay type of instance, 0: prepaid, 1: postpaid.
4747
* `period` - (Optional, **Deprecated**) It has been deprecated from version 1.36.0. Period of instance. NOTES: Only supported prepaid instance.
4848
* `prepaid_period` - (Optional) Period of instance. NOTES: Only supported prepaid instance.
4949
* `security_groups` - (Optional) Security groups to use.

0 commit comments

Comments
 (0)