mirror of
https://github.com/opentofu/opentofu.git
synced 2025-02-25 18:45:20 -06:00
core: Re-implement EvalReadDataApply against new provider interface
This commit is contained in:
parent
d96284f2e2
commit
235f582ae5
@ -71,6 +71,11 @@ func dataSourceRemoteStateRead(d *cty.Value) (cty.Value, tfdiags.Diagnostics) {
|
|||||||
b := f()
|
b := f()
|
||||||
|
|
||||||
config := d.GetAttr("config")
|
config := d.GetAttr("config")
|
||||||
|
if config.IsNull() {
|
||||||
|
// We'll treat this as an empty configuration and see if the backend's
|
||||||
|
// schema and validation code will accept it.
|
||||||
|
config = cty.EmptyObjectVal
|
||||||
|
}
|
||||||
newState["config"] = config
|
newState["config"] = config
|
||||||
|
|
||||||
schema := b.ConfigSchema()
|
schema := b.ConfigSchema()
|
||||||
|
@ -138,24 +138,25 @@ func (n *EvalReadDataDiff) Eval(ctx EvalContext) (interface{}, error) {
|
|||||||
type EvalReadDataApply struct {
|
type EvalReadDataApply struct {
|
||||||
Addr addrs.ResourceInstance
|
Addr addrs.ResourceInstance
|
||||||
Provider *providers.Interface
|
Provider *providers.Interface
|
||||||
|
ProviderAddr addrs.AbsProviderConfig
|
||||||
|
ProviderSchema **ProviderSchema
|
||||||
Output **states.ResourceInstanceObject
|
Output **states.ResourceInstanceObject
|
||||||
|
Config *configs.Resource
|
||||||
Change **plans.ResourceInstanceChange
|
Change **plans.ResourceInstanceChange
|
||||||
}
|
}
|
||||||
|
|
||||||
func (n *EvalReadDataApply) Eval(ctx EvalContext) (interface{}, error) {
|
func (n *EvalReadDataApply) Eval(ctx EvalContext) (interface{}, error) {
|
||||||
return nil, fmt.Errorf("EvalReadDataApply not yet updated for new state/plan/provider types")
|
|
||||||
/*
|
|
||||||
provider := *n.Provider
|
provider := *n.Provider
|
||||||
change := *n.Change
|
change := *n.Change
|
||||||
|
providerSchema := *n.ProviderSchema
|
||||||
absAddr := n.Addr.Absolute(ctx.Path())
|
absAddr := n.Addr.Absolute(ctx.Path())
|
||||||
|
|
||||||
// The provider and hook APIs still expect our legacy InstanceInfo type.
|
var diags tfdiags.Diagnostics
|
||||||
legacyInfo := NewInstanceInfo(n.Addr.Absolute(ctx.Path()))
|
|
||||||
|
|
||||||
// If the diff is for *destroying* this resource then we'll
|
// If the diff is for *destroying* this resource then we'll
|
||||||
// just drop its state and move on, since data resources don't
|
// just drop its state and move on, since data resources don't
|
||||||
// support an actual "destroy" action.
|
// support an actual "destroy" action.
|
||||||
if diff != nil && diff.GetDestroy() {
|
if change != nil && change.Action == plans.Delete {
|
||||||
if n.Output != nil {
|
if n.Output != nil {
|
||||||
*n.Output = nil
|
*n.Output = nil
|
||||||
}
|
}
|
||||||
@ -168,28 +169,55 @@ func (n *EvalReadDataApply) Eval(ctx EvalContext) (interface{}, error) {
|
|||||||
err := ctx.Hook(func(h Hook) (HookAction, error) {
|
err := ctx.Hook(func(h Hook) (HookAction, error) {
|
||||||
// We don't have a state yet, so we'll just give the hook an
|
// We don't have a state yet, so we'll just give the hook an
|
||||||
// empty one to work with.
|
// empty one to work with.
|
||||||
return h.PreRefresh(absAddr, cty.NullVal(cty.DynamicPseudoType))
|
return h.PreRefresh(absAddr, states.CurrentGen, cty.NullVal(cty.DynamicPseudoType))
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
state, err := provider.ReadDataApply(legacyInfo, diff)
|
resp := provider.ReadDataSource(providers.ReadDataSourceRequest{
|
||||||
if err != nil {
|
TypeName: n.Addr.Resource.Type,
|
||||||
return nil, fmt.Errorf("%s: %s", n.Addr.Absolute(ctx.Path()).String(), err)
|
Config: change.After,
|
||||||
|
})
|
||||||
|
diags = diags.Append(resp.Diagnostics.InConfigBody(n.Config.Config))
|
||||||
|
if diags.HasErrors() {
|
||||||
|
return nil, diags.Err()
|
||||||
|
}
|
||||||
|
|
||||||
|
schema := providerSchema.DataSources[n.Addr.Resource.Type]
|
||||||
|
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.ProviderConfig.Type, tfdiags.FormatErrorPrefixed(err, absAddr.String()),
|
||||||
|
),
|
||||||
|
))
|
||||||
|
}
|
||||||
|
if diags.HasErrors() {
|
||||||
|
return nil, diags.Err()
|
||||||
}
|
}
|
||||||
|
|
||||||
err = ctx.Hook(func(h Hook) (HookAction, error) {
|
err = ctx.Hook(func(h Hook) (HookAction, error) {
|
||||||
return h.PostRefresh(absAddr, state)
|
return h.PostRefresh(absAddr, states.CurrentGen, change.Before, newVal)
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if n.Output != nil {
|
if n.Output != nil {
|
||||||
*n.Output = state
|
*n.Output = &states.ResourceInstanceObject{
|
||||||
|
Value: newVal,
|
||||||
|
Status: states.ObjectReady,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil, nil
|
return nil, diags.ErrWithWarnings()
|
||||||
*/
|
|
||||||
}
|
}
|
||||||
|
@ -178,8 +178,11 @@ func (n *NodeRefreshableDataResourceInstance) EvalTree() EvalNode {
|
|||||||
|
|
||||||
&EvalReadDataApply{
|
&EvalReadDataApply{
|
||||||
Addr: addr.Resource,
|
Addr: addr.Resource,
|
||||||
|
Config: n.Config,
|
||||||
Change: &change,
|
Change: &change,
|
||||||
Provider: &provider,
|
Provider: &provider,
|
||||||
|
ProviderAddr: n.ResolvedProvider,
|
||||||
|
ProviderSchema: &providerSchema,
|
||||||
Output: &state,
|
Output: &state,
|
||||||
},
|
},
|
||||||
|
|
||||||
|
@ -167,8 +167,11 @@ func (n *NodeApplyableResourceInstance) evalTreeDataResource(addr addrs.AbsResou
|
|||||||
|
|
||||||
&EvalReadDataApply{
|
&EvalReadDataApply{
|
||||||
Addr: addr.Resource,
|
Addr: addr.Resource,
|
||||||
|
Config: n.Config,
|
||||||
Change: &change,
|
Change: &change,
|
||||||
Provider: &provider,
|
Provider: &provider,
|
||||||
|
ProviderAddr: n.ResolvedProvider,
|
||||||
|
ProviderSchema: &providerSchema,
|
||||||
Output: &state,
|
Output: &state,
|
||||||
},
|
},
|
||||||
|
|
||||||
|
@ -261,8 +261,11 @@ func (n *NodeDestroyResourceInstance) EvalTree() EvalNode {
|
|||||||
|
|
||||||
Then: &EvalReadDataApply{
|
Then: &EvalReadDataApply{
|
||||||
Addr: addr.Resource,
|
Addr: addr.Resource,
|
||||||
|
Config: n.Config,
|
||||||
Change: &changeApply,
|
Change: &changeApply,
|
||||||
Provider: &provider,
|
Provider: &provider,
|
||||||
|
ProviderAddr: n.ResolvedProvider,
|
||||||
|
ProviderSchema: &providerSchema,
|
||||||
Output: &state,
|
Output: &state,
|
||||||
},
|
},
|
||||||
Else: &EvalApply{
|
Else: &EvalApply{
|
||||||
|
Loading…
Reference in New Issue
Block a user