From a3fd7a5ebb75c628b81eb4d527e56f1be8ce3b7b Mon Sep 17 00:00:00 2001 From: Jakub Kania Date: Mon, 2 Mar 2020 02:16:35 +0100 Subject: [PATCH 1/7] [#9209] Fix dx gateway association proposal so it works even if expired --- aws/resource_aws_dx_gateway_association_proposal.go | 9 ++++++--- .../docs/r/dx_gateway_association_proposal.html.markdown | 2 ++ 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/aws/resource_aws_dx_gateway_association_proposal.go b/aws/resource_aws_dx_gateway_association_proposal.go index e77a1814c69..ae35e78b4e0 100644 --- a/aws/resource_aws_dx_gateway_association_proposal.go +++ b/aws/resource_aws_dx_gateway_association_proposal.go @@ -32,7 +32,11 @@ func resourceAwsDxGatewayAssociationProposal() *schema.Resource { return false } - return proposal != nil && aws.StringValue(proposal.ProposalState) == directconnect.GatewayAssociationProposalStateRequested + if proposal == nil { + return true + } + + return aws.StringValue(proposal.ProposalState) == directconnect.GatewayAssociationProposalStateRequested }), ), @@ -104,8 +108,7 @@ func resourceAwsDxGatewayAssociationProposalRead(d *schema.ResourceData, meta in } if proposal == nil { - log.Printf("[WARN] Direct Connect Gateway Association Proposal (%s) not found, removing from state", d.Id()) - d.SetId("") + //The resource may have expired, return the last known state return nil } diff --git a/website/docs/r/dx_gateway_association_proposal.html.markdown b/website/docs/r/dx_gateway_association_proposal.html.markdown index 4fae6c595fa..866c3629a07 100644 --- a/website/docs/r/dx_gateway_association_proposal.html.markdown +++ b/website/docs/r/dx_gateway_association_proposal.html.markdown @@ -10,6 +10,8 @@ description: |- Manages a Direct Connect Gateway Association Proposal, typically for enabling cross-account associations. For single account associations, see the [`aws_dx_gateway_association` resource](/docs/providers/aws/r/dx_gateway_association.html). +~> **NOTE:** The proposal expires after some time so the resource keeps returning the last known state. If you need to create it again you have to taint it. + ## Example Usage ```hcl From 9c77cd253973168b1a70e6c69edb385e0075ceca Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Thu, 5 Mar 2020 10:04:13 -0500 Subject: [PATCH 2/7] Revert "[#9209] Fix dx gateway association proposal so it works even if expired" This reverts commit c13b25caa8cd76614faae4904cd964ba0220e1d2. --- aws/resource_aws_dx_gateway_association_proposal.go | 9 +++------ .../docs/r/dx_gateway_association_proposal.html.markdown | 2 -- 2 files changed, 3 insertions(+), 8 deletions(-) diff --git a/aws/resource_aws_dx_gateway_association_proposal.go b/aws/resource_aws_dx_gateway_association_proposal.go index ae35e78b4e0..e77a1814c69 100644 --- a/aws/resource_aws_dx_gateway_association_proposal.go +++ b/aws/resource_aws_dx_gateway_association_proposal.go @@ -32,11 +32,7 @@ func resourceAwsDxGatewayAssociationProposal() *schema.Resource { return false } - if proposal == nil { - return true - } - - return aws.StringValue(proposal.ProposalState) == directconnect.GatewayAssociationProposalStateRequested + return proposal != nil && aws.StringValue(proposal.ProposalState) == directconnect.GatewayAssociationProposalStateRequested }), ), @@ -108,7 +104,8 @@ func resourceAwsDxGatewayAssociationProposalRead(d *schema.ResourceData, meta in } if proposal == nil { - //The resource may have expired, return the last known state + log.Printf("[WARN] Direct Connect Gateway Association Proposal (%s) not found, removing from state", d.Id()) + d.SetId("") return nil } diff --git a/website/docs/r/dx_gateway_association_proposal.html.markdown b/website/docs/r/dx_gateway_association_proposal.html.markdown index 866c3629a07..4fae6c595fa 100644 --- a/website/docs/r/dx_gateway_association_proposal.html.markdown +++ b/website/docs/r/dx_gateway_association_proposal.html.markdown @@ -10,8 +10,6 @@ description: |- Manages a Direct Connect Gateway Association Proposal, typically for enabling cross-account associations. For single account associations, see the [`aws_dx_gateway_association` resource](/docs/providers/aws/r/dx_gateway_association.html). -~> **NOTE:** The proposal expires after some time so the resource keeps returning the last known state. If you need to create it again you have to taint it. - ## Example Usage ```hcl From ae2f3fa41841e98342559fda7035d39e192a0ae7 Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Thu, 5 Mar 2020 13:34:43 -0500 Subject: [PATCH 3/7] r/aws_dx_gateway_association: Remove ForceNew from proposal_id attribute. --- aws/resource_aws_dx_gateway_association.go | 9 +- ...esource_aws_dx_gateway_association_test.go | 115 ++++++++++++++++-- 2 files changed, 111 insertions(+), 13 deletions(-) diff --git a/aws/resource_aws_dx_gateway_association.go b/aws/resource_aws_dx_gateway_association.go index bb237b99f41..a574640275e 100644 --- a/aws/resource_aws_dx_gateway_association.go +++ b/aws/resource_aws_dx_gateway_association.go @@ -82,10 +82,17 @@ func resourceAwsDxGatewayAssociation() *schema.Resource { }, "proposal_id": { + Type: schema.TypeString, + Optional: true, + ConflictsWith: []string{"associated_gateway_id", "vpn_gateway_id"}, + }, + + "vpn_gateway_id": { Type: schema.TypeString, Optional: true, ForceNew: true, - ConflictsWith: []string{"associated_gateway_id"}, + ConflictsWith: []string{"associated_gateway_id", "associated_gateway_owner_account_id", "proposal_id"}, + Deprecated: "use 'associated_gateway_id' argument instead", }, }, diff --git a/aws/resource_aws_dx_gateway_association_test.go b/aws/resource_aws_dx_gateway_association_test.go index 3b5eacc1085..701a1b5d987 100644 --- a/aws/resource_aws_dx_gateway_association_test.go +++ b/aws/resource_aws_dx_gateway_association_test.go @@ -187,6 +187,8 @@ func TestAccAwsDxGatewayAssociation_V0StateUpgrade(t *testing.T) { resourceName := "aws_dx_gateway_association.test" rName := fmt.Sprintf("terraform-testacc-dxgwassoc-%d", acctest.RandInt()) rBgpAsn := acctest.RandIntRange(64512, 65534) + var ga directconnect.GatewayAssociation + var gap directconnect.GatewayAssociationProposal resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -196,7 +198,7 @@ func TestAccAwsDxGatewayAssociation_V0StateUpgrade(t *testing.T) { { Config: testAccDxGatewayAssociationConfig_basicVpnGatewaySingleAccount(rName, rBgpAsn), Check: resource.ComposeTestCheckFunc( - testAccCheckAwsDxGatewayAssociationExists(resourceName), + testAccCheckAwsDxGatewayAssociationExists(resourceName, &ga, &gap), testAccCheckAwsDxGatewayAssociationStateUpgradeV0(resourceName), ), }, @@ -210,6 +212,8 @@ func TestAccAwsDxGatewayAssociation_basicVpnGatewaySingleAccount(t *testing.T) { resourceNameVgw := "aws_vpn_gateway.test" rName := fmt.Sprintf("terraform-testacc-dxgwassoc-%d", acctest.RandInt()) rBgpAsn := acctest.RandIntRange(64512, 65534) + var ga directconnect.GatewayAssociation + var gap directconnect.GatewayAssociationProposal resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -219,7 +223,7 @@ func TestAccAwsDxGatewayAssociation_basicVpnGatewaySingleAccount(t *testing.T) { { Config: testAccDxGatewayAssociationConfig_basicVpnGatewaySingleAccount(rName, rBgpAsn), Check: resource.ComposeTestCheckFunc( - testAccCheckAwsDxGatewayAssociationExists(resourceName), + testAccCheckAwsDxGatewayAssociationExists(resourceName, &ga, &gap), resource.TestCheckResourceAttrPair(resourceName, "dx_gateway_id", resourceNameDxGw, "id"), resource.TestCheckResourceAttrPair(resourceName, "associated_gateway_id", resourceNameVgw, "id"), resource.TestCheckResourceAttrSet(resourceName, "dx_gateway_association_id"), @@ -254,6 +258,8 @@ func TestAccAwsDxGatewayAssociation_basicVpnGatewayCrossAccount(t *testing.T) { resourceNameVgw := "aws_vpn_gateway.test" rName := fmt.Sprintf("terraform-testacc-dxgwassoc-%d", acctest.RandInt()) rBgpAsn := acctest.RandIntRange(64512, 65534) + var ga directconnect.GatewayAssociation + var gap directconnect.GatewayAssociationProposal resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { @@ -266,7 +272,7 @@ func TestAccAwsDxGatewayAssociation_basicVpnGatewayCrossAccount(t *testing.T) { { Config: testAccDxGatewayAssociationConfig_basicVpnGatewayCrossAccount(rName, rBgpAsn), Check: resource.ComposeTestCheckFunc( - testAccCheckAwsDxGatewayAssociationExists(resourceName), + testAccCheckAwsDxGatewayAssociationExists(resourceName, &ga, &gap), resource.TestCheckResourceAttrPair(resourceName, "dx_gateway_id", resourceNameDxGw, "id"), resource.TestCheckResourceAttrPair(resourceName, "associated_gateway_id", resourceNameVgw, "id"), resource.TestCheckResourceAttrSet(resourceName, "dx_gateway_association_id"), @@ -288,6 +294,8 @@ func TestAccAwsDxGatewayAssociation_basicTransitGatewaySingleAccount(t *testing. resourceNameTgw := "aws_ec2_transit_gateway.test" rName := fmt.Sprintf("terraform-testacc-dxgwassoc-%d", acctest.RandInt()) rBgpAsn := acctest.RandIntRange(64512, 65534) + var ga directconnect.GatewayAssociation + var gap directconnect.GatewayAssociationProposal resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -297,7 +305,7 @@ func TestAccAwsDxGatewayAssociation_basicTransitGatewaySingleAccount(t *testing. { Config: testAccDxGatewayAssociationConfig_basicTransitGatewaySingleAccount(rName, rBgpAsn), Check: resource.ComposeTestCheckFunc( - testAccCheckAwsDxGatewayAssociationExists(resourceName), + testAccCheckAwsDxGatewayAssociationExists(resourceName, &ga, &gap), resource.TestCheckResourceAttrPair(resourceName, "dx_gateway_id", resourceNameDxGw, "id"), resource.TestCheckResourceAttrPair(resourceName, "associated_gateway_id", resourceNameTgw, "id"), resource.TestCheckResourceAttrSet(resourceName, "dx_gateway_association_id"), @@ -333,6 +341,8 @@ func TestAccAwsDxGatewayAssociation_basicTransitGatewayCrossAccount(t *testing.T resourceNameTgw := "aws_ec2_transit_gateway.test" rName := fmt.Sprintf("terraform-testacc-dxgwassoc-%d", acctest.RandInt()) rBgpAsn := acctest.RandIntRange(64512, 65534) + var ga directconnect.GatewayAssociation + var gap directconnect.GatewayAssociationProposal resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { @@ -345,7 +355,7 @@ func TestAccAwsDxGatewayAssociation_basicTransitGatewayCrossAccount(t *testing.T { Config: testAccDxGatewayAssociationConfig_basicTransitGatewayCrossAccount(rName, rBgpAsn), Check: resource.ComposeTestCheckFunc( - testAccCheckAwsDxGatewayAssociationExists(resourceName), + testAccCheckAwsDxGatewayAssociationExists(resourceName, &ga, &gap), resource.TestCheckResourceAttrPair(resourceName, "dx_gateway_id", resourceNameDxGw, "id"), resource.TestCheckResourceAttrPair(resourceName, "associated_gateway_id", resourceNameTgw, "id"), resource.TestCheckResourceAttrSet(resourceName, "dx_gateway_association_id"), @@ -368,6 +378,8 @@ func TestAccAwsDxGatewayAssociation_multiVpnGatewaysSingleAccount(t *testing.T) rName1 := fmt.Sprintf("terraform-testacc-dxgwassoc-%d", acctest.RandInt()) rName2 := fmt.Sprintf("terraform-testacc-dxgwassoc-%d", acctest.RandInt()) rBgpAsn := acctest.RandIntRange(64512, 65534) + var ga directconnect.GatewayAssociation + var gap directconnect.GatewayAssociationProposal resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -377,8 +389,8 @@ func TestAccAwsDxGatewayAssociation_multiVpnGatewaysSingleAccount(t *testing.T) { Config: testAccDxGatewayAssociationConfig_multiVpnGatewaysSingleAccount(rName1, rName2, rBgpAsn), Check: resource.ComposeTestCheckFunc( - testAccCheckAwsDxGatewayAssociationExists(resourceName1), - testAccCheckAwsDxGatewayAssociationExists(resourceName2), + testAccCheckAwsDxGatewayAssociationExists(resourceName1, &ga, &gap), + testAccCheckAwsDxGatewayAssociationExists(resourceName2, &ga, &gap), resource.TestCheckResourceAttrSet(resourceName1, "dx_gateway_association_id"), resource.TestCheckResourceAttr(resourceName1, "allowed_prefixes.#", "1"), resource.TestCheckTypeSetElemAttr(resourceName1, "allowed_prefixes.*", "10.255.255.16/28"), @@ -397,6 +409,8 @@ func TestAccAwsDxGatewayAssociation_allowedPrefixesVpnGatewaySingleAccount(t *te resourceNameVgw := "aws_vpn_gateway.test" rName := fmt.Sprintf("terraform-testacc-dxgwassoc-%d", acctest.RandInt()) rBgpAsn := acctest.RandIntRange(64512, 65534) + var ga directconnect.GatewayAssociation + var gap directconnect.GatewayAssociationProposal resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -406,7 +420,7 @@ func TestAccAwsDxGatewayAssociation_allowedPrefixesVpnGatewaySingleAccount(t *te { Config: testAccDxGatewayAssociationConfig_allowedPrefixesVpnGatewaySingleAccount(rName, rBgpAsn), Check: resource.ComposeTestCheckFunc( - testAccCheckAwsDxGatewayAssociationExists(resourceName), + testAccCheckAwsDxGatewayAssociationExists(resourceName, &ga, &gap), resource.TestCheckResourceAttrPair(resourceName, "dx_gateway_id", resourceNameDxGw, "id"), resource.TestCheckResourceAttrPair(resourceName, "associated_gateway_id", resourceNameVgw, "id"), resource.TestCheckResourceAttrSet(resourceName, "dx_gateway_association_id"), @@ -418,7 +432,7 @@ func TestAccAwsDxGatewayAssociation_allowedPrefixesVpnGatewaySingleAccount(t *te { Config: testAccDxGatewayAssociationConfig_allowedPrefixesVpnGatewaySingleAccountUpdated(rName, rBgpAsn), Check: resource.ComposeTestCheckFunc( - testAccCheckAwsDxGatewayAssociationExists(resourceName), + testAccCheckAwsDxGatewayAssociationExists(resourceName, &ga, &gap), resource.TestCheckResourceAttr(resourceName, "allowed_prefixes.#", "1"), resource.TestCheckTypeSetElemAttr(resourceName, "allowed_prefixes.*", "10.255.255.8/29"), ), @@ -434,6 +448,8 @@ func TestAccAwsDxGatewayAssociation_allowedPrefixesVpnGatewayCrossAccount(t *tes resourceNameVgw := "aws_vpn_gateway.test" rName := fmt.Sprintf("terraform-testacc-dxgwassoc-%d", acctest.RandInt()) rBgpAsn := acctest.RandIntRange(64512, 65534) + var ga directconnect.GatewayAssociation + var gap directconnect.GatewayAssociationProposal resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { @@ -446,7 +462,7 @@ func TestAccAwsDxGatewayAssociation_allowedPrefixesVpnGatewayCrossAccount(t *tes { Config: testAccDxGatewayAssociationConfig_allowedPrefixesVpnGatewayCrossAccount(rName, rBgpAsn), Check: resource.ComposeTestCheckFunc( - testAccCheckAwsDxGatewayAssociationExists(resourceName), + testAccCheckAwsDxGatewayAssociationExists(resourceName, &ga, &gap), resource.TestCheckResourceAttrPair(resourceName, "dx_gateway_id", resourceNameDxGw, "id"), resource.TestCheckResourceAttrPair(resourceName, "associated_gateway_id", resourceNameVgw, "id"), resource.TestCheckResourceAttrSet(resourceName, "dx_gateway_association_id"), @@ -459,7 +475,7 @@ func TestAccAwsDxGatewayAssociation_allowedPrefixesVpnGatewayCrossAccount(t *tes { Config: testAccDxGatewayAssociationConfig_allowedPrefixesVpnGatewayCrossAccountUpdated(rName, rBgpAsn), Check: resource.ComposeTestCheckFunc( - testAccCheckAwsDxGatewayAssociationExists(resourceName), + testAccCheckAwsDxGatewayAssociationExists(resourceName, &ga, &gap), resource.TestCheckResourceAttrPair(resourceName, "dx_gateway_id", resourceNameDxGw, "id"), resource.TestCheckResourceAttrPair(resourceName, "associated_gateway_id", resourceNameVgw, "id"), resource.TestCheckResourceAttrSet(resourceName, "dx_gateway_association_id"), @@ -472,6 +488,42 @@ func TestAccAwsDxGatewayAssociation_allowedPrefixesVpnGatewayCrossAccount(t *tes }) } +func TestAccAwsDxGatewayAssociation_recreateProposal(t *testing.T) { + var providers []*schema.Provider + resourceName := "aws_dx_gateway_association.test" + rName := fmt.Sprintf("terraform-testacc-dxgwassoc-%d", acctest.RandInt()) + rBgpAsn := acctest.RandIntRange(64512, 65534) + var ga1, ga2 directconnect.GatewayAssociation + var gap1, gap2 directconnect.GatewayAssociationProposal + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { + testAccPreCheck(t) + testAccAlternateAccountPreCheck(t) + }, + ProviderFactories: testAccProviderFactoriesAlternate(&providers), + CheckDestroy: testAccCheckAwsDxGatewayAssociationDestroy, + Steps: []resource.TestStep{ + { + Config: testAccDxGatewayAssociationConfig_basicVpnGatewayCrossAccount(rName, rBgpAsn), + Check: resource.ComposeTestCheckFunc( + testAccCheckAwsDxGatewayAssociationExists(resourceName, &ga1, &gap1), + testAccCheckAwsDxGatewayAssociationProposalDisappears(&gap1), + ), + ExpectNonEmptyPlan: true, + }, + { + Config: testAccDxGatewayAssociationConfig_basicVpnGatewayCrossAccount(rName, rBgpAsn), + Check: resource.ComposeTestCheckFunc( + testAccCheckAwsDxGatewayAssociationExists(resourceName, &ga2, &gap2), + testAccCheckAwsDxGatewayAssociationSameAssociation(&ga1, &ga2), + testAccCheckAwsDxGatewayAssociationDifferentProposal(&gap1, &gap2), + ), + }, + }, + }) +} + func testAccCheckAwsDxGatewayAssociationDestroy(s *terraform.State) error { conn := testAccProvider.Meta().(*AWSClient).dxconn @@ -496,7 +548,7 @@ func testAccCheckAwsDxGatewayAssociationDestroy(s *terraform.State) error { return nil } -func testAccCheckAwsDxGatewayAssociationExists(name string) resource.TestCheckFunc { +func testAccCheckAwsDxGatewayAssociationExists(name string, ga *directconnect.GatewayAssociation, gap *directconnect.GatewayAssociationProposal) resource.TestCheckFunc { return func(s *terraform.State) error { rs, ok := s.RootModule().Resources[name] if !ok { @@ -506,6 +558,45 @@ func testAccCheckAwsDxGatewayAssociationExists(name string) resource.TestCheckFu return fmt.Errorf("No ID is set") } + conn := testAccProvider.Meta().(*AWSClient).dxconn + resp, err := conn.DescribeDirectConnectGatewayAssociations(&directconnect.DescribeDirectConnectGatewayAssociationsInput{ + AssociationId: aws.String(rs.Primary.Attributes["dx_gateway_association_id"]), + }) + if err != nil { + return err + } + + *ga = *resp.DirectConnectGatewayAssociations[0] + + if proposalId := rs.Primary.Attributes["proposal_id"]; proposalId != "" && gap != nil { + v, err := describeDirectConnectGatewayAssociationProposal(conn, proposalId) + if err != nil { + return err + } + + *gap = *v + } + + return nil + } +} + +func testAccCheckAwsDxGatewayAssociationSameAssociation(ga1, ga2 *directconnect.GatewayAssociation) resource.TestCheckFunc { + return func(s *terraform.State) error { + if aws.StringValue(ga1.AssociationId) != aws.StringValue(ga2.AssociationId) { + return fmt.Errorf("Association IDs differ") + } + + return nil + } +} + +func testAccCheckAwsDxGatewayAssociationDifferentProposal(gap1, gap2 *directconnect.GatewayAssociationProposal) resource.TestCheckFunc { + return func(s *terraform.State) error { + if aws.StringValue(gap1.ProposalId) == aws.StringValue(gap2.ProposalId) { + return fmt.Errorf("Proposals IDs are equal") + } + return nil } } From 81dd3183eff2b9a36350690be11c940e7f8c2213 Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Fri, 12 Feb 2021 12:12:15 -0500 Subject: [PATCH 4/7] r/aws_dx_gateway_association: Modernize acceptance tests. --- ...esource_aws_dx_gateway_association_test.go | 174 ++++++++++-------- aws/resource_aws_dx_gateway_test.go | 5 +- 2 files changed, 96 insertions(+), 83 deletions(-) diff --git a/aws/resource_aws_dx_gateway_association_test.go b/aws/resource_aws_dx_gateway_association_test.go index 701a1b5d987..86fc43f51f6 100644 --- a/aws/resource_aws_dx_gateway_association_test.go +++ b/aws/resource_aws_dx_gateway_association_test.go @@ -185,7 +185,7 @@ func testSweepDirectConnectGatewayAssociations(region string) error { // V0 state upgrade testing must be done via acceptance testing due to API call func TestAccAwsDxGatewayAssociation_V0StateUpgrade(t *testing.T) { resourceName := "aws_dx_gateway_association.test" - rName := fmt.Sprintf("terraform-testacc-dxgwassoc-%d", acctest.RandInt()) + rName := acctest.RandomWithPrefix("tf-acc-test") rBgpAsn := acctest.RandIntRange(64512, 65534) var ga directconnect.GatewayAssociation var gap directconnect.GatewayAssociationProposal @@ -210,7 +210,7 @@ func TestAccAwsDxGatewayAssociation_basicVpnGatewaySingleAccount(t *testing.T) { resourceName := "aws_dx_gateway_association.test" resourceNameDxGw := "aws_dx_gateway.test" resourceNameVgw := "aws_vpn_gateway.test" - rName := fmt.Sprintf("terraform-testacc-dxgwassoc-%d", acctest.RandInt()) + rName := acctest.RandomWithPrefix("tf-acc-test") rBgpAsn := acctest.RandIntRange(64512, 65534) var ga directconnect.GatewayAssociation var gap directconnect.GatewayAssociationProposal @@ -235,15 +235,8 @@ func TestAccAwsDxGatewayAssociation_basicVpnGatewaySingleAccount(t *testing.T) { ), }, { - ResourceName: resourceName, - ImportStateIdFunc: func(s *terraform.State) (string, error) { - rs, ok := s.RootModule().Resources[resourceName] - if !ok { - return "", fmt.Errorf("Not Found: %s", resourceName) - } - - return fmt.Sprintf("%s/%s", rs.Primary.Attributes["dx_gateway_id"], rs.Primary.Attributes["associated_gateway_id"]), nil - }, + ResourceName: resourceName, + ImportStateIdFunc: testAccAwsDxGatewayAssociationImportStateIdFunc(resourceName), ImportState: true, ImportStateVerify: true, }, @@ -256,7 +249,7 @@ func TestAccAwsDxGatewayAssociation_basicVpnGatewayCrossAccount(t *testing.T) { resourceName := "aws_dx_gateway_association.test" resourceNameDxGw := "aws_dx_gateway.test" resourceNameVgw := "aws_vpn_gateway.test" - rName := fmt.Sprintf("terraform-testacc-dxgwassoc-%d", acctest.RandInt()) + rName := acctest.RandomWithPrefix("tf-acc-test") rBgpAsn := acctest.RandIntRange(64512, 65534) var ga directconnect.GatewayAssociation var gap directconnect.GatewayAssociationProposal @@ -292,7 +285,7 @@ func TestAccAwsDxGatewayAssociation_basicTransitGatewaySingleAccount(t *testing. resourceName := "aws_dx_gateway_association.test" resourceNameDxGw := "aws_dx_gateway.test" resourceNameTgw := "aws_ec2_transit_gateway.test" - rName := fmt.Sprintf("terraform-testacc-dxgwassoc-%d", acctest.RandInt()) + rName := acctest.RandomWithPrefix("tf-acc-test") rBgpAsn := acctest.RandIntRange(64512, 65534) var ga directconnect.GatewayAssociation var gap directconnect.GatewayAssociationProposal @@ -318,15 +311,8 @@ func TestAccAwsDxGatewayAssociation_basicTransitGatewaySingleAccount(t *testing. ), }, { - ResourceName: resourceName, - ImportStateIdFunc: func(s *terraform.State) (string, error) { - rs, ok := s.RootModule().Resources[resourceName] - if !ok { - return "", fmt.Errorf("Not Found: %s", resourceName) - } - - return fmt.Sprintf("%s/%s", rs.Primary.Attributes["dx_gateway_id"], rs.Primary.Attributes["associated_gateway_id"]), nil - }, + ResourceName: resourceName, + ImportStateIdFunc: testAccAwsDxGatewayAssociationImportStateIdFunc(resourceName), ImportState: true, ImportStateVerify: true, }, @@ -339,7 +325,7 @@ func TestAccAwsDxGatewayAssociation_basicTransitGatewayCrossAccount(t *testing.T resourceName := "aws_dx_gateway_association.test" resourceNameDxGw := "aws_dx_gateway.test" resourceNameTgw := "aws_ec2_transit_gateway.test" - rName := fmt.Sprintf("terraform-testacc-dxgwassoc-%d", acctest.RandInt()) + rName := acctest.RandomWithPrefix("tf-acc-test") rBgpAsn := acctest.RandIntRange(64512, 65534) var ga directconnect.GatewayAssociation var gap directconnect.GatewayAssociationProposal @@ -373,10 +359,9 @@ func TestAccAwsDxGatewayAssociation_basicTransitGatewayCrossAccount(t *testing.T } func TestAccAwsDxGatewayAssociation_multiVpnGatewaysSingleAccount(t *testing.T) { - resourceName1 := "aws_dx_gateway_association.test1" - resourceName2 := "aws_dx_gateway_association.test2" - rName1 := fmt.Sprintf("terraform-testacc-dxgwassoc-%d", acctest.RandInt()) - rName2 := fmt.Sprintf("terraform-testacc-dxgwassoc-%d", acctest.RandInt()) + resourceName1 := "aws_dx_gateway_association.test.0" + resourceName2 := "aws_dx_gateway_association.test.1" + rName := acctest.RandomWithPrefix("tf-acc-test") rBgpAsn := acctest.RandIntRange(64512, 65534) var ga directconnect.GatewayAssociation var gap directconnect.GatewayAssociationProposal @@ -387,7 +372,7 @@ func TestAccAwsDxGatewayAssociation_multiVpnGatewaysSingleAccount(t *testing.T) CheckDestroy: testAccCheckAwsDxGatewayAssociationDestroy, Steps: []resource.TestStep{ { - Config: testAccDxGatewayAssociationConfig_multiVpnGatewaysSingleAccount(rName1, rName2, rBgpAsn), + Config: testAccDxGatewayAssociationConfig_multiVpnGatewaysSingleAccount(rName, rBgpAsn), Check: resource.ComposeTestCheckFunc( testAccCheckAwsDxGatewayAssociationExists(resourceName1, &ga, &gap), testAccCheckAwsDxGatewayAssociationExists(resourceName2, &ga, &gap), @@ -399,6 +384,18 @@ func TestAccAwsDxGatewayAssociation_multiVpnGatewaysSingleAccount(t *testing.T) resource.TestCheckTypeSetElemAttr(resourceName2, "allowed_prefixes.*", "10.255.255.32/28"), ), }, + { + ResourceName: resourceName1, + ImportStateIdFunc: testAccAwsDxGatewayAssociationImportStateIdFunc(resourceName1), + ImportState: true, + ImportStateVerify: true, + }, + { + ResourceName: resourceName2, + ImportStateIdFunc: testAccAwsDxGatewayAssociationImportStateIdFunc(resourceName2), + ImportState: true, + ImportStateVerify: true, + }, }, }) } @@ -407,7 +404,7 @@ func TestAccAwsDxGatewayAssociation_allowedPrefixesVpnGatewaySingleAccount(t *te resourceName := "aws_dx_gateway_association.test" resourceNameDxGw := "aws_dx_gateway.test" resourceNameVgw := "aws_vpn_gateway.test" - rName := fmt.Sprintf("terraform-testacc-dxgwassoc-%d", acctest.RandInt()) + rName := acctest.RandomWithPrefix("tf-acc-test") rBgpAsn := acctest.RandIntRange(64512, 65534) var ga directconnect.GatewayAssociation var gap directconnect.GatewayAssociationProposal @@ -429,6 +426,12 @@ func TestAccAwsDxGatewayAssociation_allowedPrefixesVpnGatewaySingleAccount(t *te resource.TestCheckTypeSetElemAttr(resourceName, "allowed_prefixes.*", "10.255.255.8/30"), ), }, + { + ResourceName: resourceName, + ImportStateIdFunc: testAccAwsDxGatewayAssociationImportStateIdFunc(resourceName), + ImportState: true, + ImportStateVerify: true, + }, { Config: testAccDxGatewayAssociationConfig_allowedPrefixesVpnGatewaySingleAccountUpdated(rName, rBgpAsn), Check: resource.ComposeTestCheckFunc( @@ -446,7 +449,7 @@ func TestAccAwsDxGatewayAssociation_allowedPrefixesVpnGatewayCrossAccount(t *tes resourceName := "aws_dx_gateway_association.test" resourceNameDxGw := "aws_dx_gateway.test" resourceNameVgw := "aws_vpn_gateway.test" - rName := fmt.Sprintf("terraform-testacc-dxgwassoc-%d", acctest.RandInt()) + rName := acctest.RandomWithPrefix("tf-acc-test") rBgpAsn := acctest.RandIntRange(64512, 65534) var ga directconnect.GatewayAssociation var gap directconnect.GatewayAssociationProposal @@ -491,7 +494,7 @@ func TestAccAwsDxGatewayAssociation_allowedPrefixesVpnGatewayCrossAccount(t *tes func TestAccAwsDxGatewayAssociation_recreateProposal(t *testing.T) { var providers []*schema.Provider resourceName := "aws_dx_gateway_association.test" - rName := fmt.Sprintf("terraform-testacc-dxgwassoc-%d", acctest.RandInt()) + rName := acctest.RandomWithPrefix("tf-acc-test") rBgpAsn := acctest.RandIntRange(64512, 65534) var ga1, ga2 directconnect.GatewayAssociation var gap1, gap2 directconnect.GatewayAssociationProposal @@ -524,6 +527,17 @@ func TestAccAwsDxGatewayAssociation_recreateProposal(t *testing.T) { }) } +func testAccAwsDxGatewayAssociationImportStateIdFunc(resourceName string) resource.ImportStateIdFunc { + return func(s *terraform.State) (string, error) { + rs, ok := s.RootModule().Resources[resourceName] + if !ok { + return "", fmt.Errorf("Not Found: %s", resourceName) + } + + return fmt.Sprintf("%s/%s", rs.Primary.Attributes["dx_gateway_id"], rs.Primary.Attributes["associated_gateway_id"]), nil + } +} + func testAccCheckAwsDxGatewayAssociationDestroy(s *terraform.State) error { conn := testAccProvider.Meta().(*AWSClient).dxconn @@ -659,7 +673,9 @@ resource "aws_vpn_gateway_attachment" "test" { } func testAccDxGatewayAssociationConfigBase_vpnGatewayCrossAccount(rName string, rBgpAsn int) string { - return testAccAlternateAccountProviderConfig() + fmt.Sprintf(` + return composeConfig( + testAccAlternateAccountProviderConfig(), + fmt.Sprintf(` # Creator data "aws_caller_identity" "creator" {} @@ -689,20 +705,24 @@ resource "aws_dx_gateway" "test" { amazon_side_asn = %[2]d name = %[1]q } -`, rName, rBgpAsn) +`, rName, rBgpAsn)) } func testAccDxGatewayAssociationConfig_basicVpnGatewaySingleAccount(rName string, rBgpAsn int) string { - return testAccDxGatewayAssociationConfigBase_vpnGatewaySingleAccount(rName, rBgpAsn) + ` + return composeConfig( + testAccDxGatewayAssociationConfigBase_vpnGatewaySingleAccount(rName, rBgpAsn), + ` resource "aws_dx_gateway_association" "test" { dx_gateway_id = aws_dx_gateway.test.id associated_gateway_id = aws_vpn_gateway_attachment.test.vpn_gateway_id } -` +`) } func testAccDxGatewayAssociationConfig_basicVpnGatewayCrossAccount(rName string, rBgpAsn int) string { - return testAccDxGatewayAssociationConfigBase_vpnGatewayCrossAccount(rName, rBgpAsn) + ` + return composeConfig( + testAccDxGatewayAssociationConfigBase_vpnGatewayCrossAccount(rName, rBgpAsn), + ` # Creator resource "aws_dx_gateway_association_proposal" "test" { dx_gateway_id = aws_dx_gateway.test.id @@ -718,7 +738,7 @@ resource "aws_dx_gateway_association" "test" { dx_gateway_id = aws_dx_gateway.test.id associated_gateway_owner_account_id = data.aws_caller_identity.creator.account_id } -` +`) } func testAccDxGatewayAssociationConfig_basicTransitGatewaySingleAccount(rName string, rBgpAsn int) string { @@ -747,7 +767,9 @@ resource "aws_dx_gateway_association" "test" { } func testAccDxGatewayAssociationConfig_basicTransitGatewayCrossAccount(rName string, rBgpAsn int) string { - return testAccAlternateAccountProviderConfig() + fmt.Sprintf(` + return composeConfig( + testAccAlternateAccountProviderConfig(), + fmt.Sprintf(` # Creator data "aws_caller_identity" "creator" {} @@ -785,68 +807,54 @@ resource "aws_dx_gateway_association" "test" { dx_gateway_id = aws_dx_gateway.test.id associated_gateway_owner_account_id = data.aws_caller_identity.creator.account_id } -`, rName, rBgpAsn) +`, rName, rBgpAsn)) } -func testAccDxGatewayAssociationConfig_multiVpnGatewaysSingleAccount(rName1, rName2 string, rBgpAsn int) string { +func testAccDxGatewayAssociationConfig_multiVpnGatewaysSingleAccount(rName string, rBgpAsn int) string { return fmt.Sprintf(` resource "aws_dx_gateway" "test" { name = %[1]q - amazon_side_asn = "%[3]d" + amazon_side_asn = "%[2]d" } -resource "aws_vpc" "test1" { - cidr_block = "10.255.255.16/28" +resource "aws_vpc" "test" { + count = 2 - tags = { - Name = %[1]q - } -} + cidr_block = cidrsubnet("10.255.255.16/26", 2, count.index) -resource "aws_vpn_gateway" "test1" { tags = { Name = %[1]q } } -resource "aws_vpn_gateway_attachment" "test1" { - vpc_id = aws_vpc.test1.id - vpn_gateway_id = aws_vpn_gateway.test1.id -} - -resource "aws_dx_gateway_association" "test1" { - dx_gateway_id = aws_dx_gateway.test.id - associated_gateway_id = aws_vpn_gateway_attachment.test1.vpn_gateway_id -} - -resource "aws_vpc" "test2" { - cidr_block = "10.255.255.32/28" +resource "aws_vpn_gateway" "test" { + count = 2 tags = { - Name = %[2]q + Name = %[1]q } } -resource "aws_vpn_gateway" "test2" { - tags = { - Name = %[2]q - } -} +resource "aws_vpn_gateway_attachment" "test" { + count = 2 -resource "aws_vpn_gateway_attachment" "test2" { - vpc_id = aws_vpc.test2.id - vpn_gateway_id = aws_vpn_gateway.test2.id + vpc_id = aws_vpc.test[count.index].id + vpn_gateway_id = aws_vpn_gateway.test[count.index].id } -resource "aws_dx_gateway_association" "test2" { +resource "aws_dx_gateway_association" "test" { + count = 2 + dx_gateway_id = aws_dx_gateway.test.id - associated_gateway_id = aws_vpn_gateway_attachment.test2.vpn_gateway_id + associated_gateway_id = aws_vpn_gateway_attachment.test[count.index].vpn_gateway_id } -`, rName1, rName2, rBgpAsn) +`, rName, rBgpAsn) } func testAccDxGatewayAssociationConfig_allowedPrefixesVpnGatewaySingleAccount(rName string, rBgpAsn int) string { - return testAccDxGatewayAssociationConfigBase_vpnGatewaySingleAccount(rName, rBgpAsn) + ` + return composeConfig( + testAccDxGatewayAssociationConfigBase_vpnGatewaySingleAccount(rName, rBgpAsn), + ` resource "aws_dx_gateway_association" "test" { dx_gateway_id = aws_dx_gateway.test.id associated_gateway_id = aws_vpn_gateway_attachment.test.vpn_gateway_id @@ -856,11 +864,13 @@ resource "aws_dx_gateway_association" "test" { "10.255.255.8/30", ] } -` +`) } func testAccDxGatewayAssociationConfig_allowedPrefixesVpnGatewaySingleAccountUpdated(rName string, rBgpAsn int) string { - return testAccDxGatewayAssociationConfigBase_vpnGatewaySingleAccount(rName, rBgpAsn) + ` + return composeConfig( + testAccDxGatewayAssociationConfigBase_vpnGatewaySingleAccount(rName, rBgpAsn), + ` resource "aws_dx_gateway_association" "test" { dx_gateway_id = aws_dx_gateway.test.id associated_gateway_id = aws_vpn_gateway_attachment.test.vpn_gateway_id @@ -869,11 +879,13 @@ resource "aws_dx_gateway_association" "test" { "10.255.255.8/29", ] } -` +`) } func testAccDxGatewayAssociationConfig_allowedPrefixesVpnGatewayCrossAccount(rName string, rBgpAsn int) string { - return testAccDxGatewayAssociationConfigBase_vpnGatewayCrossAccount(rName, rBgpAsn) + ` + return composeConfig( + testAccDxGatewayAssociationConfigBase_vpnGatewayCrossAccount(rName, rBgpAsn), + ` # Creator resource "aws_dx_gateway_association_proposal" "test" { dx_gateway_id = aws_dx_gateway.test.id @@ -898,11 +910,13 @@ resource "aws_dx_gateway_association" "test" { "10.255.255.8/29", ] } -` +`) } func testAccDxGatewayAssociationConfig_allowedPrefixesVpnGatewayCrossAccountUpdated(rName string, rBgpAsn int) string { - return testAccDxGatewayAssociationConfigBase_vpnGatewayCrossAccount(rName, rBgpAsn) + ` + return composeConfig( + testAccDxGatewayAssociationConfigBase_vpnGatewayCrossAccount(rName, rBgpAsn), + ` # Creator resource "aws_dx_gateway_association_proposal" "test" { dx_gateway_id = aws_dx_gateway.test.id @@ -923,5 +937,5 @@ resource "aws_dx_gateway_association" "test" { "10.255.255.8/30", ] } -` +`) } diff --git a/aws/resource_aws_dx_gateway_test.go b/aws/resource_aws_dx_gateway_test.go index 61e4c8ce934..54462b8e1f9 100644 --- a/aws/resource_aws_dx_gateway_test.go +++ b/aws/resource_aws_dx_gateway_test.go @@ -139,8 +139,7 @@ func TestAccAwsDxGateway_basic(t *testing.T) { } func TestAccAwsDxGateway_complex(t *testing.T) { - rName1 := fmt.Sprintf("terraform-testacc-dxgwassoc-%d", acctest.RandInt()) - rName2 := fmt.Sprintf("terraform-testacc-dxgwassoc-%d", acctest.RandInt()) + rName := acctest.RandomWithPrefix("tf-acc-test") rBgpAsn := acctest.RandIntRange(64512, 65534) resourceName := "aws_dx_gateway.test" @@ -150,7 +149,7 @@ func TestAccAwsDxGateway_complex(t *testing.T) { CheckDestroy: testAccCheckAwsDxGatewayDestroy, Steps: []resource.TestStep{ { - Config: testAccDxGatewayAssociationConfig_multiVpnGatewaysSingleAccount(rName1, rName2, rBgpAsn), + Config: testAccDxGatewayAssociationConfig_multiVpnGatewaysSingleAccount(rName, rBgpAsn), Check: resource.ComposeTestCheckFunc( testAccCheckAwsDxGatewayExists(resourceName), testAccCheckResourceAttrAccountID(resourceName, "owner_account_id"), From 6532e2e95170b9a0fd45c7fbb9ce96d3a41c9c8a Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Fri, 12 Feb 2021 14:28:45 -0500 Subject: [PATCH 5/7] r/aws_ec2_transit_gateway: Increase deletion IncorrectState retry to 5 minutes. --- aws/resource_aws_ec2_transit_gateway.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/aws/resource_aws_ec2_transit_gateway.go b/aws/resource_aws_ec2_transit_gateway.go index 77fc51c2cc7..681a7d558bd 100644 --- a/aws/resource_aws_ec2_transit_gateway.go +++ b/aws/resource_aws_ec2_transit_gateway.go @@ -266,7 +266,7 @@ func resourceAwsEc2TransitGatewayDelete(d *schema.ResourceData, meta interface{} } log.Printf("[DEBUG] Deleting EC2 Transit Gateway (%s): %s", d.Id(), input) - err := resource.Retry(2*time.Minute, func() *resource.RetryError { + err := resource.Retry(5*time.Minute, func() *resource.RetryError { _, err := conn.DeleteTransitGateway(input) if isAWSErr(err, "IncorrectState", "has non-deleted Transit Gateway Attachments") { From 1877fc2b2b2e7314e5db7e37292cd99864e40066 Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Sat, 13 Feb 2021 16:44:58 -0500 Subject: [PATCH 6/7] r/aws_dx_gateway_association: Fix 'TestAccAwsDxGatewayAssociation_multiVpnGatewaysSingleAccount'. --- ...resource_aws_dx_gateway_association_test.go | 18 +++--------------- 1 file changed, 3 insertions(+), 15 deletions(-) diff --git a/aws/resource_aws_dx_gateway_association_test.go b/aws/resource_aws_dx_gateway_association_test.go index 86fc43f51f6..ee7f28d5e33 100644 --- a/aws/resource_aws_dx_gateway_association_test.go +++ b/aws/resource_aws_dx_gateway_association_test.go @@ -378,24 +378,12 @@ func TestAccAwsDxGatewayAssociation_multiVpnGatewaysSingleAccount(t *testing.T) testAccCheckAwsDxGatewayAssociationExists(resourceName2, &ga, &gap), resource.TestCheckResourceAttrSet(resourceName1, "dx_gateway_association_id"), resource.TestCheckResourceAttr(resourceName1, "allowed_prefixes.#", "1"), - resource.TestCheckTypeSetElemAttr(resourceName1, "allowed_prefixes.*", "10.255.255.16/28"), + resource.TestCheckTypeSetElemAttr(resourceName1, "allowed_prefixes.*", "10.255.255.0/28"), resource.TestCheckResourceAttrSet(resourceName2, "dx_gateway_association_id"), resource.TestCheckResourceAttr(resourceName2, "allowed_prefixes.#", "1"), - resource.TestCheckTypeSetElemAttr(resourceName2, "allowed_prefixes.*", "10.255.255.32/28"), + resource.TestCheckTypeSetElemAttr(resourceName2, "allowed_prefixes.*", "10.255.255.16/28"), ), }, - { - ResourceName: resourceName1, - ImportStateIdFunc: testAccAwsDxGatewayAssociationImportStateIdFunc(resourceName1), - ImportState: true, - ImportStateVerify: true, - }, - { - ResourceName: resourceName2, - ImportStateIdFunc: testAccAwsDxGatewayAssociationImportStateIdFunc(resourceName2), - ImportState: true, - ImportStateVerify: true, - }, }, }) } @@ -820,7 +808,7 @@ resource "aws_dx_gateway" "test" { resource "aws_vpc" "test" { count = 2 - cidr_block = cidrsubnet("10.255.255.16/26", 2, count.index) + cidr_block = cidrsubnet("10.255.255.0/26", 2, count.index) tags = { Name = %[1]q From 20ae7b5f4720e0bbf7dc5a2b228497395ac7f9fd Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Sat, 13 Feb 2021 17:15:04 -0500 Subject: [PATCH 7/7] Add CHANGELOG entry. --- .changelog/12482.txt | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 .changelog/12482.txt diff --git a/.changelog/12482.txt b/.changelog/12482.txt new file mode 100644 index 00000000000..f8a17f14080 --- /dev/null +++ b/.changelog/12482.txt @@ -0,0 +1,3 @@ +```release-note:enhancement +resource/aws_dx_gateway_association: Changes to `proposal_id` do not force resource recreation +```