Skip to content

Commit e5c4d77

Browse files
authored
Fix/mysql db init (#885)
* fix: mysql db init after create * fix: optimize init polling * fix: mysql init vport
1 parent 756c9f6 commit e5c4d77

File tree

2 files changed

+70
-5
lines changed

2 files changed

+70
-5
lines changed

tencentcloud/resource_tc_mysql_instance.go

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -564,7 +564,11 @@ func mysqlCreateInstancePayByUse(ctx context.Context, d *schema.ResourceData, me
564564
if len(response.Response.InstanceIds) != 1 {
565565
return fmt.Errorf("mysql CreateDBInstanceHour return len(InstanceIds) is not 1,but %d", len(response.Response.InstanceIds))
566566
}
567-
d.SetId(*response.Response.InstanceIds[0])
567+
568+
id := *response.Response.InstanceIds[0]
569+
570+
d.SetId(id)
571+
568572
return nil
569573
}
570574

@@ -618,6 +622,52 @@ func resourceTencentCloudMysqlInstanceCreate(d *schema.ResourceData, meta interf
618622
return err
619623
}
620624

625+
// Initialize mysql instance
626+
var (
627+
password = d.Get("root_password").(string)
628+
charset = d.Get("parameters.character_set_server").(string)
629+
lowercase = d.Get("parameters.lower_case_table_names").(string)
630+
vPort int
631+
)
632+
633+
port, portOk := d.GetOk("intranet_port")
634+
635+
if portOk && port.(int) != 0 {
636+
vPort = port.(int)
637+
}
638+
639+
aReqId, err := mysqlService.InitDBInstances(ctx, mysqlID, password, charset, lowercase, vPort)
640+
641+
if err != nil {
642+
return err
643+
}
644+
645+
err = resource.Retry(readRetryTimeout, func() *resource.RetryError {
646+
// Available status:INITIAL, RUNNING, SUCCESS, FAILED, KILLED, REMOVED, PAUSED
647+
taskStatus, message, err := mysqlService.DescribeAsyncRequestInfo(ctx, aReqId)
648+
649+
if err != nil {
650+
if _, ok := err.(*errors.TencentCloudSDKError); !ok {
651+
return resource.RetryableError(err)
652+
} else {
653+
return resource.NonRetryableError(err)
654+
}
655+
}
656+
if taskStatus == MYSQL_TASK_STATUS_SUCCESS {
657+
return nil
658+
}
659+
if taskStatus == MYSQL_TASK_STATUS_INITIAL || taskStatus == MYSQL_TASK_STATUS_RUNNING {
660+
return resource.RetryableError(fmt.Errorf("create account task status is %s", taskStatus))
661+
}
662+
err = fmt.Errorf("initialize db task status is %s,we won't wait for it finish ,it show message:%s", ",",
663+
message)
664+
return resource.NonRetryableError(err)
665+
})
666+
667+
if err != nil {
668+
log.Printf("[WARN] initial DB error: %s", err.Error())
669+
}
670+
621671
//internet service
622672
internetService := d.Get("internet_service").(int)
623673
if internetService == 1 {

tencentcloud/service_tencentcloud_mysql.go

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -932,16 +932,31 @@ func (me *MysqlService) DescribeDBInstanceConfig(ctx context.Context, mysqlId st
932932
return
933933
}
934934

935-
func (me *MysqlService) InitDBInstances(ctx context.Context, mysqlId string, password string) (asyncRequestId string, errRet error) {
935+
func (me *MysqlService) InitDBInstances(ctx context.Context, mysqlId, password, charset, lowerCase string, port int) (asyncRequestId string, errRet error) {
936936
logId := getLogId(ctx)
937937
request := cdb.NewInitDBInstancesRequest()
938938
request.InstanceIds = []*string{&mysqlId}
939-
request.NewPassword = &password
939+
if password != "" {
940+
request.NewPassword = &password
941+
}
942+
943+
if port != 0 {
944+
request.Vport = helper.IntInt64(port)
945+
}
940946

941947
paramsMap := map[string]string{
942-
"character_set_server": "utf8mb4", // ["utf8","latin1","gbk","utf8mb4"]
943-
"lower_case_table_names": "1", // ["0","1"]
948+
"character_set_server": "LATIN1", // ["utf8","latin1","gbk","utf8mb4"]
949+
"lower_case_table_names": "1", // ["0","1"]
950+
}
951+
952+
if charset != "" {
953+
paramsMap["character_set_server"] = charset // ["utf8","latin1","gbk","utf8mb4"]
954+
}
955+
956+
if lowerCase != "" {
957+
paramsMap["lower_case_table_names"] = lowerCase // ["0","1"]
944958
}
959+
945960
request.Parameters = make([]*cdb.ParamInfo, 0, len(paramsMap))
946961
for k, v := range paramsMap {
947962
name := k

0 commit comments

Comments
 (0)