Skip to content

Commit

Permalink
New Data Source: aws_vpc_peering_connections (#9491)
Browse files Browse the repository at this point in the history
Output from acceptance testing:

```
--- PASS: TestAccDataSourceAwsVpcPeeringConnections_basic (14.21s)
```
  • Loading branch information
tmccombs authored Sep 1, 2020
1 parent 11a4812 commit 30620bc
Show file tree
Hide file tree
Showing 4 changed files with 204 additions and 1 deletion.
66 changes: 66 additions & 0 deletions aws/data_source_aws_vpc_peering_connections.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package aws

import (
"fmt"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/terraform-providers/terraform-provider-aws/aws/internal/keyvaluetags"
)

func dataSourceAwsVpcPeeringConnections() *schema.Resource {
return &schema.Resource{
Read: dataSourceAwsVpcPeeringConnectionsRead,

Schema: map[string]*schema.Schema{
"filter": ec2CustomFiltersSchema(),
"tags": tagsSchemaComputed(),
"ids": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
Set: schema.HashString,
},
},
}
}

func dataSourceAwsVpcPeeringConnectionsRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).ec2conn

req := &ec2.DescribeVpcPeeringConnectionsInput{}

req.Filters = append(req.Filters, buildEC2TagFilterList(
keyvaluetags.New(d.Get("tags").(map[string]interface{})).Ec2Tags(),
)...)
req.Filters = append(req.Filters, buildEC2CustomFilterList(
d.Get("filter").(*schema.Set),
)...)
if len(req.Filters) == 0 {
// Don't send an empty filters list; the EC2 API won't accept it.
req.Filters = nil
}

resp, err := conn.DescribeVpcPeeringConnections(req)
if err != nil {
return err
}
if resp == nil || len(resp.VpcPeeringConnections) == 0 {
return fmt.Errorf("no matching VPC peering connections found")
}

var ids []string
for _, pcx := range resp.VpcPeeringConnections {
ids = append(ids, aws.StringValue(pcx.VpcPeeringConnectionId))
}

d.SetId(resource.UniqueId())
err = d.Set("ids", ids)
if err != nil {
return err
}

return nil
}
80 changes: 80 additions & 0 deletions aws/data_source_aws_vpc_peering_connections_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package aws

import (
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)

func TestAccDataSourceAwsVpcPeeringConnections_basic(t *testing.T) {
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourceAwsVpcPeeringConnectionsConfig,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("data.aws_vpc_peering_connections.test_by_filters", "ids.#", "2"),
),
},
},
})
}

const testAccDataSourceAwsVpcPeeringConnectionsConfig = `
resource "aws_vpc" "foo" {
cidr_block = "10.1.0.0/16"
tags = {
Name = "terraform-testacc-vpc-peering-connection-data-source-foo"
Type = "primary"
}
}
resource "aws_vpc" "bar" {
cidr_block = "10.2.0.0/16"
tags = {
Name = "terraform-testacc-vpc-peering-connection-data-source-bar"
Type = "secondary"
}
}
resource "aws_vpc" "baz" {
cidr_block = "10.3.0.0/16"
tags = {
Name = "terraform-testacc-vpc-peering-connection-data-source-baz"
Type = "secondary"
}
}
resource "aws_vpc_peering_connection" "conn1" {
vpc_id = aws_vpc.foo.id
peer_vpc_id = aws_vpc.bar.id
auto_accept = true
tags = {
Name = "terraform-testacc-vpc-peering-connection-data-source-foo-to-bar"
Environment = "test"
}
}
resource "aws_vpc_peering_connection" "conn2" {
vpc_id = aws_vpc.foo.id
peer_vpc_id = aws_vpc.baz.id
auto_accept = true
tags = {
Name = "terraform-testacc-vpc-peering-connection-data-source-foo-to-baz"
Environment = "test"
}
}
data "aws_vpc_peering_connections" "test_by_filters" {
filter {
name = "vpc-peering-connection-id"
values = [aws_vpc_peering_connection.conn1.id, aws_vpc_peering_connection.conn2.id]
}
}
`
2 changes: 1 addition & 1 deletion aws/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,7 @@ func Provider() *schema.Provider {
"aws_vpc_endpoint": dataSourceAwsVpcEndpoint(),
"aws_vpc_endpoint_service": dataSourceAwsVpcEndpointService(),
"aws_vpc_peering_connection": dataSourceAwsVpcPeeringConnection(),
"aws_vpc_peering_connections": dataSourceAwsVpcPeeringConnections(),
"aws_vpn_gateway": dataSourceAwsVpnGateway(),
"aws_waf_ipset": dataSourceAwsWafIpSet(),
"aws_waf_rule": dataSourceAwsWafRule(),
Expand All @@ -357,7 +358,6 @@ func Provider() *schema.Provider {
"aws_wafv2_web_acl": dataSourceAwsWafv2WebACL(),
"aws_workspaces_bundle": dataSourceAwsWorkspacesBundle(),
"aws_workspaces_directory": dataSourceAwsWorkspacesDirectory(),

// Adding the Aliases for the ALB -> LB Rename
"aws_lb": dataSourceAwsLb(),
"aws_alb": dataSourceAwsLb(),
Expand Down
57 changes: 57 additions & 0 deletions website/docs/d/vpc_peering_connections.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
---
subcategory: "VPC"
layout: "aws"
page_title: "AWS: aws_vpc_peering_connections"
description: |-
Lists peering connections
---

# Data Source: aws_vpc_peering_connections

Use this data source to get IDs of Amazon VPC peering connections
To get more details on each connection, use the data resource [aws_vpc_peering_connection](/docs/providers/aws/d/vpc_peering_connection.html)

Note: To use this data source in a count, the resources should exist before trying to access
the data source, as noted in [issue 4149](https://github.com/hashicorp/terraform/issues/4149)

## Example Usage

```hcl
# Declare the data source
data "aws_vpc_peering_connections" "pcs" {
filter {
name = "requester-vpc-info.vpc-id"
values = [aws_vpc.foo.id]
}
}
# get the details of each resource
data "aws_vpc_peering_connection" "pc" {
count = length(data.aws_vpc_peering_connections.pcs.ids)
id = data.aws_vpc_peering_connections.pcs.ids[count.index]
}
```

## Argument Reference

The arguments of this data source act as filters for querying the available VPC peering connections.

* `filter` - (Optional) Custom filter block as described below.

* `tags` - (Optional) A mapping of tags, each pair of which must exactly match
a pair on the desired VPC Peering Connection.

More complex filters can be expressed using one or more `filter` sub-blocks,
which take the following arguments:

* `name` - (Required) The name of the field to filter by, as defined by
[the underlying AWS API](http://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_DescribeVpcPeeringConnections.html).

* `values` - (Required) Set of values that are accepted for the given field.
A VPC Peering Connection will be selected if any one of the given values matches.

## Attributes Reference

All of the argument attributes except `filter` are also exported as result attributes.

* `ids` - The IDs of the VPC Peering Connections.

0 comments on commit 30620bc

Please sign in to comment.