Skip to content

Commit a2cba44

Browse files
authored
feat: tdsql-c - support serverless creation (#1406)
* feat: tdsql-c - support serverless creation * fix: cynos add serverless testcase * changelog 1406 * fix: cynos lint and import * kill unused defaultSecurityGroup2 * fix: cynos add immutable update checkings
1 parent 07e2032 commit a2cba44

File tree

8 files changed

+175
-30
lines changed

8 files changed

+175
-30
lines changed

.changelog/1406.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:enhancement
2+
resource/tencentcloud_cynosdb_cluster: feat: tdsql-c - support serverless creation
3+
```

tencentcloud/basic_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,7 @@ const (
9292
defaultGaapRealserverIp2 = "1.1.1.5"
9393
defaultHttpsDomainCertificateId = "cert-crg2aynt"
9494

95-
defaultSecurityGroup = "sg-ijato2x1"
96-
defaultSecurityGroup2 = "sg-51rgzop1"
95+
defaultSecurityGroup = "sg-ijato2x1"
9796

9897
defaultProjectId = "1250480"
9998

tencentcloud/extension_cynosdb.go

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
const (
99
CYNOSDB_CHARGE_TYPE_POSTPAID = COMMON_PAYTYPE_POSTPAID
1010
CYNOSDB_CHARGE_TYPE_PREPAID = COMMON_PAYTYPE_PREPAID
11+
CYNOSDB_SERVERLESS = "SERVERLESS"
1112

1213
CYNOSDB_STATUS_RUNNING = "running"
1314
CYNOSDB_STATUS_OFFLINE = "offlined"
@@ -38,13 +39,13 @@ func TencentCynosdbInstanceBaseInfo() map[string]*schema.Schema {
3839
return map[string]*schema.Schema{
3940
"instance_cpu_core": {
4041
Type: schema.TypeInt,
41-
Required: true,
42-
Description: "The number of CPU cores of read-write type instance in the CynosDB cluster. Note: modification of this field will take effect immediately, if want to upgrade on maintenance window, please upgrade from console.",
42+
Optional: true,
43+
Description: "The number of CPU cores of read-write type instance in the CynosDB cluster. Required while creating normal cluster. Note: modification of this field will take effect immediately, if want to upgrade on maintenance window, please upgrade from console.",
4344
},
4445
"instance_memory_size": {
4546
Type: schema.TypeInt,
46-
Required: true,
47-
Description: "Memory capacity of read-write type instance, unit in GB. Note: modification of this field will take effect immediately, if want to upgrade on maintenance window, please upgrade from console.",
47+
Optional: true,
48+
Description: "Memory capacity of read-write type instance, unit in GB. Required while creating normal cluster. Note: modification of this field will take effect immediately, if want to upgrade on maintenance window, please upgrade from console.",
4849
},
4950
"instance_id": {
5051
Type: schema.TypeString,
@@ -346,6 +347,31 @@ func TencentCynosdbClusterBaseInfo() map[string]*schema.Schema {
346347
Computed: true,
347348
Description: "The ID of the parameter template.",
348349
},
350+
"db_mode": {
351+
Type: schema.TypeString,
352+
Optional: true,
353+
Description: "Specify DB mode, only available when `db_type` is `MYSQL`. Values: `NORMAL` (Default), `SERVERLESS`.",
354+
},
355+
"min_cpu": {
356+
Optional: true,
357+
Type: schema.TypeFloat,
358+
Description: "Minimum CPU core count, required while `db_mode` is `SERVERLESS`, request DescribeServerlessInstanceSpecs for more reference.",
359+
},
360+
"max_cpu": {
361+
Optional: true,
362+
Type: schema.TypeFloat,
363+
Description: "Maximum CPU core count, required while `db_mode` is `SERVERLESS`, request DescribeServerlessInstanceSpecs for more reference.",
364+
},
365+
"auto_pause": {
366+
Type: schema.TypeString,
367+
Optional: true,
368+
Description: "Specify whether the cluster can auto-pause while `db_mode` is `SERVERLESS`. Values: `yes` (default), `no`.",
369+
},
370+
"auto_pause_delay": {
371+
Type: schema.TypeInt,
372+
Optional: true,
373+
Description: "Specify auto-pause delay in second while `db_mode` is `SERVERLESS`. Value range: `[600, 691200]`. Default: `600`.",
374+
},
349375
}
350376

351377
for k, v := range TencentCynosdbInstanceBaseInfo() {

tencentcloud/resource_tc_cynosdb_cluster.go

Lines changed: 50 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,22 @@ func resourceTencentCloudCynosdbClusterCreate(d *schema.ResourceData, meta inter
115115
request.AdminPassword = helper.String(d.Get("password").(string))
116116
request.RollbackStrategy = helper.String("noneRollback")
117117

118+
if v, ok := d.GetOk("db_mode"); ok {
119+
request.DbMode = helper.String(v.(string))
120+
}
121+
if v, ok := d.GetOk("min_cpu"); ok {
122+
request.MinCpu = helper.Float64(v.(float64))
123+
}
124+
if v, ok := d.GetOk("max_cpu"); ok {
125+
request.MaxCpu = helper.Float64(v.(float64))
126+
}
127+
if v, ok := d.GetOk("auto_pause"); ok {
128+
request.AutoPause = helper.String(v.(string))
129+
}
130+
if v, ok := d.GetOk("auto_pause_delay"); ok {
131+
request.AutoPauseDelay = helper.IntInt64(v.(int))
132+
}
133+
118134
if v, ok := d.GetOk("storage_limit"); ok {
119135
request.StorageLimit = helper.IntInt64(v.(int))
120136
}
@@ -139,10 +155,17 @@ func resourceTencentCloudCynosdbClusterCreate(d *schema.ResourceData, meta inter
139155
if v, ok := d.GetOk("prarm_template_id"); ok {
140156
request.ParamTemplateId = helper.IntInt64(v.(int))
141157
}
142-
143-
// instance info
144-
request.Cpu = helper.IntInt64(d.Get("instance_cpu_core").(int))
145-
request.Memory = helper.IntInt64(d.Get("instance_memory_size").(int))
158+
isServerless := d.Get("db_mode").(string) == CYNOSDB_SERVERLESS
159+
if v, ok := d.GetOk("instance_cpu_core"); ok {
160+
request.Cpu = helper.IntInt64(v.(int))
161+
} else if !isServerless {
162+
return fmt.Errorf("`instance_cpu_core` is required while creating non-serverless cluster")
163+
}
164+
if v, ok := d.GetOk("instance_memory_size"); ok {
165+
request.Memory = helper.IntInt64(v.(int))
166+
} else if !isServerless {
167+
return fmt.Errorf("`instance_memory_size` is required while creating non-serverless cluster")
168+
}
146169

147170
var chargeType int64 = 0
148171
if v, ok := d.GetOk("charge_type"); ok {
@@ -312,7 +335,7 @@ func resourceTencentCloudCynosdbClusterRead(d *schema.ResourceData, meta interfa
312335

313336
client := meta.(*TencentCloudClient).apiV3Conn
314337
cynosdbService := CynosdbService{client: client}
315-
renewFlag, cluster, has, err := cynosdbService.DescribeClusterById(ctx, id)
338+
item, cluster, has, err := cynosdbService.DescribeClusterById(ctx, id)
316339
if err != nil {
317340
return err
318341
}
@@ -334,7 +357,11 @@ func resourceTencentCloudCynosdbClusterRead(d *schema.ResourceData, meta interfa
334357
_ = d.Set("cluster_status", cluster.Status)
335358
_ = d.Set("create_time", cluster.CreateTime)
336359
_ = d.Set("storage_used", *cluster.UsedStorage/1000/1000)
337-
_ = d.Set("auto_renew_flag", renewFlag)
360+
_ = d.Set("auto_renew_flag", *item.RenewFlag)
361+
362+
if _, ok := d.GetOk("db_mode"); ok || *item.DbMode == CYNOSDB_SERVERLESS {
363+
_ = d.Set("db_mode", item.DbMode)
364+
}
338365

339366
//tag
340367
tagService := &TagService{client: client}
@@ -357,8 +384,10 @@ func resourceTencentCloudCynosdbClusterRead(d *schema.ResourceData, meta interfa
357384
if err != nil {
358385
return err
359386
}
360-
_ = d.Set("instance_cpu_core", instance.Cpu)
361-
_ = d.Set("instance_memory_size", instance.Memory)
387+
if *item.DbMode != CYNOSDB_SERVERLESS {
388+
_ = d.Set("instance_cpu_core", instance.Cpu)
389+
_ = d.Set("instance_memory_size", instance.Memory)
390+
}
362391
_ = d.Set("instance_id", instance.InstanceId)
363392
_ = d.Set("instance_name", instance.InstanceName)
364393
_ = d.Set("instance_status", instance.Status)
@@ -485,6 +514,19 @@ func resourceTencentCloudCynosdbClusterUpdate(d *schema.ResourceData, meta inter
485514
tagService = TagService{client: client}
486515
region = client.Region
487516
)
517+
immutableArgs := []string{
518+
"db_mode",
519+
"min_cpu",
520+
"max_cpu",
521+
"auto_pause",
522+
"auto_pause_delay",
523+
}
524+
525+
for _, a := range immutableArgs {
526+
if d.HasChange(a) {
527+
return fmt.Errorf("argument %s cannot be modified", a)
528+
}
529+
}
488530

489531
d.Partial(true)
490532

tencentcloud/resource_tc_cynosdb_cluster_test.go

Lines changed: 65 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,37 @@ func TestAccTencentCloudCynosdbClusterResource(t *testing.T) {
128128
},
129129
})
130130
}
131+
func TestAccTencentCloudCynosdbClusterResourceServerless(t *testing.T) {
132+
t.Parallel()
133+
resource.Test(t, resource.TestCase{
134+
PreCheck: func() { testAccPreCheck(t) },
135+
Providers: testAccProviders,
136+
CheckDestroy: testAccCheckCynosdbClusterDestroy,
137+
Steps: []resource.TestStep{
138+
{
139+
Config: testAccCynosdbClusterServerless,
140+
Check: resource.ComposeTestCheckFunc(
141+
testAccCheckCynosdbClusterExists("tencentcloud_cynosdb_cluster.foo"),
142+
resource.TestCheckResourceAttr("tencentcloud_cynosdb_cluster.foo", "db_mode", "SERVERLESS"),
143+
),
144+
},
145+
{
146+
ResourceName: "tencentcloud_cynosdb_cluster.foo",
147+
ImportState: true,
148+
ImportStateVerify: true,
149+
ImportStateVerifyIgnore: []string{
150+
"password",
151+
"force_delete",
152+
"storage_limit",
153+
"min_cpu",
154+
"max_cpu",
155+
"auto_pause",
156+
"auto_pause_delay",
157+
},
158+
},
159+
},
160+
})
161+
}
131162

132163
func testAccCheckCynosdbClusterDestroy(s *terraform.State) error {
133164
logId := getLogId(contextNil)
@@ -179,7 +210,7 @@ func testAccCheckCynosdbClusterExists(n string) resource.TestCheckFunc {
179210
}
180211
}
181212

182-
const testAccCynosdbBasic = `
213+
const testAccCynosdbBasic = defaultSecurityGroupData + `
183214
variable "availability_zone" {
184215
default = "ap-guangzhou-4"
185216
}
@@ -237,10 +268,10 @@ resource "tencentcloud_cynosdb_cluster" "foo" {
237268
force_delete = true
238269
239270
rw_group_sg = [
240-
"` + defaultSecurityGroup + `",
271+
local.sg_id
241272
]
242273
ro_group_sg = [
243-
"` + defaultSecurityGroup + `",
274+
local.sg_id
244275
]
245276
prarm_template_id = var.my_param_template
246277
}
@@ -289,10 +320,39 @@ resource "tencentcloud_cynosdb_cluster" "foo" {
289320
force_delete = true
290321
291322
rw_group_sg = [
292-
"` + defaultSecurityGroup2 + `",
323+
local.sg_id2
293324
]
294325
ro_group_sg = [
295-
"` + defaultSecurityGroup2 + `",
326+
local.sg_id2
296327
]
297328
}
298329
`
330+
331+
const testAccCynosdbClusterServerless = testAccCynosdbBasic + `
332+
resource "tencentcloud_cynosdb_cluster" "foo" {
333+
available_zone = var.availability_zone
334+
vpc_id = var.my_vpc
335+
subnet_id = var.my_subnet
336+
db_type = "MYSQL"
337+
db_version = "5.7"
338+
cluster_name = "tf-cynosdb-s"
339+
password = "cynos@123"
340+
db_mode = "SERVERLESS"
341+
min_cpu = 0.25
342+
max_cpu = 1
343+
auto_pause = "yes"
344+
auto_pause_delay = 1000
345+
instance_maintain_duration = 3600
346+
instance_maintain_start_time = 10800
347+
instance_maintain_weekdays = [
348+
"Fri",
349+
"Mon",
350+
"Sat",
351+
"Sun",
352+
"Thu",
353+
"Wed",
354+
"Tue",
355+
]
356+
357+
force_delete = true
358+
}`

tencentcloud/service_tencentcloud_cynosdb.go

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,12 @@ func (me *CynosdbService) DescribeClusters(ctx context.Context, filters map[stri
8181
return
8282
}
8383

84-
func (me *CynosdbService) DescribeClusterById(ctx context.Context, clusterId string) (renewFlag int64, clusterInfo *cynosdb.CynosdbClusterDetail, has bool, errRet error) {
84+
/**
85+
Return values:
86+
clusterItem: ResponseBody of DescribeClusters, include `renew_flag` and `db_mode`
87+
clusterInfo: ResponseBody of DescribeClusterDetailResponse, primary args setter.
88+
*/
89+
func (me *CynosdbService) DescribeClusterById(ctx context.Context, clusterId string) (clusterItem *cynosdb.CynosdbCluster, clusterInfo *cynosdb.CynosdbClusterDetail, has bool, errRet error) {
8590
logId := getLogId(ctx)
8691
request := cynosdb.NewDescribeClusterDetailRequest()
8792
request.ClusterId = &clusterId
@@ -102,15 +107,20 @@ func (me *CynosdbService) DescribeClusterById(ctx context.Context, clusterId str
102107
if len(clusters) != 1 {
103108
return resource.NonRetryableError(fmt.Errorf("[CRITAL] mutiple cluster found by cluster id %s", clusterId))
104109
}
105-
if *clusters[0].Status == CYNOSDB_STATUS_ISOLATED || *clusters[0].Status == CYNOSDB_STATUS_OFFLINE || *clusters[0].Status == CYNOSDB_STATUS_DELETED {
106-
notExist = true
110+
clusterItem = clusters[0]
111+
clusterStatus := clusterItem.Status
112+
if clusterStatus == nil {
113+
return resource.NonRetryableError(fmt.Errorf("cluster %s status is nil", clusterId))
114+
}
115+
if *clusterStatus == CYNOSDB_STATUS_RUNNING {
107116
return nil
108-
} else if *clusters[0].Status == CYNOSDB_STATUS_RUNNING {
109-
renewFlag = *clusters[0].RenewFlag
117+
}
118+
if *clusterStatus == CYNOSDB_STATUS_ISOLATED || *clusterStatus == CYNOSDB_STATUS_OFFLINE || *clusterStatus == CYNOSDB_STATUS_DELETED {
119+
notExist = true
110120
return nil
111-
} else {
112-
return resource.RetryableError(fmt.Errorf("cynosdb cluster %s is still in processing", clusterId))
113121
}
122+
123+
return resource.RetryableError(fmt.Errorf("cynosdb cluster %s is still in processing", clusterId))
114124
})
115125
if errRet != nil || notExist {
116126
return

website/docs/r/cynosdb_cluster.html.markdown

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,17 +68,22 @@ The following arguments are supported:
6868
* `cluster_name` - (Required, String, ForceNew) Name of CynosDB cluster.
6969
* `db_type` - (Required, String, ForceNew) Type of CynosDB, and available values include `MYSQL`.
7070
* `db_version` - (Required, String, ForceNew) Version of CynosDB, which is related to `db_type`. For `MYSQL`, available value is `5.7`.
71-
* `instance_cpu_core` - (Required, Int) The number of CPU cores of read-write type instance in the CynosDB cluster. Note: modification of this field will take effect immediately, if want to upgrade on maintenance window, please upgrade from console.
72-
* `instance_memory_size` - (Required, Int) Memory capacity of read-write type instance, unit in GB. Note: modification of this field will take effect immediately, if want to upgrade on maintenance window, please upgrade from console.
7371
* `password` - (Required, String, ForceNew) Password of `root` account.
7472
* `subnet_id` - (Required, String, ForceNew) ID of the subnet within this VPC.
7573
* `vpc_id` - (Required, String, ForceNew) ID of the VPC.
74+
* `auto_pause_delay` - (Optional, Int) Specify auto-pause delay in second while `db_mode` is `SERVERLESS`. Value range: `[600, 691200]`. Default: `600`.
75+
* `auto_pause` - (Optional, String) Specify whether the cluster can auto-pause while `db_mode` is `SERVERLESS`. Values: `yes` (default), `no`.
7676
* `auto_renew_flag` - (Optional, Int, ForceNew) Auto renew flag. Valid values are `0`(MANUAL_RENEW), `1`(AUTO_RENEW). Default value is `0`. Only works for PREPAID cluster.
7777
* `charge_type` - (Optional, String, ForceNew) The charge type of instance. Valid values are `PREPAID` and `POSTPAID_BY_HOUR`. Default value is `POSTPAID_BY_HOUR`.
78+
* `db_mode` - (Optional, String) Specify DB mode, only available when `db_type` is `MYSQL`. Values: `NORMAL` (Default), `SERVERLESS`.
7879
* `force_delete` - (Optional, Bool) Indicate whether to delete cluster instance directly or not. Default is false. If set true, the cluster and its `All RELATED INSTANCES` will be deleted instead of staying recycle bin. Note: works for both `PREPAID` and `POSTPAID_BY_HOUR` cluster.
80+
* `instance_cpu_core` - (Optional, Int) The number of CPU cores of read-write type instance in the CynosDB cluster. Required while creating normal cluster. Note: modification of this field will take effect immediately, if want to upgrade on maintenance window, please upgrade from console.
7981
* `instance_maintain_duration` - (Optional, Int) Duration time for maintenance, unit in second. `3600` by default.
8082
* `instance_maintain_start_time` - (Optional, Int) Offset time from 00:00, unit in second. For example, 03:00am should be `10800`. `10800` by default.
8183
* `instance_maintain_weekdays` - (Optional, Set: [`String`]) Weekdays for maintenance. `["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]` by default.
84+
* `instance_memory_size` - (Optional, Int) Memory capacity of read-write type instance, unit in GB. Required while creating normal cluster. Note: modification of this field will take effect immediately, if want to upgrade on maintenance window, please upgrade from console.
85+
* `max_cpu` - (Optional, Float64) Maximum CPU core count, required while `db_mode` is `SERVERLESS`, request DescribeServerlessInstanceSpecs for more reference.
86+
* `min_cpu` - (Optional, Float64) Minimum CPU core count, required while `db_mode` is `SERVERLESS`, request DescribeServerlessInstanceSpecs for more reference.
8287
* `param_items` - (Optional, List) Specify parameter list of database. It is valid when prarm_template_id is set in create cluster. Use `data.tencentcloud_mysql_default_params` to query available parameter details.
8388
* `port` - (Optional, Int, ForceNew) Port of CynosDB cluster.
8489
* `prarm_template_id` - (Optional, Int) The ID of the parameter template.

website/docs/r/cynosdb_readonly_instance.html.markdown

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,13 @@ resource "tencentcloud_cynosdb_readonly_instance" "foo" {
4040
The following arguments are supported:
4141

4242
* `cluster_id` - (Required, String, ForceNew) Cluster ID which the readonly instance belongs to.
43-
* `instance_cpu_core` - (Required, Int) The number of CPU cores of read-write type instance in the CynosDB cluster. Note: modification of this field will take effect immediately, if want to upgrade on maintenance window, please upgrade from console.
44-
* `instance_memory_size` - (Required, Int) Memory capacity of read-write type instance, unit in GB. Note: modification of this field will take effect immediately, if want to upgrade on maintenance window, please upgrade from console.
4543
* `instance_name` - (Required, String, ForceNew) Name of instance.
4644
* `force_delete` - (Optional, Bool) Indicate whether to delete readonly instance directly or not. Default is false. If set true, instance will be deleted instead of staying recycle bin. Note: works for both `PREPAID` and `POSTPAID_BY_HOUR` cluster.
45+
* `instance_cpu_core` - (Optional, Int) The number of CPU cores of read-write type instance in the CynosDB cluster. Required while creating normal cluster. Note: modification of this field will take effect immediately, if want to upgrade on maintenance window, please upgrade from console.
4746
* `instance_maintain_duration` - (Optional, Int) Duration time for maintenance, unit in second. `3600` by default.
4847
* `instance_maintain_start_time` - (Optional, Int) Offset time from 00:00, unit in second. For example, 03:00am should be `10800`. `10800` by default.
4948
* `instance_maintain_weekdays` - (Optional, Set: [`String`]) Weekdays for maintenance. `["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]` by default.
49+
* `instance_memory_size` - (Optional, Int) Memory capacity of read-write type instance, unit in GB. Required while creating normal cluster. Note: modification of this field will take effect immediately, if want to upgrade on maintenance window, please upgrade from console.
5050

5151
## Attributes Reference
5252

0 commit comments

Comments
 (0)