Skip to content

Commit

Permalink
Merge pull request #34547 from hashicorp/b-delete-unattached-netmgr-v…
Browse files Browse the repository at this point in the history
…pc-attachment

resource/aws_networkmanager_vpc_attachment: Allow deleting when not attached
  • Loading branch information
gdavison authored Nov 29, 2023
2 parents fab5517 + 9b435a1 commit a2d3708
Show file tree
Hide file tree
Showing 11 changed files with 743 additions and 227 deletions.
11 changes: 11 additions & 0 deletions .changelog/34547.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
```release-note:bug
resource/aws_networkmanager_vpc_attachment: Fixes error where VPC Attachments waiting for acceptance could not be deleted
```

```release-note:bug
resource/aws_networkmanager_vpc_attachment: Fixes error when modifying `options` fields while waiting for acceptance
```

```release-note:bug
resource/aws_networkmanager_attachment_accepter: Now revokes attachment on deletion for VPC Attachments
```
4 changes: 2 additions & 2 deletions internal/service/networkmanager/README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# Terraform AWS Provider NetworkManager Package
# Terraform AWS Provider Network Manager Package

This area is primarily for AWS provider contributors and maintainers. For information on _using_ Terraform and the AWS provider, see the links below.

## Handy Links

* [Find out about contributing](https://hashicorp.github.io/terraform-provider-aws/#contribute) to the AWS provider!
* AWS Provider Docs: [Home](https://registry.terraform.io/providers/hashicorp/aws/latest/docs)
* AWS Docs: [AWS SDK for Go NetworkManager](https://docs.aws.amazon.com/sdk-for-go/api/service/networkmanager/)
* AWS Docs: [AWS SDK for Go Network Manager](https://docs.aws.amazon.com/sdk-for-go/api/service/networkmanager/)
55 changes: 44 additions & 11 deletions internal/service/networkmanager/attachment_accepter.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,27 @@ import (

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/networkmanager"
"github.com/hashicorp/aws-sdk-go-base/v2/awsv1shim/v2/tfawserr"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/hashicorp/terraform-provider-aws/internal/conns"
"github.com/hashicorp/terraform-provider-aws/internal/errs/sdkdiag"
"github.com/hashicorp/terraform-provider-aws/internal/tfresource"
)

// AttachmentAccepter does not require AttachmentType. However, querying attachments for status updates requires knowing tyupe
// AttachmentAccepter does not require AttachmentType. However, querying attachments for status updates requires knowing type
// To facilitate querying and waiters on specific attachment types, attachment_type set to required

// @SDKResource("aws_networkmanager_attachment_accepter")
func ResourceAttachmentAccepter() *schema.Resource {
return &schema.Resource{
CreateWithoutTimeout: resourceAttachmentAccepterCreate,
ReadWithoutTimeout: resourceAttachmentAccepterRead,
DeleteWithoutTimeout: schema.NoopContext,
DeleteWithoutTimeout: resourceAttachmentAccepterDelete,

Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(10 * time.Minute),
Create: schema.DefaultTimeout(15 * time.Minute),
},

Schema: map[string]*schema.Schema{
Expand Down Expand Up @@ -82,6 +84,8 @@ func ResourceAttachmentAccepter() *schema.Resource {
}

func resourceAttachmentAccepterCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics

conn := meta.(*conns.AWSClient).NetworkManagerConn(ctx)

var state string
Expand Down Expand Up @@ -150,8 +154,8 @@ func resourceAttachmentAccepterCreate(ctx context.Context, d *schema.ResourceDat

switch attachmentType {
case networkmanager.AttachmentTypeVpc:
if _, err := waitVPCAttachmentCreated(ctx, conn, attachmentID, d.Timeout(schema.TimeoutCreate)); err != nil {
return diag.Errorf("waiting for Network Manager VPC Attachment (%s) create: %s", attachmentID, err)
if _, err := waitVPCAttachmentAvailable(ctx, conn, attachmentID, d.Timeout(schema.TimeoutCreate)); err != nil {
return diag.Errorf("waiting for Network Manager VPC Attachment (%s) to be attached: %s", attachmentID, err)
}

case networkmanager.AttachmentTypeSiteToSiteVpn:
Expand All @@ -171,10 +175,12 @@ func resourceAttachmentAccepterCreate(ctx context.Context, d *schema.ResourceDat
}
}

return resourceAttachmentAccepterRead(ctx, d, meta)
return append(diags, resourceAttachmentAccepterRead(ctx, d, meta)...)
}

func resourceAttachmentAccepterRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics

conn := meta.(*conns.AWSClient).NetworkManagerConn(ctx)

var a *networkmanager.Attachment
Expand All @@ -186,7 +192,7 @@ func resourceAttachmentAccepterRead(ctx context.Context, d *schema.ResourceData,
if !d.IsNewResource() && tfresource.NotFound(err) {
log.Printf("[WARN] Network Manager VPC Attachment %s not found, removing from state", d.Id())
d.SetId("")
return nil
return diags
}

if err != nil {
Expand All @@ -201,7 +207,7 @@ func resourceAttachmentAccepterRead(ctx context.Context, d *schema.ResourceData,
if !d.IsNewResource() && tfresource.NotFound(err) {
log.Printf("[WARN] Network Manager Site To Site VPN Attachment %s not found, removing from state", d.Id())
d.SetId("")
return nil
return diags
}

if err != nil {
Expand All @@ -216,7 +222,7 @@ func resourceAttachmentAccepterRead(ctx context.Context, d *schema.ResourceData,
if !d.IsNewResource() && tfresource.NotFound(err) {
log.Printf("[WARN] Network Manager Connect Attachment %s not found, removing from state", d.Id())
d.SetId("")
return nil
return diags
}

if err != nil {
Expand All @@ -231,7 +237,7 @@ func resourceAttachmentAccepterRead(ctx context.Context, d *schema.ResourceData,
if !d.IsNewResource() && tfresource.NotFound(err) {
log.Printf("[WARN] Network Manager Transit Gateway Route Table Attachment %s not found, removing from state", d.Id())
d.SetId("")
return nil
return diags
}

if err != nil {
Expand All @@ -250,5 +256,32 @@ func resourceAttachmentAccepterRead(ctx context.Context, d *schema.ResourceData,
d.Set("segment_name", a.SegmentName)
d.Set("state", a.State)

return nil
return diags
}

func resourceAttachmentAccepterDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics

conn := meta.(*conns.AWSClient).NetworkManagerConn(ctx)

switch d.Get("attachment_type") {
case networkmanager.AttachmentTypeVpc:
_, err := conn.DeleteAttachmentWithContext(ctx, &networkmanager.DeleteAttachmentInput{
AttachmentId: aws.String(d.Id()),
})

if tfawserr.ErrCodeEquals(err, networkmanager.ErrCodeResourceNotFoundException) {
return diags
}

if err != nil {
return sdkdiag.AppendErrorf(diags, "deleting Network Manager VPC Attachment (%s): %s", d.Id(), err)
}

if _, err := waitVPCAttachmentDeleted(ctx, conn, d.Id(), d.Timeout(schema.TimeoutDelete)); err != nil {
return sdkdiag.AppendErrorf(diags, "waiting for Network Manager VPC Attachment (%s) delete: %s", d.Id(), err)
}
}

return diags
}
12 changes: 7 additions & 5 deletions internal/service/networkmanager/core_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const (
// Using the following in the FindCoreNetworkPolicyByID function will default to get the latest policy version
latestPolicyVersionID = -1
// Wait time value for core network policy - the default update for the core network policy of 30 minutes is excessive
waitCoreNetworkPolicyCreatedTimeInMinutes = 4
waitCoreNetworkPolicyCreatedTimeInMinutes = 5
)

// @SDKResource("aws_networkmanager_core_network", name="Core Network")
Expand Down Expand Up @@ -436,10 +436,12 @@ func waitCoreNetworkUpdated(ctx context.Context, conn *networkmanager.NetworkMan

func waitCoreNetworkDeleted(ctx context.Context, conn *networkmanager.NetworkManager, id string, timeout time.Duration) (*networkmanager.CoreNetwork, error) {
stateConf := &retry.StateChangeConf{
Pending: []string{networkmanager.CoreNetworkStateDeleting},
Target: []string{},
Timeout: timeout,
Refresh: statusCoreNetworkState(ctx, conn, id),
Pending: []string{networkmanager.CoreNetworkStateDeleting},
Target: []string{},
Timeout: timeout,
Delay: 5 * time.Minute,
MinTimeout: 10 * time.Second,
Refresh: statusCoreNetworkState(ctx, conn, id),
}

outputRaw, err := stateConf.WaitForStateContext(ctx)
Expand Down
Loading

0 comments on commit a2d3708

Please sign in to comment.