Skip to content

Commit

Permalink
resource/alicloud_polardb_cluster: create dbCluster reduce time consu…
Browse files Browse the repository at this point in the history
…mption
  • Loading branch information
mengliux committed Jun 24, 2024
1 parent f8e300d commit b000750
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 9 deletions.
2 changes: 2 additions & 0 deletions alicloud/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,8 @@ const DefaultTimeoutMedium = 500
// timeout for long time progerss product, rds e.g.
const DefaultLongTimeout = 1000

const LongTimeOut = 1200

const DefaultIntervalMini = 2

const DefaultIntervalShort = 5
Expand Down
36 changes: 30 additions & 6 deletions alicloud/resource_alicloud_polardb_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -405,9 +405,10 @@ func resourceAlicloudPolarDBCluster() *schema.Resource {
DiffSuppressFunc: polardbDiffSuppressFunc,
},
"db_node_num": {
Type: schema.TypeInt,
Optional: true,
ValidateFunc: IntInSlice([]int{1, 2}),
Type: schema.TypeInt,
Optional: true,
ValidateFunc: IntInSlice([]int{1, 2}),
DiffSuppressFunc: polardbProxyTypeDiffSuppressFunc,
},
"parameter_group_id": {
Type: schema.TypeString,
Expand Down Expand Up @@ -512,16 +513,33 @@ func resourceAlicloudPolarDBClusterCreate(d *schema.ResourceData, meta interface
d.SetId(fmt.Sprint(response["DBClusterId"]))

// wait cluster status change from Creating to running
stateConf := BuildStateConf([]string{"Creating"}, []string{"Running"}, d.Timeout(schema.TimeoutCreate), 5*time.Minute, polarDBService.PolarDBClusterStateRefreshFunc(d.Id(), []string{"Deleting"}))
stateConf := BuildStateConf([]string{"Creating", "WarmCreating"}, []string{"Running"}, 10*time.Minute, 10*time.Second, polarDBService.PolarDBClusterStateRefreshFunc(d.Id(), []string{"Deleting"}))
if _, err := stateConf.WaitForState(); err != nil {
return WrapErrorf(err, IdMsg, d.Id())
}
if v, ok := d.GetOk("db_type"); ok && v.(string) == "MySQL" {
categoryConf := BuildStateConf([]string{}, []string{"Normal", "Basic", "ArchiveNormal", "NormalMultimaster", "SENormal"}, d.Timeout(schema.TimeoutUpdate), 3*time.Minute, polarDBService.PolarDBClusterCategoryRefreshFunc(d.Id(), []string{}))
categoryConf := BuildStateConf([]string{}, []string{"Normal", "Basic", "ArchiveNormal", "NormalMultimaster", "SENormal"}, 10*time.Minute, 10*time.Second, polarDBService.PolarDBClusterCategoryRefreshFunc(d.Id(), []string{}))
if _, err := categoryConf.WaitForState(); err != nil {
return WrapErrorf(err, IdMsg, d.Id())
}
}
allConfig := make(map[string]string)
if v, ok := d.GetOk("loose_polar_log_bin"); ok {
allConfig["loose_polar_log_bin"] = fmt.Sprint(v)
}
if v, ok := d.GetOk("default_time_zone"); ok {
allConfig["default_time_zone"] = fmt.Sprint(v)
}
if v, ok := d.GetOk("lower_case_table_names"); ok {
allConfig["lower_case_table_names"] = fmt.Sprint(v)
}
// wait instance parameter expect after modifying
if len(allConfig) > 0 {
if err := polarDBService.WaitForPolarDBParameter(d.Id(), DefaultLongTimeout, allConfig); err != nil {
return WrapError(err)
}
}

return resourceAlicloudPolarDBClusterUpdate(d, meta)
}

Expand All @@ -530,7 +548,7 @@ func resourceAlicloudPolarDBClusterUpdate(d *schema.ResourceData, meta interface
polarDBService := PolarDBService{client}
d.Partial(true)

if d.HasChange("default_time_zone") || d.HasChange("lower_case_table_names") || d.HasChange("loose_polar_log_bin") {
if !d.IsNewResource() && (d.HasChange("default_time_zone") || d.HasChange("lower_case_table_names") || d.HasChange("loose_polar_log_bin")) {
if err := polarDBService.CreateClusterParamsModifyParameters(d); err != nil {
return WrapError(err)
}
Expand Down Expand Up @@ -1764,6 +1782,12 @@ func buildPolarDBCreateRequest(d *schema.ResourceData, meta interface{}) (map[st
request["DefaultTimeZone"] = v.(string)
}

if v, ok := d.GetOk("security_ips"); ok {
ipList := expandStringList(v.(*schema.Set).List())
ipstr := strings.Join(ipList[:], COMMA_SEPARATED)
request["SecurityIPList"] = ipstr
}

return request, nil
}

Expand Down
1 change: 0 additions & 1 deletion alicloud/resource_alicloud_polardb_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,6 @@ func TestAccAliCloudPolarDBCluster_Update(t *testing.T) {
{
Config: testAccConfig(map[string]interface{}{
"db_node_count": "3",
"imci_switch": "ON",
}),
Check: resource.ComposeTestCheckFunc(
testAccCheck(map[string]string{
Expand Down
4 changes: 2 additions & 2 deletions alicloud/service_alicloud_polardb.go
Original file line number Diff line number Diff line change
Expand Up @@ -965,7 +965,7 @@ func (s *PolarDBService) ModifyParameters(d *schema.ResourceData) error {
value := i.(map[string]interface{})["value"].(string)
allConfig[key] = value
}
if err := s.WaitForPolarDBParameter(d.Id(), DefaultTimeoutMedium, allConfig); err != nil {
if err := s.WaitForPolarDBParameter(d.Id(), DefaultLongTimeout, allConfig); err != nil {
return WrapError(err)
}
}
Expand Down Expand Up @@ -1002,7 +1002,7 @@ func (s *PolarDBService) CreateClusterParamsModifyParameters(d *schema.ResourceD

addDebug(request.GetActionName(), raw, request.RpcRequest, request)
// wait instance parameter expect after modifying
if err := s.WaitForPolarDBParameter(d.Id(), DefaultLongTimeout, allConfig); err != nil {
if err := s.WaitForPolarDBParameter(d.Id(), LongTimeOut, allConfig); err != nil {
return WrapError(err)
}
for _, i := range changeParams {
Expand Down

0 comments on commit b000750

Please sign in to comment.