Skip to content

Commit

Permalink
Merge pull request #28245 from gatsbysghost/f-aws_vpc_public_ipv4_poo…
Browse files Browse the repository at this point in the history
…ls_data_source

New Data Source: aws_vpc_public_ipv4_pools
  • Loading branch information
ewbankkit authored Mar 14, 2023
2 parents b2532fd + 9d7a453 commit 4ffa2b0
Show file tree
Hide file tree
Showing 13 changed files with 509 additions and 2 deletions.
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

0 comments on commit 4ffa2b0

Please sign in to comment.