-
Notifications
You must be signed in to change notification settings - Fork 9.6k
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
Add aws_vpn_gateway_attachment resource. #7870
Merged
stack72
merged 1 commit into
hashicorp:master
from
kwilczynski:feature/resource_aws_vpn_gateway_attachment
Aug 6, 2016
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
210 changes: 210 additions & 0 deletions
210
builtin/providers/aws/resource_aws_vpn_gateway_attachment.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,210 @@ | ||
package aws | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"time" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/aws/awserr" | ||
"github.com/aws/aws-sdk-go/service/ec2" | ||
"github.com/hashicorp/terraform/helper/hashcode" | ||
"github.com/hashicorp/terraform/helper/resource" | ||
"github.com/hashicorp/terraform/helper/schema" | ||
) | ||
|
||
func resourceAwsVpnGatewayAttachment() *schema.Resource { | ||
return &schema.Resource{ | ||
Create: resourceAwsVpnGatewayAttachmentCreate, | ||
Read: resourceAwsVpnGatewayAttachmentRead, | ||
Delete: resourceAwsVpnGatewayAttachmentDelete, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"vpc_id": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
}, | ||
"vpn_gateway_id": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func resourceAwsVpnGatewayAttachmentCreate(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*AWSClient).ec2conn | ||
|
||
vpcId := d.Get("vpc_id").(string) | ||
vgwId := d.Get("vpn_gateway_id").(string) | ||
|
||
createOpts := &ec2.AttachVpnGatewayInput{ | ||
VpcId: aws.String(vpcId), | ||
VpnGatewayId: aws.String(vgwId), | ||
} | ||
log.Printf("[DEBUG] VPN Gateway attachment options: %#v", *createOpts) | ||
|
||
_, err := conn.AttachVpnGateway(createOpts) | ||
if err != nil { | ||
return fmt.Errorf("Error attaching VPN Gateway %q to VPC %q: %s", | ||
vgwId, vpcId, err) | ||
} | ||
|
||
d.SetId(vpnGatewayAttachmentId(vpcId, vgwId)) | ||
log.Printf("[INFO] VPN Gateway %q attachment ID: %s", vgwId, d.Id()) | ||
|
||
stateConf := &resource.StateChangeConf{ | ||
Pending: []string{"detached", "attaching"}, | ||
Target: []string{"attached"}, | ||
Refresh: vpnGatewayAttachmentStateRefresh(conn, vpcId, vgwId), | ||
Timeout: 5 * time.Minute, | ||
Delay: 10 * time.Second, | ||
MinTimeout: 3 * time.Second, | ||
} | ||
|
||
_, err = stateConf.WaitForState() | ||
if err != nil { | ||
return fmt.Errorf("Error waiting for VPN Gateway %q to attach to VPC %q: %s", | ||
vgwId, vpcId, err) | ||
} | ||
log.Printf("[DEBUG] VPN Gateway %q attached to VPC %q.", vgwId, vpcId) | ||
|
||
return resourceAwsVpnGatewayAttachmentRead(d, meta) | ||
} | ||
|
||
func resourceAwsVpnGatewayAttachmentRead(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*AWSClient).ec2conn | ||
|
||
vgwId := d.Get("vpn_gateway_id").(string) | ||
|
||
resp, err := conn.DescribeVpnGateways(&ec2.DescribeVpnGatewaysInput{ | ||
VpnGatewayIds: []*string{aws.String(vgwId)}, | ||
}) | ||
|
||
if err != nil { | ||
awsErr, ok := err.(awserr.Error) | ||
if ok && awsErr.Code() == "InvalidVPNGatewayID.NotFound" { | ||
log.Printf("[WARN] VPN Gateway %q not found.", vgwId) | ||
d.SetId("") | ||
return nil | ||
} | ||
return err | ||
} | ||
|
||
vgw := resp.VpnGateways[0] | ||
if *vgw.State == "deleted" { | ||
log.Printf("[INFO] VPN Gateway %q appears to have been deleted.", vgwId) | ||
d.SetId("") | ||
return nil | ||
} | ||
|
||
vga := vpnGatewayGetAttachment(vgw) | ||
if len(vgw.VpcAttachments) == 0 || *vga.State == "detached" { | ||
d.Set("vpc_id", "") | ||
return nil | ||
} | ||
|
||
d.Set("vpc_id", *vga.VpcId) | ||
return nil | ||
} | ||
|
||
func resourceAwsVpnGatewayAttachmentDelete(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*AWSClient).ec2conn | ||
|
||
vpcId := d.Get("vpc_id").(string) | ||
vgwId := d.Get("vpn_gateway_id").(string) | ||
|
||
if vpcId == "" { | ||
log.Printf("[DEBUG] Not detaching VPN Gateway %q as no VPC ID is set.", vgwId) | ||
return nil | ||
} | ||
|
||
_, err := conn.DetachVpnGateway(&ec2.DetachVpnGatewayInput{ | ||
VpcId: aws.String(vpcId), | ||
VpnGatewayId: aws.String(vgwId), | ||
}) | ||
|
||
if err != nil { | ||
awsErr, ok := err.(awserr.Error) | ||
if ok { | ||
switch awsErr.Code() { | ||
case "InvalidVPNGatewayID.NotFound": | ||
log.Printf("[WARN] VPN Gateway %q not found.", vgwId) | ||
d.SetId("") | ||
return nil | ||
case "InvalidVpnGatewayAttachment.NotFound": | ||
log.Printf( | ||
"[WARN] VPN Gateway %q attachment to VPC %q not found.", | ||
vgwId, vpcId) | ||
d.SetId("") | ||
return nil | ||
} | ||
} | ||
|
||
return fmt.Errorf("Error detaching VPN Gateway %q from VPC %q: %s", | ||
vgwId, vpcId, err) | ||
} | ||
|
||
stateConf := &resource.StateChangeConf{ | ||
Pending: []string{"attached", "detaching"}, | ||
Target: []string{"detached"}, | ||
Refresh: vpnGatewayAttachmentStateRefresh(conn, vpcId, vgwId), | ||
Timeout: 5 * time.Minute, | ||
Delay: 10 * time.Second, | ||
MinTimeout: 3 * time.Second, | ||
} | ||
|
||
_, err = stateConf.WaitForState() | ||
if err != nil { | ||
return fmt.Errorf("Error waiting for VPN Gateway %q to detach from VPC %q: %s", | ||
vgwId, vpcId, err) | ||
} | ||
log.Printf("[DEBUG] VPN Gateway %q detached from VPC %q.", vgwId, vpcId) | ||
|
||
d.SetId("") | ||
return nil | ||
} | ||
|
||
func vpnGatewayAttachmentStateRefresh(conn *ec2.EC2, vpcId, vgwId string) resource.StateRefreshFunc { | ||
return func() (interface{}, string, error) { | ||
resp, err := conn.DescribeVpnGateways(&ec2.DescribeVpnGatewaysInput{ | ||
Filters: []*ec2.Filter{ | ||
&ec2.Filter{ | ||
Name: aws.String("attachment.vpc-id"), | ||
Values: []*string{aws.String(vpcId)}, | ||
}, | ||
}, | ||
VpnGatewayIds: []*string{aws.String(vgwId)}, | ||
}) | ||
|
||
if err != nil { | ||
awsErr, ok := err.(awserr.Error) | ||
if ok { | ||
switch awsErr.Code() { | ||
case "InvalidVPNGatewayID.NotFound": | ||
fallthrough | ||
case "InvalidVpnGatewayAttachment.NotFound": | ||
return nil, "", nil | ||
} | ||
} | ||
|
||
return nil, "", err | ||
} | ||
|
||
vgw := resp.VpnGateways[0] | ||
if len(vgw.VpcAttachments) == 0 { | ||
return vgw, "detached", nil | ||
} | ||
|
||
vga := vpnGatewayGetAttachment(vgw) | ||
|
||
log.Printf("[DEBUG] VPN Gateway %q attachment status: %s", vgwId, *vga.State) | ||
return vgw, *vga.State, nil | ||
} | ||
} | ||
|
||
func vpnGatewayAttachmentId(vpcId, vgwId string) string { | ||
return fmt.Sprintf("vpn-attachment-%x", hashcode.String(fmt.Sprintf("%s-%s", vpcId, vgwId))) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if we are adding state == "deleted" here, then we should remove it from line 89 :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@stack72 corrected!