remove EvalReadDataApply

EvalReadDataApply was all dead code, as it was only using during delete
and simply set the state to nil.
This commit is contained in:
James Bardin 2020-05-06 15:02:52 -04:00
parent cf2dd43f30
commit be20a7941d
2 changed files with 6 additions and 122 deletions

View File

@ -385,114 +385,3 @@ func (n *EvalReadData) Eval(ctx EvalContext) (interface{}, error) {
return nil, diags.ErrWithWarnings()
}
// EvalReadDataApply is an EvalNode implementation that executes a data
// resource's ReadDataApply method to read data from the data source.
type EvalReadDataApply struct {
Addr addrs.ResourceInstance
Provider *providers.Interface
ProviderAddr addrs.AbsProviderConfig
ProviderMeta *configs.ProviderMeta
ProviderSchema **ProviderSchema
Output **states.ResourceInstanceObject
Config *configs.Resource
Change **plans.ResourceInstanceChange
}
func (n *EvalReadDataApply) Eval(ctx EvalContext) (interface{}, error) {
provider := *n.Provider
change := *n.Change
providerSchema := *n.ProviderSchema
absAddr := n.Addr.Absolute(ctx.Path())
var diags tfdiags.Diagnostics
// If the diff is for *destroying* this resource then we'll
// just drop its state and move on, since data resources don't
// support an actual "destroy" action.
if change != nil && change.Action == plans.Delete {
if n.Output != nil {
*n.Output = nil
}
return nil, nil
}
metaConfigVal := cty.NullVal(cty.DynamicPseudoType)
if n.ProviderMeta != nil {
// if the provider doesn't support this feature, throw an error
if (*n.ProviderSchema).ProviderMeta == nil {
diags = diags.Append(&hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: fmt.Sprintf("Provider %s doesn't support provider_meta", n.ProviderAddr.Provider.String()),
Detail: fmt.Sprintf("The resource %s belongs to a provider that doesn't support provider_meta blocks", n.Addr),
Subject: &n.ProviderMeta.ProviderRange,
})
} else {
var configDiags tfdiags.Diagnostics
metaConfigVal, _, configDiags = ctx.EvaluateBlock(n.ProviderMeta.Config, (*n.ProviderSchema).ProviderMeta, nil, EvalDataForNoInstanceKey)
diags = diags.Append(configDiags)
if configDiags.HasErrors() {
return nil, diags.Err()
}
}
}
// For the purpose of external hooks we present a data apply as a
// "Refresh" rather than an "Apply" because creating a data source
// is presented to users/callers as a "read" operation.
err := ctx.Hook(func(h Hook) (HookAction, error) {
// We don't have a state yet, so we'll just give the hook an
// empty one to work with.
return h.PreRefresh(absAddr, states.CurrentGen, cty.NullVal(cty.DynamicPseudoType))
})
if err != nil {
return nil, err
}
resp := provider.ReadDataSource(providers.ReadDataSourceRequest{
TypeName: n.Addr.Resource.Type,
Config: change.After,
ProviderMeta: metaConfigVal,
})
diags = diags.Append(resp.Diagnostics.InConfigBody(n.Config.Config))
if diags.HasErrors() {
return nil, diags.Err()
}
schema, _ := providerSchema.SchemaForResourceAddr(n.Addr.ContainingResource())
if schema == nil {
// Should be caught during validation, so we don't bother with a pretty error here
return nil, fmt.Errorf("provider does not support data source %q", n.Addr.Resource.Type)
}
newVal := resp.State
for _, err := range newVal.Type().TestConformance(schema.ImpliedType()) {
diags = diags.Append(tfdiags.Sourceless(
tfdiags.Error,
"Provider produced invalid object",
fmt.Sprintf(
"Provider %q planned an invalid value for %s. The result could not be saved.\n\nThis is a bug in the provider, which should be reported in the provider's own issue tracker.",
n.ProviderAddr.Provider.String(), tfdiags.FormatErrorPrefixed(err, absAddr.String()),
),
))
}
if diags.HasErrors() {
return nil, diags.Err()
}
err = ctx.Hook(func(h Hook) (HookAction, error) {
return h.PostRefresh(absAddr, states.CurrentGen, change.Before, newVal)
})
if err != nil {
return nil, err
}
if n.Output != nil {
*n.Output = &states.ResourceInstanceObject{
Value: newVal,
Status: states.ObjectReady,
}
}
return nil, diags.ErrWithWarnings()
}

View File

@ -237,19 +237,14 @@ func (n *NodeDestroyResourceInstance) EvalTree() EvalNode {
// Make sure we handle data sources properly.
&EvalIf{
If: func(ctx EvalContext) (bool, error) {
return addr.Resource.Resource.Mode == addrs.DataResourceMode, nil
if addr.Resource.Resource.Mode == addrs.DataResourceMode {
// deleting a data source is simply removing the state
state = nil
}
return addr.Resource.Resource.Mode == addrs.ManagedResourceMode, nil
},
Then: &EvalReadDataApply{
Addr: addr.Resource,
Config: n.Config,
Change: &changeApply,
Provider: &provider,
ProviderAddr: n.ResolvedProvider,
ProviderSchema: &providerSchema,
Output: &state,
},
Else: &EvalApply{
Then: &EvalApply{
Addr: addr.Resource,
Config: nil, // No configuration because we are destroying
State: &state,