Skip to content

Commit

Permalink
TF CreateDBCluster bug fix.
Browse files Browse the repository at this point in the history
  • Loading branch information
zjjuser1 committed Jun 25, 2024
1 parent f086e2b commit 31e6159
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 5 deletions.
2 changes: 1 addition & 1 deletion alicloud/resource_alicloud_polardb_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,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
91 changes: 91 additions & 0 deletions alicloud/resource_alicloud_polardb_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1512,6 +1512,97 @@ func TestAccAliCloudPolarDBCluster_SteadyServerless(t *testing.T) {
})
}

func TestAccAliCloudPolarDBCluster_CreateDBCluster(t *testing.T) {
var v *polardb.DescribeDBClusterAttributeResponse
name := "tf-testAccPolarDBClusterCreateNormal"
resourceId := "alicloud_polardb_cluster.default"
var basicMap = map[string]string{
"description": CHECKSET,
"db_node_class": CHECKSET,
"vswitch_id": CHECKSET,
"db_type": CHECKSET,
"db_version": CHECKSET,
"connection_string": REGEXMATCH + clusterConnectionStringRegexp,
"port": "3306",
}
ra := resourceAttrInit(resourceId, basicMap)
serviceFunc := func() interface{} {
return &PolarDBService{testAccProvider.Meta().(*connectivity.AliyunClient)}
}
rc := resourceCheckInitWithDescribeMethod(resourceId, &v, serviceFunc, "DescribePolarDBClusterAttribute")
rac := resourceAttrCheckInit(rc, ra)

testAccCheck := rac.resourceAttrMapUpdateSet()
testAccConfig := resourceTestAccConfigFunc(resourceId, name, resourcePolarDBClusterConfigDependence)

resource.Test(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
},

// module name
IDRefreshName: resourceId,

Providers: testAccProviders,
CheckDestroy: rac.checkResourceDestroy(),
Steps: []resource.TestStep{
{
Config: testAccConfig(map[string]interface{}{
"db_type": "MySQL",
"db_version": "8.0",
"pay_type": "PostPaid",
"db_node_count": "2",
"db_node_class": "${data.alicloud_polardb_node_classes.this.classes.0.supported_engines.0.available_resources.0.db_node_class}",
"vswitch_id": "${local.vswitch_id}",
"zone_id": "${data.alicloud_polardb_node_classes.this.classes.0.zone_id}",
"creation_category": "Normal",
"db_node_num": "2",
"default_time_zone": "+1:00",
"description": "${var.name}",
"resource_group_id": "${data.alicloud_resource_manager_resource_groups.default.ids.0}",
"parameter_group_id": "${data.alicloud_polardb_parameter_groups.default.groups.0.id}",
"lower_case_table_names": "0",
"backup_retention_policy_on_cluster_deletion": "NONE",
}),
Check: resource.ComposeTestCheckFunc(
testAccCheck(map[string]string{
"resource_group_id": CHECKSET,
"zone_id": CHECKSET,
"lower_case_table_names": CHECKSET,
"default_time_zone": CHECKSET,
}),
),
},
{
Config: testAccConfig(map[string]interface{}{
"creation_category": "Normal",
}),
Check: resource.ComposeTestCheckFunc(
testAccCheck(map[string]string{
"creation_category": "Normal",
}),
),
},
{
ResourceName: resourceId,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"modify_type", "creation_option", "db_node_num", "parameter_group_id", "backup_retention_policy_on_cluster_deletion"},
},
{
Config: testAccConfig(map[string]interface{}{
"default_time_zone": "+2:00",
}),
Check: resource.ComposeTestCheckFunc(
testAccCheck(map[string]string{
"default_time_zone": "+2:00",
}),
),
},
},
})
}

func testAccCheckKeyValueInMapsForPolarDB(ps []map[string]interface{}, propName, key, value string) resource.TestCheckFunc {
return func(s *terraform.State) error {
for _, policy := range ps {
Expand Down
9 changes: 5 additions & 4 deletions alicloud/service_alicloud_polardb.go
Original file line number Diff line number Diff line change
Expand Up @@ -981,10 +981,11 @@ func (s *PolarDBService) CreateClusterParamsModifyParameters(d *schema.ResourceD
allConfig := make(map[string]string)
changeParams := []string{"loose_polar_log_bin", "default_time_zone"}
for _, i := range changeParams {
v := d.Get(i)
if v != nil {
config[i] = v
allConfig[i] = fmt.Sprint(v)
if v, ok := d.GetOk(i); ok {
if d.HasChange(i) {
config[i] = v
allConfig[i] = fmt.Sprint(v)
}
}
}
cfg, _ := json.Marshal(config)
Expand Down

0 comments on commit 31e6159

Please sign in to comment.