diff --git a/aws/data_source_aws_autoscaling_group.go b/aws/data_source_aws_autoscaling_group.go index 9510fef8e81..ad92aa60af4 100644 --- a/aws/data_source_aws_autoscaling_group.go +++ b/aws/data_source_aws_autoscaling_group.go @@ -49,6 +49,26 @@ func dataSourceAwsAutoscalingGroup() *schema.Resource { Type: schema.TypeString, Computed: true, }, + "launch_template": { + Type: schema.TypeList, + Computed: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "id": { + Type: schema.TypeString, + Computed: true, + }, + "name": { + Type: schema.TypeString, + Computed: true, + }, + "version": { + Type: schema.TypeString, + Computed: true, + }, + }, + }, + }, "load_balancers": { Type: schema.TypeSet, Computed: true, @@ -120,7 +140,7 @@ func dataSourceAwsAutoscalingGroupRead(d *schema.ResourceData, meta interface{}) log.Printf("[DEBUG] Checking for error: %s", err) if err != nil { - return fmt.Errorf("error describing AutoScaling Groups: %s", err) + return fmt.Errorf("error describing AutoScaling Groups: %w", err) } log.Printf("[DEBUG] Found Autoscaling Group: %s", result) @@ -144,15 +164,18 @@ func dataSourceAwsAutoscalingGroupRead(d *schema.ResourceData, meta interface{}) d.Set("name", group.AutoScalingGroupName) d.Set("arn", group.AutoScalingGroupARN) if err := d.Set("availability_zones", aws.StringValueSlice(group.AvailabilityZones)); err != nil { - return err + return fmt.Errorf("error setting availability_zones: %w", err) } d.Set("default_cooldown", group.DefaultCooldown) d.Set("desired_capacity", group.DesiredCapacity) d.Set("health_check_grace_period", group.HealthCheckGracePeriod) d.Set("health_check_type", group.HealthCheckType) d.Set("launch_configuration", group.LaunchConfigurationName) + if err := d.Set("launch_template", flattenLaunchTemplateSpecification(group.LaunchTemplate)); err != nil { + return fmt.Errorf("error setting launch_template: %w", err) + } if err := d.Set("load_balancers", aws.StringValueSlice(group.LoadBalancerNames)); err != nil { - return err + return fmt.Errorf("error setting load_balancers: %w", err) } d.Set("new_instances_protected_from_scale_in", group.NewInstancesProtectedFromScaleIn) d.Set("max_size", group.MaxSize) @@ -161,10 +184,10 @@ func dataSourceAwsAutoscalingGroupRead(d *schema.ResourceData, meta interface{}) d.Set("service_linked_role_arn", group.ServiceLinkedRoleARN) d.Set("status", group.Status) if err := d.Set("target_group_arns", aws.StringValueSlice(group.TargetGroupARNs)); err != nil { - return err + return fmt.Errorf("error setting target_group_arns: %w", err) } if err := d.Set("termination_policies", aws.StringValueSlice(group.TerminationPolicies)); err != nil { - return err + return fmt.Errorf("error setting termination_policies: %w", err) } d.Set("vpc_zone_identifier", group.VPCZoneIdentifier) diff --git a/aws/data_source_aws_autoscaling_group_test.go b/aws/data_source_aws_autoscaling_group_test.go index 0ec717d381e..970215b10fe 100644 --- a/aws/data_source_aws_autoscaling_group_test.go +++ b/aws/data_source_aws_autoscaling_group_test.go @@ -9,9 +9,10 @@ import ( ) func TestAccAwsAutoScalingGroupDataSource_basic(t *testing.T) { - datasourceName := "data.aws_autoscaling_group.good_match" - resourceName := "aws_autoscaling_group.foo" - rName := fmt.Sprintf("tf-test-asg-%d", acctest.RandInt()) + datasourceName := "data.aws_autoscaling_group.test" + resourceName := "aws_autoscaling_group.match" + rName := acctest.RandomWithPrefix("tf-acc-test") + resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, Providers: testAccProviders, @@ -27,6 +28,7 @@ func TestAccAwsAutoScalingGroupDataSource_basic(t *testing.T) { resource.TestCheckResourceAttrPair(datasourceName, "health_check_grace_period", resourceName, "health_check_grace_period"), resource.TestCheckResourceAttrPair(datasourceName, "health_check_type", resourceName, "health_check_type"), resource.TestCheckResourceAttrPair(datasourceName, "launch_configuration", resourceName, "launch_configuration"), + resource.TestCheckResourceAttr(datasourceName, "launch_template.#", "0"), resource.TestCheckResourceAttrPair(datasourceName, "load_balancers.#", resourceName, "load_balancers.#"), resource.TestCheckResourceAttr(datasourceName, "new_instances_protected_from_scale_in", "false"), resource.TestCheckResourceAttrPair(datasourceName, "max_size", resourceName, "max_size"), @@ -39,6 +41,41 @@ func TestAccAwsAutoScalingGroupDataSource_basic(t *testing.T) { }) } +func TestAccAwsAutoScalingGroupDataSource_launchTemplate(t *testing.T) { + datasourceName := "data.aws_autoscaling_group.test" + resourceName := "aws_autoscaling_group.test" + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: testAccAutoScalingGroupDataResourceConfig_launchTemplate(), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttrPair(datasourceName, "name", resourceName, "name"), + resource.TestCheckResourceAttrPair(datasourceName, "arn", resourceName, "arn"), + resource.TestCheckResourceAttrPair(datasourceName, "availability_zones.#", resourceName, "availability_zones.#"), + resource.TestCheckResourceAttrPair(datasourceName, "default_cooldown", resourceName, "default_cooldown"), + resource.TestCheckResourceAttrPair(datasourceName, "desired_capacity", resourceName, "desired_capacity"), + resource.TestCheckResourceAttrPair(datasourceName, "health_check_grace_period", resourceName, "health_check_grace_period"), + resource.TestCheckResourceAttrPair(datasourceName, "health_check_type", resourceName, "health_check_type"), + resource.TestCheckResourceAttr(datasourceName, "launch_configuration", ""), + resource.TestCheckResourceAttrPair(datasourceName, "launch_template.#", resourceName, "launch_template.#"), + resource.TestCheckResourceAttrPair(datasourceName, "load_balancers.#", resourceName, "load_balancers.#"), + resource.TestCheckResourceAttrPair(datasourceName, "load_balancers.0.id", resourceName, "load_balancers.0.id"), + resource.TestCheckResourceAttrPair(datasourceName, "load_balancers.0.name", resourceName, "load_balancers.0.name"), + resource.TestCheckResourceAttrPair(datasourceName, "load_balancers.0.version", resourceName, "load_balancers.0.version"), + resource.TestCheckResourceAttr(datasourceName, "new_instances_protected_from_scale_in", "false"), + resource.TestCheckResourceAttrPair(datasourceName, "max_size", resourceName, "max_size"), + resource.TestCheckResourceAttrPair(datasourceName, "min_size", resourceName, "min_size"), + resource.TestCheckResourceAttrPair(datasourceName, "target_group_arns.#", resourceName, "target_group_arns.#"), + resource.TestCheckResourceAttr(datasourceName, "vpc_zone_identifier", ""), + ), + }, + }, + }) +} + // Lookup based on AutoScalingGroupName func testAccAutoScalingGroupDataResourceConfig(rName string) string { return composeConfig( @@ -46,14 +83,12 @@ func testAccAutoScalingGroupDataResourceConfig(rName string) string { testAccAvailableAZsNoOptInConfig(), testAccAvailableEc2InstanceTypeForAvailabilityZone("data.aws_availability_zones.available.names[0]", "t3.micro", "t2.micro"), fmt.Sprintf(` -resource "aws_launch_configuration" "data_source_aws_autoscaling_group_test" { - name = "%[1]s" - image_id = data.aws_ami.amzn-ami-minimal-hvm-ebs.id - instance_type = data.aws_ec2_instance_type_offering.available.instance_type +data "aws_autoscaling_group" "test" { + name = aws_autoscaling_group.match.name } -resource "aws_autoscaling_group" "foo" { - name = "%[1]s_foo" +resource "aws_autoscaling_group" "match" { + name = "%[1]s_match" max_size = 0 min_size = 0 health_check_grace_period = 300 @@ -64,8 +99,8 @@ resource "aws_autoscaling_group" "foo" { availability_zones = [data.aws_availability_zones.available.names[0], data.aws_availability_zones.available.names[1]] } -resource "aws_autoscaling_group" "bar" { - name = "%[1]s_bar" +resource "aws_autoscaling_group" "no_match" { + name = "%[1]s_no_match" max_size = 0 min_size = 0 health_check_grace_period = 300 @@ -76,8 +111,39 @@ resource "aws_autoscaling_group" "bar" { availability_zones = [data.aws_availability_zones.available.names[0], data.aws_availability_zones.available.names[1]] } -data "aws_autoscaling_group" "good_match" { - name = aws_autoscaling_group.foo.name +resource "aws_launch_configuration" "data_source_aws_autoscaling_group_test" { + name = "%[1]s" + image_id = data.aws_ami.amzn-ami-minimal-hvm-ebs.id + instance_type = data.aws_ec2_instance_type_offering.available.instance_type } `, rName)) } + +func testAccAutoScalingGroupDataResourceConfig_launchTemplate() string { + return composeConfig( + testAccLatestAmazonLinuxHvmEbsAmiConfig(), + testAccAvailableAZsNoOptInConfig(), + testAccAvailableEc2InstanceTypeForAvailabilityZone("data.aws_availability_zones.available.names[0]", "t3.micro", "t2.micro"), + fmt.Sprintf(` +data "aws_autoscaling_group" "test" { + name = aws_autoscaling_group.test.name +} + +resource "aws_autoscaling_group" "test" { + availability_zones = [data.aws_availability_zones.available.names[0]] + desired_capacity = 0 + max_size = 0 + min_size = 0 + launch_template { + id = aws_launch_template.test.id + version = aws_launch_template.test.default_version + } +} + +resource "aws_launch_template" "test" { + name_prefix = "test" + image_id = data.aws_ami.amzn-ami-minimal-hvm-ebs.id + instance_type = data.aws_ec2_instance_type_offering.available.instance_type +} +`)) +}