Skip to content

Commit

Permalink
Merge pull request #26285 from hashicorp/jbardin/save-refreshed-data
Browse files Browse the repository at this point in the history
save read data in the refresh state during plan
  • Loading branch information
jbardin committed Sep 18, 2020
2 parents e77c367 + 669da06 commit 6d7904c
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 1 deletion.
37 changes: 37 additions & 0 deletions terraform/context_plan_test.go
Expand Up @@ -6333,3 +6333,40 @@ func TestContext2Plan_targetedModuleInstance(t *testing.T) {
}
}
}

func TestContext2Plan_dataRefreshedInPlan(t *testing.T) {
m := testModuleInline(t, map[string]string{
"main.tf": `
data "test_data_source" "d" {
}
`})

p := testProvider("test")
p.ReadDataSourceResponse = providers.ReadDataSourceResponse{
State: cty.ObjectVal(map[string]cty.Value{
"id": cty.StringVal("this"),
"foo": cty.NullVal(cty.String),
}),
}

ctx := testContext2(t, &ContextOpts{
Config: m,
Providers: map[addrs.Provider]providers.Factory{
addrs.NewDefaultProvider("test"): testProviderFuncFixed(p),
},
})

plan, diags := ctx.Plan()
if diags.HasErrors() {
t.Fatal(diags.ErrWithWarnings())
}

d := plan.State.ResourceInstance(mustResourceInstanceAddr("data.test_data_source.d"))
if d == nil || d.Current == nil {
t.Fatal("data.test_data_source.d not found in state:", plan.State)
}

if d.Current.Status != states.ObjectReady {
t.Fatal("expected data.test_data_source.d to be fully read in refreshed state, got status", d.Current.Status)
}
}
4 changes: 3 additions & 1 deletion terraform/eval_read_data_plan.go
Expand Up @@ -138,6 +138,8 @@ func (n *evalReadDataPlan) Eval(ctx EvalContext) (interface{}, error) {
}
}

// We still default to read here, to indicate any changes in the plan, even
// though this will already be written in the refreshed state.
action := plans.Read
if priorVal.Equals(newVal).True() {
action = plans.NoOp
Expand All @@ -159,7 +161,7 @@ func (n *evalReadDataPlan) Eval(ctx EvalContext) (interface{}, error) {

*n.State = &states.ResourceInstanceObject{
Value: newVal,
Status: states.ObjectPlanned,
Status: states.ObjectReady,
}

if err := ctx.Hook(func(h Hook) (HookAction, error) {
Expand Down
10 changes: 10 additions & 0 deletions terraform/node_resource_plan_instance.go
Expand Up @@ -86,6 +86,16 @@ func (n *NodePlannableResourceInstance) evalTreeDataResource(addr addrs.AbsResou
},
},

// write the data source into both the refresh state and the
// working state
&EvalWriteState{
Addr: addr.Resource,
ProviderAddr: n.ResolvedProvider,
ProviderSchema: &providerSchema,
State: &state,
targetState: refreshState,
},

&EvalWriteState{
Addr: addr.Resource,
ProviderAddr: n.ResolvedProvider,
Expand Down

0 comments on commit 6d7904c

Please sign in to comment.