stacks: make error message more precise

pull/34737/head
Daniel Schmidt 2 years ago
parent 8da61e2d20
commit 2a0962328d
No known key found for this signature in database
GPG Key ID: 377C3A4D62FBBBE2

@ -110,7 +110,7 @@ func (c *Component) CheckForEachValue(ctx context.Context, phase EvalPhase) (cty
switch {
case cfg.ForEach != nil:
result, moreDiags := evaluateForEachExpr(ctx, cfg.ForEach, phase, c.Stack(ctx))
result, moreDiags := evaluateForEachExpr(ctx, cfg.ForEach, phase, c.Stack(ctx), "component")
diags = diags.Append(moreDiags)
if diags.HasErrors() {
return cty.DynamicVal, diags

@ -182,7 +182,7 @@ func TestComponentCheckInstances(t *testing.T) {
gotVal, diags := component.CheckForEachValue(ctx, InspectPhase)
assertMatchingDiag(t, diags, func(diag tfdiags.Diagnostic) bool {
return (diag.Severity() == tfdiags.Error &&
diag.Description().Detail == "The for_each expression must produce either a map of any type or a set of strings. The keys of the map or the set elements will serve as unique identifiers for multiple instances of this resource / data source / provider / component.")
diag.Description().Detail == "The for_each expression must produce either a map of any type or a set of strings. The keys of the map or the set elements will serve as unique identifiers for multiple instances of this component.")
})
wantVal := cty.DynamicVal // placeholder for invalid result
if !wantVal.RawEquals(gotVal) {

@ -21,7 +21,7 @@ import (
// The caller might still need to do some further validation or post-processing
// of the result for concerns that are specific to a particular phase or
// evaluation context.
func evaluateForEachExpr(ctx context.Context, expr hcl.Expression, phase EvalPhase, scope ExpressionScope) (ExprResultValue, tfdiags.Diagnostics) {
func evaluateForEachExpr(ctx context.Context, expr hcl.Expression, phase EvalPhase, scope ExpressionScope, callerDiagName string) (ExprResultValue, tfdiags.Diagnostics) {
var diags tfdiags.Diagnostics
result, moreDiags := EvalExprAndEvalContext(
ctx, expr, phase, scope,
@ -37,7 +37,7 @@ func evaluateForEachExpr(ctx context.Context, expr hcl.Expression, phase EvalPha
ty := result.Value.Type()
const invalidForEachSummary = "Invalid for_each value"
const invalidForEachDetail = "The for_each expression must produce either a map of any type or a set of strings. The keys of the map or the set elements will serve as unique identifiers for multiple instances of this resource / data source / provider / component."
invalidForEachDetail := fmt.Sprintf("The for_each expression must produce either a map of any type or a set of strings. The keys of the map or the set elements will serve as unique identifiers for multiple instances of this %s.", callerDiagName)
const sensitiveForEachDetail = "Sensitive values, or values derived from sensitive values, cannot be used as for_each arguments. If used, the sensitive value could be exposed as a resource instance key."
switch {
case result.Value.HasMark(marks.Sensitive):

@ -144,7 +144,7 @@ func TestEvaluateForEachExpr(t *testing.T) {
for name, test := range tests {
t.Run(name, func(t *testing.T) {
gotResult, diags := evaluateForEachExpr(ctx, test.Expr, PlanPhase, scope)
gotResult, diags := evaluateForEachExpr(ctx, test.Expr, PlanPhase, scope, "test")
got := gotResult.Value
if test.WantErr != "" {

@ -118,7 +118,7 @@ func (p *Provider) CheckForEachValue(ctx context.Context, phase EvalPhase) (cty.
switch {
case cfg.ForEach != nil:
result, moreDiags := evaluateForEachExpr(ctx, cfg.ForEach, phase, p.Stack(ctx))
result, moreDiags := evaluateForEachExpr(ctx, cfg.ForEach, phase, p.Stack(ctx), "provider")
diags = diags.Append(moreDiags)
if diags.HasErrors() {
return cty.DynamicVal, diags

@ -171,7 +171,7 @@ func TestProviderCheckInstances(t *testing.T) {
gotVal, diags := provider.CheckForEachValue(ctx, InspectPhase)
assertMatchingDiag(t, diags, func(diag tfdiags.Diagnostic) bool {
return (diag.Severity() == tfdiags.Error &&
diag.Description().Detail == "The for_each expression must produce either a map of any type or a set of strings. The keys of the map or the set elements will serve as unique identifiers for multiple instances of this resource / data source / provider / component.")
diag.Description().Detail == "The for_each expression must produce either a map of any type or a set of strings. The keys of the map or the set elements will serve as unique identifiers for multiple instances of this provider.")
})
wantVal := cty.DynamicVal // placeholder for invalid result
if !wantVal.RawEquals(gotVal) {

@ -103,7 +103,7 @@ func (c *StackCall) CheckForEachValue(ctx context.Context, phase EvalPhase) (cty
switch {
case cfg.ForEach != nil:
result, moreDiags := evaluateForEachExpr(ctx, cfg.ForEach, phase, c.Caller(ctx))
result, moreDiags := evaluateForEachExpr(ctx, cfg.ForEach, phase, c.Caller(ctx), "stack")
diags = diags.Append(moreDiags)
if diags.HasErrors() {
return cty.DynamicVal, diags

@ -115,7 +115,7 @@ func (s *StackCallConfig) validateForEachValueInner(ctx context.Context) (cty.Va
return cty.NilVal, diags
}
result, moreDiags := evaluateForEachExpr(ctx, s.config.ForEach, ValidatePhase, s.CallerConfig(ctx))
result, moreDiags := evaluateForEachExpr(ctx, s.config.ForEach, ValidatePhase, s.CallerConfig(ctx), "stack")
diags = diags.Append(moreDiags)
return result.Value, diags
}

@ -177,7 +177,7 @@ func TestStackCallCheckInstances(t *testing.T) {
gotVal, diags := call.CheckForEachValue(ctx, InspectPhase)
assertMatchingDiag(t, diags, func(diag tfdiags.Diagnostic) bool {
return (diag.Severity() == tfdiags.Error &&
diag.Description().Detail == "The for_each expression must produce either a map of any type or a set of strings. The keys of the map or the set elements will serve as unique identifiers for multiple instances of this resource / data source / provider / component.")
diag.Description().Detail == "The for_each expression must produce either a map of any type or a set of strings. The keys of the map or the set elements will serve as unique identifiers for multiple instances of this stack.")
})
wantVal := cty.DynamicVal // placeholder for invalid result
if !wantVal.RawEquals(gotVal) {

Loading…
Cancel
Save