github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/aws/data_source_aws_autoscaling_groups.go (about)

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  	"sort"
     7  	"time"
     8  
     9  	"github.com/aws/aws-sdk-go/service/autoscaling"
    10  	"github.com/hashicorp/terraform/helper/schema"
    11  )
    12  
    13  func dataSourceAwsAutoscalingGroups() *schema.Resource {
    14  	return &schema.Resource{
    15  		Read: dataSourceAwsAutoscalingGroupsRead,
    16  
    17  		Schema: map[string]*schema.Schema{
    18  			"names": {
    19  				Type:     schema.TypeList,
    20  				Computed: true,
    21  				Elem:     &schema.Schema{Type: schema.TypeString},
    22  			},
    23  		},
    24  	}
    25  }
    26  
    27  func dataSourceAwsAutoscalingGroupsRead(d *schema.ResourceData, meta interface{}) error {
    28  	conn := meta.(*AWSClient).autoscalingconn
    29  
    30  	log.Printf("[DEBUG] Reading Autoscaling Groups.")
    31  	d.SetId(time.Now().UTC().String())
    32  
    33  	resp, err := conn.DescribeAutoScalingGroups(&autoscaling.DescribeAutoScalingGroupsInput{})
    34  	if err != nil {
    35  		return fmt.Errorf("Error fetching Autoscaling Groups: %s", err)
    36  	}
    37  
    38  	raw := make([]string, len(resp.AutoScalingGroups))
    39  	for i, v := range resp.AutoScalingGroups {
    40  		raw[i] = *v.AutoScalingGroupName
    41  	}
    42  
    43  	sort.Strings(raw)
    44  
    45  	if err := d.Set("names", raw); err != nil {
    46  		return fmt.Errorf("[WARN] Error setting Autoscaling Group Names: %s", err)
    47  	}
    48  
    49  	return nil
    50  
    51  }