github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/builtin/providers/aws/data_source_aws_subnet.go (about) 1 package aws 2 3 import ( 4 "fmt" 5 "log" 6 7 "github.com/aws/aws-sdk-go/aws" 8 "github.com/aws/aws-sdk-go/service/ec2" 9 "github.com/hashicorp/terraform/helper/schema" 10 ) 11 12 func dataSourceAwsSubnet() *schema.Resource { 13 return &schema.Resource{ 14 Read: dataSourceAwsSubnetRead, 15 16 Schema: map[string]*schema.Schema{ 17 "availability_zone": &schema.Schema{ 18 Type: schema.TypeString, 19 Optional: true, 20 Computed: true, 21 }, 22 23 "cidr_block": &schema.Schema{ 24 Type: schema.TypeString, 25 Optional: true, 26 Computed: true, 27 }, 28 29 "default_for_az": &schema.Schema{ 30 Type: schema.TypeBool, 31 Optional: true, 32 Computed: true, 33 }, 34 35 "filter": ec2CustomFiltersSchema(), 36 37 "id": &schema.Schema{ 38 Type: schema.TypeString, 39 Optional: true, 40 Computed: true, 41 }, 42 43 "state": &schema.Schema{ 44 Type: schema.TypeString, 45 Optional: true, 46 Computed: true, 47 }, 48 49 "tags": tagsSchemaComputed(), 50 51 "vpc_id": &schema.Schema{ 52 Type: schema.TypeString, 53 Optional: true, 54 Computed: true, 55 }, 56 }, 57 } 58 } 59 60 func dataSourceAwsSubnetRead(d *schema.ResourceData, meta interface{}) error { 61 conn := meta.(*AWSClient).ec2conn 62 63 req := &ec2.DescribeSubnetsInput{} 64 65 if id := d.Get("id"); id != "" { 66 req.SubnetIds = []*string{aws.String(id.(string))} 67 } 68 69 // We specify default_for_az as boolean, but EC2 filters want 70 // it to be serialized as a string. Note that setting it to 71 // "false" here does not actually filter by it *not* being 72 // the default, because Terraform can't distinguish between 73 // "false" and "not set". 74 defaultForAzStr := "" 75 if d.Get("default_for_az").(bool) { 76 defaultForAzStr = "true" 77 } 78 79 req.Filters = buildEC2AttributeFilterList( 80 map[string]string{ 81 "availabilityZone": d.Get("availability_zone").(string), 82 "cidrBlock": d.Get("cidr_block").(string), 83 "defaultForAz": defaultForAzStr, 84 "state": d.Get("state").(string), 85 "vpc-id": d.Get("vpc_id").(string), 86 }, 87 ) 88 req.Filters = append(req.Filters, buildEC2TagFilterList( 89 tagsFromMap(d.Get("tags").(map[string]interface{})), 90 )...) 91 req.Filters = append(req.Filters, buildEC2CustomFilterList( 92 d.Get("filter").(*schema.Set), 93 )...) 94 if len(req.Filters) == 0 { 95 // Don't send an empty filters list; the EC2 API won't accept it. 96 req.Filters = nil 97 } 98 99 log.Printf("[DEBUG] DescribeSubnets %s\n", req) 100 resp, err := conn.DescribeSubnets(req) 101 if err != nil { 102 return err 103 } 104 if resp == nil || len(resp.Subnets) == 0 { 105 return fmt.Errorf("no matching subnet found") 106 } 107 if len(resp.Subnets) > 1 { 108 return fmt.Errorf("multiple subnets matched; use additional constraints to reduce matches to a single subnet") 109 } 110 111 subnet := resp.Subnets[0] 112 113 d.SetId(*subnet.SubnetId) 114 d.Set("id", subnet.SubnetId) 115 d.Set("vpc_id", subnet.VpcId) 116 d.Set("availability_zone", subnet.AvailabilityZone) 117 d.Set("cidr_block", subnet.CidrBlock) 118 d.Set("default_for_az", subnet.DefaultForAz) 119 d.Set("state", subnet.State) 120 d.Set("tags", tagsToMap(subnet.Tags)) 121 122 return nil 123 }