Skip to content

Commit

Permalink
New Data Sources: aws_ec2_local_gateway_virtual_interface_group(s)
Browse files Browse the repository at this point in the history
Reference: #13502

Output from acceptance testing:

```
--- PASS: TestAccDataSourceAwsEc2LocalGatewayVirtualInterfaceGroup_LocalGatewayId (17.89s)
--- PASS: TestAccDataSourceAwsEc2LocalGatewayVirtualInterfaceGroup_Filter (18.16s)

--- PASS: TestAccDataSourceAwsEc2LocalGatewayVirtualInterfaceGroups_basic (16.94s)
--- PASS: TestAccDataSourceAwsEc2LocalGatewayVirtualInterfaceGroups_Filter (21.14s)
```
  • Loading branch information
bflad committed Jun 16, 2020
1 parent 844f6b9 commit 242d9e8
Show file tree
Hide file tree
Showing 8 changed files with 605 additions and 172 deletions.
96 changes: 96 additions & 0 deletions aws/data_source_aws_ec2_local_gateway_virtual_interface_group.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
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 dataSourceAwsEc2LocalGatewayVirtualInterfaceGroup() *schema.Resource {
return &schema.Resource{
Read: dataSourceAwsEc2LocalGatewayVirtualInterfaceGroupRead,

Schema: map[string]*schema.Schema{
"filter": ec2CustomFiltersSchema(),
"id": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"local_gateway_id": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"local_gateway_virtual_interface_ids": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"tags": tagsSchema(),
},
}
}

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

input := &ec2.DescribeLocalGatewayVirtualInterfaceGroupsInput{}

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

input.Filters = buildEC2AttributeFilterList(
map[string]string{
"local-gateway-id": d.Get("local_gateway_id").(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.DescribeLocalGatewayVirtualInterfaceGroups(input)

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

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

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

localGatewayVirtualInterfaceGroup := output.LocalGatewayVirtualInterfaceGroups[0]

d.SetId(aws.StringValue(localGatewayVirtualInterfaceGroup.LocalGatewayVirtualInterfaceGroupId))
d.Set("local_gateway_id", localGatewayVirtualInterfaceGroup.LocalGatewayId)
d.Set("local_gateway_virtual_interface_group_id", localGatewayVirtualInterfaceGroup.LocalGatewayVirtualInterfaceGroupId)

if err := d.Set("local_gateway_virtual_interface_ids", aws.StringValueSlice(localGatewayVirtualInterfaceGroup.LocalGatewayVirtualInterfaceIds)); err != nil {
return fmt.Errorf("error setting local_gateway_virtual_interface_ids: %w", err)
}

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

return nil
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package aws

import (
"os"
"regexp"
"testing"

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

func TestAccDataSourceAwsEc2LocalGatewayVirtualInterfaceGroup_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_group.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourceAwsEc2LocalGatewayVirtualInterfaceGroupConfigFilter(),
Check: resource.ComposeTestCheckFunc(
resource.TestMatchResourceAttr(dataSourceName, "id", regexp.MustCompile(`^lgw-vif-grp-`)),
resource.TestMatchResourceAttr(dataSourceName, "local_gateway_id", regexp.MustCompile(`^lgw-`)),
resource.TestCheckResourceAttr(dataSourceName, "local_gateway_virtual_interface_ids.#", "2"),
resource.TestCheckResourceAttr(dataSourceName, "tags.%", "0"),
),
},
},
})
}

func TestAccDataSourceAwsEc2LocalGatewayVirtualInterfaceGroup_LocalGatewayId(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_group.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourceAwsEc2LocalGatewayVirtualInterfaceGroupConfigLocalGatewayId(),
Check: resource.ComposeTestCheckFunc(
resource.TestMatchResourceAttr(dataSourceName, "id", regexp.MustCompile(`^lgw-vif-grp-`)),
resource.TestMatchResourceAttr(dataSourceName, "local_gateway_id", regexp.MustCompile(`^lgw-`)),
resource.TestCheckResourceAttr(dataSourceName, "local_gateway_virtual_interface_ids.#", "2"),
resource.TestCheckResourceAttr(dataSourceName, "tags.%", "0"),
),
},
},
})
}

func testAccDataSourceAwsEc2LocalGatewayVirtualInterfaceGroupConfigFilter() string {
return `
data "aws_ec2_local_gateways" "test" {}
data "aws_ec2_local_gateway_virtual_interface_group" "test" {
filter {
name = "local-gateway-id"
values = [tolist(data.aws_ec2_local_gateways.test.ids)[0]]
}
}
`
}

func testAccDataSourceAwsEc2LocalGatewayVirtualInterfaceGroupConfigLocalGatewayId() 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]
}
`
}
79 changes: 79 additions & 0 deletions aws/data_source_aws_ec2_local_gateway_virtual_interface_groups.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package aws

import (
"fmt"

"github.com/aws/aws-sdk-go/service/ec2"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/terraform-providers/terraform-provider-aws/aws/internal/keyvaluetags"
)

func dataSourceAwsEc2LocalGatewayVirtualInterfaceGroups() *schema.Resource {
return &schema.Resource{
Read: dataSourceAwsEc2LocalGatewayVirtualInterfaceGroupsRead,

Schema: map[string]*schema.Schema{
"filter": ec2CustomFiltersSchema(),
"ids": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"local_gateway_virtual_interface_ids": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"tags": tagsSchema(),
},
}
}

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

input := &ec2.DescribeLocalGatewayVirtualInterfaceGroupsInput{}

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.DescribeLocalGatewayVirtualInterfaceGroups(input)

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

if output == nil || len(output.LocalGatewayVirtualInterfaceGroups) == 0 {
return fmt.Errorf("no matching Virtual Interface Group found")
}

var ids, localGatewayVirtualInterfaceIds []*string

for _, group := range output.LocalGatewayVirtualInterfaceGroups {
ids = append(ids, group.LocalGatewayVirtualInterfaceGroupId)
localGatewayVirtualInterfaceIds = append(localGatewayVirtualInterfaceIds, group.LocalGatewayVirtualInterfaceIds...)
}

d.SetId(resource.UniqueId())

if err := d.Set("ids", ids); err != nil {
return fmt.Errorf("error setting ids: %w", err)
}

if err := d.Set("local_gateway_virtual_interface_ids", localGatewayVirtualInterfaceIds); err != nil {
return fmt.Errorf("error setting local_gateway_virtual_interface_ids: %w", err)
}

return nil
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package aws

import (
"os"
"testing"

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

func TestAccDataSourceAwsEc2LocalGatewayVirtualInterfaceGroups_basic(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_groups.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourceAwsEc2LocalGatewayVirtualInterfaceGroupsConfig(),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(dataSourceName, "ids.#", "1"),
resource.TestCheckResourceAttr(dataSourceName, "local_gateway_virtual_interface_ids.#", "2"),
),
},
},
})
}

func TestAccDataSourceAwsEc2LocalGatewayVirtualInterfaceGroups_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_groups.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourceAwsEc2LocalGatewayVirtualInterfaceGroupsConfigFilter(),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(dataSourceName, "ids.#", "1"),
resource.TestCheckResourceAttr(dataSourceName, "local_gateway_virtual_interface_ids.#", "2"),
),
},
},
})
}

func testAccDataSourceAwsEc2LocalGatewayVirtualInterfaceGroupsConfig() string {
return `
data "aws_ec2_local_gateway_virtual_interface_groups" "test" {}
`
}

func testAccDataSourceAwsEc2LocalGatewayVirtualInterfaceGroupsConfigFilter() string {
return `
data "aws_ec2_local_gateways" "test" {}
data "aws_ec2_local_gateway_virtual_interface_groups" "test" {
filter {
name = "local-gateway-id"
values = [tolist(data.aws_ec2_local_gateways.test.ids)[0]]
}
}
`
}
Loading

0 comments on commit 242d9e8

Please sign in to comment.