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

support aws_vpc_ipam cascade for easier deletes #23973

Merged
merged 1 commit into from
Apr 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
3 changes: 3 additions & 0 deletions .changelog/23973.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
resource/aws_vpc_ipam: add `cascade` argument
```
10 changes: 9 additions & 1 deletion internal/service/ec2/vpc_ipam.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ func ResourceVPCIpam() *schema.Resource {
Type: schema.TypeInt,
Computed: true,
},
"cascade": {
Type: schema.TypeBool,
Optional: true,
},
"tags": tftags.TagsSchema(),
"tags_all": tftags.TagsSchemaComputed(),
},
Expand Down Expand Up @@ -209,6 +213,10 @@ func resourceVPCIpamDelete(d *schema.ResourceData, meta interface{}) error {
IpamId: aws.String(d.Id()),
}

if v, ok := d.GetOk("cascade"); ok {
input.Cascade = aws.Bool(v.(bool))
}

log.Printf("[DEBUG] Deleting IPAM: %s", d.Id())
_, err := conn.DeleteIpam(input)
if err != nil {
Expand Down Expand Up @@ -263,7 +271,7 @@ func WaitIpamAvailable(conn *ec2.EC2, ipamId string, timeout time.Duration) (*ec

func WaiterIpamDeleted(conn *ec2.EC2, ipamId string, timeout time.Duration) (*ec2.Ipam, error) {
stateConf := &resource.StateChangeConf{
Pending: []string{ec2.IpamStateCreateComplete, ec2.IpamStateModifyComplete},
Pending: []string{ec2.IpamStateCreateComplete, ec2.IpamStateModifyComplete, ec2.IpamStateDeleteInProgress},
Target: []string{InvalidIpamIdNotFound},
Refresh: statusIpamStatus(conn, ipamId),
Timeout: timeout,
Expand Down
62 changes: 62 additions & 0 deletions internal/service/ec2/vpc_ipam_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,36 @@ func TestAccVPCIpam_modify(t *testing.T) {
})
}

func TestAccVPCIpam_cascade(t *testing.T) {
resourceName := "aws_vpc_ipam.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(t); testAccIPAMPreCheck(t) },
ErrorCheck: acctest.ErrorCheck(t, ec2.EndpointsID),
Providers: acctest.Providers,
CheckDestroy: testAccCheckVPCIpamDestroy,
Steps: []resource.TestStep{
{
Config: testAccVPCIpamCascade(),
Check: resource.ComposeTestCheckFunc(
acctest.MatchResourceAttrGlobalARN(resourceName, "arn", "ec2", regexp.MustCompile(`ipam/ipam-[\da-f]+$`)),
resource.TestCheckResourceAttr(resourceName, "operating_regions.#", "1"),
resource.TestCheckResourceAttr(resourceName, "scope_count", "2"),
resource.TestMatchResourceAttr(resourceName, "private_default_scope_id", regexp.MustCompile(`^ipam-scope-[\da-f]+`)),
resource.TestMatchResourceAttr(resourceName, "public_default_scope_id", regexp.MustCompile(`^ipam-scope-[\da-f]+`)),
testAccCheckVPCIpamScopeCreate(resourceName),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"cascade"},
},
},
})
}

func TestAccVPCIpam_tags(t *testing.T) {
resourceName := "aws_vpc_ipam.test"

Expand Down Expand Up @@ -157,6 +187,24 @@ func testAccCheckVPCIpamDestroy(s *terraform.State) error {
return nil
}

func testAccCheckVPCIpamScopeCreate(resourceName string) resource.TestCheckFunc {
return func(s *terraform.State) error {
for _, rs := range s.RootModule().Resources {
if rs.Type != "aws_vpc_ipam" {
continue
}
conn := acctest.Provider.Meta().(*conns.AWSClient).EC2Conn
input := &ec2.CreateIpamScopeInput{
ClientToken: aws.String(resource.UniqueId()),
IpamId: aws.String(rs.Primary.ID),
}
_, err := conn.CreateIpamScope(input)
return err
}
return fmt.Errorf("could not create VPC IPAM Scope")
}
}

const testAccVPCIpamBase = `
data "aws_region" "current" {}

Expand All @@ -168,6 +216,20 @@ resource "aws_vpc_ipam" "test" {
}
`

func testAccVPCIpamCascade() string {
return `
data "aws_region" "current" {}

resource "aws_vpc_ipam" "test" {
description = "test"
operating_regions {
region_name = data.aws_region.current.name
}
cascade = true
}
`
}

const testAccVPCIpamBaseAlternateDescription = `
data "aws_region" "current" {}

Expand Down
1 change: 1 addition & 0 deletions website/docs/r/vpc_ipam.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ The following arguments are supported:
* `description` - (Optional) A description for the IPAM.
* `operating_regions` - (Required) Determines which locales can be chosen when you create pools. Locale is the Region where you want to make an IPAM pool available for allocations. You can only create pools with locales that match the operating Regions of the IPAM. You can only create VPCs from a pool whose locale matches the VPC's Region. You specify a region using the [region_name](#operating_regions) parameter. You **must** set your provider block region as an operating_region.
* `tags` - (Optional) A map of tags to assign to the resource. If configured with a provider [`default_tags` configuration block](/docs/providers/aws/index.html#default_tags-configuration-block) present, tags with matching keys will overwrite those defined at the provider-level.
* `cascade` - (Optional) Enables you to quickly delete an IPAM, private scopes, pools in private scopes, and any allocations in the pools in private scopes.

### operating_regions

Expand Down