add diags to data eval

This commit is contained in:
James Bardin 2020-10-28 11:57:45 -04:00
parent b42aad5856
commit 64491df856
4 changed files with 45 additions and 43 deletions

View File

@ -15,7 +15,7 @@ type evalReadDataApply struct {
evalReadData evalReadData
} }
func (n *evalReadDataApply) Eval(ctx EvalContext) (interface{}, error) { func (n *evalReadDataApply) Eval(ctx EvalContext) tfdiags.Diagnostics {
absAddr := n.Addr.Absolute(ctx.Path()) absAddr := n.Addr.Absolute(ctx.Path())
var diags tfdiags.Diagnostics var diags tfdiags.Diagnostics
@ -26,22 +26,25 @@ func (n *evalReadDataApply) Eval(ctx EvalContext) (interface{}, error) {
} }
if n.ProviderSchema == nil || *n.ProviderSchema == nil { if n.ProviderSchema == nil || *n.ProviderSchema == nil {
return nil, fmt.Errorf("provider schema not available for %s", n.Addr) diags = diags.Append(fmt.Errorf("provider schema not available for %s", n.Addr))
return diags
} }
if planned != nil && planned.Action != plans.Read { if planned != nil && planned.Action != plans.Read {
// If any other action gets in here then that's always a bug; this // If any other action gets in here then that's always a bug; this
// EvalNode only deals with reading. // EvalNode only deals with reading.
return nil, fmt.Errorf( diags = diags.Append(fmt.Errorf(
"invalid action %s for %s: only Read is supported (this is a bug in Terraform; please report it!)", "invalid action %s for %s: only Read is supported (this is a bug in Terraform; please report it!)",
planned.Action, absAddr, planned.Action, absAddr,
) ))
return diags
} }
if err := ctx.Hook(func(h Hook) (HookAction, error) { diags = diags.Append(ctx.Hook(func(h Hook) (HookAction, error) {
return h.PreApply(absAddr, states.CurrentGen, planned.Action, planned.Before, planned.After) return h.PreApply(absAddr, states.CurrentGen, planned.Action, planned.Before, planned.After)
}); err != nil { }))
return nil, err if diags.HasErrors() {
return diags
} }
config := *n.Config config := *n.Config
@ -49,7 +52,8 @@ func (n *evalReadDataApply) Eval(ctx EvalContext) (interface{}, error) {
schema, _ := providerSchema.SchemaForResourceAddr(n.Addr.ContainingResource()) schema, _ := providerSchema.SchemaForResourceAddr(n.Addr.ContainingResource())
if schema == nil { if schema == nil {
// Should be caught during validation, so we don't bother with a pretty error here // Should be caught during validation, so we don't bother with a pretty error here
return nil, fmt.Errorf("provider %q does not support data source %q", n.ProviderAddr.Provider.String(), n.Addr.Resource.Type) diags = diags.Append(fmt.Errorf("provider %q does not support data source %q", n.ProviderAddr.Provider.String(), n.Addr.Resource.Type))
return diags
} }
forEach, _ := evaluateForEachExpression(config.ForEach, ctx) forEach, _ := evaluateForEachExpression(config.ForEach, ctx)
@ -58,13 +62,13 @@ func (n *evalReadDataApply) Eval(ctx EvalContext) (interface{}, error) {
configVal, _, configDiags := ctx.EvaluateBlock(config.Config, schema, nil, keyData) configVal, _, configDiags := ctx.EvaluateBlock(config.Config, schema, nil, keyData)
diags = diags.Append(configDiags) diags = diags.Append(configDiags)
if configDiags.HasErrors() { if configDiags.HasErrors() {
return nil, diags.ErrWithWarnings() return diags
} }
newVal, readDiags := n.readDataSource(ctx, configVal) newVal, readDiags := n.readDataSource(ctx, configVal)
diags = diags.Append(readDiags) diags = diags.Append(readDiags)
if diags.HasErrors() { if diags.HasErrors() {
return nil, diags.ErrWithWarnings() return diags
} }
*n.State = &states.ResourceInstanceObject{ *n.State = &states.ResourceInstanceObject{
@ -72,11 +76,9 @@ func (n *evalReadDataApply) Eval(ctx EvalContext) (interface{}, error) {
Status: states.ObjectReady, Status: states.ObjectReady,
} }
if err := ctx.Hook(func(h Hook) (HookAction, error) { diags = diags.Append(ctx.Hook(func(h Hook) (HookAction, error) {
return h.PostApply(absAddr, states.CurrentGen, newVal, diags.Err()) return h.PostApply(absAddr, states.CurrentGen, newVal, diags.Err())
}); err != nil { }))
diags = diags.Append(err)
}
return nil, diags.ErrWithWarnings() return diags
} }

View File

@ -21,14 +21,15 @@ type evalReadDataPlan struct {
evalReadData evalReadData
} }
func (n *evalReadDataPlan) Eval(ctx EvalContext) (interface{}, error) { func (n *evalReadDataPlan) Eval(ctx EvalContext) tfdiags.Diagnostics {
absAddr := n.Addr.Absolute(ctx.Path()) absAddr := n.Addr.Absolute(ctx.Path())
var diags tfdiags.Diagnostics var diags tfdiags.Diagnostics
var configVal cty.Value var configVal cty.Value
if n.ProviderSchema == nil || *n.ProviderSchema == nil { if n.ProviderSchema == nil || *n.ProviderSchema == nil {
return nil, fmt.Errorf("provider schema not available for %s", n.Addr) diags = diags.Append(fmt.Errorf("provider schema not available for %s", n.Addr))
return diags
} }
config := *n.Config config := *n.Config
@ -36,7 +37,8 @@ func (n *evalReadDataPlan) Eval(ctx EvalContext) (interface{}, error) {
schema, _ := providerSchema.SchemaForResourceAddr(n.Addr.ContainingResource()) schema, _ := providerSchema.SchemaForResourceAddr(n.Addr.ContainingResource())
if schema == nil { if schema == nil {
// Should be caught during validation, so we don't bother with a pretty error here // Should be caught during validation, so we don't bother with a pretty error here
return nil, fmt.Errorf("provider %q does not support data source %q", n.ProviderAddr.Provider.String(), n.Addr.Resource.Type) diags = diags.Append(fmt.Errorf("provider %q does not support data source %q", n.ProviderAddr.Provider.String(), n.Addr.Resource.Type))
return diags
} }
objTy := schema.ImpliedType() objTy := schema.ImpliedType()
@ -52,7 +54,7 @@ func (n *evalReadDataPlan) Eval(ctx EvalContext) (interface{}, error) {
configVal, _, configDiags = ctx.EvaluateBlock(config.Config, schema, nil, keyData) configVal, _, configDiags = ctx.EvaluateBlock(config.Config, schema, nil, keyData)
diags = diags.Append(configDiags) diags = diags.Append(configDiags)
if configDiags.HasErrors() { if configDiags.HasErrors() {
return nil, diags.ErrWithWarnings() return diags
} }
configKnown := configVal.IsWhollyKnown() configKnown := configVal.IsWhollyKnown()
@ -69,11 +71,11 @@ func (n *evalReadDataPlan) Eval(ctx EvalContext) (interface{}, error) {
proposedNewVal := objchange.PlannedDataResourceObject(schema, configVal) proposedNewVal := objchange.PlannedDataResourceObject(schema, configVal)
if err := ctx.Hook(func(h Hook) (HookAction, error) { diags = diags.Append(ctx.Hook(func(h Hook) (HookAction, error) {
return h.PreDiff(absAddr, states.CurrentGen, priorVal, proposedNewVal) return h.PreDiff(absAddr, states.CurrentGen, priorVal, proposedNewVal)
}); err != nil { }))
diags = diags.Append(err) if diags.HasErrors() {
return nil, diags.ErrWithWarnings() return diags
} }
// Apply detects that the data source will need to be read by the After // Apply detects that the data source will need to be read by the After
@ -93,13 +95,11 @@ func (n *evalReadDataPlan) Eval(ctx EvalContext) (interface{}, error) {
Status: states.ObjectPlanned, Status: states.ObjectPlanned,
} }
if err := ctx.Hook(func(h Hook) (HookAction, error) { diags = diags.Append(ctx.Hook(func(h Hook) (HookAction, error) {
return h.PostDiff(absAddr, states.CurrentGen, plans.Read, priorVal, proposedNewVal) return h.PostDiff(absAddr, states.CurrentGen, plans.Read, priorVal, proposedNewVal)
}); err != nil { }))
diags = diags.Append(err)
}
return nil, diags.ErrWithWarnings() return diags
} }
// We have a complete configuration with no dependencies to wait on, so we // We have a complete configuration with no dependencies to wait on, so we
@ -107,7 +107,7 @@ func (n *evalReadDataPlan) Eval(ctx EvalContext) (interface{}, error) {
newVal, readDiags := n.readDataSource(ctx, configVal) newVal, readDiags := n.readDataSource(ctx, configVal)
diags = diags.Append(readDiags) diags = diags.Append(readDiags)
if diags.HasErrors() { if diags.HasErrors() {
return nil, diags.ErrWithWarnings() return diags
} }
// if we have a prior value, we can check for any irregularities in the response // if we have a prior value, we can check for any irregularities in the response
@ -137,13 +137,10 @@ func (n *evalReadDataPlan) Eval(ctx EvalContext) (interface{}, error) {
Status: states.ObjectReady, Status: states.ObjectReady,
} }
if err := ctx.Hook(func(h Hook) (HookAction, error) { diags = diags.Append(ctx.Hook(func(h Hook) (HookAction, error) {
return h.PostDiff(absAddr, states.CurrentGen, plans.Update, priorVal, newVal) return h.PostDiff(absAddr, states.CurrentGen, plans.Update, priorVal, newVal)
}); err != nil { }))
return nil, err return diags
}
return nil, diags.ErrWithWarnings()
} }
// forcePlanRead determines if we need to override the usual behavior of // forcePlanRead determines if we need to override the usual behavior of

View File

@ -134,6 +134,7 @@ func (n *NodeApplyableResourceInstance) Execute(ctx EvalContext, op walkOperatio
} }
func (n *NodeApplyableResourceInstance) dataResourceExecute(ctx EvalContext) error { func (n *NodeApplyableResourceInstance) dataResourceExecute(ctx EvalContext) error {
var diags tfdiags.Diagnostics
addr := n.ResourceInstanceAddr().Resource addr := n.ResourceInstanceAddr().Resource
provider, providerSchema, err := GetProvider(ctx, n.ResolvedProvider) provider, providerSchema, err := GetProvider(ctx, n.ResolvedProvider)
@ -166,9 +167,9 @@ func (n *NodeApplyableResourceInstance) dataResourceExecute(ctx EvalContext) err
State: &state, State: &state,
}, },
} }
_, err = readDataApply.Eval(ctx) diags = readDataApply.Eval(ctx)
if err != nil { if diags.HasErrors() {
return err return diags.ErrWithWarnings()
} }
writeState := &EvalWriteState{ writeState := &EvalWriteState{
@ -187,7 +188,7 @@ func (n *NodeApplyableResourceInstance) dataResourceExecute(ctx EvalContext) err
ProviderSchema: &providerSchema, ProviderSchema: &providerSchema,
Change: nil, Change: nil,
} }
diags := writeDiff.Eval(ctx) diags = writeDiff.Eval(ctx)
if diags.HasErrors() { if diags.HasErrors() {
return diags.ErrWithWarnings() return diags.ErrWithWarnings()
} }

View File

@ -5,6 +5,7 @@ import (
"github.com/hashicorp/terraform/plans" "github.com/hashicorp/terraform/plans"
"github.com/hashicorp/terraform/states" "github.com/hashicorp/terraform/states"
"github.com/hashicorp/terraform/tfdiags"
"github.com/hashicorp/terraform/addrs" "github.com/hashicorp/terraform/addrs"
) )
@ -45,6 +46,7 @@ func (n *NodePlannableResourceInstance) Execute(ctx EvalContext, op walkOperatio
} }
func (n *NodePlannableResourceInstance) dataResourceExecute(ctx EvalContext) error { func (n *NodePlannableResourceInstance) dataResourceExecute(ctx EvalContext) error {
var diags tfdiags.Diagnostics
config := n.Config config := n.Config
addr := n.ResourceInstanceAddr() addr := n.ResourceInstanceAddr()
@ -84,9 +86,9 @@ func (n *NodePlannableResourceInstance) dataResourceExecute(ctx EvalContext) err
dependsOn: n.dependsOn, dependsOn: n.dependsOn,
}, },
} }
_, err = readDataPlan.Eval(ctx) diags = readDataPlan.Eval(ctx)
if err != nil { if diags.HasErrors() {
return err return diags.ErrWithWarnings()
} }
// write the data source into both the refresh state and the // write the data source into both the refresh state and the
@ -119,7 +121,7 @@ func (n *NodePlannableResourceInstance) dataResourceExecute(ctx EvalContext) err
ProviderSchema: &providerSchema, ProviderSchema: &providerSchema,
Change: &change, Change: &change,
} }
diags := writeDiff.Eval(ctx) diags = writeDiff.Eval(ctx)
return diags.ErrWithWarnings() return diags.ErrWithWarnings()
} }