-
Notifications
You must be signed in to change notification settings - Fork 9k
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鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
New Data Source: aws_ec2_local_gateway_virtual_interface #13770
Merged
Merged
Changes from all commits
Commits
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
110 changes: 110 additions & 0 deletions
110
aws/data_source_aws_ec2_local_gateway_virtual_interface.go
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,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
166
aws/data_source_aws_ec2_local_gateway_virtual_interface_test.go
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,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) | ||
} |
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
47 changes: 47 additions & 0 deletions
47
website/docs/d/ec2_local_gateway_virtual_interface.html.markdown
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,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. |
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.
馃ぉ