-
Notifications
You must be signed in to change notification settings - Fork 9.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New Data Source: aws_vpc_peering_connections (#9491)
Output from acceptance testing: ``` --- PASS: TestAccDataSourceAwsVpcPeeringConnections_basic (14.21s) ```
- Loading branch information
Showing
4 changed files
with
204 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] | ||
} | ||
} | ||
` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |