Skip to content

Commit

Permalink
data-source/aws_network_interface: Add association carrier_ip and cus…
Browse files Browse the repository at this point in the history
…tomer_owned_ip attributes (#16723)

* d/aws_network_interface: Add 'association.carrier_ip' attribute.

* d/aws_network_interface: Move all configurations below acceptance tests.

* d/aws_network_interface: Refactor existing acceptance tests before testing new functionality.

Acceptance test output:

$ make testacc TEST=./aws/ TESTARGS='-run=TestAccDataSourceAwsNetworkInterface_' ACCTEST_PARALLELISM=2
==> Checking that code complies with gofmt requirements...
TF_ACC=1 go test ./aws -v -count 1 -parallel 2 -run=TestAccDataSourceAwsNetworkInterface_ -timeout 120m
=== RUN   TestAccDataSourceAwsNetworkInterface_basic
=== PAUSE TestAccDataSourceAwsNetworkInterface_basic
=== RUN   TestAccDataSourceAwsNetworkInterface_filters
=== PAUSE TestAccDataSourceAwsNetworkInterface_filters
=== CONT  TestAccDataSourceAwsNetworkInterface_basic
=== CONT  TestAccDataSourceAwsNetworkInterface_filters
--- PASS: TestAccDataSourceAwsNetworkInterface_filters (57.63s)
--- PASS: TestAccDataSourceAwsNetworkInterface_basic (57.84s)
PASS
ok  	github.com/terraform-providers/terraform-provider-aws/aws	57.929s

* d/aws_network_interface: Add 'TestAccDataSourceAwsNetworkInterface_EIPAssociation'.

Acceptance test output:

$ make testacc TEST=./aws/ TESTARGS='-run=TestAccDataSourceAwsNetworkInterface_EIPAssociation' ACCTEST_PARALLELISM=2
==> Checking that code complies with gofmt requirements...
TF_ACC=1 go test ./aws -v -count 1 -parallel 2 -run=TestAccDataSourceAwsNetworkInterface_EIPAssociation -timeout 120m
=== RUN   TestAccDataSourceAwsNetworkInterface_EIPAssociation
=== PAUSE TestAccDataSourceAwsNetworkInterface_EIPAssociation
=== CONT  TestAccDataSourceAwsNetworkInterface_EIPAssociation
--- PASS: TestAccDataSourceAwsNetworkInterface_EIPAssociation (67.86s)
PASS
ok  	github.com/terraform-providers/terraform-provider-aws/aws	67.946s

* d/aws_network_interface: Add 'TestAccDataSourceAwsNetworkInterface_CarrierIPAssociation'.

* Fix linter errors.

* 'EIPAssociation' -> 'PublicIPAssociation'.

* d/aws_network_interface: Add 'association.customer_owned_ip' attribute.

Acceptance test output:

$ make testacc TEST=./aws/ TESTARGS='-run=TestAccDataSourceAwsNetworkInterface_PublicIPAssociation'
==> Checking that code complies with gofmt requirements...
TF_ACC=1 go test ./aws -v -count 1 -parallel 20 -run=TestAccDataSourceAwsNetworkInterface_PublicIPAssociation -timeout 120m
=== RUN   TestAccDataSourceAwsNetworkInterface_PublicIPAssociation
=== PAUSE TestAccDataSourceAwsNetworkInterface_PublicIPAssociation
=== CONT  TestAccDataSourceAwsNetworkInterface_PublicIPAssociation
--- PASS: TestAccDataSourceAwsNetworkInterface_PublicIPAssociation (67.42s)
PASS
ok  	github.com/terraform-providers/terraform-provider-aws/aws	67.484s

* Modify 'flattenEc2NetworkInterfaceAssociation' to conform to suggested code pattern (#16748).
  • Loading branch information
ewbankkit authored Dec 15, 2020
1 parent acf1efa commit 073f1da
Show file tree
Hide file tree
Showing 4 changed files with 250 additions and 65 deletions.
8 changes: 8 additions & 0 deletions aws/data_source_aws_network_interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ func dataSourceAwsNetworkInterface() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},
"carrier_ip": {
Type: schema.TypeString,
Computed: true,
},
"customer_owned_ip": {
Type: schema.TypeString,
Computed: true,
},
"ip_owner_id": {
Type: schema.TypeString,
Computed: true,
Expand Down
281 changes: 224 additions & 57 deletions aws/data_source_aws_network_interface_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,46 +9,156 @@ import (
)

func TestAccDataSourceAwsNetworkInterface_basic(t *testing.T) {
rName := acctest.RandString(5)
datasourceName := "data.aws_network_interface.test"
resourceName := "aws_network_interface.test"
rName := acctest.RandomWithPrefix("tf-acc-test")

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourceAwsNetworkInterface_basic(rName),
Config: testAccDataSourceAwsNetworkInterfaceConfigBasic(rName),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("data.aws_network_interface.test", "private_ips.#", "1"),
resource.TestCheckResourceAttr("data.aws_network_interface.test", "security_groups.#", "1"),
resource.TestCheckResourceAttrPair("data.aws_network_interface.test", "private_ip", "aws_network_interface.test", "private_ip"),
resource.TestCheckResourceAttrSet("data.aws_network_interface.test", "availability_zone"),
resource.TestCheckResourceAttrPair("data.aws_network_interface.test", "description", "aws_network_interface.test", "description"),
resource.TestCheckResourceAttrSet("data.aws_network_interface.test", "interface_type"),
resource.TestCheckResourceAttrPair("data.aws_network_interface.test", "private_dns_name", "aws_network_interface.test", "private_dns_name"),
resource.TestCheckResourceAttrPair("data.aws_network_interface.test", "subnet_id", "aws_network_interface.test", "subnet_id"),
resource.TestCheckResourceAttr("data.aws_network_interface.test", "outpost_arn", ""),
resource.TestCheckResourceAttrSet("data.aws_network_interface.test", "vpc_id"),
resource.TestCheckResourceAttr(datasourceName, "private_ips.#", "1"),
resource.TestCheckResourceAttr(datasourceName, "security_groups.#", "1"),
resource.TestCheckResourceAttrPair(datasourceName, "private_ip", resourceName, "private_ip"),
resource.TestCheckResourceAttrSet(datasourceName, "availability_zone"),
resource.TestCheckResourceAttrPair(datasourceName, "description", resourceName, "description"),
resource.TestCheckResourceAttrSet(datasourceName, "interface_type"),
resource.TestCheckResourceAttrPair(datasourceName, "private_dns_name", resourceName, "private_dns_name"),
resource.TestCheckResourceAttrPair(datasourceName, "subnet_id", resourceName, "subnet_id"),
resource.TestCheckResourceAttr(datasourceName, "outpost_arn", ""),
resource.TestCheckResourceAttrSet(datasourceName, "vpc_id"),
),
},
},
})
}

func testAccDataSourceAwsNetworkInterface_basic(rName string) string {
return fmt.Sprintf(`
data "aws_availability_zones" "available" {
state = "available"
func TestAccDataSourceAwsNetworkInterface_filters(t *testing.T) {
datasourceName := "data.aws_network_interface.test"
rName := acctest.RandomWithPrefix("tf-acc-test")

filter {
name = "opt-in-status"
values = ["opt-in-not-required"]
}
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourceAwsNetworkInterfaceConfigFilters(rName),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(datasourceName, "private_ips.#", "1"),
resource.TestCheckResourceAttr(datasourceName, "security_groups.#", "1"),
),
},
},
})
}

func TestAccDataSourceAwsNetworkInterface_CarrierIPAssociation(t *testing.T) {
datasourceName := "data.aws_network_interface.test"
resourceName := "aws_network_interface.test"
eipResourceName := "aws_eip.test"
eipAssociationResourceName := "aws_eip_association.test"
securityGroupResourceName := "aws_security_group.test"
vpcResourceName := "aws_vpc.test"
rName := acctest.RandomWithPrefix("tf-acc-test")

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t); testAccPreCheckAWSWavelengthZoneAvailable(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourceAwsNetworkInterfaceConfigCarrierIPAssociation(rName),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(datasourceName, "association.#", "1"),
resource.TestCheckResourceAttrPair(datasourceName, "association.0.allocation_id", eipResourceName, "id"),
resource.TestCheckResourceAttrPair(datasourceName, "association.0.association_id", eipAssociationResourceName, "id"),
resource.TestCheckResourceAttrPair(datasourceName, "association.0.carrier_ip", eipResourceName, "carrier_ip"),
resource.TestCheckResourceAttr(datasourceName, "association.0.customer_owned_ip", ""),
testAccCheckResourceAttrAccountID(datasourceName, "association.0.ip_owner_id"),
resource.TestCheckResourceAttr(datasourceName, "association.0.public_dns_name", ""),
resource.TestCheckResourceAttr(datasourceName, "association.0.public_ip", ""),
resource.TestCheckResourceAttr(datasourceName, "attachment.#", "0"),
resource.TestCheckResourceAttrSet(datasourceName, "availability_zone"),
resource.TestCheckResourceAttrPair(datasourceName, "description", resourceName, "description"),
resource.TestCheckResourceAttr(datasourceName, "interface_type", "interface"),
resource.TestCheckResourceAttr(datasourceName, "ipv6_addresses.#", "0"),
resource.TestCheckResourceAttrSet(datasourceName, "mac_address"),
resource.TestCheckResourceAttr(datasourceName, "outpost_arn", ""),
testAccCheckResourceAttrAccountID(datasourceName, "owner_id"),
resource.TestCheckResourceAttrPair(datasourceName, "private_dns_name", resourceName, "private_dns_name"),
resource.TestCheckResourceAttrPair(datasourceName, "private_ip", resourceName, "private_ip"),
resource.TestCheckResourceAttrPair(datasourceName, "private_ips.#", resourceName, "private_ips.#"),
resource.TestCheckResourceAttrPair(datasourceName, "private_ips.0", resourceName, "private_ip"),
resource.TestCheckResourceAttrPair(datasourceName, "security_groups.#", resourceName, "security_groups.#"),
resource.TestCheckTypeSetElemAttrPair(datasourceName, "security_groups.*", securityGroupResourceName, "id"),
resource.TestCheckResourceAttrPair(datasourceName, "subnet_id", resourceName, "subnet_id"),
resource.TestCheckResourceAttrPair(datasourceName, "tags.%", resourceName, "tags.%"),
resource.TestCheckResourceAttrPair(datasourceName, "vpc_id", vpcResourceName, "id"),
),
},
},
})
}

func TestAccDataSourceAwsNetworkInterface_PublicIPAssociation(t *testing.T) {
datasourceName := "data.aws_network_interface.test"
resourceName := "aws_network_interface.test"
eipResourceName := "aws_eip.test"
eipAssociationResourceName := "aws_eip_association.test"
securityGroupResourceName := "aws_security_group.test"
vpcResourceName := "aws_vpc.test"
rName := acctest.RandomWithPrefix("tf-acc-test")

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourceAwsNetworkInterfaceConfigPublicIPAssociation(rName),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(datasourceName, "association.#", "1"),
resource.TestCheckResourceAttrPair(datasourceName, "association.0.allocation_id", eipResourceName, "id"),
resource.TestCheckResourceAttrPair(datasourceName, "association.0.association_id", eipAssociationResourceName, "id"),
resource.TestCheckResourceAttr(datasourceName, "association.0.carrier_ip", ""),
resource.TestCheckResourceAttr(datasourceName, "association.0.customer_owned_ip", ""),
testAccCheckResourceAttrAccountID(datasourceName, "association.0.ip_owner_id"),
// Public DNS name is not set by the EC2 API.
resource.TestCheckResourceAttr(datasourceName, "association.0.public_dns_name", ""),
resource.TestCheckResourceAttrPair(datasourceName, "association.0.public_ip", eipResourceName, "public_ip"),
resource.TestCheckResourceAttr(datasourceName, "attachment.#", "0"),
resource.TestCheckResourceAttrSet(datasourceName, "availability_zone"),
resource.TestCheckResourceAttrPair(datasourceName, "description", resourceName, "description"),
resource.TestCheckResourceAttr(datasourceName, "interface_type", "interface"),
resource.TestCheckResourceAttr(datasourceName, "ipv6_addresses.#", "0"),
resource.TestCheckResourceAttrSet(datasourceName, "mac_address"),
resource.TestCheckResourceAttr(datasourceName, "outpost_arn", ""),
testAccCheckResourceAttrAccountID(datasourceName, "owner_id"),
resource.TestCheckResourceAttrPair(datasourceName, "private_dns_name", resourceName, "private_dns_name"),
resource.TestCheckResourceAttrPair(datasourceName, "private_ip", resourceName, "private_ip"),
resource.TestCheckResourceAttrPair(datasourceName, "private_ips.#", resourceName, "private_ips.#"),
resource.TestCheckResourceAttrPair(datasourceName, "private_ips.0", resourceName, "private_ip"),
resource.TestCheckResourceAttrPair(datasourceName, "security_groups.#", resourceName, "security_groups.#"),
resource.TestCheckTypeSetElemAttrPair(datasourceName, "security_groups.*", securityGroupResourceName, "id"),
resource.TestCheckResourceAttrPair(datasourceName, "subnet_id", resourceName, "subnet_id"),
resource.TestCheckResourceAttrPair(datasourceName, "tags.%", resourceName, "tags.%"),
resource.TestCheckResourceAttrPair(datasourceName, "vpc_id", vpcResourceName, "id"),
),
},
},
})
}

func testAccDataSourceAwsNetworkInterfaceConfigBase(rName string) string {
return composeConfig(
testAccAvailableAZsNoOptInConfig(),
fmt.Sprintf(`
resource "aws_vpc" "test" {
cidr_block = "10.0.0.0/16"
tags = {
Name = "terraform-testacc-eni-data-source-basic"
Name = %[1]q
}
}
Expand All @@ -58,60 +168,46 @@ resource "aws_subnet" "test" {
vpc_id = aws_vpc.test.id
tags = {
Name = "tf-acc-eni-data-source-basic"
Name = %[1]q
}
}
resource "aws_security_group" "test" {
name = "tf-sg-%s"
name = %[1]q
vpc_id = aws_vpc.test.id
}
resource "aws_network_interface" "test" {
subnet_id = aws_subnet.test.id
private_ips = ["10.0.0.50"]
security_groups = [aws_security_group.test.id]
}
data "aws_network_interface" "test" {
id = aws_network_interface.test.id
tags = {
Name = %[1]q
}
}
`, rName)
`, rName))
}

func TestAccDataSourceAwsNetworkInterface_filters(t *testing.T) {
rName := acctest.RandString(5)
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourceAwsNetworkInterface_filters(rName),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("data.aws_network_interface.test", "private_ips.#", "1"),
resource.TestCheckResourceAttr("data.aws_network_interface.test", "security_groups.#", "1"),
),
},
},
})
func testAccDataSourceAwsNetworkInterfaceConfigBasic(rName string) string {
return composeConfig(
testAccDataSourceAwsNetworkInterfaceConfigBase(rName),
`
data "aws_network_interface" "test" {
id = aws_network_interface.test.id
}

func testAccDataSourceAwsNetworkInterface_filters(rName string) string {
return fmt.Sprintf(`
data "aws_availability_zones" "available" {
state = "available"
filter {
name = "opt-in-status"
values = ["opt-in-not-required"]
}
`)
}

func testAccDataSourceAwsNetworkInterfaceConfigCarrierIPAssociation(rName string) string {
return composeConfig(
testAccAvailableAZsWavelengthZonesDefaultExcludeConfig(),
fmt.Sprintf(`
resource "aws_vpc" "test" {
cidr_block = "10.0.0.0/16"
tags = {
Name = "terraform-testacc-eni-data-source-filters"
Name = %[1]q
}
}
Expand All @@ -121,26 +217,97 @@ resource "aws_subnet" "test" {
vpc_id = aws_vpc.test.id
tags = {
Name = "tf-acc-eni-data-source-filters"
Name = %[1]q
}
}
resource "aws_security_group" "test" {
name = "tf-sg-%s"
name = %[1]q
vpc_id = aws_vpc.test.id
}
resource "aws_network_interface" "test" {
subnet_id = aws_subnet.test.id
private_ips = ["10.0.0.60"]
private_ips = ["10.0.0.50"]
security_groups = [aws_security_group.test.id]
tags = {
Name = %[1]q
}
}
resource "aws_ec2_carrier_gateway" "test" {
vpc_id = aws_vpc.test.id
tags = {
Name = %[1]q
}
}
data "aws_availability_zone" "available" {
name = data.aws_availability_zones.available.names[0]
}
resource "aws_eip" "test" {
vpc = true
network_border_group = data.aws_availability_zone.available.network_border_group
tags = {
Name = %[1]q
}
}
resource "aws_eip_association" "test" {
allocation_id = aws_eip.test.id
network_interface_id = aws_network_interface.test.id
}
data "aws_network_interface" "test" {
id = aws_eip_association.test.network_interface_id
}
`, rName))
}

func testAccDataSourceAwsNetworkInterfaceConfigPublicIPAssociation(rName string) string {
return composeConfig(
testAccDataSourceAwsNetworkInterfaceConfigBase(rName),
fmt.Sprintf(`
resource "aws_internet_gateway" "test" {
vpc_id = aws_vpc.test.id
tags = {
Name = %[1]q
}
}
resource "aws_eip" "test" {
vpc = true
tags = {
Name = %[1]q
}
}
resource "aws_eip_association" "test" {
allocation_id = aws_eip.test.id
network_interface_id = aws_network_interface.test.id
}
data "aws_network_interface" "test" {
id = aws_eip_association.test.network_interface_id
}
`, rName))
}

func testAccDataSourceAwsNetworkInterfaceConfigFilters(rName string) string {
return composeConfig(
testAccDataSourceAwsNetworkInterfaceConfigBase(rName),
`
data "aws_network_interface" "test" {
filter {
name = "network-interface-id"
values = [aws_network_interface.test.id]
}
}
`, rName)
`)
}
Loading

0 comments on commit 073f1da

Please sign in to comment.