Merge pull request #1531 from hashicorp/f-aws-upstream-vpn-gateway
provider/aws: Convert VPN Gateway to upstream
This commit is contained in:
commit
5a815efdbb
|
@ -5,8 +5,8 @@ import (
|
|||
"log"
|
||||
"time"
|
||||
|
||||
"github.com/hashicorp/aws-sdk-go/aws"
|
||||
"github.com/hashicorp/aws-sdk-go/gen/ec2"
|
||||
"github.com/awslabs/aws-sdk-go/aws"
|
||||
"github.com/awslabs/aws-sdk-go/service/ec2"
|
||||
"github.com/hashicorp/terraform/helper/resource"
|
||||
"github.com/hashicorp/terraform/helper/schema"
|
||||
)
|
||||
|
@ -36,16 +36,16 @@ func resourceAwsVpnGateway() *schema.Resource {
|
|||
}
|
||||
|
||||
func resourceAwsVpnGatewayCreate(d *schema.ResourceData, meta interface{}) error {
|
||||
ec2conn := meta.(*AWSClient).ec2conn
|
||||
conn := meta.(*AWSClient).ec2SDKconn
|
||||
|
||||
createOpts := &ec2.CreateVPNGatewayRequest{
|
||||
createOpts := &ec2.CreateVPNGatewayInput{
|
||||
AvailabilityZone: aws.String(d.Get("availability_zone").(string)),
|
||||
Type: aws.String("ipsec.1"),
|
||||
}
|
||||
|
||||
// Create the VPN gateway
|
||||
log.Printf("[DEBUG] Creating VPN gateway")
|
||||
resp, err := ec2conn.CreateVPNGateway(createOpts)
|
||||
resp, err := conn.CreateVPNGateway(createOpts)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Error creating VPN gateway: %s", err)
|
||||
}
|
||||
|
@ -60,19 +60,28 @@ func resourceAwsVpnGatewayCreate(d *schema.ResourceData, meta interface{}) error
|
|||
}
|
||||
|
||||
func resourceAwsVpnGatewayRead(d *schema.ResourceData, meta interface{}) error {
|
||||
ec2conn := meta.(*AWSClient).ec2conn
|
||||
conn := meta.(*AWSClient).ec2SDKconn
|
||||
|
||||
vpnGatewayRaw, _, err := vpnGatewayStateRefreshFunc(ec2conn, d.Id())()
|
||||
resp, err := conn.DescribeVPNGateways(&ec2.DescribeVPNGatewaysInput{
|
||||
VPNGatewayIDs: []*string{aws.String(d.Id())},
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
if ec2err, ok := err.(aws.APIError); ok && ec2err.Code == "InvalidVpnGatewayID.NotFound" {
|
||||
d.SetId("")
|
||||
return nil
|
||||
} else {
|
||||
log.Printf("[ERROR] Error finding VpnGateway: %s", err)
|
||||
return err
|
||||
}
|
||||
}
|
||||
if vpnGatewayRaw == nil {
|
||||
|
||||
vpnGateway := resp.VPNGateways[0]
|
||||
if vpnGateway == nil {
|
||||
// Seems we have lost our VPN gateway
|
||||
d.SetId("")
|
||||
return nil
|
||||
}
|
||||
|
||||
vpnGateway := vpnGatewayRaw.(*ec2.VPNGateway)
|
||||
if len(vpnGateway.VPCAttachments) == 0 {
|
||||
// Gateway exists but not attached to the VPC
|
||||
d.Set("vpc_id", "")
|
||||
|
@ -80,7 +89,7 @@ func resourceAwsVpnGatewayRead(d *schema.ResourceData, meta interface{}) error {
|
|||
d.Set("vpc_id", vpnGateway.VPCAttachments[0].VPCID)
|
||||
}
|
||||
d.Set("availability_zone", vpnGateway.AvailabilityZone)
|
||||
d.Set("tags", tagsToMap(vpnGateway.Tags))
|
||||
d.Set("tags", tagsToMapSDK(vpnGateway.Tags))
|
||||
|
||||
return nil
|
||||
}
|
||||
|
@ -98,9 +107,9 @@ func resourceAwsVpnGatewayUpdate(d *schema.ResourceData, meta interface{}) error
|
|||
}
|
||||
}
|
||||
|
||||
ec2conn := meta.(*AWSClient).ec2conn
|
||||
conn := meta.(*AWSClient).ec2SDKconn
|
||||
|
||||
if err := setTags(ec2conn, d); err != nil {
|
||||
if err := setTagsSDK(conn, d); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
|
@ -110,7 +119,7 @@ func resourceAwsVpnGatewayUpdate(d *schema.ResourceData, meta interface{}) error
|
|||
}
|
||||
|
||||
func resourceAwsVpnGatewayDelete(d *schema.ResourceData, meta interface{}) error {
|
||||
ec2conn := meta.(*AWSClient).ec2conn
|
||||
conn := meta.(*AWSClient).ec2SDKconn
|
||||
|
||||
// Detach if it is attached
|
||||
if err := resourceAwsVpnGatewayDetach(d, meta); err != nil {
|
||||
|
@ -120,7 +129,7 @@ func resourceAwsVpnGatewayDelete(d *schema.ResourceData, meta interface{}) error
|
|||
log.Printf("[INFO] Deleting VPN gateway: %s", d.Id())
|
||||
|
||||
return resource.Retry(5*time.Minute, func() error {
|
||||
err := ec2conn.DeleteVPNGateway(&ec2.DeleteVPNGatewayRequest{
|
||||
_, err := conn.DeleteVPNGateway(&ec2.DeleteVPNGatewayInput{
|
||||
VPNGatewayID: aws.String(d.Id()),
|
||||
})
|
||||
if err == nil {
|
||||
|
@ -144,7 +153,7 @@ func resourceAwsVpnGatewayDelete(d *schema.ResourceData, meta interface{}) error
|
|||
}
|
||||
|
||||
func resourceAwsVpnGatewayAttach(d *schema.ResourceData, meta interface{}) error {
|
||||
ec2conn := meta.(*AWSClient).ec2conn
|
||||
conn := meta.(*AWSClient).ec2SDKconn
|
||||
|
||||
if d.Get("vpc_id").(string) == "" {
|
||||
log.Printf(
|
||||
|
@ -158,7 +167,7 @@ func resourceAwsVpnGatewayAttach(d *schema.ResourceData, meta interface{}) error
|
|||
d.Id(),
|
||||
d.Get("vpc_id").(string))
|
||||
|
||||
_, err := ec2conn.AttachVPNGateway(&ec2.AttachVPNGatewayRequest{
|
||||
_, err := conn.AttachVPNGateway(&ec2.AttachVPNGatewayInput{
|
||||
VPNGatewayID: aws.String(d.Id()),
|
||||
VPCID: aws.String(d.Get("vpc_id").(string)),
|
||||
})
|
||||
|
@ -176,7 +185,7 @@ func resourceAwsVpnGatewayAttach(d *schema.ResourceData, meta interface{}) error
|
|||
stateConf := &resource.StateChangeConf{
|
||||
Pending: []string{"detached", "attaching"},
|
||||
Target: "available",
|
||||
Refresh: VpnGatewayAttachStateRefreshFunc(ec2conn, d.Id(), "available"),
|
||||
Refresh: vpnGatewayAttachStateRefreshFunc(conn, d.Id(), "available"),
|
||||
Timeout: 1 * time.Minute,
|
||||
}
|
||||
if _, err := stateConf.WaitForState(); err != nil {
|
||||
|
@ -189,7 +198,7 @@ func resourceAwsVpnGatewayAttach(d *schema.ResourceData, meta interface{}) error
|
|||
}
|
||||
|
||||
func resourceAwsVpnGatewayDetach(d *schema.ResourceData, meta interface{}) error {
|
||||
ec2conn := meta.(*AWSClient).ec2conn
|
||||
conn := meta.(*AWSClient).ec2SDKconn
|
||||
|
||||
// Get the old VPC ID to detach from
|
||||
vpcID, _ := d.GetChange("vpc_id")
|
||||
|
@ -207,7 +216,7 @@ func resourceAwsVpnGatewayDetach(d *schema.ResourceData, meta interface{}) error
|
|||
vpcID.(string))
|
||||
|
||||
wait := true
|
||||
err := ec2conn.DetachVPNGateway(&ec2.DetachVPNGatewayRequest{
|
||||
_, err := conn.DetachVPNGateway(&ec2.DetachVPNGatewayInput{
|
||||
VPNGatewayID: aws.String(d.Id()),
|
||||
VPCID: aws.String(d.Get("vpc_id").(string)),
|
||||
})
|
||||
|
@ -237,7 +246,7 @@ func resourceAwsVpnGatewayDetach(d *schema.ResourceData, meta interface{}) error
|
|||
stateConf := &resource.StateChangeConf{
|
||||
Pending: []string{"attached", "detaching", "available"},
|
||||
Target: "detached",
|
||||
Refresh: VpnGatewayAttachStateRefreshFunc(ec2conn, d.Id(), "detached"),
|
||||
Refresh: vpnGatewayAttachStateRefreshFunc(conn, d.Id(), "detached"),
|
||||
Timeout: 1 * time.Minute,
|
||||
}
|
||||
if _, err := stateConf.WaitForState(); err != nil {
|
||||
|
@ -249,43 +258,17 @@ func resourceAwsVpnGatewayDetach(d *schema.ResourceData, meta interface{}) error
|
|||
return nil
|
||||
}
|
||||
|
||||
// vpnGatewayStateRefreshFunc returns a resource.StateRefreshFunc that is used to watch a VPNGateway.
|
||||
func vpnGatewayStateRefreshFunc(conn *ec2.EC2, id string) resource.StateRefreshFunc {
|
||||
return func() (interface{}, string, error) {
|
||||
resp, err := conn.DescribeVPNGateways(&ec2.DescribeVPNGatewaysRequest{
|
||||
VPNGatewayIDs: []string{id},
|
||||
})
|
||||
if err != nil {
|
||||
if ec2err, ok := err.(aws.APIError); ok && ec2err.Code == "InvalidVpnGatewayID.NotFound" {
|
||||
resp = nil
|
||||
} else {
|
||||
log.Printf("[ERROR] Error on VpnGatewayStateRefresh: %s", err)
|
||||
return nil, "", err
|
||||
}
|
||||
}
|
||||
|
||||
if resp == nil {
|
||||
// Sometimes AWS just has consistency issues and doesn't see
|
||||
// our instance yet. Return an empty state.
|
||||
return nil, "", nil
|
||||
}
|
||||
|
||||
vpnGateway := &resp.VPNGateways[0]
|
||||
return vpnGateway, *vpnGateway.State, nil
|
||||
}
|
||||
}
|
||||
|
||||
// VpnGatewayAttachStateRefreshFunc returns a resource.StateRefreshFunc that is used to watch
|
||||
// vpnGatewayAttachStateRefreshFunc returns a resource.StateRefreshFunc that is used to watch
|
||||
// the state of a VPN gateway's attachment
|
||||
func VpnGatewayAttachStateRefreshFunc(conn *ec2.EC2, id string, expected string) resource.StateRefreshFunc {
|
||||
func vpnGatewayAttachStateRefreshFunc(conn *ec2.EC2, id string, expected string) resource.StateRefreshFunc {
|
||||
var start time.Time
|
||||
return func() (interface{}, string, error) {
|
||||
if start.IsZero() {
|
||||
start = time.Now()
|
||||
}
|
||||
|
||||
resp, err := conn.DescribeVPNGateways(&ec2.DescribeVPNGatewaysRequest{
|
||||
VPNGatewayIDs: []string{id},
|
||||
resp, err := conn.DescribeVPNGateways(&ec2.DescribeVPNGatewaysInput{
|
||||
VPNGatewayIDs: []*string{aws.String(id)},
|
||||
})
|
||||
if err != nil {
|
||||
if ec2err, ok := err.(aws.APIError); ok && ec2err.Code == "InvalidVpnGatewayID.NotFound" {
|
||||
|
@ -302,7 +285,7 @@ func VpnGatewayAttachStateRefreshFunc(conn *ec2.EC2, id string, expected string)
|
|||
return nil, "", nil
|
||||
}
|
||||
|
||||
vpnGateway := &resp.VPNGateways[0]
|
||||
vpnGateway := resp.VPNGateways[0]
|
||||
|
||||
if time.Now().Sub(start) > 10*time.Second {
|
||||
return vpnGateway, expected, nil
|
||||
|
|
|
@ -4,13 +4,13 @@ import (
|
|||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/hashicorp/aws-sdk-go/aws"
|
||||
"github.com/hashicorp/aws-sdk-go/gen/ec2"
|
||||
"github.com/awslabs/aws-sdk-go/aws"
|
||||
"github.com/awslabs/aws-sdk-go/service/ec2"
|
||||
"github.com/hashicorp/terraform/helper/resource"
|
||||
"github.com/hashicorp/terraform/terraform"
|
||||
)
|
||||
|
||||
func TestAccAWSVpnGateway(t *testing.T) {
|
||||
func TestAccAWSVpnGateway_basic(t *testing.T) {
|
||||
var v, v2 ec2.VPNGateway
|
||||
|
||||
testNotEqual := func(*terraform.State) error {
|
||||
|
@ -98,7 +98,7 @@ func TestAccVpnGateway_tags(t *testing.T) {
|
|||
Config: testAccCheckVpnGatewayConfigTags,
|
||||
Check: resource.ComposeTestCheckFunc(
|
||||
testAccCheckVpnGatewayExists("aws_vpn_gateway.foo", &v),
|
||||
testAccCheckTags(&v.Tags, "foo", "bar"),
|
||||
testAccCheckTagsSDK(&v.Tags, "foo", "bar"),
|
||||
),
|
||||
},
|
||||
|
||||
|
@ -106,8 +106,8 @@ func TestAccVpnGateway_tags(t *testing.T) {
|
|||
Config: testAccCheckVpnGatewayConfigTagsUpdate,
|
||||
Check: resource.ComposeTestCheckFunc(
|
||||
testAccCheckVpnGatewayExists("aws_vpn_gateway.foo", &v),
|
||||
testAccCheckTags(&v.Tags, "foo", ""),
|
||||
testAccCheckTags(&v.Tags, "bar", "baz"),
|
||||
testAccCheckTagsSDK(&v.Tags, "foo", ""),
|
||||
testAccCheckTagsSDK(&v.Tags, "bar", "baz"),
|
||||
),
|
||||
},
|
||||
},
|
||||
|
@ -115,7 +115,7 @@ func TestAccVpnGateway_tags(t *testing.T) {
|
|||
}
|
||||
|
||||
func testAccCheckVpnGatewayDestroy(s *terraform.State) error {
|
||||
ec2conn := testAccProvider.Meta().(*AWSClient).ec2conn
|
||||
ec2conn := testAccProvider.Meta().(*AWSClient).ec2SDKconn
|
||||
|
||||
for _, rs := range s.RootModule().Resources {
|
||||
if rs.Type != "aws_vpn_gateway" {
|
||||
|
@ -123,8 +123,8 @@ func testAccCheckVpnGatewayDestroy(s *terraform.State) error {
|
|||
}
|
||||
|
||||
// Try to find the resource
|
||||
resp, err := ec2conn.DescribeVPNGateways(&ec2.DescribeVPNGatewaysRequest{
|
||||
VPNGatewayIDs: []string{rs.Primary.ID},
|
||||
resp, err := ec2conn.DescribeVPNGateways(&ec2.DescribeVPNGatewaysInput{
|
||||
VPNGatewayIDs: []*string{aws.String(rs.Primary.ID)},
|
||||
})
|
||||
if err == nil {
|
||||
if len(resp.VPNGateways) > 0 {
|
||||
|
@ -158,9 +158,9 @@ func testAccCheckVpnGatewayExists(n string, ig *ec2.VPNGateway) resource.TestChe
|
|||
return fmt.Errorf("No ID is set")
|
||||
}
|
||||
|
||||
ec2conn := testAccProvider.Meta().(*AWSClient).ec2conn
|
||||
resp, err := ec2conn.DescribeVPNGateways(&ec2.DescribeVPNGatewaysRequest{
|
||||
VPNGatewayIDs: []string{rs.Primary.ID},
|
||||
ec2conn := testAccProvider.Meta().(*AWSClient).ec2SDKconn
|
||||
resp, err := ec2conn.DescribeVPNGateways(&ec2.DescribeVPNGatewaysInput{
|
||||
VPNGatewayIDs: []*string{aws.String(rs.Primary.ID)},
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
|
@ -169,7 +169,7 @@ func testAccCheckVpnGatewayExists(n string, ig *ec2.VPNGateway) resource.TestChe
|
|||
return fmt.Errorf("VPNGateway not found")
|
||||
}
|
||||
|
||||
*ig = resp.VPNGateways[0]
|
||||
*ig = *resp.VPNGateways[0]
|
||||
|
||||
return nil
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue