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

New Data Source: aws_vpc_public_ipv4_pools #28245

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
e5ae663
Initial work on public_ipv4_pools data source
Dec 7, 2022
044d241
Remove test since we can't AccTest BYOIP-related resources
Dec 7, 2022
c47b4ee
Merge branch 'hashicorp:main' into f-aws_vpc_public_ipv4_pools_data_s…
theonlymalwhere Dec 11, 2022
8908c51
Merge branch 'main' into f-aws_vpc_public_ipv4_pools_data_source
theonlymalwhere Mar 13, 2023
166bde7
Correct tftags.New calls to use new API requirement (ctx)
Mar 13, 2023
dfb9062
Add CHANGELOG entry.
ewbankkit Mar 13, 2023
43ae24d
'FindPublicIpv4Pool(s)' -> 'FindPublicIPv4Pool(s)'.
ewbankkit Mar 13, 2023
1a93dcf
Add and use 'errCodeInvalidPublicIpv4PoolIDNotFound'.
ewbankkit Mar 13, 2023
265fc08
Use 'FindPublicIPv4Pools' in 'FindPublicIPv4Pool'.
ewbankkit Mar 13, 2023
6320447
d/aws_vpc_public_ipv4_pools: Simplify attributes.
ewbankkit Mar 13, 2023
ff27008
Add 'FindPublicIPv4PoolByID'.
ewbankkit Mar 13, 2023
9f56343
d/aws_vpc_public_ipv4_pool: Simplify attributes.
ewbankkit Mar 13, 2023
a5681aa
d/aws_vpc_public_ipv4_pools: Add acceptance tests.
ewbankkit Mar 14, 2023
70094b3
d/aws_vpc_public_ipv4_pool: Add acceptance tests.
ewbankkit Mar 14, 2023
f7d36e9
'aws_vpc_public_ipv4_pools' -> 'aws_ec2_public_ipv4_pools'.
ewbankkit Mar 14, 2023
5577530
'aws_vpc_public_ipv4_pool' -> 'aws_ec2_public_ipv4_pool'.
ewbankkit Mar 14, 2023
a27be32
Fix semgrep 'ci.semgrep.migrate.aws-api-context'.
ewbankkit Mar 14, 2023
a907712
Fix semgrep 'ci.caps5-in-func-name'.
ewbankkit Mar 14, 2023
5f8e886
Fix markdown-lint 'MD007/ul-indent Unordered list indentation [Expect…
ewbankkit Mar 14, 2023
ece0ecf
Fix markdown-lint 'MD031/blanks-around-fences Fenced code blocks shou…
ewbankkit Mar 14, 2023
2635c24
Fix semgrep 'ci.caps5-in-const-name' and 'ci.caps5-in-var-name'.
ewbankkit Mar 14, 2023
9d7a453
Add prefix for EC2 Public IPv4 Pools.
ewbankkit Mar 14, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changelog/28245.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
```release-note:new-data-source
aws_ec2_public_ipv4_pools
```

```release-note:new-data-source
aws_ec2_public_ipv4_pool
```
2 changes: 1 addition & 1 deletion .github/labeler-issue-triage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ service/dynamodbstreams:
service/ebs:
- '((\*|-)\s*`?|(data|resource)\s+"?)aws_ebs_'
service/ec2:
- '((\*|-)\s*`?|(data|resource)\s+"?)aws_(ami|availability_zone|ec2_(availability|capacity|fleet|host|instance|serial|spot|tag)|eip|instance|key_pair|launch_template|placement_group|spot)'
- '((\*|-)\s*`?|(data|resource)\s+"?)aws_(ami|availability_zone|ec2_(availability|capacity|fleet|host|instance|public_ipv4_pool|serial|spot|tag)|eip|instance|key_pair|launch_template|placement_group|spot)'
service/ec2ebs:
- '((\*|-)\s*`?|(data|resource)\s+"?)aws_(ebs_|volume_attach|snapshot_create)'
service/ec2instanceconnect:
Expand Down
1 change: 1 addition & 0 deletions .github/labeler-pr-triage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,7 @@ service/ec2:
- 'website/**/ec2_fleet*'
- 'website/**/ec2_host*'
- 'website/**/ec2_instance_*'
- 'website/**/ec2_public_ipv4_pool*'
- 'website/**/ec2_serial_*'
- 'website/**/ec2_spot_*'
- 'website/**/ec2_tag*'
Expand Down
139 changes: 139 additions & 0 deletions internal/service/ec2/ec2_public_ipv4_pool_data_source.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
package ec2

import (
"context"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-provider-aws/internal/conns"
"github.com/hashicorp/terraform-provider-aws/internal/errs/sdkdiag"
tftags "github.com/hashicorp/terraform-provider-aws/internal/tags"
)

// @SDKDataSource("aws_ec2_public_ipv4_pool")
func DataSourcePublicIPv4Pool() *schema.Resource {
return &schema.Resource{
ReadWithoutTimeout: dataSourcePublicIPv4PoolRead,

Schema: map[string]*schema.Schema{
"description": {
Type: schema.TypeString,
Computed: true,
},
"network_border_group": {
Type: schema.TypeString,
Computed: true,
},
"pool_address_ranges": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"address_count": {
Type: schema.TypeInt,
Computed: true,
},
"available_address_count": {
Type: schema.TypeInt,
Computed: true,
},
"first_address": {
Type: schema.TypeString,
Computed: true,
},
"last_address": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
"pool_id": {
Type: schema.TypeString,
Required: true,
},
"tags": tftags.TagsSchemaComputed(),
"total_address_count": {
Type: schema.TypeInt,
Computed: true,
},
"total_available_address_count": {
Type: schema.TypeInt,
Computed: true,
},
},
}
}

func dataSourcePublicIPv4PoolRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics
conn := meta.(*conns.AWSClient).EC2Conn()
ignoreTagsConfig := meta.(*conns.AWSClient).IgnoreTagsConfig

poolID := d.Get("pool_id").(string)
pool, err := FindPublicIPv4PoolByID(ctx, conn, poolID)

if err != nil {
return sdkdiag.AppendErrorf(diags, "reading EC2 Public IPv4 Pool (%s): %s", poolID, err)
}

d.SetId(poolID)
d.Set("description", pool.Description)
d.Set("network_border_group", pool.NetworkBorderGroup)
if err := d.Set("pool_address_ranges", flattenPublicIPv4PoolRanges(pool.PoolAddressRanges)); err != nil {
return sdkdiag.AppendErrorf(diags, "setting pool_address_ranges: %s", err)
}
if err := d.Set("tags", KeyValueTags(ctx, pool.Tags).IgnoreAWS().IgnoreConfig(ignoreTagsConfig).Map()); err != nil {
return sdkdiag.AppendErrorf(diags, "setting tags: %s", err)
}
d.Set("total_address_count", pool.TotalAddressCount)
d.Set("total_available_address_count", pool.TotalAvailableAddressCount)

return nil
}

func flattenPublicIPv4PoolRange(apiObject *ec2.PublicIpv4PoolRange) map[string]interface{} {
if apiObject == nil {
return nil
}

tfMap := map[string]interface{}{}

if v := apiObject.AddressCount; v != nil {
tfMap["address_count"] = aws.Int64Value(v)
}

if v := apiObject.AvailableAddressCount; v != nil {
tfMap["available_address_count"] = aws.Int64Value(v)
}

if v := apiObject.FirstAddress; v != nil {
tfMap["first_address"] = aws.StringValue(v)
}

if v := apiObject.LastAddress; v != nil {
tfMap["last_address"] = aws.StringValue(v)
}

return tfMap
}

func flattenPublicIPv4PoolRanges(apiObjects []*ec2.PublicIpv4PoolRange) []interface{} {
if len(apiObjects) == 0 {
return nil
}

var tfList []interface{}

for _, apiObject := range apiObjects {
if apiObject == nil {
continue
}

tfList = append(tfList, flattenPublicIPv4PoolRange(apiObject))
}

return tfList
}
59 changes: 59 additions & 0 deletions internal/service/ec2/ec2_public_ipv4_pool_data_source_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package ec2_test

import (
"context"
"testing"

"github.com/aws/aws-sdk-go/service/ec2"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-provider-aws/internal/acctest"
"github.com/hashicorp/terraform-provider-aws/internal/conns"
tfec2 "github.com/hashicorp/terraform-provider-aws/internal/service/ec2"
)

func TestAccEC2PublicIPv4PoolDataSource_basic(t *testing.T) {
ctx := acctest.Context(t)
dataSourceName := "data.aws_ec2_public_ipv4_pool.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(ctx, t); testAccPreCheckPublicIPv4Pools(ctx, t) },
ErrorCheck: acctest.ErrorCheck(t, ec2.EndpointsID),
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
Steps: []resource.TestStep{
{
Config: testPublicIPv4PoolDataSourceConfig_basic,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet(dataSourceName, "total_address_count"),
resource.TestCheckResourceAttrSet(dataSourceName, "total_available_address_count"),
),
},
},
})
}

func testAccPreCheckPublicIPv4Pools(ctx context.Context, t *testing.T) {
conn := acctest.Provider.Meta().(*conns.AWSClient).EC2Conn()

output, err := tfec2.FindPublicIPv4Pools(ctx, conn, &ec2.DescribePublicIpv4PoolsInput{})

if acctest.PreCheckSkipError(err) {
t.Skipf("skipping acceptance testing: %s", err)
}

if err != nil {
t.Fatalf("unexpected PreCheck error: %s", err)
}

// Ensure there is at least one pool.
if len(output) == 0 {
t.Skip("skipping since no EC2 Public IPv4 Pools found")
}
}

const testPublicIPv4PoolDataSourceConfig_basic = `
data "aws_ec2_public_ipv4_pools" "test" {}

data "aws_ec2_public_ipv4_pool" "test" {
pool_id = data.aws_ec2_public_ipv4_pools.test.pool_ids[0]
}
`
66 changes: 66 additions & 0 deletions internal/service/ec2/ec2_public_ipv4_pools_data_source.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package ec2

import (
"context"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-provider-aws/internal/conns"
"github.com/hashicorp/terraform-provider-aws/internal/errs/sdkdiag"
tftags "github.com/hashicorp/terraform-provider-aws/internal/tags"
)

// @SDKDataSource("aws_ec2_public_ipv4_pools")
func DataSourcePublicIPv4Pools() *schema.Resource {
return &schema.Resource{
ReadWithoutTimeout: dataSourcePublicIPv4PoolsRead,

Schema: map[string]*schema.Schema{
"filter": DataSourceFiltersSchema(),
"pool_ids": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"tags": tftags.TagsSchemaComputed(),
},
}
}

func dataSourcePublicIPv4PoolsRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics
conn := meta.(*conns.AWSClient).EC2Conn()

input := &ec2.DescribePublicIpv4PoolsInput{}

input.Filters = append(input.Filters, BuildTagFilterList(
Tags(tftags.New(ctx, d.Get("tags").(map[string]interface{}))),
)...)

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

if len(input.Filters) == 0 {
input.Filters = nil
}

output, err := FindPublicIPv4Pools(ctx, conn, input)

if err != nil {
return sdkdiag.AppendErrorf(diags, "reading EC2 Public IPv4 Pools: %s", err)
}

var poolIDs []string

for _, v := range output {
poolIDs = append(poolIDs, aws.StringValue(v.PoolId))
}

d.SetId(meta.(*conns.AWSClient).Region)
d.Set("pool_ids", poolIDs)

return diags
}
64 changes: 64 additions & 0 deletions internal/service/ec2/ec2_public_ipv4_pools_data_source_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package ec2_test

import (
"fmt"
"testing"

"github.com/aws/aws-sdk-go/service/ec2"
sdkacctest "github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-provider-aws/internal/acctest"
)

func TestAccEC2PublicIPv4PoolsDataSource_basic(t *testing.T) {
ctx := acctest.Context(t)
dataSourceName := "data.aws_ec2_public_ipv4_pools.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(ctx, t) },
ErrorCheck: acctest.ErrorCheck(t, ec2.EndpointsID),
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
Steps: []resource.TestStep{
{
Config: testPublicIPv4PoolsDataSourceConfig_basic,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet(dataSourceName, "pool_ids.#"),
),
},
},
})
}

func TestAccEC2PublicIPv4PoolsDataSource_tags(t *testing.T) {
ctx := acctest.Context(t)
dataSourceName := "data.aws_ec2_public_ipv4_pools.test"
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(ctx, t) },
ErrorCheck: acctest.ErrorCheck(t, ec2.EndpointsID),
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
Steps: []resource.TestStep{
{
Config: testPublicIPv4PoolsDataSourceConfig_tags(rName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(dataSourceName, "pool_ids.#", "0"),
),
},
},
})
}

const testPublicIPv4PoolsDataSourceConfig_basic = `
data "aws_ec2_public_ipv4_pools" "test" {}
`

func testPublicIPv4PoolsDataSourceConfig_tags(rName string) string {
return fmt.Sprintf(`
data "aws_ec2_public_ipv4_pools" "test" {
tags = {
Name = %[1]q
}
}
`, rName)
}
1 change: 1 addition & 0 deletions internal/service/ec2/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ const (
errCodeInvalidPoolIDNotFound = "InvalidPoolID.NotFound"
errCodeInvalidPrefixListIDNotFound = "InvalidPrefixListID.NotFound"
errCodeInvalidPrefixListIdNotFound = "InvalidPrefixListId.NotFound"
errCodeInvalidPublicIpv4PoolIDNotFound = "InvalidPublicIpv4PoolID.NotFound" // nosemgrep:ci.caps5-in-const-name,ci.caps5-in-var-name
errCodeInvalidRouteNotFound = "InvalidRoute.NotFound"
errCodeInvalidRouteTableIDNotFound = "InvalidRouteTableID.NotFound"
errCodeInvalidRouteTableIdNotFound = "InvalidRouteTableId.NotFound"
Expand Down
Loading