From 31e61599007b2823f553142967c3f2eb75bd5baa Mon Sep 17 00:00:00 2001 From: zjj01575524 <1174304468@qq.com> Date: Tue, 25 Jun 2024 16:02:58 +0800 Subject: [PATCH] TF CreateDBCluster bug fix. --- alicloud/resource_alicloud_polardb_cluster.go | 2 +- .../resource_alicloud_polardb_cluster_test.go | 91 +++++++++++++++++++ alicloud/service_alicloud_polardb.go | 9 +- 3 files changed, 97 insertions(+), 5 deletions(-) diff --git a/alicloud/resource_alicloud_polardb_cluster.go b/alicloud/resource_alicloud_polardb_cluster.go index 9b85bee39e37..f258bd8c5b89 100644 --- a/alicloud/resource_alicloud_polardb_cluster.go +++ b/alicloud/resource_alicloud_polardb_cluster.go @@ -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) } diff --git a/alicloud/resource_alicloud_polardb_cluster_test.go b/alicloud/resource_alicloud_polardb_cluster_test.go index de7280edb3d0..e10b486d3cf1 100644 --- a/alicloud/resource_alicloud_polardb_cluster_test.go +++ b/alicloud/resource_alicloud_polardb_cluster_test.go @@ -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 { diff --git a/alicloud/service_alicloud_polardb.go b/alicloud/service_alicloud_polardb.go index 2ea4da70bcd5..b74fd1aa9f05 100644 --- a/alicloud/service_alicloud_polardb.go +++ b/alicloud/service_alicloud_polardb.go @@ -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)