Skip to content

Commit

Permalink
Merge pull request #5766 from Guimove/1909_aws_autoscaling_groups
Browse files Browse the repository at this point in the history
 Add "arns" as an output to "data aws_autoscaling_groups"
  • Loading branch information
bflad authored Sep 20, 2018
2 parents 95ae7c5 + 486a75e commit 8fae25b
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 6 deletions.
45 changes: 39 additions & 6 deletions aws/data_source_aws_autoscaling_groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand All @@ -59,14 +65,36 @@ 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
})

maxAutoScalingGroupNames := 1600
for i := 0; i < len(rawName); i += maxAutoScalingGroupNames {
end := i + maxAutoScalingGroupNames

if end > len(rawName) {
end = len(rawName)
}

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 {
raw = append(raw, aws.StringValue(group.AutoScalingGroupName))
rawName = append(rawName, aws.StringValue(group.AutoScalingGroupName))
rawArn = append(rawArn, aws.StringValue(group.AutoScalingGroupARN))
}
return !lastPage
})
Expand All @@ -75,10 +103,15 @@ 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", rawName); err != nil {
return fmt.Errorf("[WARN] Error setting Autoscaling Group Names: %s", err)
}

if err := d.Set("names", raw); err != nil {
return fmt.Errorf("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
Expand Down
1 change: 1 addition & 0 deletions aws/data_source_aws_autoscaling_groups_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
),
},
},
Expand Down
1 change: 1 addition & 0 deletions website/docs/d/autoscaling_groups.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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.

0 comments on commit 8fae25b

Please sign in to comment.