Skip to content

Commit ac0f828

Browse files
committed
WIP
1 parent a2acc33 commit ac0f828

File tree

4 files changed

+54
-19
lines changed

4 files changed

+54
-19
lines changed

internal/backend/backendrun/operation.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,11 @@ type Operation struct {
7575
//
7676
// PlanOutBackend is the backend to store with the plan. This is the
7777
// backend that will be used when applying the plan.
78-
PlanId string
79-
PlanRefresh bool // PlanRefresh will do a refresh before a plan
80-
PlanOutPath string // PlanOutPath is the path to save the plan
81-
PlanOutBackend *plans.Backend
78+
PlanId string
79+
PlanRefresh bool // PlanRefresh will do a refresh before a plan
80+
PlanOutPath string // PlanOutPath is the path to save the plan
81+
PlanOutBackend *plans.Backend
82+
PlanOutStateStore *plans.StateStore
8283

8384
// ConfigDir is the path to the directory containing the configuration's
8485
// root module.

internal/backend/local/backend_plan.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,14 @@ func (b *Local) opPlan(
146146
op.ReportResult(runningOp, diags)
147147
return
148148
}
149-
plan.Backend = *op.PlanOutBackend
149+
switch {
150+
case op.PlanOutBackend != nil:
151+
plan.Backend = *op.PlanOutBackend
152+
case op.PlanOutStateStore != nil:
153+
plan.StateStore = *op.PlanOutStateStore
154+
default:
155+
panic("backend and state_store configuration data missing from Operation")
156+
}
150157

151158
// We may have updated the state in the refresh step above, but we
152159
// will freeze that updated state in the plan file for now and

internal/command/meta.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,8 +201,9 @@ type Meta struct {
201201
// It is initialized on first use.
202202
configLoader *configload.Loader
203203

204-
// backendState is the currently active backend state
205-
backendState *workdir.BackendConfigState
204+
// backendConfigState is the currently active backend state
205+
backendConfigState *workdir.BackendConfigState
206+
stateStoreConfigState *workdir.StateStoreConfigState
206207

207208
// Variables for the context (private)
208209
variableArgs arguments.FlagNameValueSlice

internal/command/meta_backend.go

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -196,13 +196,13 @@ func (m *Meta) Backend(opts *BackendOpts) (backendrun.OperationsBackend, tfdiags
196196
// the user, since the local backend should only be used when learning or
197197
// in exceptional cases and so it's better to help the user learn that
198198
// by introducing it as a concept.
199-
if m.backendState == nil {
199+
if m.backendConfigState == nil {
200200
// NOTE: This synthetic object is intentionally _not_ retained in the
201201
// on-disk record of the backend configuration, which was already dealt
202202
// with inside backendFromConfig, because we still need that codepath
203203
// to be able to recognize the lack of a config as distinct from
204204
// explicitly setting local until we do some more refactoring here.
205-
m.backendState = &workdir.BackendConfigState{
205+
m.backendConfigState = &workdir.BackendConfigState{
206206
Type: "local",
207207
ConfigRaw: json.RawMessage("{}"),
208208
}
@@ -412,13 +412,34 @@ func (m *Meta) Operation(b backend.Backend, vt arguments.ViewType) *backendrun.O
412412
// here first is a bug, so panic.
413413
panic(fmt.Sprintf("invalid workspace: %s", err))
414414
}
415-
planOutBackend, err := m.backendState.Plan(schema, workspace)
416-
if err != nil {
417-
// Always indicates an implementation error in practice, because
418-
// errors here indicate invalid encoding of the backend configuration
419-
// in memory, and we should always have validated that by the time
420-
// we get here.
421-
panic(fmt.Sprintf("failed to encode backend configuration for plan: %s", err))
415+
416+
var planOutBackend *plans.Backend
417+
var planOutStateStore *plans.StateStore
418+
switch {
419+
case m.backendConfigState == nil && m.stateStoreConfigState == nil:
420+
// Neither set
421+
panic(fmt.Sprintf("failed to encode backend configuration for plan: neither backend nor state_store data present"))
422+
case m.backendConfigState != nil && m.stateStoreConfigState != nil:
423+
// Both set
424+
panic(fmt.Sprintf("failed to encode backend configuration for plan: both backend and state_store data present but they are mutually exclusive"))
425+
case m.backendConfigState != nil:
426+
planOutBackend, err = m.backendConfigState.Plan(schema, workspace)
427+
if err != nil {
428+
// Always indicates an implementation error in practice, because
429+
// errors here indicate invalid encoding of the backend configuration
430+
// in memory, and we should always have validated that by the time
431+
// we get here.
432+
panic(fmt.Sprintf("failed to encode backend configuration for plan: %s", err))
433+
}
434+
case m.stateStoreConfigState != nil:
435+
planOutStateStore, err = m.stateStoreConfigState.Plan(schema, workspace)
436+
if err != nil {
437+
// Always indicates an implementation error in practice, because
438+
// errors here indicate invalid encoding of the state_store configuration
439+
// in memory, and we should always have validated that by the time
440+
// we get here.
441+
panic(fmt.Sprintf("failed to encode state_store configuration for plan: %s", err))
442+
}
422443
}
423444

424445
stateLocker := clistate.NewNoopLocker()
@@ -438,7 +459,12 @@ func (m *Meta) Operation(b backend.Backend, vt arguments.ViewType) *backendrun.O
438459
}
439460

440461
return &backendrun.Operation{
441-
PlanOutBackend: planOutBackend,
462+
463+
// These two fields are mutually exclusive,
464+
// and one is being assigned a nil value below.
465+
PlanOutBackend: planOutBackend,
466+
PlanOutStateStore: planOutStateStore,
467+
442468
Targets: m.targets,
443469
UIIn: m.UIInput(),
444470
UIOut: m.Ui,
@@ -578,10 +604,10 @@ func (m *Meta) backendFromConfig(opts *BackendOpts) (backend.Backend, tfdiags.Di
578604

579605
// Upon return, we want to set the state we're using in-memory so that
580606
// we can access it for commands.
581-
m.backendState = nil
607+
m.backendConfigState = nil
582608
defer func() {
583609
if s := sMgr.State(); s != nil && !s.Backend.Empty() {
584-
m.backendState = s.Backend
610+
m.backendConfigState = s.Backend
585611
}
586612
}()
587613

0 commit comments

Comments
 (0)