From 661280db60a62c55d71f351c3680c40031318c55 Mon Sep 17 00:00:00 2001 From: Guimove Date: Tue, 4 Sep 2018 15:41:04 +0200 Subject: [PATCH 1/3] Add arns as an output to data aws_autoscaling_groups and doc --- aws/data_source_aws_autoscaling_groups.go | 38 ++++++++++++++++--- ...data_source_aws_autoscaling_groups_test.go | 1 + .../docs/d/autoscaling_groups.html.markdown | 1 + 3 files changed, 35 insertions(+), 5 deletions(-) diff --git a/aws/data_source_aws_autoscaling_groups.go b/aws/data_source_aws_autoscaling_groups.go index 7bc4dc9c350..92468774746 100644 --- a/aws/data_source_aws_autoscaling_groups.go +++ b/aws/data_source_aws_autoscaling_groups.go @@ -21,6 +21,11 @@ func dataSourceAwsAutoscalingGroups() *schema.Resource { Computed: true, Elem: &schema.Schema{Type: schema.TypeString}, }, + "arns": { + Type: schema.TypeList, + Computed: true, + Elem: &schema.Schema{Type: schema.TypeString}, + }, "filter": { Type: schema.TypeSet, Optional: true, @@ -49,7 +54,8 @@ func dataSourceAwsAutoscalingGroupsRead(d *schema.ResourceData, meta interface{} log.Printf("[DEBUG] Reading Autoscaling Groups.") d.SetId(time.Now().UTC().String()) - var raw []string + var rawName []string + var rawArn []string var err error tf := d.Get("filter").(*schema.Set) @@ -59,14 +65,31 @@ func dataSourceAwsAutoscalingGroupsRead(d *schema.ResourceData, meta interface{} } err = conn.DescribeTagsPages(input, func(resp *autoscaling.DescribeTagsOutput, lastPage bool) bool { for _, v := range resp.Tags { - raw = append(raw, aws.StringValue(v.ResourceId)) + rawName = append(rawName, aws.StringValue(v.ResourceId)) } return !lastPage }) + + for _, v := range rawName { + input := &autoscaling.DescribeAutoScalingGroupsInput{ + AutoScalingGroupNames: []*string{ + aws.String(v), + }, + } + + err = conn.DescribeAutoScalingGroupsPages(input, func(resp *autoscaling.DescribeAutoScalingGroupsOutput, lastPage bool) bool { + for _, group := range resp.AutoScalingGroups { + rawArn = append(rawArn, aws.StringValue(group.AutoScalingGroupARN)) + } + return !lastPage + }) + } + } else { err = conn.DescribeAutoScalingGroupsPages(&autoscaling.DescribeAutoScalingGroupsInput{}, func(resp *autoscaling.DescribeAutoScalingGroupsOutput, lastPage bool) bool { for _, group := range resp.AutoScalingGroups { - raw = append(raw, aws.StringValue(group.AutoScalingGroupName)) + rawName = append(rawName, aws.StringValue(group.AutoScalingGroupName)) + rawArn = append(rawArn, aws.StringValue(group.AutoScalingGroupARN)) } return !lastPage }) @@ -75,12 +98,17 @@ func dataSourceAwsAutoscalingGroupsRead(d *schema.ResourceData, meta interface{} return fmt.Errorf("Error fetching Autoscaling Groups: %s", err) } - sort.Strings(raw) + sort.Strings(rawName) + sort.Strings(rawArn) - if err := d.Set("names", raw); err != nil { + if err := d.Set("names", rawName); err != nil { return fmt.Errorf("[WARN] Error setting Autoscaling Group Names: %s", err) } + if err := d.Set("arns", rawArn); err != nil { + return fmt.Errorf("[WARN] Error setting Autoscaling Group Arns: %s", err) + } + return nil } diff --git a/aws/data_source_aws_autoscaling_groups_test.go b/aws/data_source_aws_autoscaling_groups_test.go index 67a478c59bf..a6f8c0306ca 100644 --- a/aws/data_source_aws_autoscaling_groups_test.go +++ b/aws/data_source_aws_autoscaling_groups_test.go @@ -25,6 +25,7 @@ func TestAccAWSAutoscalingGroups_basic(t *testing.T) { Check: resource.ComposeTestCheckFunc( testAccCheckAwsAutoscalingGroups("data.aws_autoscaling_groups.group_list"), resource.TestCheckResourceAttr("data.aws_autoscaling_groups.group_list", "names.#", "3"), + resource.TestCheckResourceAttr("data.aws_autoscaling_groups.group_list", "arns.#", "3"), ), }, }, diff --git a/website/docs/d/autoscaling_groups.html.markdown b/website/docs/d/autoscaling_groups.html.markdown index 4112a6bd826..f5b4c49c6eb 100644 --- a/website/docs/d/autoscaling_groups.html.markdown +++ b/website/docs/d/autoscaling_groups.html.markdown @@ -51,3 +51,4 @@ resource "aws_autoscaling_notification" "slack_notifications" { In addition to all arguments above, the following attributes are exported: * `names` - A list of the Autoscaling Groups in the current region. +* `arns` - A list of the Autoscaling Groups Arns in the current region. From 4fe2615ee34f82efc12b5cb7229f14661b6db707 Mon Sep 17 00:00:00 2001 From: Guimove Date: Fri, 14 Sep 2018 12:43:36 +0200 Subject: [PATCH 2/3] groupe request per 1600 records --- aws/data_source_aws_autoscaling_groups.go | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/aws/data_source_aws_autoscaling_groups.go b/aws/data_source_aws_autoscaling_groups.go index 92468774746..a3b8bb709fd 100644 --- a/aws/data_source_aws_autoscaling_groups.go +++ b/aws/data_source_aws_autoscaling_groups.go @@ -70,21 +70,26 @@ func dataSourceAwsAutoscalingGroupsRead(d *schema.ResourceData, meta interface{} return !lastPage }) - for _, v := range rawName { - input := &autoscaling.DescribeAutoScalingGroupsInput{ - AutoScalingGroupNames: []*string{ - aws.String(v), - }, + maxAutoScalingGroupNames := 1600 + for i := 0; i < len(rawName); i += maxAutoScalingGroupNames { + end := i + maxAutoScalingGroupNames + + if end > len(rawName) { + end = len(rawName) } - err = conn.DescribeAutoScalingGroupsPages(input, func(resp *autoscaling.DescribeAutoScalingGroupsOutput, lastPage bool) bool { + nameInput := &autoscaling.DescribeAutoScalingGroupsInput{ + AutoScalingGroupNames: aws.StringSlice(rawName[i:end]), + MaxRecords: aws.Int64(100), + } + + err = conn.DescribeAutoScalingGroupsPages(nameInput, func(resp *autoscaling.DescribeAutoScalingGroupsOutput, lastPage bool) bool { for _, group := range resp.AutoScalingGroups { rawArn = append(rawArn, aws.StringValue(group.AutoScalingGroupARN)) } return !lastPage }) } - } else { err = conn.DescribeAutoScalingGroupsPages(&autoscaling.DescribeAutoScalingGroupsInput{}, func(resp *autoscaling.DescribeAutoScalingGroupsOutput, lastPage bool) bool { for _, group := range resp.AutoScalingGroups { From 486a75ed6ca564dd48cdc9407df9c41603299bfc Mon Sep 17 00:00:00 2001 From: Guimove Date: Thu, 20 Sep 2018 11:54:25 +0200 Subject: [PATCH 3/3] correct fmt --- aws/data_source_aws_autoscaling_groups.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/aws/data_source_aws_autoscaling_groups.go b/aws/data_source_aws_autoscaling_groups.go index cf5e741251c..a3b8bb709fd 100644 --- a/aws/data_source_aws_autoscaling_groups.go +++ b/aws/data_source_aws_autoscaling_groups.go @@ -106,7 +106,7 @@ func dataSourceAwsAutoscalingGroupsRead(d *schema.ResourceData, meta interface{} sort.Strings(rawName) sort.Strings(rawArn) - if err := d.Set("names", rawName); err != nil { + if err := d.Set("names", rawName); err != nil { return fmt.Errorf("[WARN] Error setting Autoscaling Group Names: %s", err) }