-
Notifications
You must be signed in to change notification settings - Fork 47
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
fix: avoid inconsistent state errors for vlans #642
Changes from all commits
7718847
9004801
db9e3c0
ec96d66
bc903ac
7ffb3a3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,6 +29,19 @@ resource "equinix_metal_vlan" "foovlan" { | |
`, projSuffix, metro, desc) | ||
} | ||
|
||
func testAccCheckMetalVlanConfig_NoDescription(projSuffix, metro string) string { | ||
return fmt.Sprintf(` | ||
resource "equinix_metal_project" "foobar" { | ||
name = "tfacc-vlan-%s" | ||
} | ||
|
||
resource "equinix_metal_vlan" "foovlan" { | ||
project_id = equinix_metal_project.foobar.id | ||
metro = "%s" | ||
} | ||
`, projSuffix, metro) | ||
} | ||
|
||
func testAccCheckMetalVlanConfig_facility(projSuffix, facility, desc string) string { | ||
return fmt.Sprintf(` | ||
resource "equinix_metal_project" "foobar" { | ||
|
@@ -43,6 +56,84 @@ resource "equinix_metal_vlan" "foovlan" { | |
} | ||
|
||
func TestAccMetalVlan_metro(t *testing.T) { | ||
var vlan packngo.VirtualNetwork | ||
rs := acctest.RandString(10) | ||
lowerSiliconValley := "sv" | ||
upperDallas := "DA" | ||
|
||
resource.ParallelTest(t, resource.TestCase{ | ||
PreCheck: func() { acceptance.TestAccPreCheckMetal(t) }, | ||
ExternalProviders: acceptance.TestExternalProviders, | ||
ProtoV5ProviderFactories: acceptance.ProtoV5ProviderFactories, | ||
CheckDestroy: testAccMetalVlanCheckDestroyed, | ||
Steps: []resource.TestStep{ | ||
{ | ||
// Create VLAN with metro "sv" (lower-case) | ||
Config: testAccCheckMetalVlanConfig_metro(rs, lowerSiliconValley, "tfacc-vlan"), | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccCheckMetalVlanExists("equinix_metal_vlan.foovlan", &vlan), | ||
resource.TestCheckResourceAttr( | ||
"equinix_metal_vlan.foovlan", "description", "tfacc-vlan"), | ||
resource.TestCheckResourceAttr( | ||
"equinix_metal_vlan.foovlan", "metro", lowerSiliconValley), | ||
), | ||
}, | ||
{ | ||
// Confirm no changes if metro is changed to "SV" (upper-case) | ||
Config: testAccCheckMetalVlanConfig_metro(rs, strings.ToUpper(lowerSiliconValley), "tfacc-vlan"), | ||
PlanOnly: true, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This test step confirms that there are no changes to apply after changing the metro from There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Any reason you don't just use upperMetro here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
}, | ||
{ | ||
// Recreate VLAN with metro "DA" (upper-case) | ||
Config: testAccCheckMetalVlanConfig_metro(rs, upperDallas, "tfacc-vlan"), | ||
ConfigPlanChecks: resource.ConfigPlanChecks{ | ||
PreApply: []plancheck.PlanCheck{ | ||
plancheck.ExpectResourceAction("equinix_metal_vlan.foovlan", plancheck.ResourceActionDestroyBeforeCreate), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This plan check confirms that this test step will destroy and recreate the VLAN because there was a material change to the metro from Silicon Valley to Dallas |
||
}, | ||
}, | ||
Check: resource.ComposeTestCheckFunc( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a check we can add to ensure that the resource was effectively recreated when the metro changed? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The model is written to throw an error if the metro received from the API is different from the one in state (other than casing): https://github.com/equinix/terraform-provider-equinix/pull/642/files#diff-8f6d46b17b852aeb7d85b49d7e72ea5359e1345953070c31a27b9264aa683f42R93-R96 If we weren't recreating the VLAN, we should hit that error and fail the test. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thinking about this more: we should probably update the test to validate that the VLAN isn't recreated when we change the metro casing There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Test has been updated to check that the VLAN ID stays the same when the metro casing changes; I validated by temporarily removing the CaseInsensitiveString plan modifier from the resource (so that casing changes are seen as requiring an apply) and confirming that the test fails. |
||
testAccCheckMetalVlanExists("equinix_metal_vlan.foovlan", &vlan), | ||
resource.TestCheckResourceAttr( | ||
"equinix_metal_vlan.foovlan", "description", "tfacc-vlan"), | ||
resource.TestCheckResourceAttr( | ||
"equinix_metal_vlan.foovlan", "metro", upperDallas), | ||
), | ||
}, | ||
{ | ||
// Confirm no changes if metro is changed to "da" (lower-case) | ||
Config: testAccCheckMetalVlanConfig_metro(rs, strings.ToLower(upperDallas), "tfacc-vlan"), | ||
PlanOnly: true, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This test step confirms that there are no changes to apply after changing the metro from There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And here you could use metro, right? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I can see how this test is hard to reason about; in the latest commit I tried changing the names of the |
||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func TestAccMetalVlan_NoDescription(t *testing.T) { | ||
var vlan packngo.VirtualNetwork | ||
rs := acctest.RandString(10) | ||
metro := "sv" | ||
|
||
resource.ParallelTest(t, resource.TestCase{ | ||
PreCheck: func() { acceptance.TestAccPreCheckMetal(t) }, | ||
ExternalProviders: acceptance.TestExternalProviders, | ||
ProtoV5ProviderFactories: acceptance.ProtoV5ProviderFactories, | ||
CheckDestroy: testAccMetalVlanCheckDestroyed, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccCheckMetalVlanConfig_NoDescription(rs, metro), | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccCheckMetalVlanExists("equinix_metal_vlan.foovlan", &vlan), | ||
resource.TestCheckNoResourceAttr( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This test checks that we don't get "inconsistent state" errors when description is omitted in the initial config |
||
"equinix_metal_vlan.foovlan", "description"), | ||
resource.TestCheckResourceAttr( | ||
"equinix_metal_vlan.foovlan", "metro", metro), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func TestAccMetalVlan_RemoveDescription(t *testing.T) { | ||
var vlan packngo.VirtualNetwork | ||
rs := acctest.RandString(10) | ||
metro := "sv" | ||
|
@@ -63,6 +154,16 @@ func TestAccMetalVlan_metro(t *testing.T) { | |
"equinix_metal_vlan.foovlan", "metro", metro), | ||
), | ||
}, | ||
{ | ||
Config: testAccCheckMetalVlanConfig_NoDescription(rs, metro), | ||
Check: resource.ComposeTestCheckFunc( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This test checks that we don't get "inconsistent state" errors when description is included in the initial config and then deleted later. |
||
testAccCheckMetalVlanExists("equinix_metal_vlan.foovlan", &vlan), | ||
resource.TestCheckNoResourceAttr( | ||
"equinix_metal_vlan.foovlan", "description"), | ||
resource.TestCheckResourceAttr( | ||
"equinix_metal_vlan.foovlan", "metro", metro), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
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.
Does this effectively prevent TF from recognizing an API cleared description?
Terraform initially set and continues to want: "foo"
API was updated to: ""
Terraform ignores the diff?
This same code is fine within the datasource parse.
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.
VLAN doesn't have an update endpoint according to the spec & both SDKs (all attributes of VLAN resources are marked as requiring replacement, and have been for quite a while). I added a test, though, to confirm that the description is removed; if we ever add support for updating VLAN attributes, that will help confirm that description continues to work as expected.