Skip to content
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 9 commits into from
Sep 1, 2020
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())
Copy link
Contributor

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.

Copy link
Contributor Author

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)?

Copy link
Contributor

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. 👍

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 @@ -338,6 +338,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 @@ -353,7 +354,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.