Skip to content

Commit a2b3550

Browse files
committed
Consolidate 'findIPAMPoolAllocationsForEIP' and 'findIPAMPoolAllocationsForVPC' into 'findIPAMPoolAllocationForResource'.
1 parent 9a3e677 commit a2b3550

File tree

5 files changed

+7
-43
lines changed

5 files changed

+7
-43
lines changed

internal/service/ec2/ec2_eip.go

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,10 @@ import (
1616
awstypes "github.com/aws/aws-sdk-go-v2/service/ec2/types"
1717
"github.com/hashicorp/aws-sdk-go-base/v2/tfawserr"
1818
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
19-
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
2019
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
2120
"github.com/hashicorp/terraform-provider-aws/internal/conns"
2221
"github.com/hashicorp/terraform-provider-aws/internal/enum"
2322
"github.com/hashicorp/terraform-provider-aws/internal/errs/sdkdiag"
24-
tfslices "github.com/hashicorp/terraform-provider-aws/internal/slices"
2523
tftags "github.com/hashicorp/terraform-provider-aws/internal/tags"
2624
"github.com/hashicorp/terraform-provider-aws/internal/tfresource"
2725
"github.com/hashicorp/terraform-provider-aws/names"
@@ -339,7 +337,7 @@ func resourceEIPDelete(ctx context.Context, d *schema.ResourceData, meta any) di
339337
timeout = 10 * time.Minute // IPAM eventual consistency
340338
)
341339
_, err := tfresource.RetryUntilNotFound(ctx, timeout, func(ctx context.Context) (any, error) {
342-
return findIPAMPoolAllocationsForEIP(ctx, conn, ipamPoolID, d.Get("allocation_id").(string))
340+
return findIPAMPoolAllocationForResource(ctx, conn, ipamPoolID, d.Get("allocation_id").(string))
343341
})
344342

345343
if err != nil {
@@ -430,25 +428,3 @@ func disassociateEIP(ctx context.Context, conn *ec2.Client, associationID string
430428
func eipARN(ctx context.Context, c *conns.AWSClient, allocationID string) string {
431429
return c.RegionalARN(ctx, names.EC2, "elastic-ip/"+allocationID)
432430
}
433-
434-
func findIPAMPoolAllocationsForEIP(ctx context.Context, conn *ec2.Client, ipamPoolID, eipAllocationID string) ([]awstypes.IpamPoolAllocation, error) {
435-
input := ec2.GetIpamPoolAllocationsInput{
436-
IpamPoolId: aws.String(ipamPoolID),
437-
}
438-
439-
output, err := findIPAMPoolAllocations(ctx, conn, &input)
440-
441-
if err != nil {
442-
return nil, err
443-
}
444-
445-
output = tfslices.Filter(output, func(v awstypes.IpamPoolAllocation) bool {
446-
return v.ResourceType == awstypes.IpamPoolAllocationResourceTypeEip && aws.ToString(v.ResourceId) == eipAllocationID
447-
})
448-
449-
if len(output) == 0 {
450-
return nil, &retry.NotFoundError{}
451-
}
452-
453-
return output, nil
454-
}

internal/service/ec2/exports_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ var (
162162
FindHostByID = findHostByID
163163
FindIPAMByID = findIPAMByID
164164
FindIPAMPoolAllocationByTwoPartKey = findIPAMPoolAllocationByTwoPartKey
165-
FindIPAMPoolAllocationsForVPC = findIPAMPoolAllocationsForVPC
165+
FindIPAMPoolAllocationForResource = findIPAMPoolAllocationForResource
166166
FindIPAMPoolByID = findIPAMPoolByID
167167
FindIPAMPoolCIDRByTwoPartKey = findIPAMPoolCIDRByTwoPartKey
168168
FindIPAMResourceDiscoveryAssociationByID = findIPAMResourceDiscoveryAssociationByID

internal/service/ec2/find.go

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4188,26 +4188,14 @@ func findIPAMPoolAllocationsByIPAMPoolIDAndResourceID(ctx context.Context, conn
41884188
}), nil
41894189
}
41904190

4191-
func findIPAMPoolAllocationsForVPC(ctx context.Context, conn *ec2.Client, poolID, vpcID string) ([]awstypes.IpamPoolAllocation, error) {
4192-
input := ec2.GetIpamPoolAllocationsInput{
4193-
IpamPoolId: aws.String(poolID),
4194-
}
4195-
4196-
output, err := findIPAMPoolAllocations(ctx, conn, &input)
4191+
func findIPAMPoolAllocationForResource(ctx context.Context, conn *ec2.Client, ipamPoolID, resourceID string) (*awstypes.IpamPoolAllocation, error) {
4192+
output, err := findIPAMPoolAllocationsByIPAMPoolIDAndResourceID(ctx, conn, ipamPoolID, resourceID)
41974193

41984194
if err != nil {
41994195
return nil, err
42004196
}
42014197

4202-
output = tfslices.Filter(output, func(v awstypes.IpamPoolAllocation) bool {
4203-
return v.ResourceType == awstypes.IpamPoolAllocationResourceTypeVpc && aws.ToString(v.ResourceId) == vpcID
4204-
})
4205-
4206-
if len(output) == 0 {
4207-
return nil, &retry.NotFoundError{}
4208-
}
4209-
4210-
return output, nil
4198+
return tfresource.AssertFirstValueResult(output)
42114199
}
42124200

42134201
func findIPAMPoolCIDR(ctx context.Context, conn *ec2.Client, input *ec2.GetIpamPoolCidrsInput) (*awstypes.IpamPoolCidr, error) {

internal/service/ec2/vpc_.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -483,7 +483,7 @@ func resourceVPCDelete(ctx context.Context, d *schema.ResourceData, meta any) di
483483
timeout = 35 * time.Minute // IPAM eventual consistency. It can take ~30 min to release allocations.
484484
)
485485
_, err := tfresource.RetryUntilNotFound(ctx, timeout, func(ctx context.Context) (any, error) {
486-
return findIPAMPoolAllocationsForVPC(ctx, conn, ipamPoolID, d.Id())
486+
return findIPAMPoolAllocationForResource(ctx, conn, ipamPoolID, d.Id())
487487
})
488488

489489
if err != nil {

internal/service/ec2/vpc_ipv4_cidr_block_association_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ func testAccCheckVPCIPv4CIDRBlockAssociationWaitVPCIPAMPoolAllocationDeleted(ctx
277277
timeout = 35 * time.Minute // IPAM eventual consistency. It can take ~30 min to release allocations.
278278
)
279279
_, err := tfresource.RetryUntilNotFound(ctx, timeout, func(ctx context.Context) (any, error) {
280-
return tfec2.FindIPAMPoolAllocationsForVPC(ctx, conn, rsIPAMPool.Primary.ID, rsVPC.Primary.ID)
280+
return tfec2.FindIPAMPoolAllocationForResource(ctx, conn, rsIPAMPool.Primary.ID, rsVPC.Primary.ID)
281281
})
282282

283283
return err

0 commit comments

Comments
 (0)