github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/aws/data_source_aws_vpn_gateway.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 dataSourceAwsVpnGateway() *schema.Resource { 13 return &schema.Resource{ 14 Read: dataSourceAwsVpnGatewayRead, 15 16 Schema: map[string]*schema.Schema{ 17 "id": { 18 Type: schema.TypeString, 19 Optional: true, 20 Computed: true, 21 }, 22 "state": { 23 Type: schema.TypeString, 24 Optional: true, 25 Computed: true, 26 }, 27 "attached_vpc_id": { 28 Type: schema.TypeString, 29 Optional: true, 30 Computed: true, 31 }, 32 "availability_zone": { 33 Type: schema.TypeString, 34 Optional: true, 35 Computed: true, 36 }, 37 "filter": ec2CustomFiltersSchema(), 38 "tags": tagsSchemaComputed(), 39 }, 40 } 41 } 42 43 func dataSourceAwsVpnGatewayRead(d *schema.ResourceData, meta interface{}) error { 44 conn := meta.(*AWSClient).ec2conn 45 46 log.Printf("[DEBUG] Reading VPN Gateways.") 47 48 req := &ec2.DescribeVpnGatewaysInput{} 49 50 if id, ok := d.GetOk("id"); ok { 51 req.VpnGatewayIds = aws.StringSlice([]string{id.(string)}) 52 } 53 54 req.Filters = buildEC2AttributeFilterList( 55 map[string]string{ 56 "state": d.Get("state").(string), 57 "availability-zone": d.Get("availability_zone").(string), 58 }, 59 ) 60 if id, ok := d.GetOk("attached_vpc_id"); ok { 61 req.Filters = append(req.Filters, buildEC2AttributeFilterList( 62 map[string]string{ 63 "attachment.state": "attached", 64 "attachment.vpc-id": id.(string), 65 }, 66 )...) 67 } 68 req.Filters = append(req.Filters, buildEC2TagFilterList( 69 tagsFromMap(d.Get("tags").(map[string]interface{})), 70 )...) 71 req.Filters = append(req.Filters, buildEC2CustomFilterList( 72 d.Get("filter").(*schema.Set), 73 )...) 74 if len(req.Filters) == 0 { 75 // Don't send an empty filters list; the EC2 API won't accept it. 76 req.Filters = nil 77 } 78 79 resp, err := conn.DescribeVpnGateways(req) 80 if err != nil { 81 return err 82 } 83 if resp == nil || len(resp.VpnGateways) == 0 { 84 return fmt.Errorf("no matching VPN gateway found: %#v", req) 85 } 86 if len(resp.VpnGateways) > 1 { 87 return fmt.Errorf("multiple VPN gateways matched; use additional constraints to reduce matches to a single VPN gateway") 88 } 89 90 vgw := resp.VpnGateways[0] 91 92 d.SetId(aws.StringValue(vgw.VpnGatewayId)) 93 d.Set("state", vgw.State) 94 d.Set("availability_zone", vgw.AvailabilityZone) 95 d.Set("tags", tagsToMap(vgw.Tags)) 96 97 for _, attachment := range vgw.VpcAttachments { 98 if *attachment.State == "attached" { 99 d.Set("attached_vpc_id", attachment.VpcId) 100 break 101 } 102 } 103 104 return nil 105 }