Skip to content

Commit cccfa5e

Browse files
authored
Merge pull request #32111 from hashicorp/jbardin/refresh-only-data-read
don't plan data source reads during refresh-only
2 parents 6521355 + 300ad02 commit cccfa5e

File tree

3 files changed

+15
-4
lines changed

3 files changed

+15
-4
lines changed

internal/terraform/context_refresh_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,12 +201,12 @@ func TestContext2Refresh_dataComputedModuleVar(t *testing.T) {
201201
},
202202
})
203203

204-
s, diags := ctx.Refresh(m, states.NewState(), &PlanOpts{Mode: plans.NormalMode})
204+
plan, diags := ctx.Plan(m, states.NewState(), &PlanOpts{Mode: plans.RefreshOnlyMode})
205205
if diags.HasErrors() {
206206
t.Fatalf("refresh errors: %s", diags.Err())
207207
}
208208

209-
checkStateString(t, s, `
209+
checkStateString(t, plan.PriorState, `
210210
<no state>
211211
`)
212212
}

internal/terraform/node_resource_abstract_instance.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1562,7 +1562,7 @@ func (n *NodeAbstractResourceInstance) providerMetas(ctx EvalContext) (cty.Value
15621562
// value, but it still matches the previous state, then we can record a NoNop
15631563
// change. If the states don't match then we record a Read change so that the
15641564
// new value is applied to the state.
1565-
func (n *NodeAbstractResourceInstance) planDataSource(ctx EvalContext, checkRuleSeverity tfdiags.Severity) (*plans.ResourceInstanceChange, *states.ResourceInstanceObject, instances.RepetitionData, tfdiags.Diagnostics) {
1565+
func (n *NodeAbstractResourceInstance) planDataSource(ctx EvalContext, checkRuleSeverity tfdiags.Severity, skipPlanChanges bool) (*plans.ResourceInstanceChange, *states.ResourceInstanceObject, instances.RepetitionData, tfdiags.Diagnostics) {
15661566
var diags tfdiags.Diagnostics
15671567
var keyData instances.RepetitionData
15681568
var configVal cty.Value
@@ -1616,6 +1616,17 @@ func (n *NodeAbstractResourceInstance) planDataSource(ctx EvalContext, checkRule
16161616
// producing a "Read" change for this resource, and a placeholder value for
16171617
// it in the state.
16181618
if depsPending || !configKnown {
1619+
// We can't plan any changes if we're only refreshing, so the only
1620+
// value we can set here is whatever was in state previously.
1621+
if skipPlanChanges {
1622+
plannedNewState := &states.ResourceInstanceObject{
1623+
Value: priorVal,
1624+
Status: states.ObjectReady,
1625+
}
1626+
1627+
return nil, plannedNewState, keyData, diags
1628+
}
1629+
16191630
var reason plans.ResourceInstanceChangeActionReason
16201631
switch {
16211632
case !configKnown:

internal/terraform/node_resource_plan_instance.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ func (n *NodePlannableResourceInstance) dataResourceExecute(ctx EvalContext) (di
8787
checkRuleSeverity = tfdiags.Warning
8888
}
8989

90-
change, state, repeatData, planDiags := n.planDataSource(ctx, checkRuleSeverity)
90+
change, state, repeatData, planDiags := n.planDataSource(ctx, checkRuleSeverity, n.skipPlanChanges)
9191
diags = diags.Append(planDiags)
9292
if diags.HasErrors() {
9393
return diags

0 commit comments

Comments
 (0)