Skip to content

Commit

Permalink
service/ec2: Add arn attribute to aws_vpn_gateway resources (#13827)
Browse files Browse the repository at this point in the history
Output from acceptance testing:

```
--- PASS: TestAccAWSVpnGateway_basic (79.64s)
--- PASS: TestAccAWSVpnGateway_delete (49.63s)
--- PASS: TestAccAWSVpnGateway_disappears (48.79s)
--- PASS: TestAccAWSVpnGateway_reattach (79.59s)
--- PASS: TestAccAWSVpnGateway_tags (65.85s)
--- PASS: TestAccAWSVpnGateway_withAmazonSideAsnSetToState (40.14s)
--- PASS: TestAccAWSVpnGateway_withAvailabilityZoneSetToState (69.85s)

--- PASS: TestAccDataSourceAwsVpnGateway_attached (38.94s)
--- PASS: TestAccDataSourceAwsVpnGateway_unattached (20.19s)
```
  • Loading branch information
DrFaust92 authored Jun 23, 2020
1 parent d76fd92 commit 6b042e6
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 130 deletions.
17 changes: 16 additions & 1 deletion aws/data_source_aws_vpn_gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"strconv"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/arn"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/terraform-providers/terraform-provider-aws/aws/internal/keyvaluetags"
Expand All @@ -16,6 +17,10 @@ func dataSourceAwsVpnGateway() *schema.Resource {
Read: dataSourceAwsVpnGatewayRead,

Schema: map[string]*schema.Schema{
"arn": {
Type: schema.TypeString,
Computed: true,
},
"id": {
Type: schema.TypeString,
Optional: true,
Expand Down Expand Up @@ -113,11 +118,21 @@ func dataSourceAwsVpnGatewayRead(d *schema.ResourceData, meta interface{}) error
}

for _, attachment := range vgw.VpcAttachments {
if *attachment.State == "attached" {
if aws.StringValue(attachment.State) == ec2.AttachmentStatusAttached {
d.Set("attached_vpc_id", attachment.VpcId)
break
}
}

arn := arn.ARN{
Partition: meta.(*AWSClient).partition,
Service: "ec2",
Region: meta.(*AWSClient).region,
AccountID: meta.(*AWSClient).accountid,
Resource: fmt.Sprintf("vpn-gateway/%s", d.Id()),
}.String()

d.Set("arn", arn)

return nil
}
58 changes: 27 additions & 31 deletions aws/data_source_aws_vpn_gateway_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ import (

func TestAccDataSourceAwsVpnGateway_unattached(t *testing.T) {
rInt := acctest.RandInt()
dataSourceNameById := "data.aws_vpn_gateway.test_by_id"
dataSourceNameByTags := "data.aws_vpn_gateway.test_by_tags"
dataSourceNameByAsn := "data.aws_vpn_gateway.test_by_amazon_side_asn"
resourceName := "aws_vpn_gateway.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Expand All @@ -19,19 +23,14 @@ func TestAccDataSourceAwsVpnGateway_unattached(t *testing.T) {
{
Config: testAccDataSourceAwsVpnGatewayUnattachedConfig(rInt),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrPair(
"data.aws_vpn_gateway.test_by_id", "id",
"aws_vpn_gateway.unattached", "id"),
resource.TestCheckResourceAttrPair(
"data.aws_vpn_gateway.test_by_tags", "id",
"aws_vpn_gateway.unattached", "id"),
resource.TestCheckResourceAttrPair(
"data.aws_vpn_gateway.test_by_amazon_side_asn", "id",
"aws_vpn_gateway.unattached", "id"),
resource.TestCheckResourceAttrSet("data.aws_vpn_gateway.test_by_id", "state"),
resource.TestCheckResourceAttr("data.aws_vpn_gateway.test_by_tags", "tags.%", "3"),
resource.TestCheckNoResourceAttr("data.aws_vpn_gateway.test_by_id", "attached_vpc_id"),
resource.TestCheckResourceAttr("data.aws_vpn_gateway.test_by_amazon_side_asn", "amazon_side_asn", "4294967293"),
resource.TestCheckResourceAttrPair(dataSourceNameById, "id", resourceName, "id"),
resource.TestCheckResourceAttrPair(dataSourceNameById, "arn", resourceName, "arn"),
resource.TestCheckResourceAttrPair(dataSourceNameByTags, "id", resourceName, "id"),
resource.TestCheckResourceAttrPair(dataSourceNameByAsn, "id", resourceName, "id"),
resource.TestCheckResourceAttrSet(dataSourceNameById, "state"),
resource.TestCheckResourceAttr(dataSourceNameByTags, "tags.%", "3"),
resource.TestCheckNoResourceAttr(dataSourceNameById, "attached_vpc_id"),
resource.TestCheckResourceAttr(dataSourceNameByAsn, "amazon_side_asn", "4294967293"),
),
},
},
Expand All @@ -40,6 +39,7 @@ func TestAccDataSourceAwsVpnGateway_unattached(t *testing.T) {

func TestAccDataSourceAwsVpnGateway_attached(t *testing.T) {
rInt := acctest.RandInt()
dataSourceName := "data.aws_vpn_gateway.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Expand All @@ -48,13 +48,9 @@ func TestAccDataSourceAwsVpnGateway_attached(t *testing.T) {
{
Config: testAccDataSourceAwsVpnGatewayAttachedConfig(rInt),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrPair(
"data.aws_vpn_gateway.test_by_attached_vpc_id", "id",
"aws_vpn_gateway.attached", "id"),
resource.TestCheckResourceAttrPair(
"data.aws_vpn_gateway.test_by_attached_vpc_id", "attached_vpc_id",
"aws_vpc.foo", "id"),
resource.TestMatchResourceAttr("data.aws_vpn_gateway.test_by_attached_vpc_id", "state", regexp.MustCompile("(?i)available")),
resource.TestCheckResourceAttrPair(dataSourceName, "id", "aws_vpn_gateway.test", "id"),
resource.TestCheckResourceAttrPair(dataSourceName, "attached_vpc_id", "aws_vpc.test", "id"),
resource.TestMatchResourceAttr(dataSourceName, "state", regexp.MustCompile("(?i)available")),
),
},
},
Expand All @@ -63,7 +59,7 @@ func TestAccDataSourceAwsVpnGateway_attached(t *testing.T) {

func testAccDataSourceAwsVpnGatewayUnattachedConfig(rInt int) string {
return fmt.Sprintf(`
resource "aws_vpn_gateway" "unattached" {
resource "aws_vpn_gateway" "test" {
tags = {
Name = "terraform-testacc-vpn-gateway-data-source-unattached-%d"
ABC = "testacc-%d"
Expand All @@ -74,43 +70,43 @@ resource "aws_vpn_gateway" "unattached" {
}
data "aws_vpn_gateway" "test_by_id" {
id = "${aws_vpn_gateway.unattached.id}"
id = "${aws_vpn_gateway.test.id}"
}
data "aws_vpn_gateway" "test_by_tags" {
tags = "${aws_vpn_gateway.unattached.tags}"
tags = "${aws_vpn_gateway.test.tags}"
}
data "aws_vpn_gateway" "test_by_amazon_side_asn" {
amazon_side_asn = "${aws_vpn_gateway.unattached.amazon_side_asn}"
amazon_side_asn = "${aws_vpn_gateway.test.amazon_side_asn}"
state = "available"
}
`, rInt, rInt+1, rInt-1)
}

func testAccDataSourceAwsVpnGatewayAttachedConfig(rInt int) string {
return fmt.Sprintf(`
resource "aws_vpc" "foo" {
resource "aws_vpc" "test" {
cidr_block = "10.1.0.0/16"
tags = {
Name = "terraform-testacc-vpn-gateway-data-source-attached-%d"
}
}
resource "aws_vpn_gateway" "attached" {
resource "aws_vpn_gateway" "test" {
tags = {
Name = "terraform-testacc-vpn-gateway-data-source-attached-%d"
}
}
resource "aws_vpn_gateway_attachment" "vpn_attachment" {
vpc_id = "${aws_vpc.foo.id}"
vpn_gateway_id = "${aws_vpn_gateway.attached.id}"
resource "aws_vpn_gateway_attachment" "test" {
vpc_id = "${aws_vpc.test.id}"
vpn_gateway_id = "${aws_vpn_gateway.test.id}"
}
data "aws_vpn_gateway" "test_by_attached_vpc_id" {
attached_vpc_id = "${aws_vpn_gateway_attachment.vpn_attachment.vpc_id}"
data "aws_vpn_gateway" "test" {
attached_vpc_id = "${aws_vpn_gateway_attachment.test.vpc_id}"
}
`, rInt, rInt)
}
63 changes: 35 additions & 28 deletions aws/resource_aws_vpn_gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"time"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/arn"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
Expand All @@ -25,6 +25,10 @@ func resourceAwsVpnGateway() *schema.Resource {
},

Schema: map[string]*schema.Schema{
"arn": {
Type: schema.TypeString,
Computed: true,
},
"availability_zone": {
Type: schema.TypeString,
Optional: true,
Expand Down Expand Up @@ -55,7 +59,7 @@ func resourceAwsVpnGatewayCreate(d *schema.ResourceData, meta interface{}) error

createOpts := &ec2.CreateVpnGatewayInput{
AvailabilityZone: aws.String(d.Get("availability_zone").(string)),
Type: aws.String("ipsec.1"),
Type: aws.String(ec2.GatewayTypeIpsec1),
}
if asn, ok := d.GetOk("amazon_side_asn"); ok {
i, err := strconv.ParseInt(asn.(string), 10, 64)
Expand Down Expand Up @@ -97,7 +101,8 @@ func resourceAwsVpnGatewayRead(d *schema.ResourceData, meta interface{}) error {
VpnGatewayIds: []*string{aws.String(d.Id())},
})
if err != nil {
if ec2err, ok := err.(awserr.Error); ok && ec2err.Code() == "InvalidVpnGatewayID.NotFound" {
if isAWSErr(err, "InvalidVpnGatewayID.NotFound", "") {
log.Printf("[WARN] VPC Gateway (%s) not found, removing from state", d.Id())
d.SetId("")
return nil
} else {
Expand All @@ -107,8 +112,8 @@ func resourceAwsVpnGatewayRead(d *schema.ResourceData, meta interface{}) error {
}

vpnGateway := resp.VpnGateways[0]
if vpnGateway == nil || *vpnGateway.State == "deleted" {
// Seems we have lost our VPN gateway
if vpnGateway == nil || aws.StringValue(vpnGateway.State) == ec2.VpnStateDeleted {
log.Printf("[WARN] VPC Gateway (%s) not found, removing from state", d.Id())
d.SetId("")
return nil
}
Expand All @@ -121,7 +126,7 @@ func resourceAwsVpnGatewayRead(d *schema.ResourceData, meta interface{}) error {
d.Set("vpc_id", vpnAttachment.VpcId)
}

if vpnGateway.AvailabilityZone != nil && *vpnGateway.AvailabilityZone != "" {
if vpnGateway.AvailabilityZone != nil && aws.StringValue(vpnGateway.AvailabilityZone) != "" {
d.Set("availability_zone", vpnGateway.AvailabilityZone)
}
d.Set("amazon_side_asn", strconv.FormatInt(aws.Int64Value(vpnGateway.AmazonSideAsn), 10))
Expand All @@ -130,6 +135,16 @@ func resourceAwsVpnGatewayRead(d *schema.ResourceData, meta interface{}) error {
return fmt.Errorf("error setting tags: %s", err)
}

arn := arn.ARN{
Partition: meta.(*AWSClient).partition,
Service: "ec2",
Region: meta.(*AWSClient).region,
AccountID: meta.(*AWSClient).accountid,
Resource: fmt.Sprintf("vpn-gateway/%s", d.Id()),
}.String()

d.Set("arn", arn)

return nil
}

Expand Down Expand Up @@ -205,9 +220,7 @@ func resourceAwsVpnGatewayAttach(d *schema.ResourceData, meta interface{}) error
vpcId := d.Get("vpc_id").(string)

if vpcId == "" {
log.Printf(
"[DEBUG] Not attaching VPN Gateway '%s' as no VPC ID is set",
d.Id())
log.Printf("[DEBUG] Not attaching VPN Gateway '%s' as no VPC ID is set", d.Id())
return nil
}

Expand Down Expand Up @@ -242,15 +255,13 @@ func resourceAwsVpnGatewayAttach(d *schema.ResourceData, meta interface{}) error
// Wait for it to be fully attached before continuing
log.Printf("[DEBUG] Waiting for VPN gateway (%s) to attach", d.Id())
stateConf := &resource.StateChangeConf{
Pending: []string{"detached", "attaching"},
Target: []string{"attached"},
Pending: []string{ec2.AttachmentStatusDetached, ec2.AttachmentStatusAttaching},
Target: []string{ec2.AttachmentStatusAttached},
Refresh: vpnGatewayAttachmentStateRefresh(conn, vpcId, d.Id()),
Timeout: 15 * time.Minute,
}
if _, err := stateConf.WaitForState(); err != nil {
return fmt.Errorf(
"Error waiting for VPN gateway (%s) to attach: %s",
d.Id(), err)
return fmt.Errorf("Error waiting for VPN gateway (%s) to attach: %s", d.Id(), err)
}

return nil
Expand Down Expand Up @@ -281,15 +292,13 @@ func resourceAwsVpnGatewayDetach(d *schema.ResourceData, meta interface{}) error
VpcId: aws.String(vpcId),
})
if err != nil {
ec2err, ok := err.(awserr.Error)
if ok {
if ec2err.Code() == "InvalidVpnGatewayID.NotFound" {
err = nil
wait = false
} else if ec2err.Code() == "InvalidVpnGatewayAttachment.NotFound" {
err = nil
wait = false
}
if isAWSErr(err, "InvalidVpnGatewayID.NotFound", "") {
err = nil
wait = false
}
if isAWSErr(err, "InvalidVpnGatewayAttachment.NotFound", "") {
err = nil
wait = false
}

if err != nil {
Expand All @@ -304,15 +313,13 @@ func resourceAwsVpnGatewayDetach(d *schema.ResourceData, meta interface{}) error
// Wait for it to be fully detached before continuing
log.Printf("[DEBUG] Waiting for VPN gateway (%s) to detach", d.Id())
stateConf := &resource.StateChangeConf{
Pending: []string{"attached", "detaching", "available"},
Target: []string{"detached"},
Pending: []string{ec2.AttachmentStatusAttached, ec2.AttachmentStatusDetaching, "available"},
Target: []string{ec2.AttachmentStatusDetached},
Refresh: vpnGatewayAttachmentStateRefresh(conn, vpcId, d.Id()),
Timeout: 10 * time.Minute,
}
if _, err := stateConf.WaitForState(); err != nil {
return fmt.Errorf(
"Error waiting for vpn gateway (%s) to detach: %s",
d.Id(), err)
return fmt.Errorf("Error waiting for vpn gateway (%s) to detach: %s", d.Id(), err)
}

return nil
Expand Down
Loading

0 comments on commit 6b042e6

Please sign in to comment.