Skip to content

Commit

Permalink
Merge pull request #35774 from hashicorp/b-launch-template
Browse files Browse the repository at this point in the history
Fix launch template bug with versions
  • Loading branch information
nam054 committed Feb 13, 2024
2 parents 1202377 + e9392dc commit 098803b
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 20 deletions.
3 changes: 3 additions & 0 deletions .changelog/35774.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
resource/aws_autoscaling_group: Fix version to computed for inconsistent final plan issue
```
47 changes: 28 additions & 19 deletions internal/service/autoscaling/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,7 @@ func ResourceGroup() *schema.Resource {
"version": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ValidateFunc: validation.StringLenBetween(1, 255),
},
},
Expand Down Expand Up @@ -444,7 +445,7 @@ func ResourceGroup() *schema.Resource {
"version": {
Type: schema.TypeString,
Optional: true,
Default: "$Default",
Computed: true,
},
},
},
Expand Down Expand Up @@ -748,7 +749,7 @@ func ResourceGroup() *schema.Resource {
"version": {
Type: schema.TypeString,
Optional: true,
Default: "$Default",
Computed: true,
},
},
},
Expand Down Expand Up @@ -1079,7 +1080,7 @@ func resourceGroupCreate(ctx context.Context, d *schema.ResourceData, meta inter
}

if v, ok := d.GetOk("launch_template"); ok && len(v.([]interface{})) > 0 && v.([]interface{})[0] != nil {
createInput.LaunchTemplate = expandLaunchTemplateSpecification(v.([]interface{})[0].(map[string]interface{}))
createInput.LaunchTemplate = expandLaunchTemplateSpecification(v.([]interface{})[0].(map[string]interface{}), false)
}

if v, ok := d.GetOk("load_balancers"); ok && v.(*schema.Set).Len() > 0 {
Expand All @@ -1091,7 +1092,7 @@ func resourceGroupCreate(ctx context.Context, d *schema.ResourceData, meta inter
}

if v, ok := d.GetOk("mixed_instances_policy"); ok && len(v.([]interface{})) > 0 && v.([]interface{})[0] != nil {
createInput.MixedInstancesPolicy = expandMixedInstancesPolicy(v.([]interface{})[0].(map[string]interface{}))
createInput.MixedInstancesPolicy = expandMixedInstancesPolicy(v.([]interface{})[0].(map[string]interface{}), true)
}

if v, ok := d.GetOk("placement_group"); ok {
Expand Down Expand Up @@ -1403,7 +1404,7 @@ func resourceGroupUpdate(ctx context.Context, d *schema.ResourceData, meta inter

if d.HasChange("launch_template") {
if v, ok := d.GetOk("launch_template"); ok && len(v.([]interface{})) > 0 && v.([]interface{})[0] != nil {
input.LaunchTemplate = expandLaunchTemplateSpecification(v.([]interface{})[0].(map[string]interface{}))
input.LaunchTemplate = expandLaunchTemplateSpecification(v.([]interface{})[0].(map[string]interface{}), false)
}
shouldRefreshInstances = true
}
Expand All @@ -1423,7 +1424,7 @@ func resourceGroupUpdate(ctx context.Context, d *schema.ResourceData, meta inter

if d.HasChange("mixed_instances_policy") {
if v, ok := d.GetOk("mixed_instances_policy"); ok && len(v.([]interface{})) > 0 && v.([]interface{})[0] != nil {
input.MixedInstancesPolicy = expandMixedInstancesPolicy(v.([]interface{})[0].(map[string]interface{}))
input.MixedInstancesPolicy = expandMixedInstancesPolicy(v.([]interface{})[0].(map[string]interface{}), true)
}
shouldRefreshInstances = true
}
Expand Down Expand Up @@ -1622,13 +1623,13 @@ func resourceGroupUpdate(ctx context.Context, d *schema.ResourceData, meta inter
var launchTemplate *autoscaling.LaunchTemplateSpecification

if v, ok := d.GetOk("launch_template"); ok && len(v.([]interface{})) > 0 && v.([]interface{})[0] != nil {
launchTemplate = expandLaunchTemplateSpecification(v.([]interface{})[0].(map[string]interface{}))
launchTemplate = expandLaunchTemplateSpecification(v.([]interface{})[0].(map[string]interface{}), false)
}

var mixedInstancesPolicy *autoscaling.MixedInstancesPolicy

if v, ok := d.GetOk("mixed_instances_policy"); ok && len(v.([]interface{})) > 0 && v.([]interface{})[0] != nil {
mixedInstancesPolicy = expandMixedInstancesPolicy(v.([]interface{})[0].(map[string]interface{}))
mixedInstancesPolicy = expandMixedInstancesPolicy(v.([]interface{})[0].(map[string]interface{}), true)
}

if err := startInstanceRefresh(ctx, conn, expandStartInstanceRefreshInput(d.Id(), tfMap, launchTemplate, mixedInstancesPolicy)); err != nil {
Expand Down Expand Up @@ -2802,25 +2803,25 @@ func expandInstancesDistribution(tfMap map[string]interface{}) *autoscaling.Inst
return apiObject
}

func expandLaunchTemplate(tfMap map[string]interface{}) *autoscaling.LaunchTemplate {
func expandLaunchTemplate(tfMap map[string]interface{}, hasDefaultVersion bool) *autoscaling.LaunchTemplate {
if tfMap == nil {
return nil
}

apiObject := &autoscaling.LaunchTemplate{}

if v, ok := tfMap["launch_template_specification"].([]interface{}); ok && len(v) > 0 {
apiObject.LaunchTemplateSpecification = expandLaunchTemplateSpecificationForMixedInstancesPolicy(v[0].(map[string]interface{}))
apiObject.LaunchTemplateSpecification = expandLaunchTemplateSpecificationForMixedInstancesPolicy(v[0].(map[string]interface{}), hasDefaultVersion)
}

if v, ok := tfMap["override"].([]interface{}); ok && len(v) > 0 {
apiObject.Overrides = expandLaunchTemplateOverrideses(v)
apiObject.Overrides = expandLaunchTemplateOverrideses(v, hasDefaultVersion)
}

return apiObject
}

func expandLaunchTemplateOverrides(tfMap map[string]interface{}) *autoscaling.LaunchTemplateOverrides {
func expandLaunchTemplateOverrides(tfMap map[string]interface{}, hasDefaultVersion bool) *autoscaling.LaunchTemplateOverrides {
if tfMap == nil {
return nil
}
Expand All @@ -2832,7 +2833,7 @@ func expandLaunchTemplateOverrides(tfMap map[string]interface{}) *autoscaling.La
}

if v, ok := tfMap["launch_template_specification"].([]interface{}); ok && len(v) > 0 {
apiObject.LaunchTemplateSpecification = expandLaunchTemplateSpecificationForMixedInstancesPolicy(v[0].(map[string]interface{}))
apiObject.LaunchTemplateSpecification = expandLaunchTemplateSpecificationForMixedInstancesPolicy(v[0].(map[string]interface{}), hasDefaultVersion)
}

if v, ok := tfMap["instance_type"].(string); ok && v != "" {
Expand All @@ -2846,7 +2847,7 @@ func expandLaunchTemplateOverrides(tfMap map[string]interface{}) *autoscaling.La
return apiObject
}

func expandLaunchTemplateOverrideses(tfList []interface{}) []*autoscaling.LaunchTemplateOverrides {
func expandLaunchTemplateOverrideses(tfList []interface{}, hasDefaultVersion bool) []*autoscaling.LaunchTemplateOverrides {
if len(tfList) == 0 {
return nil
}
Expand All @@ -2860,7 +2861,7 @@ func expandLaunchTemplateOverrideses(tfList []interface{}) []*autoscaling.Launch
continue
}

apiObject := expandLaunchTemplateOverrides(tfMap)
apiObject := expandLaunchTemplateOverrides(tfMap, hasDefaultVersion)

if apiObject == nil {
continue
Expand Down Expand Up @@ -3154,7 +3155,7 @@ func expandVCPUCountRequest(tfMap map[string]interface{}) *autoscaling.VCpuCount
return apiObject
}

func expandLaunchTemplateSpecificationForMixedInstancesPolicy(tfMap map[string]interface{}) *autoscaling.LaunchTemplateSpecification {
func expandLaunchTemplateSpecificationForMixedInstancesPolicy(tfMap map[string]interface{}, hasDefaultVersion bool) *autoscaling.LaunchTemplateSpecification {
if tfMap == nil {
return nil
}
Expand All @@ -3170,14 +3171,18 @@ func expandLaunchTemplateSpecificationForMixedInstancesPolicy(tfMap map[string]i
apiObject.LaunchTemplateName = aws.String(v.(string))
}

if hasDefaultVersion {
apiObject.Version = aws.String("$Default")
}

if v, ok := tfMap["version"].(string); ok && v != "" {
apiObject.Version = aws.String(v)
}

return apiObject
}

func expandLaunchTemplateSpecification(tfMap map[string]interface{}) *autoscaling.LaunchTemplateSpecification {
func expandLaunchTemplateSpecification(tfMap map[string]interface{}, hasDefaultVersion bool) *autoscaling.LaunchTemplateSpecification {
if tfMap == nil {
return nil
}
Expand All @@ -3192,14 +3197,18 @@ func expandLaunchTemplateSpecification(tfMap map[string]interface{}) *autoscalin
apiObject.LaunchTemplateName = aws.String(v.(string))
}

if hasDefaultVersion {
apiObject.Version = aws.String("$Default")
}

if v, ok := tfMap["version"].(string); ok && v != "" {
apiObject.Version = aws.String(v)
}

return apiObject
}

func expandMixedInstancesPolicy(tfMap map[string]interface{}) *autoscaling.MixedInstancesPolicy {
func expandMixedInstancesPolicy(tfMap map[string]interface{}, hasDefaultVersion bool) *autoscaling.MixedInstancesPolicy {
if tfMap == nil {
return nil
}
Expand All @@ -3211,7 +3220,7 @@ func expandMixedInstancesPolicy(tfMap map[string]interface{}) *autoscaling.Mixed
}

if v, ok := tfMap["launch_template"].([]interface{}); ok && len(v) > 0 {
apiObject.LaunchTemplate = expandLaunchTemplate(v[0].(map[string]interface{}))
apiObject.LaunchTemplate = expandLaunchTemplate(v[0].(map[string]interface{}), hasDefaultVersion)
}

return apiObject
Expand Down
2 changes: 1 addition & 1 deletion internal/service/autoscaling/group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1333,7 +1333,7 @@ func TestAccAutoScalingGroup_LaunchTemplate_update(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, "launch_template.#", "1"),
resource.TestCheckResourceAttrPair(resourceName, "launch_template.0.id", "aws_launch_template.test", "id"),
resource.TestCheckResourceAttrPair(resourceName, "launch_template.0.name", "aws_launch_template.test", "name"),
resource.TestCheckResourceAttr(resourceName, "launch_template.0.version", ""),
resource.TestCheckResourceAttr(resourceName, "launch_template.0.version", "1"),
),
},
},
Expand Down

0 comments on commit 098803b

Please sign in to comment.