Skip to content

Commit fa24a5f

Browse files
authored
feat: mysql - support device type (#1070)
1 parent 402cf2d commit fa24a5f

6 files changed

+124
-14
lines changed

tencentcloud/resource_tc_mysql_instance.go

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,11 @@ func TencentMsyqlBasicInfo() map[string]*schema.Schema {
185185
Optional: true,
186186
Description: "Specify whether to enable fast upgrade when upgrade instance spec, available value: `1` - enabled, `0` - disabled.",
187187
},
188+
"device_type": {
189+
Type: schema.TypeString,
190+
Optional: true,
191+
Description: "Specify device type, available values: `UNIVERSAL` (default), `EXCLUSIVE`, `BASIC`.",
192+
},
188193
"tags": {
189194
Type: schema.TypeMap,
190195
Optional: true,
@@ -448,6 +453,15 @@ func mysqlAllInstanceRoleSet(ctx context.Context, requestInter interface{}, d *s
448453
requestByUse.ParamTemplateId = paramTemplateId
449454
}
450455
}
456+
457+
if v, ok := d.GetOk("device_type"); ok {
458+
deviceType := helper.String(v.(string))
459+
if okByMonth {
460+
requestByMonth.DeviceType = deviceType
461+
} else {
462+
requestByUse.DeviceType = deviceType
463+
}
464+
}
451465
return nil
452466

453467
}
@@ -459,7 +473,8 @@ func mysqlMasterInstanceRoleSet(ctx context.Context, requestInter interface{}, d
459473
requestByMonth, okByMonth := requestInter.(*cdb.CreateDBInstanceRequest)
460474
requestByUse, _ := requestInter.(*cdb.CreateDBInstanceHourRequest)
461475

462-
if parametersMap, ok := d.Get("parameters").(map[string]interface{}); ok {
476+
isBasic := isBasicDevice(d)
477+
if parametersMap, ok := d.Get("parameters").(map[string]interface{}); ok && !isBasic {
463478
requestParamList := make([]*cdb.ParamInfo, 0, len(parametersMap))
464479
for k, v := range parametersMap {
465480
key := k
@@ -498,8 +513,8 @@ func mysqlMasterInstanceRoleSet(ctx context.Context, requestInter interface{}, d
498513
}
499514
}
500515

501-
if stringInterface, ok := d.GetOk("root_password"); ok {
502-
str := stringInterface.(string)
516+
if v, ok := d.GetOk("root_password"); ok && v.(string) != "" && !isBasic {
517+
str := v.(string)
503518
if okByMonth {
504519
requestByMonth.Password = &str
505520
} else {
@@ -672,10 +687,8 @@ func resourceTencentCloudMysqlInstanceCreate(d *schema.ResourceData, meta interf
672687
vPort int
673688
)
674689

675-
if v, ok := d.GetOk("root_password"); ok && v.(string) != "" {
690+
if v, ok := d.GetOk("root_password"); ok {
676691
password = v.(string)
677-
} else {
678-
return fmt.Errorf("`root_password` cannot be empty when creating")
679692
}
680693

681694
// 8.0 does not support lower_case_table_names modified, skip this params
@@ -817,6 +830,11 @@ func tencentMsyqlBasicInfoRead(ctx context.Context, d *schema.ResourceData, meta
817830
errRet = d.Set("subnet_id", mysqlInfo.UniqSubnetId)
818831
}
819832

833+
isUniversal := mysqlInfo.DeviceType != nil && *mysqlInfo.DeviceType == "UNIVERSAL"
834+
if _, ok := d.GetOk("device_type"); ok || !isUniversal {
835+
_ = d.Set("device_type", mysqlInfo.DeviceType)
836+
}
837+
820838
securityGroups, err := mysqlService.DescribeDBSecurityGroups(ctx, d.Id())
821839
if err != nil {
822840
sdkErr, ok := err.(*errors.TencentCloudSDKError)
@@ -1022,17 +1040,22 @@ func mysqlAllInstanceRoleUpdate(ctx context.Context, d *schema.ResourceData, met
10221040
}
10231041
}
10241042

1025-
if d.HasChange("mem_size") || d.HasChange("cpu") || d.HasChange("volume_size") {
1043+
if d.HasChange("mem_size") || d.HasChange("cpu") || d.HasChange("volume_size") || d.HasChange("device_type") {
10261044

10271045
memSize := int64(d.Get("mem_size").(int))
10281046
cpu := int64(d.Get("cpu").(int))
10291047
volumeSize := int64(d.Get("volume_size").(int))
1048+
deviceType := ""
1049+
10301050
fastUpgrade := int64(0)
10311051
if v, ok := d.GetOk("fast_upgrade"); ok {
10321052
fastUpgrade = int64(v.(int))
10331053
}
1054+
if v, ok := d.GetOk("device_type"); ok {
1055+
deviceType = v.(string)
1056+
}
10341057

1035-
asyncRequestId, err := mysqlService.UpgradeDBInstance(ctx, d.Id(), memSize, cpu, volumeSize, fastUpgrade)
1058+
asyncRequestId, err := mysqlService.UpgradeDBInstance(ctx, d.Id(), memSize, cpu, volumeSize, fastUpgrade, deviceType)
10361059

10371060
if err != nil {
10381061
return err
@@ -1470,3 +1493,11 @@ func getPayType(d *schema.ResourceData) (payType interface{}) {
14701493
}
14711494
return
14721495
}
1496+
1497+
func isBasicDevice(d *schema.ResourceData) bool {
1498+
v, ok := d.GetOk("device_type")
1499+
if !ok {
1500+
return false
1501+
}
1502+
return v.(string) == "BASIC"
1503+
}

tencentcloud/resource_tc_mysql_instance_test.go

Lines changed: 75 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,36 @@ func testSweepMySQLInstance(region string) error {
9595
return nil
9696
}
9797

98+
func TestAccTencentCloudMysqlDeviceType(t *testing.T) {
99+
resource.Test(t, resource.TestCase{
100+
PreCheck: func() { testAccPreCheck(t) },
101+
Providers: testAccProviders,
102+
CheckDestroy: testAccCheckMysqlMasterInstanceDestroy,
103+
Steps: []resource.TestStep{
104+
{
105+
Config: testAccMySQLDeviceType,
106+
Check: resource.ComposeAggregateTestCheckFunc(
107+
testAccCheckMysqlMasterInstanceExists("tencentcloud_mysql_instance.mysql_exclusive"),
108+
resource.TestCheckResourceAttr("tencentcloud_mysql_instance.mysql_exclusive", "device_type", "EXCLUSIVE"),
109+
),
110+
},
111+
{
112+
ResourceName: "tencentcloud_mysql_instance.mysql_exclusive",
113+
ImportState: true,
114+
ImportStateVerify: true,
115+
ImportStateVerifyIgnore: []string{"root_password", "prepaid_period", "first_slave_zone", "force_delete", "param_template_id", "fast_upgrade"},
116+
},
117+
{
118+
Config: testAccMySQLDeviceTypeUpdate,
119+
Check: resource.ComposeAggregateTestCheckFunc(
120+
testAccCheckMysqlMasterInstanceExists("tencentcloud_mysql_instance.mysql_exclusive"),
121+
resource.TestCheckResourceAttr("tencentcloud_mysql_instance.mysql_exclusive", "device_type", "EXCLUSIVE"),
122+
),
123+
},
124+
},
125+
})
126+
}
127+
98128
func TestAccTencentCloudMysqlMasterInstance_fullslave(t *testing.T) {
99129
resource.Test(t, resource.TestCase{
100130
PreCheck: func() { testAccPreCheck(t) },
@@ -276,6 +306,51 @@ func testAccCheckMysqlMasterInstanceExists(n string) resource.TestCheckFunc {
276306
}
277307
}
278308

309+
const testAccMySQLDeviceType = `
310+
variable "temporary_param_tmpl_id" {
311+
default = 16954
312+
}
313+
314+
resource "tencentcloud_mysql_instance" "mysql_exclusive" {
315+
charge_type = "POSTPAID"
316+
mem_size = 16000
317+
cpu = 2
318+
volume_size = 50
319+
instance_name = "testAccMysqlBasic"
320+
engine_version = "5.7"
321+
intranet_port = 3360
322+
root_password = "test1234"
323+
availability_zone = "ap-guangzhou-3"
324+
first_slave_zone = "ap-guangzhou-3"
325+
force_delete = true
326+
device_type = "EXCLUSIVE"
327+
param_template_id = var.temporary_param_tmpl_id
328+
}
329+
`
330+
331+
const testAccMySQLDeviceTypeUpdate = `
332+
variable "temporary_param_tmpl_id" {
333+
default = 16954
334+
}
335+
336+
resource "tencentcloud_mysql_instance" "mysql_exclusive" {
337+
charge_type = "POSTPAID"
338+
mem_size = 16000
339+
cpu = 2
340+
volume_size = 50
341+
instance_name = "testAccMysql"
342+
engine_version = "5.7"
343+
intranet_port = 3360
344+
root_password = "test1234"
345+
availability_zone = "ap-guangzhou-3"
346+
first_slave_zone = "ap-guangzhou-3"
347+
force_delete = true
348+
device_type = "EXCLUSIVE"
349+
fast_upgrade = 1
350+
param_template_id = var.temporary_param_tmpl_id
351+
}
352+
`
353+
279354
func testAccMysqlMasterInstance_basic() string {
280355
return `
281356
resource "tencentcloud_mysql_instance" "mysql_master" {
@@ -294,10 +369,6 @@ resource "tencentcloud_mysql_instance" "mysql_master" {
294369

295370
func testAccMysqlMasterInstance_fullslave() string {
296371
return `
297-
variable "temporary_param_tmpl_id" {
298-
default = 16954
299-
}
300-
301372
resource "tencentcloud_mysql_instance" "mysql_master" {
302373
charge_type = "POSTPAID"
303374
mem_size = 1000
@@ -311,7 +382,6 @@ resource "tencentcloud_mysql_instance" "mysql_master" {
311382
first_slave_zone = "ap-guangzhou-3"
312383
second_slave_zone = "ap-guangzhou-3"
313384
slave_sync_mode = 2
314-
param_template_id = var.temporary_param_tmpl_id
315385
force_delete = true
316386
}`
317387
}

tencentcloud/resource_tc_mysql_readonly_instance.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,10 @@ func mysqlCreateReadonlyInstancePayByMonth(ctx context.Context, d *schema.Resour
111111
request.MasterRegion = &masterRegion
112112
}
113113

114+
if v, ok := d.GetOk("device_type"); ok {
115+
request.DeviceType = helper.String(v.(string))
116+
}
117+
114118
autoRenewFlag := int64(d.Get("auto_renew_flag").(int))
115119
request.AutoRenewFlag = &autoRenewFlag
116120

tencentcloud/service_tencentcloud_mysql.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1108,7 +1108,7 @@ func (me *MysqlService) ModifyDBInstanceVipVport(ctx context.Context, mysqlId, v
11081108
}
11091109

11101110
func (me *MysqlService) UpgradeDBInstance(ctx context.Context, mysqlId string,
1111-
memSize, cpu, volumeSize, fastUpgrade int64) (asyncRequestId string, errRet error) {
1111+
memSize, cpu, volumeSize, fastUpgrade int64, deviceType string) (asyncRequestId string, errRet error) {
11121112

11131113
logId := getLogId(ctx)
11141114

@@ -1121,6 +1121,9 @@ func (me *MysqlService) UpgradeDBInstance(ctx context.Context, mysqlId string,
11211121
request.Volume = &volumeSize
11221122
request.WaitSwitch = &waitSwitch
11231123
request.FastUpgrade = &fastUpgrade
1124+
if deviceType != "" {
1125+
request.DeviceType = &deviceType
1126+
}
11241127

11251128
defer func() {
11261129
if errRet != nil {

website/docs/r/mysql_instance.html.markdown

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ The following arguments are supported:
5656
* `availability_zone` - (Optional, ForceNew) Indicates which availability zone will be used.
5757
* `charge_type` - (Optional, ForceNew) Pay type of instance. Valid values:`PREPAID`, `POSTPAID`. Default is `POSTPAID`.
5858
* `cpu` - (Optional) CPU cores.
59+
* `device_type` - (Optional) Specify device type, available values: `UNIVERSAL` (default), `EXCLUSIVE`, `BASIC`.
5960
* `engine_version` - (Optional, ForceNew) The version number of the database engine to use. Supported versions include 5.5/5.6/5.7/8.0, and default is 5.7.
6061
* `fast_upgrade` - (Optional) Specify whether to enable fast upgrade when upgrade instance spec, available value: `1` - enabled, `0` - disabled.
6162
* `first_slave_zone` - (Optional, ForceNew) Zone information about first slave instance.

website/docs/r/mysql_readonly_instance.html.markdown

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ The following arguments are supported:
4444
* `auto_renew_flag` - (Optional) Auto renew flag. NOTES: Only supported prepaid instance.
4545
* `charge_type` - (Optional, ForceNew) Pay type of instance. Valid values:`PREPAID`, `POSTPAID`. Default is `POSTPAID`.
4646
* `cpu` - (Optional) CPU cores.
47+
* `device_type` - (Optional) Specify device type, available values: `UNIVERSAL` (default), `EXCLUSIVE`, `BASIC`.
4748
* `fast_upgrade` - (Optional) Specify whether to enable fast upgrade when upgrade instance spec, available value: `1` - enabled, `0` - disabled.
4849
* `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.
4950
* `intranet_port` - (Optional) Public access port. Valid value ranges: [1024~65535]. The default value is `3306`.

0 commit comments

Comments
 (0)