Skip to content
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 panic when import id is empty string #34625

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading