Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix launch template bug with versions #35774

Merged
merged 6 commits into from
Feb 13, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 29 additions & 19 deletions internal/service/autoscaling/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,7 @@ func ResourceGroup() *schema.Resource {
"launch_configuration": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ExactlyOneOf: []string{"launch_configuration", "launch_template", "mixed_instances_policy"},
},
"launch_template": {
Expand All @@ -327,6 +328,7 @@ func ResourceGroup() *schema.Resource {
"version": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ValidateFunc: validation.StringLenBetween(1, 255),
},
},
Expand Down Expand Up @@ -444,7 +446,7 @@ func ResourceGroup() *schema.Resource {
"version": {
Type: schema.TypeString,
Optional: true,
Default: "$Default",
Computed: true,
},
},
},
Expand Down Expand Up @@ -748,7 +750,7 @@ func ResourceGroup() *schema.Resource {
"version": {
Type: schema.TypeString,
Optional: true,
Default: "$Default",
Computed: true,
},
},
},
Expand Down Expand Up @@ -1079,7 +1081,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 +1093,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 +1405,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 +1425,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 +1624,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 +2804,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 +2834,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 +2848,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 +2862,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 +3156,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 +3172,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 +3198,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 +3221,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
Loading