Merge pull request #34625 from hashicorp/throw-better-error-if-import-id-is-empty-string

fix panic when import id is empty string
pull/34635/head
Daniel Schmidt 2 years ago committed by GitHub
commit 5b29b92abe
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -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")

@ -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
}

Loading…
Cancel
Save