-
Notifications
You must be signed in to change notification settings - Fork 9.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Aws vpc peering connections #9491
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
66b8991
adding aws_vpc_peering_connections
0520ce6
Clean up aws_vpn_peering_connections a little bit
tmccombs b9ef8c6
d/aws_vpc_peering_connections: Fix compilation errors after rebase.
ewbankkit bbb3228
Fix vpc_peering_connections website lint
tmccombs ab895d5
Use terraform 0.12 syntax for vpc_peering_connections tests and examp…
tmccombs d82b90a
Update aws_vpc_peering_connections to use terraform plugin sdk v2
tmccombs 9e2d938
Fix frontmatter for tfproviderdocs vpc_peering_connections
tmccombs 210619a
Apply suggestions from code review
tmccombs 7e853f7
Fix inconcsistent use of tabs
tmccombs File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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. |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please note that using an unstable ID causes issues in Terraform CLI 0.13.0 (#14579) and we will add linting for this shortly, although we haven't quite determined what solution we would prefer. Hopefully more soon.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hmm, ok. should I leave it as is for now? Or should I change it to something else (although I'm not sure what)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like 0.13.1 might have mitigated this issue in 0.13 for now, so while its less than ideal we can include this as-is until we figure out long term plans rather than leaving this PR linger even longer. 👍