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

r/aws_instance: Add support for launch template #10807

Merged
merged 5 commits into from
Jul 21, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 3 additions & 0 deletions .changelog/10807.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
resource/aws_instance: Add support for configuration with Launch Template
```
69 changes: 58 additions & 11 deletions aws/resource_aws_autoscaling_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -687,11 +687,7 @@ func resourceAwsAutoscalingGroupCreate(d *schema.ResourceData, meta interface{})
}

if v, ok := d.GetOk("launch_template"); ok {
var err error
createOpts.LaunchTemplate, err = expandLaunchTemplateSpecification(v.([]interface{}))
if err != nil {
return err
}
createOpts.LaunchTemplate = expandLaunchTemplateSpecification(v.([]interface{}))
}

// Availability Zones are optional if VPC Zone Identifier(s) are specified
Expand Down Expand Up @@ -1055,7 +1051,7 @@ func resourceAwsAutoscalingGroupUpdate(d *schema.ResourceData, meta interface{})

if d.HasChange("launch_template") {
if v, ok := d.GetOk("launch_template"); ok && len(v.([]interface{})) > 0 {
opts.LaunchTemplate, _ = expandLaunchTemplateSpecification(v.([]interface{}))
opts.LaunchTemplate = expandLaunchTemplateSpecification(v.([]interface{}))
}
shouldRefreshInstances = true
}
Expand Down Expand Up @@ -1891,15 +1887,15 @@ func expandAutoScalingInstancesDistribution(l []interface{}) *autoscaling.Instan
return instancesDistribution
}

func expandAutoScalingLaunchTemplate(l []interface{}) *autoscaling.LaunchTemplate {
func expandMixedInstancesLaunchTemplate(l []interface{}) *autoscaling.LaunchTemplate {
if len(l) == 0 || l[0] == nil {
return nil
}

m := l[0].(map[string]interface{})

launchTemplate := &autoscaling.LaunchTemplate{
LaunchTemplateSpecification: expandAutoScalingLaunchTemplateSpecification(m["launch_template_specification"].([]interface{})),
LaunchTemplateSpecification: expandMixedInstancesLaunchTemplateSpecification(m["launch_template_specification"].([]interface{})),
}

if v, ok := m["override"]; ok {
Expand Down Expand Up @@ -1934,7 +1930,7 @@ func expandAutoScalingLaunchTemplateOverride(m map[string]interface{}) *autoscal
}

if v, ok := m["launch_template_specification"]; ok && v.([]interface{}) != nil {
launchTemplateOverrides.LaunchTemplateSpecification = expandAutoScalingLaunchTemplateSpecification(m["launch_template_specification"].([]interface{}))
launchTemplateOverrides.LaunchTemplateSpecification = expandMixedInstancesLaunchTemplateSpecification(m["launch_template_specification"].([]interface{}))
}

if v, ok := m["weighted_capacity"]; ok && v.(string) != "" {
Expand All @@ -1944,7 +1940,7 @@ func expandAutoScalingLaunchTemplateOverride(m map[string]interface{}) *autoscal
return launchTemplateOverrides
}

func expandAutoScalingLaunchTemplateSpecification(l []interface{}) *autoscaling.LaunchTemplateSpecification {
func expandMixedInstancesLaunchTemplateSpecification(l []interface{}) *autoscaling.LaunchTemplateSpecification {
launchTemplateSpecification := &autoscaling.LaunchTemplateSpecification{}

if len(l) == 0 || l[0] == nil {
Expand Down Expand Up @@ -1979,7 +1975,7 @@ func expandAutoScalingMixedInstancesPolicy(l []interface{}) *autoscaling.MixedIn
m := l[0].(map[string]interface{})

mixedInstancesPolicy := &autoscaling.MixedInstancesPolicy{
LaunchTemplate: expandAutoScalingLaunchTemplate(m["launch_template"].([]interface{})),
LaunchTemplate: expandMixedInstancesLaunchTemplate(m["launch_template"].([]interface{})),
}

if v, ok := m["instances_distribution"]; ok {
Expand Down Expand Up @@ -2294,3 +2290,54 @@ func validateAutoScalingGroupInstanceRefreshTriggerFields(i interface{}, path ct

return diag.Errorf("'%s' is not a recognized parameter name for aws_autoscaling_group", v)
}

func expandLaunchTemplateSpecification(specs []interface{}) *autoscaling.LaunchTemplateSpecification {
if len(specs) < 1 {
return nil
}

spec := specs[0].(map[string]interface{})

idValue, idOk := spec["id"]
nameValue, nameOk := spec["name"]

result := &autoscaling.LaunchTemplateSpecification{}

// DescribeAutoScalingGroups returns both name and id but LaunchTemplateSpecification
// allows only one of them to be set
if idOk && idValue != "" {
result.LaunchTemplateId = aws.String(idValue.(string))
} else if nameOk && nameValue != "" {
result.LaunchTemplateName = aws.String(nameValue.(string))
}

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

return result
}

func flattenLaunchTemplateSpecification(lt *autoscaling.LaunchTemplateSpecification) []map[string]interface{} {
if lt == nil {
return []map[string]interface{}{}
}

attrs := map[string]interface{}{}
result := make([]map[string]interface{}, 0)

// id and name are always returned by DescribeAutoscalingGroups
attrs["id"] = aws.StringValue(lt.LaunchTemplateId)
attrs["name"] = aws.StringValue(lt.LaunchTemplateName)

// version is returned only if it was previously set
if lt.Version != nil {
attrs["version"] = aws.StringValue(lt.Version)
} else {
attrs["version"] = nil
}

result = append(result, attrs)

return result
}
Loading