github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/builtin/providers/aws/data_source_aws_vpc_peering_connection.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 dataSourceAwsVpcPeeringConnection() *schema.Resource {
    13  	return &schema.Resource{
    14  		Read: dataSourceAwsVpcPeeringConnectionRead,
    15  
    16  		Schema: map[string]*schema.Schema{
    17  			"id": {
    18  				Type:     schema.TypeString,
    19  				Optional: true,
    20  				Computed: true,
    21  			},
    22  			"status": {
    23  				Type:     schema.TypeString,
    24  				Optional: true,
    25  				Computed: true,
    26  			},
    27  			"vpc_id": {
    28  				Type:     schema.TypeString,
    29  				Optional: true,
    30  				Computed: true,
    31  			},
    32  			"owner_id": {
    33  				Type:     schema.TypeString,
    34  				Optional: true,
    35  				Computed: true,
    36  			},
    37  			"cidr_block": {
    38  				Type:     schema.TypeString,
    39  				Optional: true,
    40  				Computed: true,
    41  			},
    42  			"peer_vpc_id": {
    43  				Type:     schema.TypeString,
    44  				Optional: true,
    45  				Computed: true,
    46  			},
    47  			"peer_owner_id": {
    48  				Type:     schema.TypeString,
    49  				Optional: true,
    50  				Computed: true,
    51  			},
    52  			"peer_cidr_block": {
    53  				Type:     schema.TypeString,
    54  				Optional: true,
    55  				Computed: true,
    56  			},
    57  			"accepter": {
    58  				Type:     schema.TypeMap,
    59  				Computed: true,
    60  				Elem:     schema.TypeBool,
    61  			},
    62  			"requester": {
    63  				Type:     schema.TypeMap,
    64  				Computed: true,
    65  				Elem:     schema.TypeBool,
    66  			},
    67  			"filter": ec2CustomFiltersSchema(),
    68  			"tags":   tagsSchemaComputed(),
    69  		},
    70  	}
    71  }
    72  
    73  func dataSourceAwsVpcPeeringConnectionRead(d *schema.ResourceData, meta interface{}) error {
    74  	conn := meta.(*AWSClient).ec2conn
    75  
    76  	log.Printf("[DEBUG] Reading VPC Peering Connections.")
    77  
    78  	req := &ec2.DescribeVpcPeeringConnectionsInput{}
    79  
    80  	if id, ok := d.GetOk("id"); ok {
    81  		req.VpcPeeringConnectionIds = aws.StringSlice([]string{id.(string)})
    82  	}
    83  
    84  	req.Filters = buildEC2AttributeFilterList(
    85  		map[string]string{
    86  			"status-code":                   d.Get("status").(string),
    87  			"requester-vpc-info.vpc-id":     d.Get("vpc_id").(string),
    88  			"requester-vpc-info.owner-id":   d.Get("owner_id").(string),
    89  			"requester-vpc-info.cidr-block": d.Get("cidr_block").(string),
    90  			"accepter-vpc-info.vpc-id":      d.Get("peer_vpc_id").(string),
    91  			"accepter-vpc-info.owner-id":    d.Get("peer_owner_id").(string),
    92  			"accepter-vpc-info.cidr-block":  d.Get("peer_cidr_block").(string),
    93  		},
    94  	)
    95  	req.Filters = append(req.Filters, buildEC2TagFilterList(
    96  		tagsFromMap(d.Get("tags").(map[string]interface{})),
    97  	)...)
    98  	req.Filters = append(req.Filters, buildEC2CustomFilterList(
    99  		d.Get("filter").(*schema.Set),
   100  	)...)
   101  	if len(req.Filters) == 0 {
   102  		// Don't send an empty filters list; the EC2 API won't accept it.
   103  		req.Filters = nil
   104  	}
   105  
   106  	resp, err := conn.DescribeVpcPeeringConnections(req)
   107  	if err != nil {
   108  		return err
   109  	}
   110  	if resp == nil || len(resp.VpcPeeringConnections) == 0 {
   111  		return fmt.Errorf("no matching VPC peering connection found")
   112  	}
   113  	if len(resp.VpcPeeringConnections) > 1 {
   114  		return fmt.Errorf("multiple VPC peering connections matched; use additional constraints to reduce matches to a single VPC peering connection")
   115  	}
   116  
   117  	pcx := resp.VpcPeeringConnections[0]
   118  
   119  	d.SetId(aws.StringValue(pcx.VpcPeeringConnectionId))
   120  	d.Set("id", pcx.VpcPeeringConnectionId)
   121  	d.Set("status", pcx.Status.Code)
   122  	d.Set("vpc_id", pcx.RequesterVpcInfo.VpcId)
   123  	d.Set("owner_id", pcx.RequesterVpcInfo.OwnerId)
   124  	d.Set("cidr_block", pcx.RequesterVpcInfo.CidrBlock)
   125  	d.Set("peer_vpc_id", pcx.AccepterVpcInfo.VpcId)
   126  	d.Set("peer_owner_id", pcx.AccepterVpcInfo.OwnerId)
   127  	d.Set("peer_cidr_block", pcx.AccepterVpcInfo.CidrBlock)
   128  	d.Set("tags", tagsToMap(pcx.Tags))
   129  
   130  	if pcx.AccepterVpcInfo.PeeringOptions != nil {
   131  		if err := d.Set("accepter", flattenPeeringOptions(pcx.AccepterVpcInfo.PeeringOptions)[0]); err != nil {
   132  			return err
   133  		}
   134  	}
   135  
   136  	if pcx.RequesterVpcInfo.PeeringOptions != nil {
   137  		if err := d.Set("requester", flattenPeeringOptions(pcx.RequesterVpcInfo.PeeringOptions)[0]); err != nil {
   138  			return err
   139  		}
   140  	}
   141  
   142  	return nil
   143  }