Skip to content

Commit 04addb6

Browse files
gitmknanonymous
andauthored
feat: mysql account supports max_user_connections (#1874)
* feat: mysql account supports max_user_connections * feat: add changelog * fix: modify account --------- Co-authored-by: anonymous <anonymous@mail.org>
1 parent 66b29ae commit 04addb6

File tree

5 files changed

+90
-10
lines changed

5 files changed

+90
-10
lines changed

.changelog/1874.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_mysql_account: support the `max_user_connections` field.
3+
```

tencentcloud/resource_tc_mysql_account.go

Lines changed: 47 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@ Example Usage
55
66
```hcl
77
resource "tencentcloud_mysql_account" "default" {
8-
mysql_id = "terraform-test-local-database"
9-
name = "tf_account"
10-
password = "********"
11-
description = "My test account"
8+
mysql_id = "terraform-test-local-database"
9+
name = "tf_test"
10+
password = "********"
11+
description = "My test account"
12+
max_user_connections = 10
1213
}
1314
```
1415
@@ -76,6 +77,12 @@ func resourceTencentCloudMysqlAccount() *schema.Resource {
7677
ValidateFunc: validateStringLengthInRange(1, 200),
7778
Description: "Database description.",
7879
},
80+
"max_user_connections": {
81+
Optional: true,
82+
Computed: true,
83+
Type: schema.TypeInt,
84+
Description: "The maximum number of available connections for a new account, the default value is 10240, and the maximum value that can be set is 10240.",
85+
},
7986
},
8087
}
8188
}
@@ -94,9 +101,10 @@ func resourceTencentCloudMysqlAccountCreate(d *schema.ResourceData, meta interfa
94101
accountHost = d.Get("host").(string)
95102
accountPassword = d.Get("password").(string)
96103
accountDescription = d.Get("description").(string)
104+
maxUserConnections = int64(d.Get("max_user_connections").(int))
97105
)
98106

99-
asyncRequestId, err := mysqlService.CreateAccount(ctx, mysqlId, accountName, accountHost, accountPassword, accountDescription)
107+
asyncRequestId, err := mysqlService.CreateAccount(ctx, mysqlId, accountName, accountHost, accountPassword, accountDescription, maxUserConnections)
100108
if err != nil {
101109
return err
102110
}
@@ -191,6 +199,8 @@ func resourceTencentCloudMysqlAccountRead(d *schema.ResourceData, meta interface
191199
_ = d.Set("mysql_id", mysqlId)
192200
_ = d.Set("host", *accountInfo.Host)
193201
_ = d.Set("name", *accountInfo.User)
202+
_ = d.Set("max_user_connections", *accountInfo.MaxUserConnections)
203+
194204
return nil
195205
}
196206
func resourceTencentCloudMysqlAccountUpdate(d *schema.ResourceData, meta interface{}) error {
@@ -274,6 +284,38 @@ func resourceTencentCloudMysqlAccountUpdate(d *schema.ResourceData, meta interfa
274284

275285
}
276286

287+
if d.HasChange("max_user_connections") {
288+
var maxUserConnections int64
289+
if v, ok := d.GetOkExists("max_user_connections"); ok {
290+
maxUserConnections = int64(v.(int))
291+
}
292+
asyncRequestId, err := mysqlService.ModifyAccountMaxUserConnections(ctx, mysqlId, accountName, accountHost, maxUserConnections)
293+
if err != nil {
294+
return err
295+
}
296+
297+
err = resource.Retry(readRetryTimeout, func() *resource.RetryError {
298+
taskStatus, message, err := mysqlService.DescribeAsyncRequestInfo(ctx, asyncRequestId)
299+
if err != nil {
300+
return resource.NonRetryableError(err)
301+
}
302+
if taskStatus == MYSQL_TASK_STATUS_SUCCESS {
303+
return nil
304+
}
305+
if taskStatus == MYSQL_TASK_STATUS_INITIAL || taskStatus == MYSQL_TASK_STATUS_RUNNING {
306+
return resource.RetryableError(fmt.Errorf("%s modify mysql account maxUserConnections %s task status is %s", mysqlId, accountName, taskStatus))
307+
}
308+
err = fmt.Errorf("modify mysql account maxUserConnections task status is %s,we won't wait for it finish ,it show message:%s", taskStatus, message)
309+
return resource.NonRetryableError(err)
310+
})
311+
312+
if err != nil {
313+
log.Printf("[CRITAL]%s modify mysql account maxUserConnections fail, reason:%s\n ", logId, err.Error())
314+
return err
315+
}
316+
317+
}
318+
277319
d.Partial(false)
278320

279321
return nil

tencentcloud/resource_tc_mysql_account_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ func init() {
7777
})
7878
}
7979

80+
// go test -i; go test -test.run TestAccTencentCloudMysqlAccountResource_basic -v
8081
func TestAccTencentCloudMysqlAccountResource_basic(t *testing.T) {
8182
t.Parallel()
8283
resource.Test(t, resource.TestCase{
@@ -91,6 +92,7 @@ func TestAccTencentCloudMysqlAccountResource_basic(t *testing.T) {
9192
resource.TestCheckResourceAttrSet("tencentcloud_mysql_account.mysql_account", "mysql_id"),
9293
resource.TestCheckResourceAttr("tencentcloud_mysql_account.mysql_account", "name", "keep_dbbrain"),
9394
resource.TestCheckResourceAttr("tencentcloud_mysql_account.mysql_account", "description", "test from terraform"),
95+
resource.TestCheckResourceAttr("tencentcloud_mysql_account.mysql_account", "max_user_connections", "10"),
9496
),
9597
},
9698
{
@@ -206,6 +208,7 @@ resource "tencentcloud_mysql_account" "mysql_account" {
206208
host = "192.168.0.%%"
207209
password = "Test@123456#"
208210
description = "test from terraform"
211+
max_user_connections = 10
209212
}
210213
`, CommonPresetMysql)
211214
}

tencentcloud/service_tencentcloud_mysql.go

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ func (me *MysqlService) DescribeCaresParameters(ctx context.Context, instanceId
302302
}
303303

304304
func (me *MysqlService) CreateAccount(ctx context.Context, mysqlId string,
305-
accountName, accountHost, accountPassword, accountDescription string) (asyncRequestId string, errRet error) {
305+
accountName, accountHost, accountPassword, accountDescription string, maxUserConnections int64) (asyncRequestId string, errRet error) {
306306

307307
logId := getLogId(ctx)
308308

@@ -315,6 +315,7 @@ func (me *MysqlService) CreateAccount(ctx context.Context, mysqlId string,
315315
request.Password = &accountPassword
316316
request.Accounts = accountInfos
317317
request.Description = &accountDescription
318+
request.MaxUserConnections = &maxUserConnections
318319

319320
defer func() {
320321
if errRet != nil {
@@ -362,6 +363,35 @@ func (me *MysqlService) ModifyAccountPassword(ctx context.Context, mysqlId strin
362363
return
363364
}
364365

366+
func (me *MysqlService) ModifyAccountMaxUserConnections(ctx context.Context, mysqlId, accountName, accountHost string, maxUserConnections int64) (asyncRequestId string, errRet error) {
367+
368+
logId := getLogId(ctx)
369+
370+
request := cdb.NewModifyAccountMaxUserConnectionsRequest()
371+
372+
var accountInfo = cdb.Account{User: &accountName, Host: &accountHost}
373+
var accountInfos = []*cdb.Account{&accountInfo}
374+
375+
request.InstanceId = &mysqlId
376+
request.Accounts = accountInfos
377+
request.MaxUserConnections = &maxUserConnections
378+
379+
defer func() {
380+
if errRet != nil {
381+
log.Printf("[CRITAL]%s api[%s] fail, request body [%s], reason[%s]\n",
382+
logId, request.GetAction(), request.ToJsonString(), errRet.Error())
383+
}
384+
}()
385+
ratelimit.Check(request.GetAction())
386+
response, err := me.client.UseMysqlClient().ModifyAccountMaxUserConnections(request)
387+
if err != nil {
388+
errRet = err
389+
return
390+
}
391+
asyncRequestId = *response.Response.AsyncRequestId
392+
return
393+
}
394+
365395
func (me *MysqlService) ModifyAccountDescription(ctx context.Context, mysqlId string,
366396
accountName, accountHost, accountDescription string) (asyncRequestId string, errRet error) {
367397

website/docs/r/mysql_account.html.markdown

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,11 @@ Provides a MySQL account resource for database management. A MySQL instance supp
1515

1616
```hcl
1717
resource "tencentcloud_mysql_account" "default" {
18-
mysql_id = "terraform-test-local-database"
19-
name = "tf_account"
20-
password = "********"
21-
description = "My test account"
18+
mysql_id = "terraform-test-local-database"
19+
name = "tf_test"
20+
password = "********"
21+
description = "My test account"
22+
max_user_connections = 10
2223
}
2324
```
2425

@@ -31,6 +32,7 @@ The following arguments are supported:
3132
* `password` - (Required, String) Operation password.
3233
* `description` - (Optional, String) Database description.
3334
* `host` - (Optional, String, ForceNew) Account host, default is `%`.
35+
* `max_user_connections` - (Optional, Int) The maximum number of available connections for a new account, the default value is 10240, and the maximum value that can be set is 10240.
3436

3537
## Attributes Reference
3638

0 commit comments

Comments
 (0)