Skip to content

Commit

Permalink
Merge pull request #10807 from gordonbondon/instance-launch-template
Browse files Browse the repository at this point in the history
r/aws_instance: Add support for launch template
  • Loading branch information
gdavison authored Jul 21, 2021
2 parents 209ef79 + af1c082 commit 27eba3b
Show file tree
Hide file tree
Showing 8 changed files with 781 additions and 87 deletions.
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

0 comments on commit 27eba3b

Please sign in to comment.