From 595f2be153694bd992207bb031ec7aa5ce9d9a06 Mon Sep 17 00:00:00 2001 From: Ilia Lazebnik Date: Thu, 18 Jun 2020 18:22:38 +0300 Subject: [PATCH 1/5] add arn attribute + sdk wrappers --- aws/resource_aws_vpn_gateway.go | 63 +++++++++++++----------- aws/resource_aws_vpn_gateway_test.go | 61 ++++------------------- website/docs/index.html.markdown | 2 + website/docs/r/vpn_gateway.html.markdown | 2 +- 4 files changed, 47 insertions(+), 81 deletions(-) diff --git a/aws/resource_aws_vpn_gateway.go b/aws/resource_aws_vpn_gateway.go index 9fe22725bae5..64b28f0ce9b5 100644 --- a/aws/resource_aws_vpn_gateway.go +++ b/aws/resource_aws_vpn_gateway.go @@ -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" @@ -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, @@ -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) @@ -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 { @@ -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 } @@ -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)) @@ -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 } @@ -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 } @@ -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 @@ -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 { @@ -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 diff --git a/aws/resource_aws_vpn_gateway_test.go b/aws/resource_aws_vpn_gateway_test.go index 9d0b197e50e9..7fda998384b7 100644 --- a/aws/resource_aws_vpn_gateway_test.go +++ b/aws/resource_aws_vpn_gateway_test.go @@ -3,6 +3,7 @@ package aws import ( "fmt" "log" + "regexp" "testing" "time" @@ -73,8 +74,8 @@ func testSweepVPNGateways(region string) error { } stateConf := &resource.StateChangeConf{ - Pending: []string{"attached", "detaching"}, - Target: []string{"detached"}, + Pending: []string{ec2.AttachmentStatusAttached, ec2.AttachmentStatusDetaching}, + Target: []string{ec2.AttachmentStatusDetached}, Refresh: vpnGatewayAttachmentStateRefresh(conn, aws.StringValue(vpcAttachment.VpcId), aws.StringValue(vpng.VpnGatewayId)), Timeout: 10 * time.Minute, } @@ -134,8 +135,8 @@ func TestAccAWSVpnGateway_basic(t *testing.T) { { Config: testAccVpnGatewayConfig, Check: resource.ComposeTestCheckFunc( - testAccCheckVpnGatewayExists( - resourceName, &v), + testAccCheckVpnGatewayExists(resourceName, &v), + testAccMatchResourceAttrRegionalARN(resourceName, "arn", "ec2", regexp.MustCompile(`vpn-gateway/vgw-.+`)), ), }, { @@ -146,8 +147,7 @@ func TestAccAWSVpnGateway_basic(t *testing.T) { { Config: testAccVpnGatewayConfigChangeVPC, Check: resource.ComposeTestCheckFunc( - testAccCheckVpnGatewayExists( - resourceName, &v2), + testAccCheckVpnGatewayExists(resourceName, &v2), testNotEqual, ), }, @@ -209,6 +209,7 @@ func TestAccAWSVpnGateway_withAmazonSideAsnSetToState(t *testing.T) { func TestAccAWSVpnGateway_disappears(t *testing.T) { var v ec2.VpnGateway + resourceName := "aws_vpn_gateway.test" resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -218,8 +219,8 @@ func TestAccAWSVpnGateway_disappears(t *testing.T) { { Config: testAccVpnGatewayConfig, Check: resource.ComposeTestCheckFunc( - testAccCheckVpnGatewayExists("aws_vpn_gateway.test", &v), - testAccAWSVpnGatewayDisappears(&v), + testAccCheckVpnGatewayExists(resourceName, &v), + testAccCheckResourceDisappears(testAccProvider, resourceAwsVpnGateway(), resourceName), ), ExpectNonEmptyPlan: true, }, @@ -390,50 +391,6 @@ func TestAccAWSVpnGateway_tags(t *testing.T) { }) } -func testAccAWSVpnGatewayDisappears(gateway *ec2.VpnGateway) resource.TestCheckFunc { - return func(s *terraform.State) error { - conn := testAccProvider.Meta().(*AWSClient).ec2conn - - _, err := conn.DetachVpnGateway(&ec2.DetachVpnGatewayInput{ - VpnGatewayId: gateway.VpnGatewayId, - VpcId: gateway.VpcAttachments[0].VpcId, - }) - if err != nil { - return err - } - - opts := &ec2.DeleteVpnGatewayInput{ - VpnGatewayId: gateway.VpnGatewayId, - } - if _, err := conn.DeleteVpnGateway(opts); err != nil { - return err - } - return resource.Retry(40*time.Minute, func() *resource.RetryError { - opts := &ec2.DescribeVpnGatewaysInput{ - VpnGatewayIds: []*string{gateway.VpnGatewayId}, - } - resp, err := conn.DescribeVpnGateways(opts) - if err != nil { - cgw, ok := err.(awserr.Error) - if ok && cgw.Code() == "InvalidVpnGatewayID.NotFound" { - return nil - } - if ok && cgw.Code() == "IncorrectState" { - return resource.RetryableError(fmt.Errorf( - "Waiting for VPN Gateway to be in the correct state: %v", gateway.VpnGatewayId)) - } - return resource.NonRetryableError( - fmt.Errorf("Error retrieving VPN Gateway: %s", err)) - } - if *resp.VpnGateways[0].State == "deleted" { - return nil - } - return resource.RetryableError(fmt.Errorf( - "Waiting for VPN Gateway: %v", gateway.VpnGatewayId)) - }) - } -} - func testAccCheckVpnGatewayDestroy(s *terraform.State) error { ec2conn := testAccProvider.Meta().(*AWSClient).ec2conn diff --git a/website/docs/index.html.markdown b/website/docs/index.html.markdown index a4f1dc649f25..ba9685ef2c03 100644 --- a/website/docs/index.html.markdown +++ b/website/docs/index.html.markdown @@ -296,6 +296,8 @@ for more information about connecting to alternate AWS endpoints or AWS compatib - [`aws_vpc` resource](/docs/providers/aws/r/vpc.html) - [`aws_vpc_dhcp_options` data source](/docs/providers/aws/d/vpc_dhcp_options.html) - [`aws_vpc_dhcp_options` resource](/docs/providers/aws/r/vpc_dhcp_options.html) + - [`aws_vpn_gateway` data source](/docs/providers/aws/d/vpn_gateway.html) + - [`aws_vpn_gateway` resource](/docs/providers/aws/r/vpn_gateway.html) - [`aws_waf_geo_match_set` resource](/docs/providers/aws/r/waf_geo_match_set.html) - [`aws_waf_ipset` resource](/docs/providers/aws/r/waf_ipset.html) - [`aws_waf_rate_based_rule` resource](/docs/providers/aws/r/waf_rate_based_rule.html) diff --git a/website/docs/r/vpn_gateway.html.markdown b/website/docs/r/vpn_gateway.html.markdown index 28112e0d2c31..6ce75eebf2e8 100644 --- a/website/docs/r/vpn_gateway.html.markdown +++ b/website/docs/r/vpn_gateway.html.markdown @@ -35,9 +35,9 @@ The following arguments are supported: In addition to all arguments above, the following attributes are exported: +* `arn` - Amazon Resource Name (ARN) of the VPN Gateway. * `id` - The ID of the VPN Gateway. - ## Import VPN Gateways can be imported using the `vpn gateway id`, e.g. From 00276bace6fe31f030de27c561cefbed634e18fb Mon Sep 17 00:00:00 2001 From: Ilia Lazebnik Date: Thu, 18 Jun 2020 18:52:14 +0300 Subject: [PATCH 2/5] add arn attribute + sdk wrappers to data source --- aws/data_source_aws_vpn_gateway.go | 15 ++++++- aws/data_source_aws_vpn_gateway_test.go | 58 ++++++++++++------------- aws/resource_aws_vpn_gateway_test.go | 18 ++++++-- 3 files changed, 55 insertions(+), 36 deletions(-) diff --git a/aws/data_source_aws_vpn_gateway.go b/aws/data_source_aws_vpn_gateway.go index 0152c362a48c..80450180a17f 100644 --- a/aws/data_source_aws_vpn_gateway.go +++ b/aws/data_source_aws_vpn_gateway.go @@ -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" @@ -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, @@ -113,11 +118,19 @@ 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() + return nil } diff --git a/aws/data_source_aws_vpn_gateway_test.go b/aws/data_source_aws_vpn_gateway_test.go index f9067e8570f2..62ca87b4caff 100644 --- a/aws/data_source_aws_vpn_gateway_test.go +++ b/aws/data_source_aws_vpn_gateway_test.go @@ -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.unattached" resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -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"), ), }, }, @@ -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) }, @@ -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")), ), }, }, @@ -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" @@ -74,15 +70,15 @@ 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) @@ -90,7 +86,7 @@ data "aws_vpn_gateway" "test_by_amazon_side_asn" { func testAccDataSourceAwsVpnGatewayAttachedConfig(rInt int) string { return fmt.Sprintf(` -resource "aws_vpc" "foo" { +resource "aws_vpc" "test" { cidr_block = "10.1.0.0/16" tags = { @@ -98,19 +94,19 @@ resource "aws_vpc" "foo" { } } -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) } diff --git a/aws/resource_aws_vpn_gateway_test.go b/aws/resource_aws_vpn_gateway_test.go index 7fda998384b7..8c73880a0dde 100644 --- a/aws/resource_aws_vpn_gateway_test.go +++ b/aws/resource_aws_vpn_gateway_test.go @@ -137,6 +137,7 @@ func TestAccAWSVpnGateway_basic(t *testing.T) { Check: resource.ComposeTestCheckFunc( testAccCheckVpnGatewayExists(resourceName, &v), testAccMatchResourceAttrRegionalARN(resourceName, "arn", "ec2", regexp.MustCompile(`vpn-gateway/vgw-.+`)), + resource.TestCheckResourceAttr(resourceName, "tags.%", "0"), ), }, { @@ -168,8 +169,7 @@ func TestAccAWSVpnGateway_withAvailabilityZoneSetToState(t *testing.T) { Config: testAccVpnGatewayConfigWithAZ, Check: resource.ComposeTestCheckFunc( testAccCheckVpnGatewayExists(resourceName, &v), - resource.TestCheckResourceAttr( - resourceName, "availability_zone", "us-west-2a"), + resource.TestCheckResourceAttr(resourceName, "availability_zone", "us-west-2a"), ), }, { @@ -597,6 +597,15 @@ resource "aws_vpn_gateway" "test2" { ` const testAccVpnGatewayConfigWithAZ = ` +data "aws_availability_zones" "azs" { + state = "available" + + filter { + name = "opt-in-status" + values = ["opt-in-not-required"] + } +} + resource "aws_vpc" "test" { cidr_block = "10.1.0.0/16" tags = { @@ -605,8 +614,9 @@ resource "aws_vpc" "test" { } resource "aws_vpn_gateway" "test" { - vpc_id = "${aws_vpc.test.id}" - availability_zone = "us-west-2a" + vpc_id = "${aws_vpc.test.id}" + availability_zone = "${data.aws_availability_zones.azs.names[0]}" + tags = { Name = "terraform-testacc-vpn-gateway-with-az" } From 0e9675069246a43bd864acc828999a7c8f6e811c Mon Sep 17 00:00:00 2001 From: Ilia Lazebnik Date: Thu, 18 Jun 2020 19:30:20 +0300 Subject: [PATCH 3/5] refactor tags test --- aws/resource_aws_vpn_gateway_test.go | 39 ++++++++++++++++++---------- 1 file changed, 26 insertions(+), 13 deletions(-) diff --git a/aws/resource_aws_vpn_gateway_test.go b/aws/resource_aws_vpn_gateway_test.go index 8c73880a0dde..119b6e283b78 100644 --- a/aws/resource_aws_vpn_gateway_test.go +++ b/aws/resource_aws_vpn_gateway_test.go @@ -367,11 +367,11 @@ func TestAccAWSVpnGateway_tags(t *testing.T) { CheckDestroy: testAccCheckVpnGatewayDestroy, Steps: []resource.TestStep{ { - Config: testAccCheckVpnGatewayConfigTags, + Config: testAccCheckVpnGatewayConfigTags1("key1", "value1"), Check: resource.ComposeTestCheckFunc( testAccCheckVpnGatewayExists(resourceName, &v), resource.TestCheckResourceAttr(resourceName, "tags.%", "1"), - resource.TestCheckResourceAttr(resourceName, "tags.Name", "terraform-testacc-vpn-gateway-tags"), + resource.TestCheckResourceAttr(resourceName, "tags.key1", "value1"), ), }, { @@ -380,11 +380,20 @@ func TestAccAWSVpnGateway_tags(t *testing.T) { ImportStateVerify: true, }, { - Config: testAccCheckVpnGatewayConfigTagsUpdate, + Config: testAccCheckVpnGatewayConfigTags2("key1", "value1updated", "key2", "value2"), + Check: resource.ComposeTestCheckFunc( + testAccCheckVpnGatewayExists(resourceName, &v), + resource.TestCheckResourceAttr(resourceName, "tags.%", "2"), + resource.TestCheckResourceAttr(resourceName, "tags.key1", "value1updated"), + resource.TestCheckResourceAttr(resourceName, "tags.key2", "value2"), + ), + }, + { + Config: testAccCheckVpnGatewayConfigTags1("key2", "value2"), Check: resource.ComposeTestCheckFunc( testAccCheckVpnGatewayExists(resourceName, &v), resource.TestCheckResourceAttr(resourceName, "tags.%", "1"), - resource.TestCheckResourceAttr(resourceName, "tags.Name", "terraform-testacc-vpn-gateway-tags-updated"), + resource.TestCheckResourceAttr(resourceName, "tags.key2", "value2"), ), }, }, @@ -482,9 +491,6 @@ resource "aws_vpc" "test" { resource "aws_vpn_gateway" "test" { vpc_id = "${aws_vpc.test.id}" - tags = { - Name = "terraform-testacc-vpn-gateway-basic" - } } ` @@ -504,7 +510,8 @@ resource "aws_vpn_gateway" "test" { } ` -const testAccCheckVpnGatewayConfigTags = ` +func testAccCheckVpnGatewayConfigTags1(tagKey1, tagValue1 string) string { + return fmt.Sprintf(` resource "aws_vpc" "test" { cidr_block = "10.1.0.0/16" tags = { @@ -514,13 +521,16 @@ resource "aws_vpc" "test" { resource "aws_vpn_gateway" "test" { vpc_id = "${aws_vpc.test.id}" + tags = { - Name = "terraform-testacc-vpn-gateway-tags" + %[1]q = %[2]q } } -` +`, tagKey1, tagValue1) +} -const testAccCheckVpnGatewayConfigTagsUpdate = ` +func testAccCheckVpnGatewayConfigTags2(tagKey1, tagValue1, tagKey2, tagValue2 string) string { + return fmt.Sprintf(` resource "aws_vpc" "test" { cidr_block = "10.1.0.0/16" tags = { @@ -530,11 +540,14 @@ resource "aws_vpc" "test" { resource "aws_vpn_gateway" "test" { vpc_id = "${aws_vpc.test.id}" + tags = { - Name = "terraform-testacc-vpn-gateway-tags-updated" + %[1]q = %[2]q + %[3]q = %[4]q } } -` +`, tagKey1, tagValue1, tagKey2, tagValue2) +} const testAccCheckVpnGatewayConfigReattach = ` resource "aws_vpc" "test" { From a9ff3bc07af3d8a648e721bd407b6d7cf9a56daa Mon Sep 17 00:00:00 2001 From: Ilia Lazebnik Date: Thu, 18 Jun 2020 19:34:12 +0300 Subject: [PATCH 4/5] fix arn for data source --- aws/data_source_aws_vpn_gateway.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/aws/data_source_aws_vpn_gateway.go b/aws/data_source_aws_vpn_gateway.go index 80450180a17f..c65fae224187 100644 --- a/aws/data_source_aws_vpn_gateway.go +++ b/aws/data_source_aws_vpn_gateway.go @@ -132,5 +132,7 @@ func dataSourceAwsVpnGatewayRead(d *schema.ResourceData, meta interface{}) error Resource: fmt.Sprintf("vpn-gateway/%s", d.Id()), }.String() + d.Set("arn", arn) + return nil } From 8a0a192ab8f45aebf0ecf19248f5abb251571e18 Mon Sep 17 00:00:00 2001 From: Ilia Lazebnik Date: Thu, 18 Jun 2020 20:07:57 +0300 Subject: [PATCH 5/5] fix arn for data source test --- aws/data_source_aws_vpn_gateway_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/aws/data_source_aws_vpn_gateway_test.go b/aws/data_source_aws_vpn_gateway_test.go index 62ca87b4caff..95a4105cf7bb 100644 --- a/aws/data_source_aws_vpn_gateway_test.go +++ b/aws/data_source_aws_vpn_gateway_test.go @@ -14,7 +14,7 @@ func TestAccDataSourceAwsVpnGateway_unattached(t *testing.T) { 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.unattached" + resourceName := "aws_vpn_gateway.test" resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) },