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

Adds launch_template to aws_autoscaling_group data source #16297

Merged
merged 1 commit into from
Dec 14, 2020
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
33 changes: 28 additions & 5 deletions aws/data_source_aws_autoscaling_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)

Expand Down
92 changes: 79 additions & 13 deletions aws/data_source_aws_autoscaling_group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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"),
Expand All @@ -39,21 +41,54 @@ 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(
testAccLatestAmazonLinuxHvmEbsAmiConfig(),
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
Expand All @@ -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
Expand All @@ -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
}
`))
}