Skip to content

Commit

Permalink
fix panic when import id is empty string
Browse files Browse the repository at this point in the history
We can not detect the empty ID earlier (e.g. during config parsing) since it's an HCL expression to be evaluated. Since go uses the default value of "" for strings which is the value we try to guard against we can not properly differciate between an invalid configuration and the user not wanting to import the resource.

We could change the type of the importID field to *string to make this differenciation, but that seemed to invasive for the scope of this bug.

Fixes #33505
  • Loading branch information
DanielMSchmidt committed Feb 6, 2024
1 parent 64cd0da commit 14a8009
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
11 changes: 6 additions & 5 deletions internal/terraform/node_resource_abstract_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -752,15 +752,16 @@ func (n *NodeAbstractResourceInstance) plan(

// If we're importing and generating config, generate it now.
if n.Config == nil {
// This shouldn't happen. A node that isn't generating config should
// have embedded config, and the rest of Terraform should enforce this.
// If, however, we didn't do things correctly the next line will panic,
// so let's not do that and return an error message with more context.
// This can happen if the importID of a node is set to "".
// A node that isn't generating config should have embedded config,
// and the rest of Terraform should enforce this.
// If, however, the user has an import block with id="" we might
// end up here, #so let's return an error message with more context.

diags = diags.Append(tfdiags.Sourceless(
tfdiags.Error,
"Resource has no configuration",
fmt.Sprintf("Terraform attempted to process a resource at %s that has no configuration. This is a bug in Terraform; please report it!", n.Addr.String())))
fmt.Sprintf("Terraform attempted to process a resource at %s that has no configuration. This is can happen if you are using an import block and have the id to import from set to an empty string, in this case please provide a valid id to import from. If you are not doing that, then this is a bug in Terraform; please report it!", n.Addr.String())))
return nil, nil, keyData, diags
}

Expand Down
2 changes: 1 addition & 1 deletion internal/terraform/node_resource_plan_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ func (n *NodePlannableResourceInstance) managedResourceExecute(ctx EvalContext)
// If we are importing and generating a configuration, we need to
// ensure the change is written out so the configuration can be
// captured.
if len(n.generateConfigPath) > 0 {
if len(n.generateConfigPath) > 0 && len(n.generatedConfigHCL) > 0 && instanceRefreshState != nil {
// Update our return plan
change := &plans.ResourceInstanceChange{
Addr: n.Addr,
Expand Down

0 comments on commit 14a8009

Please sign in to comment.