Skip to content

Commit

Permalink
resource/alicloud_ess_scaling_group: Fixed weighted_capacity and spot…
Browse files Browse the repository at this point in the history
…_price_limit is null.
  • Loading branch information
fuliu-zln committed Jul 3, 2024
1 parent 21439f6 commit bbc4ed2
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 17 deletions.
44 changes: 28 additions & 16 deletions alicloud/resource_alicloud_ess_scaling_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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
}
}
Expand Down Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion alicloud/resource_alicloud_ess_scaling_group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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",
},
Expand Down

0 comments on commit bbc4ed2

Please sign in to comment.