github.com/jrperritt/terraform@v0.1.1-0.20170525065507-96f391dafc38/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/aws"
    10  	"github.com/aws/aws-sdk-go/service/autoscaling"
    11  	"github.com/hashicorp/terraform/helper/schema"
    12  )
    13  
    14  func dataSourceAwsAutoscalingGroups() *schema.Resource {
    15  	return &schema.Resource{
    16  		Read: dataSourceAwsAutoscalingGroupsRead,
    17  
    18  		Schema: map[string]*schema.Schema{
    19  			"names": {
    20  				Type:     schema.TypeList,
    21  				Computed: true,
    22  				Elem:     &schema.Schema{Type: schema.TypeString},
    23  			},
    24  			"filter": {
    25  				Type:     schema.TypeSet,
    26  				Optional: true,
    27  				Elem: &schema.Resource{
    28  					Schema: map[string]*schema.Schema{
    29  						"name": &schema.Schema{
    30  							Type:     schema.TypeString,
    31  							Required: true,
    32  						},
    33  						"values": &schema.Schema{
    34  							Type:     schema.TypeSet,
    35  							Required: true,
    36  							Elem:     &schema.Schema{Type: schema.TypeString},
    37  							Set:      schema.HashString,
    38  						},
    39  					},
    40  				},
    41  			},
    42  		},
    43  	}
    44  }
    45  
    46  func dataSourceAwsAutoscalingGroupsRead(d *schema.ResourceData, meta interface{}) error {
    47  	conn := meta.(*AWSClient).autoscalingconn
    48  
    49  	log.Printf("[DEBUG] Reading Autoscaling Groups.")
    50  	d.SetId(time.Now().UTC().String())
    51  
    52  	var raw []string
    53  
    54  	tf := d.Get("filter").(*schema.Set)
    55  	if tf.Len() > 0 {
    56  		out, err := conn.DescribeTags(&autoscaling.DescribeTagsInput{
    57  			Filters: expandAsgTagFilters(tf.List()),
    58  		})
    59  		if err != nil {
    60  			return err
    61  		}
    62  
    63  		raw = make([]string, len(out.Tags))
    64  		for i, v := range out.Tags {
    65  			raw[i] = *v.ResourceId
    66  		}
    67  	} else {
    68  
    69  		resp, err := conn.DescribeAutoScalingGroups(&autoscaling.DescribeAutoScalingGroupsInput{})
    70  		if err != nil {
    71  			return fmt.Errorf("Error fetching Autoscaling Groups: %s", err)
    72  		}
    73  
    74  		raw = make([]string, len(resp.AutoScalingGroups))
    75  		for i, v := range resp.AutoScalingGroups {
    76  			raw[i] = *v.AutoScalingGroupName
    77  		}
    78  	}
    79  
    80  	sort.Strings(raw)
    81  
    82  	if err := d.Set("names", raw); err != nil {
    83  		return fmt.Errorf("[WARN] Error setting Autoscaling Group Names: %s", err)
    84  	}
    85  
    86  	return nil
    87  
    88  }
    89  
    90  func expandAsgTagFilters(in []interface{}) []*autoscaling.Filter {
    91  	out := make([]*autoscaling.Filter, len(in), len(in))
    92  	for i, filter := range in {
    93  		m := filter.(map[string]interface{})
    94  		values := expandStringList(m["values"].(*schema.Set).List())
    95  
    96  		out[i] = &autoscaling.Filter{
    97  			Name:   aws.String(m["name"].(string)),
    98  			Values: values,
    99  		}
   100  	}
   101  	return out
   102  }