github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/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": { 18 Type: schema.TypeString, 19 Optional: true, 20 Computed: true, 21 }, 22 23 "cidr_block": { 24 Type: schema.TypeString, 25 Optional: true, 26 Computed: true, 27 }, 28 29 "ipv6_cidr_block": { 30 Type: schema.TypeString, 31 Optional: true, 32 Computed: true, 33 }, 34 35 "default_for_az": { 36 Type: schema.TypeBool, 37 Optional: true, 38 Computed: true, 39 }, 40 41 "filter": ec2CustomFiltersSchema(), 42 43 "id": { 44 Type: schema.TypeString, 45 Optional: true, 46 Computed: true, 47 }, 48 49 "state": { 50 Type: schema.TypeString, 51 Optional: true, 52 Computed: true, 53 }, 54 55 "tags": tagsSchemaComputed(), 56 57 "vpc_id": { 58 Type: schema.TypeString, 59 Optional: true, 60 Computed: true, 61 }, 62 63 "assign_ipv6_address_on_creation": { 64 Type: schema.TypeBool, 65 Computed: true, 66 }, 67 68 "map_public_ip_on_launch": { 69 Type: schema.TypeBool, 70 Computed: true, 71 }, 72 73 "ipv6_cidr_block_association_id": { 74 Type: schema.TypeString, 75 Computed: true, 76 }, 77 }, 78 } 79 } 80 81 func dataSourceAwsSubnetRead(d *schema.ResourceData, meta interface{}) error { 82 conn := meta.(*AWSClient).ec2conn 83 84 req := &ec2.DescribeSubnetsInput{} 85 86 if id := d.Get("id"); id != "" { 87 req.SubnetIds = []*string{aws.String(id.(string))} 88 } 89 90 // We specify default_for_az as boolean, but EC2 filters want 91 // it to be serialized as a string. Note that setting it to 92 // "false" here does not actually filter by it *not* being 93 // the default, because Terraform can't distinguish between 94 // "false" and "not set". 95 defaultForAzStr := "" 96 if d.Get("default_for_az").(bool) { 97 defaultForAzStr = "true" 98 } 99 100 filters := map[string]string{ 101 "availabilityZone": d.Get("availability_zone").(string), 102 "defaultForAz": defaultForAzStr, 103 "state": d.Get("state").(string), 104 "vpc-id": d.Get("vpc_id").(string), 105 } 106 107 if v, ok := d.GetOk("cidr_block"); ok { 108 filters["cidrBlock"] = v.(string) 109 } 110 111 if v, ok := d.GetOk("ipv6_cidr_block"); ok { 112 filters["ipv6-cidr-block-association.ipv6-cidr-block"] = v.(string) 113 } 114 115 req.Filters = buildEC2AttributeFilterList(filters) 116 req.Filters = append(req.Filters, buildEC2TagFilterList( 117 tagsFromMap(d.Get("tags").(map[string]interface{})), 118 )...) 119 req.Filters = append(req.Filters, buildEC2CustomFilterList( 120 d.Get("filter").(*schema.Set), 121 )...) 122 if len(req.Filters) == 0 { 123 // Don't send an empty filters list; the EC2 API won't accept it. 124 req.Filters = nil 125 } 126 127 log.Printf("[DEBUG] DescribeSubnets %s\n", req) 128 resp, err := conn.DescribeSubnets(req) 129 if err != nil { 130 return err 131 } 132 if resp == nil || len(resp.Subnets) == 0 { 133 return fmt.Errorf("no matching subnet found") 134 } 135 if len(resp.Subnets) > 1 { 136 return fmt.Errorf("multiple subnets matched; use additional constraints to reduce matches to a single subnet") 137 } 138 139 subnet := resp.Subnets[0] 140 141 d.SetId(*subnet.SubnetId) 142 d.Set("id", subnet.SubnetId) 143 d.Set("vpc_id", subnet.VpcId) 144 d.Set("availability_zone", subnet.AvailabilityZone) 145 d.Set("cidr_block", subnet.CidrBlock) 146 d.Set("default_for_az", subnet.DefaultForAz) 147 d.Set("state", subnet.State) 148 d.Set("tags", tagsToMap(subnet.Tags)) 149 d.Set("assign_ipv6_address_on_creation", subnet.AssignIpv6AddressOnCreation) 150 d.Set("map_public_ip_on_launch", subnet.MapPublicIpOnLaunch) 151 152 for _, a := range subnet.Ipv6CidrBlockAssociationSet { 153 if *a.Ipv6CidrBlockState.State == "associated" { //we can only ever have 1 IPv6 block associated at once 154 d.Set("ipv6_cidr_block_association_id", a.AssociationId) 155 d.Set("ipv6_cidr_block", a.Ipv6CidrBlock) 156 } 157 } 158 159 return nil 160 }