Fix panic when applying variable set to workspaces fails #628
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
This PR fixes a panic that occurs when setting the
workspace_ids
attribute to[""]
, thus causingtfeClient.VariableSets.UpdateWorkspaces()
to return an error. That error message in turn would reference anil
variable:terraform-provider-tfe/tfe/resource_tfe_variable_set.go
Lines 90 to 94 in 2a1b060
See the issue? If that method returns an error,
variableSet.ID
in L93 will cause a panic.Furthermore, I noticed some weird behavior where the variable set would be created successfully, applying to a workspace would fail, and Terraform would then try to recreate the resource since it thinks it doesn't exist (both operations occur during the creation phase). I updated the provider to write to state when the variable set is created successfully before we apply the variable set to a workspace. What this will do is if applying the variable set fails, Terraform will taint the resource to be recreated instead of attempting to create a new variable set!
Testing plan
To Reproduce
terraform apply
this TF configuration:terraform apply
again! You will get a bad request error stating the variable set name is already taken. This is because the variable set was already created but Terraform has no record of it (you can view the statefile to double check).The Fix
go install .
, and ensure you have an CLI overrides file pointinghashicorp/tfe
to your locally built provider.TF_CLI_CONFIG_FILE=/path/to/your/overrides.tfrc terraform apply
Insufficient identification for workspace assignment. no workspace id provided
. Perfect! No more panic and we get an error message letting us know we've provided an invalid or empty ID.terraform apply
again, you'll notice Terraform has now tainted the variable set resource. Don't apply the plan just yet!terraform apply
. The resource will still be marked for replacement instead of creating a new variable set. Type yes to confirm, and the apply should succeed!