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

b/aws_vpc_ipam_pool_cidr_allocation-read description and netmask_length #38618

7 changes: 7 additions & 0 deletions .changelog/38618.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
```release-note:bug
resource/aws_vpc_ipam_pool_cidr_allocation: Set `description` on Read
```

```release-note:bug
resource/aws_vpc_ipam_pool_cidr_allocation: Set `netmask_length` on Read
```
17 changes: 16 additions & 1 deletion internal/service/ec2/ipam_pool_cidr_allocation.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"context"
"fmt"
"log"
"strconv"
"strings"

"github.com/aws/aws-sdk-go-v2/aws"
Expand Down Expand Up @@ -78,6 +79,7 @@ func resourceIPAMPoolCIDRAllocation() *schema.Resource {
Type: schema.TypeInt,
Optional: true,
ForceNew: true,
Computed: true,
ValidateFunc: validation.IntBetween(0, 128),
ConflictsWith: []string{"cidr"},
},
Expand Down Expand Up @@ -164,9 +166,22 @@ func resourceIPAMPoolCIDRAllocationRead(ctx context.Context, d *schema.ResourceD
return sdkdiag.AppendErrorf(diags, "reading IPAM Pool CIDR Allocation (%s): %s", d.Id(), err)
}

d.Set("cidr", allocation.Cidr)
cidr := aws.ToString(allocation.Cidr)
d.Set("cidr", cidr)
d.Set(names.AttrDescription, allocation.Description)
d.Set("ipam_pool_allocation_id", allocation.IpamPoolAllocationId)
d.Set("ipam_pool_id", poolID)
d.Set("netmask_length", nil)
if parts := strings.Split(cidr, "/"); len(parts) == 2 {
if v, err := strconv.Atoi(parts[1]); err == nil {
d.Set("netmask_length", v)
} else {
log.Printf("[WARN] Unable to parse CIDR (%s) netmask length: %s", cidr, err)
}
} else {
log.Printf("[WARN] Invalid CIDR block format: %s", cidr)
}

d.Set(names.AttrResourceID, allocation.ResourceId)
d.Set(names.AttrResourceOwner, allocation.ResourceOwner)
d.Set(names.AttrResourceType, allocation.ResourceType)
Expand Down
88 changes: 79 additions & 9 deletions internal/service/ec2/ipam_pool_cidr_allocation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ func TestAccIPAMPoolCIDRAllocation_ipv4Basic(t *testing.T) {
resource.TestMatchResourceAttr(resourceName, names.AttrID, regexache.MustCompile(`^ipam-pool-alloc-[0-9a-f]+_ipam-pool(-[0-9a-f]+)$`)),
resource.TestMatchResourceAttr(resourceName, "ipam_pool_allocation_id", regexache.MustCompile(`^ipam-pool-alloc-[0-9a-f]+$`)),
resource.TestCheckResourceAttrPair(resourceName, "ipam_pool_id", "aws_vpc_ipam_pool.test", names.AttrID),
resource.TestCheckResourceAttr(resourceName, "netmask_length", strings.Split(cidr, "/")[1]),
),
},
{
Expand All @@ -53,6 +54,43 @@ func TestAccIPAMPoolCIDRAllocation_ipv4Basic(t *testing.T) {
})
}

func TestAccIPAMPoolCIDRAllocation_ipv4Description(t *testing.T) {
ctx := acctest.Context(t)
var allocationV1, allocationV2 awstypes.IpamPoolAllocation
resourceName := "aws_vpc_ipam_pool_cidr_allocation.test"
originalDescription := "original"
updatedDescription := "updated"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(ctx, t) },
ErrorCheck: acctest.ErrorCheck(t, names.EC2ServiceID),
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
CheckDestroy: testAccCheckIPAMPoolAllocationDestroy(ctx),
Steps: []resource.TestStep{
{
Config: testAccIPAMPoolCIDRAllocationConfig_description(originalDescription),
Check: resource.ComposeTestCheckFunc(
testAccCheckIPAMPoolCIDRAllocationExists(ctx, resourceName, &allocationV1),
resource.TestCheckResourceAttr(resourceName, names.AttrDescription, originalDescription),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
{
Config: testAccIPAMPoolCIDRAllocationConfig_description(updatedDescription),
Check: resource.ComposeTestCheckFunc(
testAccCheckIPAMPoolCIDRAllocationExists(ctx, resourceName, &allocationV2),
testAccCheckIPAMPoolCIDRAllocationRecreated(&allocationV1, &allocationV2),
resource.TestCheckResourceAttr(resourceName, names.AttrDescription, updatedDescription),
),
},
},
})
}

func TestAccIPAMPoolCIDRAllocation_disappears(t *testing.T) {
ctx := acctest.Context(t)
var allocation awstypes.IpamPoolAllocation
Expand All @@ -79,9 +117,10 @@ func TestAccIPAMPoolCIDRAllocation_disappears(t *testing.T) {

func TestAccIPAMPoolCIDRAllocation_ipv4BasicNetmask(t *testing.T) {
ctx := acctest.Context(t)
var allocation awstypes.IpamPoolAllocation
var allocationV1, allocationV2 awstypes.IpamPoolAllocation
resourceName := "aws_vpc_ipam_pool_cidr_allocation.test"
netmask := "28"
originalNetmask := "28"
updatedNetmask := "25"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(ctx, t) },
Expand All @@ -90,17 +129,24 @@ func TestAccIPAMPoolCIDRAllocation_ipv4BasicNetmask(t *testing.T) {
CheckDestroy: testAccCheckIPAMPoolAllocationDestroy(ctx),
Steps: []resource.TestStep{
{
Config: testAccIPAMPoolCIDRAllocationConfig_ipv4Netmask(netmask),
Config: testAccIPAMPoolCIDRAllocationConfig_ipv4Netmask(originalNetmask),
Check: resource.ComposeTestCheckFunc(
testAccCheckIPAMPoolCIDRAllocationExists(ctx, resourceName, &allocation),
testAccCheckIPAMCIDRPrefix(&allocation, netmask),
testAccCheckIPAMPoolCIDRAllocationExists(ctx, resourceName, &allocationV1),
testAccCheckIPAMCIDRPrefix(&allocationV1, originalNetmask),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"netmask_length"},
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
{
Config: testAccIPAMPoolCIDRAllocationConfig_ipv4Netmask(updatedNetmask),
Check: resource.ComposeTestCheckFunc(
testAccCheckIPAMPoolCIDRAllocationExists(ctx, resourceName, &allocationV2),
testAccCheckIPAMPoolCIDRAllocationRecreated(&allocationV1, &allocationV2),
testAccCheckIPAMCIDRPrefix(&allocationV2, updatedNetmask),
),
},
},
})
Expand Down Expand Up @@ -266,6 +312,16 @@ func testAccCheckIPAMPoolCIDRAllocationExistsWithProvider(ctx context.Context, n
}
}

func testAccCheckIPAMPoolCIDRAllocationRecreated(before, after *awstypes.IpamPoolAllocation) resource.TestCheckFunc {
return func(s *terraform.State) error {
if before, after := aws.ToString(before.IpamPoolAllocationId), aws.ToString(after.IpamPoolAllocationId); before == after {
return fmt.Errorf("IPAM Pool Cidr Allocation (%s) not recreated", before)
}

return nil
}
}

func testAccCheckIPAMPoolAllocationDestroy(ctx context.Context) resource.TestCheckFunc {
return func(s *terraform.State) error {
conn := acctest.Provider.Meta().(*conns.AWSClient).EC2Client(ctx)
Expand Down Expand Up @@ -326,6 +382,20 @@ resource "aws_vpc_ipam_pool_cidr_allocation" "test" {
`, cidr))
}

func testAccIPAMPoolCIDRAllocationConfig_description(description string) string {
return acctest.ConfigCompose(testAccIPAMPoolCIDRAllocationConfig_base, fmt.Sprintf(`
resource "aws_vpc_ipam_pool_cidr_allocation" "test" {
ipam_pool_id = aws_vpc_ipam_pool.test.id
cidr = "172.2.0.0/28"
description = %[1]q

depends_on = [
aws_vpc_ipam_pool_cidr.test
]
}
`, description))
}

func testAccIPAMPoolCIDRAllocationConfig_ipv4Netmask(netmask string) string {
return acctest.ConfigCompose(testAccIPAMPoolCIDRAllocationConfig_base, fmt.Sprintf(`
resource "aws_vpc_ipam_pool_cidr_allocation" "test" {
Expand Down
10 changes: 5 additions & 5 deletions website/docs/r/vpc_ipam_pool_cidr_allocation.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,11 @@ resource "aws_vpc_ipam" "example" {

This resource supports the following arguments:

* `cidr` - (Optional) The CIDR you want to assign to the pool.
* `description` - (Optional) The description for the allocation.
* `disallowed_cidrs` - (Optional) Exclude a particular CIDR range from being returned by the pool.
* `ipam_pool_id` - (Required) The ID of the pool to which you want to assign a CIDR.
* `netmask_length` - (Optional) The netmask length of the CIDR you would like to allocate to the IPAM pool. Valid Values: `0-128`.
* `cidr` - (Optional, Forces new resource) The CIDR you want to assign to the pool.
* `description` - (Optional, Forces new resource) The description for the allocation.
* `disallowed_cidrs` - (Optional, Forces new resource) Exclude a particular CIDR range from being returned by the pool.
* `ipam_pool_id` - (Required, Forces new resource) The ID of the pool to which you want to assign a CIDR.
* `netmask_length` - (Optional, Forces new resource) The netmask length of the CIDR you would like to allocate to the IPAM pool. Valid Values: `0-128`.

## Attribute Reference

Expand Down
Loading