Skip to content

Commit cd0c5b2

Browse files
committed
WIP - allowing apply operation to use plan file content about backend/state_store
1 parent ac0f828 commit cd0c5b2

File tree

2 files changed

+20
-11
lines changed

2 files changed

+20
-11
lines changed

internal/command/apply.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -210,16 +210,16 @@ func (c *ApplyCommand) PrepareBackend(planFile *planfile.WrappedPlanFile, args *
210210
))
211211
return nil, diags
212212
}
213-
if plan.Backend.Config == nil {
213+
if plan.Backend.Config == nil && plan.StateStore.Config == nil {
214214
// Should never happen; always indicates a bug in the creation of the plan file
215215
diags = diags.Append(tfdiags.Sourceless(
216216
tfdiags.Error,
217217
"Failed to read plan from plan file",
218-
"The given plan file does not have a valid backend configuration. This is a bug in the Terraform command that generated this plan file.",
218+
"The given plan file does not have either a valid backend or state_store configuration. This is a bug in the Terraform command that generated this plan file.",
219219
))
220220
return nil, diags
221221
}
222-
be, beDiags = c.BackendForLocalPlan(plan.Backend)
222+
be, beDiags = c.BackendForLocalPlan(plan)
223223
} else {
224224
// Both new plans and saved cloud plans load their backend from config.
225225
backendConfig, configDiags := c.loadBackendConfig(".")

internal/command/meta_backend.go

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -302,19 +302,28 @@ func (m *Meta) selectWorkspace(b backend.Backend) error {
302302
// The current workspace name is also stored as part of the plan, and so this
303303
// method will check that it matches the currently-selected workspace name
304304
// and produce error diagnostics if not.
305-
func (m *Meta) BackendForLocalPlan(settings plans.Backend) (backendrun.OperationsBackend, tfdiags.Diagnostics) {
305+
func (m *Meta) BackendForLocalPlan(plan *plans.Plan) (backendrun.OperationsBackend, tfdiags.Diagnostics) {
306306
var diags tfdiags.Diagnostics
307307

308-
f := backendInit.Backend(settings.Type)
309-
if f == nil {
310-
diags = diags.Append(fmt.Errorf(strings.TrimSpace(errBackendSavedUnknown), settings.Type))
311-
return nil, diags
308+
var b backend.Backend
309+
var config plans.DynamicValue
310+
if plan.StateStore.Config != nil {
311+
// TODO - code for state_store
312+
} else if plan.Backend.Config != nil {
313+
settings := plan.Backend
314+
config = plan.Backend.Config
315+
316+
f := backendInit.Backend(settings.Type)
317+
if f == nil {
318+
diags = diags.Append(fmt.Errorf(strings.TrimSpace(errBackendSavedUnknown), settings.Type))
319+
return nil, diags
320+
}
321+
b = f()
322+
log.Printf("[TRACE] Meta.BackendForLocalPlan: instantiated backend of type %T", b)
312323
}
313-
b := f()
314-
log.Printf("[TRACE] Meta.BackendForLocalPlan: instantiated backend of type %T", b)
315324

316325
schema := b.ConfigSchema()
317-
configVal, err := settings.Config.Decode(schema.ImpliedType())
326+
configVal, err := config.Decode(schema.ImpliedType())
318327
if err != nil {
319328
diags = diags.Append(fmt.Errorf("saved backend configuration is invalid: %w", err))
320329
return nil, diags

0 commit comments

Comments
 (0)