Skip to content

Commit

Permalink
New Data Source: aws_ec2_local_gateway_virtual_interface
Browse files Browse the repository at this point in the history
Reference: #13504

Output from acceptance testing:

```
--- PASS: TestAccDataSourceAwsEc2LocalGatewayVirtualInterface_Id (19.93s)
--- PASS: TestAccDataSourceAwsEc2LocalGatewayVirtualInterface_Filter (21.30s)
--- PASS: TestAccDataSourceAwsEc2LocalGatewayVirtualInterface_Tags (23.70s)
```
  • Loading branch information
bflad committed Jun 16, 2020
1 parent 8069a6f commit 09860e5
Show file tree
Hide file tree
Showing 5 changed files with 327 additions and 0 deletions.
110 changes: 110 additions & 0 deletions aws/data_source_aws_ec2_local_gateway_virtual_interface.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
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/helper/schema"
"github.com/terraform-providers/terraform-provider-aws/aws/internal/keyvaluetags"
)

func dataSourceAwsEc2LocalGatewayVirtualInterface() *schema.Resource {
return &schema.Resource{
Read: dataSourceAwsEc2LocalGatewayVirtualInterfaceRead,

Schema: map[string]*schema.Schema{
"filter": ec2CustomFiltersSchema(),
"id": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"local_address": {
Type: schema.TypeString,
Computed: true,
},
"local_bgp_asn": {
Type: schema.TypeInt,
Computed: true,
},
"local_gateway_id": {
Type: schema.TypeString,
Computed: true,
},
"local_gateway_virtual_interface_ids": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"peer_address": {
Type: schema.TypeString,
Computed: true,
},
"peer_bgp_asn": {
Type: schema.TypeInt,
Computed: true,
},
"tags": tagsSchemaComputed(),
"vlan": {
Type: schema.TypeInt,
Computed: true,
},
},
}
}

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

input := &ec2.DescribeLocalGatewayVirtualInterfacesInput{}

if v, ok := d.GetOk("id"); ok {
input.LocalGatewayVirtualInterfaceIds = []*string{aws.String(v.(string))}
}

input.Filters = append(input.Filters, buildEC2TagFilterList(
keyvaluetags.New(d.Get("tags").(map[string]interface{})).Ec2Tags(),
)...)

input.Filters = append(input.Filters, buildEC2CustomFilterList(
d.Get("filter").(*schema.Set),
)...)

if len(input.Filters) == 0 {
// Don't send an empty filters list; the EC2 API won't accept it.
input.Filters = nil
}

output, err := conn.DescribeLocalGatewayVirtualInterfaces(input)

if err != nil {
return fmt.Errorf("error describing EC2 Local Gateway Virtual Interfaces: %w", err)
}

if output == nil || len(output.LocalGatewayVirtualInterfaces) == 0 {
return fmt.Errorf("no matching EC2 Local Gateway Virtual Interface found")
}

if len(output.LocalGatewayVirtualInterfaces) > 1 {
return fmt.Errorf("multiple EC2 Local Gateway Virtual Interfaces matched; use additional constraints to reduce matches to a single EC2 Local Gateway Virtual Interface")
}

localGatewayVirtualInterface := output.LocalGatewayVirtualInterfaces[0]

d.SetId(aws.StringValue(localGatewayVirtualInterface.LocalGatewayVirtualInterfaceId))
d.Set("local_address", localGatewayVirtualInterface.LocalAddress)
d.Set("local_bgp_asn", localGatewayVirtualInterface.LocalBgpAsn)
d.Set("local_gateway_id", localGatewayVirtualInterface.LocalGatewayId)
d.Set("peer_address", localGatewayVirtualInterface.PeerAddress)
d.Set("peer_bgp_asn", localGatewayVirtualInterface.PeerBgpAsn)

if err := d.Set("tags", keyvaluetags.Ec2KeyValueTags(localGatewayVirtualInterface.Tags).IgnoreAws().IgnoreConfig(ignoreTagsConfig).Map()); err != nil {
return fmt.Errorf("error setting tags: %w", err)
}

d.Set("vlan", localGatewayVirtualInterface.Vlan)

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

import (
"fmt"
"os"
"regexp"
"testing"

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

func TestAccDataSourceAwsEc2LocalGatewayVirtualInterface_Filter(t *testing.T) {
// Hide Outposts testing behind consistent environment variable
outpostArn := os.Getenv("AWS_OUTPOST_ARN")
if outpostArn == "" {
t.Skip(
"Environment variable AWS_OUTPOST_ARN is not set. " +
"This environment variable must be set to the ARN of " +
"a deployed Outpost to enable this test.")
}

dataSourceName := "data.aws_ec2_local_gateway_virtual_interface.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourceAwsEc2LocalGatewayVirtualInterfaceConfigFilter(),
Check: resource.ComposeTestCheckFunc(
resource.TestMatchResourceAttr(dataSourceName, "id", regexp.MustCompile(`^lgw-vif-`)),
resource.TestMatchResourceAttr(dataSourceName, "local_address", regexp.MustCompile(`^\d+\.\d+\.\d+\.\d+/\d+$`)),
resource.TestMatchResourceAttr(dataSourceName, "local_bgp_asn", regexp.MustCompile(`^\d+$`)),
resource.TestMatchResourceAttr(dataSourceName, "local_gateway_id", regexp.MustCompile(`^lgw-`)),
resource.TestMatchResourceAttr(dataSourceName, "peer_address", regexp.MustCompile(`^\d+\.\d+\.\d+\.\d+/\d+$`)),
resource.TestMatchResourceAttr(dataSourceName, "peer_bgp_asn", regexp.MustCompile(`^\d+$`)),
resource.TestMatchResourceAttr(dataSourceName, "vlan", regexp.MustCompile(`^\d+$`)),
),
},
},
})
}

func TestAccDataSourceAwsEc2LocalGatewayVirtualInterface_Id(t *testing.T) {
// Hide Outposts testing behind consistent environment variable
outpostArn := os.Getenv("AWS_OUTPOST_ARN")
if outpostArn == "" {
t.Skip(
"Environment variable AWS_OUTPOST_ARN is not set. " +
"This environment variable must be set to the ARN of " +
"a deployed Outpost to enable this test.")
}

dataSourceName := "data.aws_ec2_local_gateway_virtual_interface.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourceAwsEc2LocalGatewayVirtualInterfaceConfigId(),
Check: resource.ComposeTestCheckFunc(
resource.TestMatchResourceAttr(dataSourceName, "id", regexp.MustCompile(`^lgw-vif-`)),
resource.TestMatchResourceAttr(dataSourceName, "local_address", regexp.MustCompile(`^\d+\.\d+\.\d+\.\d+/\d+$`)),
resource.TestMatchResourceAttr(dataSourceName, "local_bgp_asn", regexp.MustCompile(`^\d+$`)),
resource.TestMatchResourceAttr(dataSourceName, "local_gateway_id", regexp.MustCompile(`^lgw-`)),
resource.TestMatchResourceAttr(dataSourceName, "peer_address", regexp.MustCompile(`^\d+\.\d+\.\d+\.\d+/\d+$`)),
resource.TestMatchResourceAttr(dataSourceName, "peer_bgp_asn", regexp.MustCompile(`^\d+$`)),
resource.TestMatchResourceAttr(dataSourceName, "vlan", regexp.MustCompile(`^\d+$`)),
),
},
},
})
}

func TestAccDataSourceAwsEc2LocalGatewayVirtualInterface_Tags(t *testing.T) {
// Hide Outposts testing behind consistent environment variable
outpostArn := os.Getenv("AWS_OUTPOST_ARN")
if outpostArn == "" {
t.Skip(
"Environment variable AWS_OUTPOST_ARN is not set. " +
"This environment variable must be set to the ARN of " +
"a deployed Outpost to enable this test.")
}

rName := acctest.RandomWithPrefix("tf-acc-test")
sourceDataSourceName := "data.aws_ec2_local_gateway_virtual_interface.source"
dataSourceName := "data.aws_ec2_local_gateway_virtual_interface.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourceAwsEc2LocalGatewayVirtualInterfaceConfigTags(rName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrPair(dataSourceName, "id", sourceDataSourceName, "id"),
resource.TestCheckResourceAttrPair(dataSourceName, "local_address", sourceDataSourceName, "local_address"),
resource.TestCheckResourceAttrPair(dataSourceName, "local_bgp_asn", sourceDataSourceName, "local_bgp_asn"),
resource.TestCheckResourceAttrPair(dataSourceName, "local_gateway_id", sourceDataSourceName, "local_gateway_id"),
resource.TestCheckResourceAttrPair(dataSourceName, "peer_address", sourceDataSourceName, "peer_address"),
resource.TestCheckResourceAttrPair(dataSourceName, "peer_bgp_asn", sourceDataSourceName, "peer_bgp_asn"),
resource.TestCheckResourceAttrPair(dataSourceName, "vlan", sourceDataSourceName, "vlan"),
),
},
},
})
}

func testAccDataSourceAwsEc2LocalGatewayVirtualInterfaceConfigFilter() string {
return `
data "aws_ec2_local_gateways" "test" {}
data "aws_ec2_local_gateway_virtual_interface_group" "test" {
local_gateway_id = tolist(data.aws_ec2_local_gateways.test.ids)[0]
}
data "aws_ec2_local_gateway_virtual_interface" "test" {
filter {
name = "local-gateway-virtual-interface-id"
values = [tolist(data.aws_ec2_local_gateway_virtual_interface_group.test.local_gateway_virtual_interface_ids)[0]]
}
}
`
}

func testAccDataSourceAwsEc2LocalGatewayVirtualInterfaceConfigId() string {
return `
data "aws_ec2_local_gateways" "test" {}
data "aws_ec2_local_gateway_virtual_interface_group" "test" {
local_gateway_id = tolist(data.aws_ec2_local_gateways.test.ids)[0]
}
data "aws_ec2_local_gateway_virtual_interface" "test" {
id = tolist(data.aws_ec2_local_gateway_virtual_interface_group.test.local_gateway_virtual_interface_ids)[0]
}
`
}

func testAccDataSourceAwsEc2LocalGatewayVirtualInterfaceConfigTags(rName string) string {
return fmt.Sprintf(`
data "aws_ec2_local_gateways" "test" {}
data "aws_ec2_local_gateway_virtual_interface_group" "test" {
local_gateway_id = tolist(data.aws_ec2_local_gateways.test.ids)[0]
}
data "aws_ec2_local_gateway_virtual_interface" "source" {
id = tolist(data.aws_ec2_local_gateway_virtual_interface_group.test.local_gateway_virtual_interface_ids)[0]
}
resource "aws_ec2_tag" "test" {
key = "TerraformAccTest-aws_ec2_local_gateway_virtual_interface"
resource_id = data.aws_ec2_local_gateway_virtual_interface.source.id
value = %[1]q
}
data "aws_ec2_local_gateway_virtual_interface" "test" {
tags = {
(aws_ec2_tag.test.key) = aws_ec2_tag.test.value
}
}
`, rName)
}
1 change: 1 addition & 0 deletions aws/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ func Provider() terraform.ResourceProvider {
"aws_ec2_local_gateways": dataSourceAwsEc2LocalGateways(),
"aws_ec2_local_gateway_route_table": dataSourceAwsEc2LocalGatewayRouteTable(),
"aws_ec2_local_gateway_route_tables": dataSourceAwsEc2LocalGatewayRouteTables(),
"aws_ec2_local_gateway_virtual_interface": dataSourceAwsEc2LocalGatewayVirtualInterface(),
"aws_ec2_local_gateway_virtual_interface_group": dataSourceAwsEc2LocalGatewayVirtualInterfaceGroup(),
"aws_ec2_local_gateway_virtual_interface_groups": dataSourceAwsEc2LocalGatewayVirtualInterfaceGroups(),
"aws_ec2_transit_gateway": dataSourceAwsEc2TransitGateway(),
Expand Down
3 changes: 3 additions & 0 deletions website/aws.erb
Original file line number Diff line number Diff line change
Expand Up @@ -1136,6 +1136,9 @@
<li>
<a href="/docs/providers/aws/d/ec2_local_gateway_route_tables.html">aws_ec2_local_gateway_route_tables</a>
</li>
<li>
<a href="/docs/providers/aws/d/ec2_local_gateway_virtual_interface.html">aws_ec2_local_gateway_virtual_interface</a>
</li>
<li>
<a href="/docs/providers/aws/d/ec2_local_gateway_virtual_interface_group.html">aws_ec2_local_gateway_virtual_interface_group</a>
</li>
Expand Down
47 changes: 47 additions & 0 deletions website/docs/d/ec2_local_gateway_virtual_interface.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
---
subcategory: "EC2"
layout: "aws"
page_title: "AWS: aws_ec2_local_gateway_virtual_interface"
description: |-
Provides details about an EC2 Local Gateway Virtual Interface
---

# Data Source: aws_ec2_local_gateway_virtual_interface

Provides details about an EC2 Local Gateway Virtual Interface. More information can be found in the [Outposts User Guide](https://docs.aws.amazon.com/outposts/latest/userguide/outposts-networking-components.html#routing).

## Example Usage

```hcl
data "aws_ec2_local_gateway_virtual_interface" "example" {
for_each = data.aws_ec2_local_gateway_virtual_interface_group.example.local_gateway_virtual_interface_ids
id = each.value
}
```

## Argument Reference

The following arguments are optional:

* `filter` - (Optional) One or more configuration blocks containing name-values filters. See the [EC2 API Reference](https://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_DescribeLocalGatewayVirtualInterfaces.html) for supported filters. Detailed below.
* `id` - (Optional) Identifier of EC2 Local Gateway Virtual Interface.
* `tags` - (Optional) Key-value map of resource tags, each pair of which must exactly match a pair on the desired local gateway route table.

### filter Argument Reference

The `filter` configuration block supports the following arguments:

* `name` - (Required) Name of the filter.
* `values` - (Required) List of one or more values for the filter.

## Attribute Reference

In addition to all arguments above, the following attributes are exported:

* `local_address` - Local address.
* `local_bgp_asn` - Border Gateway Protocol (BGP) Autonomous System Number (ASN) of the EC2 Local Gateway.
* `local_gateway_id` - Identifier of the EC2 Local Gateway.
* `peer_address` - Peer address.
* `peer_bgp_asn` - Border Gateway Protocol (BGP) Autonomous System Number (ASN) of the peer.
* `vlan` - Virtual Local Area Network.

0 comments on commit 09860e5

Please sign in to comment.