github.com/cbroglie/terraform@v0.7.0-rc3.0.20170410193827-735dfc416d46/builtin/providers/aws/data_source_aws_subnet_ids.go (about) 1 package aws 2 3 import ( 4 "fmt" 5 "log" 6 7 "github.com/aws/aws-sdk-go/service/ec2" 8 "github.com/hashicorp/terraform/helper/schema" 9 ) 10 11 func dataSourceAwsSubnetIDs() *schema.Resource { 12 return &schema.Resource{ 13 Read: dataSourceAwsSubnetIDsRead, 14 Schema: map[string]*schema.Schema{ 15 "vpc_id": &schema.Schema{ 16 Type: schema.TypeString, 17 Required: true, 18 }, 19 "ids": &schema.Schema{ 20 Type: schema.TypeSet, 21 Computed: true, 22 Elem: &schema.Schema{Type: schema.TypeString}, 23 Set: schema.HashString, 24 }, 25 }, 26 } 27 } 28 29 func dataSourceAwsSubnetIDsRead(d *schema.ResourceData, meta interface{}) error { 30 conn := meta.(*AWSClient).ec2conn 31 32 req := &ec2.DescribeSubnetsInput{} 33 34 req.Filters = buildEC2AttributeFilterList( 35 map[string]string{ 36 "vpc-id": d.Get("vpc_id").(string), 37 }, 38 ) 39 40 log.Printf("[DEBUG] DescribeSubnets %s\n", req) 41 resp, err := conn.DescribeSubnets(req) 42 if err != nil { 43 return err 44 } 45 46 if resp == nil || len(resp.Subnets) == 0 { 47 return fmt.Errorf("no matching subnet found for vpc with id %s", d.Get("vpc_id").(string)) 48 } 49 50 subnets := make([]string, 0) 51 52 for _, subnet := range resp.Subnets { 53 subnets = append(subnets, *subnet.SubnetId) 54 } 55 56 d.SetId(d.Get("vpc_id").(string)) 57 d.Set("ids", subnets) 58 59 return nil 60 }