From bbc4ed2ec4ac60caa9e7bce8f591cf7258b80b93 Mon Sep 17 00:00:00 2001 From: wb-zln530580 Date: Wed, 3 Jul 2024 19:45:04 +0800 Subject: [PATCH] resource/alicloud_ess_scaling_group: Fixed weighted_capacity and spot_price_limit is null. --- .../resource_alicloud_ess_scaling_group.go | 44 ++++++++++++------- ...esource_alicloud_ess_scaling_group_test.go | 6 ++- 2 files changed, 33 insertions(+), 17 deletions(-) diff --git a/alicloud/resource_alicloud_ess_scaling_group.go b/alicloud/resource_alicloud_ess_scaling_group.go index 73efe7a17769..14924c042b41 100644 --- a/alicloud/resource_alicloud_ess_scaling_group.go +++ b/alicloud/resource_alicloud_ess_scaling_group.go @@ -351,13 +351,17 @@ func resourceAliyunEssScalingGroupRead(d *schema.ResourceData, meta interface{}) if v := object["LaunchTemplateOverrides"]; v != nil { result := make([]map[string]interface{}, 0) for _, i := range v.(map[string]interface{})["LaunchTemplateOverride"].([]interface{}) { - r := i.(map[string]interface{}) - f, _ := r["SpotPriceLimit"].(json.Number).Float64() - spotPriceLimit, _ := strconv.ParseFloat(strconv.FormatFloat(f, 'f', 2, 64), 64) + launchTemplateOverride := i.(map[string]interface{}) l := map[string]interface{}{ - "instance_type": r["InstanceType"], - "weighted_capacity": r["WeightedCapacity"], - "spot_price_limit": spotPriceLimit, + "instance_type": launchTemplateOverride["InstanceType"], + } + if launchTemplateOverride["SpotPriceLimit"] != nil { + spotPriceLimitFloatformat, _ := launchTemplateOverride["SpotPriceLimit"].(json.Number).Float64() + spotPriceLimit, _ := strconv.ParseFloat(strconv.FormatFloat(spotPriceLimitFloatformat, 'f', 2, 64), 64) + l["spot_price_limit"] = spotPriceLimit + } + if launchTemplateOverride["weighted_capacity"] != nil { + l["weighted_capacity"] = launchTemplateOverride["WeightedCapacity"] } result = append(result, l) } @@ -528,16 +532,21 @@ func resourceAliyunEssScalingGroupUpdate(d *schema.ResourceData, meta interface{ if d.HasChange("launch_template_override") { v, ok := d.GetOk("launch_template_override") if ok { - launchTemplateOverrides := make([]ess.ModifyScalingGroupLaunchTemplateOverride, 0) - for _, e := range v.(*schema.Set).List() { - pack := e.(map[string]interface{}) - l := ess.ModifyScalingGroupLaunchTemplateOverride{ - InstanceType: pack["instance_type"].(string), - SpotPriceLimit: strconv.FormatFloat(pack["spot_price_limit"].(float64), 'f', 2, 64), - WeightedCapacity: strconv.Itoa(pack["weighted_capacity"].(int)), + launchTemplateOverrides := make([]map[string]interface{}, 0) + for _, rew := range v.(*schema.Set).List() { + item := rew.(map[string]interface{}) + l := map[string]interface{}{ + "InstanceType": item["instance_type"].(string), + } + if item["spot_price_limit"].(float64) != 0 { + l["SpotPriceLimit"] = strconv.FormatFloat(item["spot_price_limit"].(float64), 'f', 2, 64) + } + if item["weighted_capacity"].(int) != 0 { + l["WeightedCapacity"] = strconv.Itoa(item["weighted_capacity"].(int)) } launchTemplateOverrides = append(launchTemplateOverrides, l) } + request["LaunchTemplateOverride"] = &launchTemplateOverrides } } @@ -728,9 +737,12 @@ func buildAlicloudEssScalingGroupArgs(d *schema.ResourceData, meta interface{}) if instanceType, ok := item["instance_type"].(string); ok && instanceType != "" { launchTemplateOverridesMap["InstanceType"] = instanceType } - launchTemplateOverridesMap["SpotPriceLimit"] = strconv.FormatFloat(item["spot_price_limit"].(float64), 'f', 2, 64) - launchTemplateOverridesMap["WeightedCapacity"] = item["weighted_capacity"].(int) - + if item["spot_price_limit"].(float64) != 0 { + launchTemplateOverridesMap["SpotPriceLimit"] = strconv.FormatFloat(item["spot_price_limit"].(float64), 'f', 2, 64) + } + if item["weighted_capacity"].(int) != 0 { + launchTemplateOverridesMap["WeightedCapacity"] = item["weighted_capacity"].(int) + } launchTemplateOverridesMaps = append(launchTemplateOverridesMaps, launchTemplateOverridesMap) } request["LaunchTemplateOverride"] = launchTemplateOverridesMaps diff --git a/alicloud/resource_alicloud_ess_scaling_group_test.go b/alicloud/resource_alicloud_ess_scaling_group_test.go index 3ccfb9c93e34..b0377b90291b 100644 --- a/alicloud/resource_alicloud_ess_scaling_group_test.go +++ b/alicloud/resource_alicloud_ess_scaling_group_test.go @@ -573,6 +573,10 @@ func TestAccAliClouddEssScalingGroup_withLaunchTemplateOverride(t *testing.T) { "removal_policies": []string{"OldestInstance", "NewestInstance"}, "launch_template_id": "${alicloud_ecs_launch_template.default.id}", "launch_template_version": "Default", + "launch_template_override": []map[string]string{{ + "instance_type": "${data.alicloud_instance_types.default.instance_types.0.id}", + }, + }, }), Check: resource.ComposeTestCheckFunc( testAccCheck(nil), @@ -590,7 +594,7 @@ func TestAccAliClouddEssScalingGroup_withLaunchTemplateOverride(t *testing.T) { "launch_template_id": "${alicloud_ecs_launch_template.default1.id}", "launch_template_version": "Latest", "launch_template_override": []map[string]string{{ - "instance_type": "${data.alicloud_instance_types.default.instance_types.0.id}", + "instance_type": "${data.alicloud_instance_types.default.instance_types.1.id}", "weighted_capacity": "4", "spot_price_limit": "2.1", },