Skip to content

Commit

Permalink
Merge pull request #34625 from hashicorp/throw-better-error-if-import…
Browse files Browse the repository at this point in the history
…-id-is-empty-string

fix panic when import id is empty string
  • Loading branch information
DanielMSchmidt authored Feb 7, 2024
2 parents 34ccf28 + 2b7c7b2 commit 5b29b92
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
24 changes: 24 additions & 0 deletions internal/terraform/context_plan_import_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,30 @@ func TestContext2Plan_importIdInvalidNull(t *testing.T) {
}
}

func TestContext2Plan_importIdInvalidEmptyString(t *testing.T) {
p := testProvider("test")
m := testModule(t, "import-id-invalid-null")
ctx := testContext2(t, &ContextOpts{
Providers: map[addrs.Provider]providers.Factory{
addrs.NewDefaultProvider("test"): testProviderFuncFixed(p),
},
})

_, diags := ctx.Plan(m, states.NewState(), &PlanOpts{
SetVariables: InputValues{
"the_id": &InputValue{
Value: cty.StringVal(""),
},
},
})
if !diags.HasErrors() {
t.Fatal("succeeded; want errors")
}
if got, want := diags.Err().Error(), "The import ID value evaluates to an empty string, please provide a non-empty value."; !strings.Contains(got, want) {
t.Fatalf("wrong error:\ngot: %s\nwant: message containing %q", got, want)
}
}

func TestContext2Plan_importIdInvalidUnknown(t *testing.T) {
p := testProvider("test")
m := testModule(t, "import-id-invalid-unknown")
Expand Down
9 changes: 9 additions & 0 deletions internal/terraform/eval_import.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,15 @@ func evaluateImportIdExpression(expr hcl.Expression, ctx EvalContext, keyData in
})
}

if importId == "" {
return "", diags.Append(&hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: "Invalid import id argument",
Detail: "The import ID value evaluates to an empty string, please provide a non-empty value.",
Subject: expr.Range().Ptr(),
})
}

return importId, diags
}

Expand Down

0 comments on commit 5b29b92

Please sign in to comment.