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

resource/networkmanager_vpn_attachment and update attachment_accepter #27387

Merged
merged 25 commits into from
Nov 10, 2022
Merged
Show file tree
Hide file tree
Changes from 22 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
9cc884c
Add networkmanager VPN attachment and update attachment_accepter
juhala-aws Oct 21, 2022
f28cf86
Add documentation for vpn_attachment and attachment_accepter
juhala-aws Oct 21, 2022
0e671c1
Format Terraform
juhala-aws Oct 21, 2022
cabcc24
Change cases to use consts
juhala-aws Nov 7, 2022
69aabdc
Change function call to individual values
juhala-aws Nov 7, 2022
2f740c1
Validate ARN with regex
juhala-aws Nov 7, 2022
56075a2
Fix logging messages
juhala-aws Nov 7, 2022
c1fcf44
Change function to private
juhala-aws Nov 7, 2022
3660bf1
Add VPN attachment resource to provider
juhala-aws Nov 7, 2022
e7ea76a
Add changelog
juhala-aws Nov 8, 2022
6b59762
Fix linting
juhala-aws Nov 10, 2022
7a82ec5
Fix semgrep
juhala-aws Nov 10, 2022
a6ab736
Merge branch 'main' into f-networkmanager_vpn_attachment
juhala-aws Nov 10, 2022
036631a
Merge branch 'main' into f-networkmanager_vpn_attachment
juhala-aws Nov 10, 2022
83555b5
Fix semgrep
juhala-aws Nov 10, 2022
ebbc05c
Tweak documentation.
ewbankkit Nov 10, 2022
a57415d
r/aws_networkmanager_vpn_attachment: Rename files (site_to_site_vpn).
ewbankkit Nov 10, 2022
c226839
r/aws_networkmanager_vpn_attachment: Rename functions (site_to_site_v…
ewbankkit Nov 10, 2022
b4e79f9
r/aws_networkmanager_vpn_attachment: Rename resource (site_to_site_vpn).
ewbankkit Nov 10, 2022
676b40e
r/aws_networkmanager_site_to_site_vpn_attachment: 'vpn_arn' -> 'vpn_c…
ewbankkit Nov 10, 2022
cd99a88
r/aws_networkmanager_site_to_site_vpn_attachment: Update error messages.
ewbankkit Nov 10, 2022
8e0f25a
r/aws_networkmanager_attachment_accepter: Update error messages.
ewbankkit Nov 10, 2022
ff4efc6
Correct 'TestAccNetworkManagerVPCAttachment_basic'.
ewbankkit Nov 10, 2022
f891366
Fix typo.
ewbankkit Nov 10, 2022
5a3289c
Documentation corrections.
ewbankkit Nov 10, 2022
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/27387.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:new-resource
aws_networkmanager_site_to_site_vpn_attachment
```
1 change: 1 addition & 0 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -1813,6 +1813,7 @@ func New(_ context.Context) (*schema.Provider, error) {
"aws_networkmanager_transit_gateway_registration": networkmanager.ResourceTransitGatewayRegistration(),
"aws_networkmanager_transit_gateway_route_table_attachment": networkmanager.ResourceTransitGatewayRouteTableAttachment(),
"aws_networkmanager_vpc_attachment": networkmanager.ResourceVPCAttachment(),
"aws_networkmanager_site_to_site_vpn_attachment": networkmanager.ResourceSiteToSiteVPNAttachment(),

"aws_opensearch_domain": opensearch.ResourceDomain(),
"aws_opensearch_domain_policy": opensearch.ResourceDomainPolicy(),
Expand Down
113 changes: 86 additions & 27 deletions internal/service/networkmanager/attachment_accepter.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package networkmanager

import (
"context"
"log"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You might have to review the way you do logging (https://developer.hashicorp.com/terraform/plugin/log/writing). That's dependent on the SDK version

"time"

"github.com/aws/aws-sdk-go/aws"
Expand All @@ -10,6 +11,7 @@ import (
"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/tfresource"
)

// AttachmentAccepter does not require AttachmentType. However, querying attachments for status updates requires knowing tyupe
Expand Down Expand Up @@ -43,9 +45,8 @@ func ResourceAttachmentAccepter() *schema.Resource {
ForceNew: true,
ValidateFunc: validation.StringInSlice([]string{
networkmanager.AttachmentTypeVpc,
networkmanager.AttachmentTypeSiteToSiteVpn,
}, false),
// Implement Values() function for validation as more types are onboarded to provider
// networkmanager.AttachmentType_Values(), false),
},
"core_network_arn": {
Type: schema.TypeString,
Expand Down Expand Up @@ -82,18 +83,38 @@ func ResourceAttachmentAccepter() *schema.Resource {
func resourceAttachmentAccepterCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
conn := meta.(*conns.AWSClient).NetworkManagerConn

if attachmentType := d.Get("attachment_type").(string); attachmentType != networkmanager.AttachmentTypeVpc {
return diag.Errorf("unsupported Network Manager Attachment type: %s", attachmentType)
}

var state string
attachmentID := d.Get("attachment_id").(string)
vpcAttachment, err := FindVPCAttachmentByID(ctx, conn, attachmentID)
attachmentType := d.Get("attachment_type").(string)

switch attachmentType {
case networkmanager.AttachmentTypeVpc:
vpcAttachment, err := FindVPCAttachmentByID(ctx, conn, attachmentID)

if err != nil {
return diag.Errorf("reading Network Manager VPC Attachment (%s): %s", attachmentID, err)
}

state = aws.StringValue(vpcAttachment.Attachment.State)

d.SetId(attachmentID)

case networkmanager.AttachmentTypeSiteToSiteVpn:
vpnAttachment, err := FindSiteToSiteVPNAttachmentByID(ctx, conn, attachmentID)

if err != nil {
return diag.Errorf("reading Network Manager Site To Site VPN Attachment (%s): %s", attachmentID, err)
}

state = aws.StringValue(vpnAttachment.Attachment.State)

d.SetId(attachmentID)

if err != nil {
return diag.Errorf("reading Network Manager VPC Attachment (%s): %s", attachmentID, err)
default:
return diag.Errorf("unsupported Network Manager Attachment type: %s", attachmentType)
}

if state := aws.StringValue(vpcAttachment.Attachment.State); state == networkmanager.AttachmentStatePendingAttachmentAcceptance || state == networkmanager.AttachmentStatePendingTagAcceptance {
if state == networkmanager.AttachmentStatePendingAttachmentAcceptance || state == networkmanager.AttachmentStatePendingTagAcceptance {
input := &networkmanager.AcceptAttachmentInput{
AttachmentId: aws.String(attachmentID),
}
Expand All @@ -104,34 +125,72 @@ func resourceAttachmentAccepterCreate(ctx context.Context, d *schema.ResourceDat
return diag.Errorf("accepting Network Manager Attachment (%s): %s", attachmentID, err)
}

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)
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)
}

case networkmanager.AttachmentTypeSiteToSiteVpn:
if _, err := waitSiteToSiteVPNAttachmentAvailable(ctx, conn, attachmentID, d.Timeout(schema.TimeoutCreate)); err != nil {
return diag.Errorf("waiting for Network Manager VPN Attachment (%s) create: %s", attachmentID, err)
}
}
}

d.SetId(attachmentID)

return resourceAttachmentAccepterRead(ctx, d, meta)
}

func resourceAttachmentAccepterRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
conn := meta.(*conns.AWSClient).NetworkManagerConn

vpcAttachment, err := FindVPCAttachmentByID(ctx, conn, d.Id())
switch aType := d.Get("attachment_type"); aType {
case networkmanager.AttachmentTypeVpc:
vpcAttachment, err := FindVPCAttachmentByID(ctx, conn, d.Id())

if err != nil {
return diag.Errorf("reading Network Manager VPC Attachment (%s): %s", d.Id(), err)
}
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
}

if err != nil {
return diag.Errorf("reading Network Manager VPC Attachment (%s): %s", d.Id(), err)
}

a := vpcAttachment.Attachment
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In both cases, a is being set to the *networkmanager.Attachment returned and then the same values are being set on the resource. Instead of duplicating the code for each case, a could be retrieved using the Find... function, and a could be handled once.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How would you do that change as the a is of different type in both cases?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In both cases, a is a *networkmanager.Attachment, so you could declare a before he switch statement, set the value in the switch statement, and then read the values after the switch.

Something like:

var a *networkmanager.Attachment
switch aType := d.Get("attachment_type"); aType {
  case "VPC":
    ...
    a = vpcAttachment.Attachment
 case  "SITE_TO_SITE_VPN":
    ...
    a = vpnAttachment.Attachment
}

d.Set("a_b_c", a.ABC)
...
d.Set("x_y_z", a.XYZ)

d.Set("attachment_policy_rule_number", a.AttachmentPolicyRuleNumber)
d.Set("core_network_arn", a.CoreNetworkArn)
d.Set("core_network_id", a.CoreNetworkId)
d.Set("edge_location", a.EdgeLocation)
d.Set("owner_account_id", a.OwnerAccountId)
d.Set("resource_arn", a.ResourceArn)
d.Set("segment_name", a.SegmentName)
d.Set("state", a.State)

case networkmanager.AttachmentTypeSiteToSiteVpn:
vpnAttachment, err := FindSiteToSiteVPNAttachmentByID(ctx, conn, d.Id())

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
}

a := vpcAttachment.Attachment
d.Set("attachment_policy_rule_number", a.AttachmentPolicyRuleNumber)
d.Set("core_network_arn", a.CoreNetworkArn)
d.Set("core_network_id", a.CoreNetworkId)
d.Set("edge_location", a.EdgeLocation)
d.Set("owner_account_id", a.OwnerAccountId)
d.Set("resource_arn", a.ResourceArn)
d.Set("segment_name", a.SegmentName)
d.Set("state", a.State)
if err != nil {
return diag.Errorf("reading Network Manager Site To Site VPN Attachment (%s): %s", d.Id(), err)
}

a := vpnAttachment.Attachment
d.Set("attachment_policy_rule_number", a.AttachmentPolicyRuleNumber)
d.Set("core_network_arn", a.CoreNetworkArn)
d.Set("core_network_id", a.CoreNetworkId)
d.Set("edge_location", a.EdgeLocation)
d.Set("owner_account_id", a.OwnerAccountId)
d.Set("resource_arn", a.ResourceArn)
d.Set("segment_name", a.SegmentName)
d.Set("state", a.State)
}

return nil
}
Loading