-
Notifications
You must be signed in to change notification settings - Fork 9.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #28245 from gatsbysghost/f-aws_vpc_public_ipv4_poo…
…ls_data_source New Data Source: aws_vpc_public_ipv4_pools
- Loading branch information
Showing
13 changed files
with
509 additions
and
2 deletions.
There are no files selected for viewing
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,7 @@ | ||
```release-note:new-data-source | ||
aws_ec2_public_ipv4_pools | ||
``` | ||
|
||
```release-note:new-data-source | ||
aws_ec2_public_ipv4_pool | ||
``` |
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
139 changes: 139 additions & 0 deletions
139
internal/service/ec2/ec2_public_ipv4_pool_data_source.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,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
59
internal/service/ec2/ec2_public_ipv4_pool_data_source_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,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] | ||
} | ||
` |
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,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
64
internal/service/ec2/ec2_public_ipv4_pools_data_source_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,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) | ||
} |
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
Oops, something went wrong.