Skip to content

Commit 2ecbed9

Browse files
committed
Add a generic method for loading an operations backend in non-init commands
1 parent 60d5333 commit 2ecbed9

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

internal/command/meta_backend.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1525,6 +1525,62 @@ func (m *Meta) updateSavedBackendHash(cHash int, sMgr *clistate.LocalState) tfdi
15251525
return diags
15261526
}
15271527

1528+
// prepareBackend returns an operations backend that may use a backend, cloud, or state_store block for state storage.
1529+
// This method should be used in NON-init operations only; it's incapable of processing new init command CLI flags used
1530+
// for partial configuration, however it will use the backend state file to use partial configuration from a previous
1531+
// init command.
1532+
func (m *Meta) prepareBackend(root *configs.Module) (backendrun.OperationsBackend, tfdiags.Diagnostics) {
1533+
var diags tfdiags.Diagnostics
1534+
1535+
var opts *BackendOpts
1536+
switch {
1537+
case root.Backend != nil:
1538+
opts = &BackendOpts{
1539+
BackendConfig: root.Backend,
1540+
}
1541+
case root.CloudConfig != nil:
1542+
backendConfig := root.CloudConfig.ToBackendConfig()
1543+
opts = &BackendOpts{
1544+
BackendConfig: &backendConfig,
1545+
}
1546+
case root.StateStore != nil:
1547+
// In addition to config, use of a state_store requires
1548+
// provider factory and provider locks data
1549+
factory, fDiags := m.getStateStoreProviderFactory(root.StateStore)
1550+
diags = diags.Append(fDiags)
1551+
if fDiags.HasErrors() {
1552+
return nil, diags
1553+
}
1554+
1555+
// TODO - Use locks from here in opts below
1556+
_, lDiags := m.lockedDependencies()
1557+
diags = diags.Append(lDiags)
1558+
if lDiags.HasErrors() {
1559+
return nil, diags
1560+
}
1561+
1562+
opts = &BackendOpts{
1563+
StateStoreConfig: root.StateStore,
1564+
ProviderFactory: factory,
1565+
// TODO - update once other work is merged into main
1566+
// Locks: locks,
1567+
}
1568+
default:
1569+
// there is no config; defaults to local state storage
1570+
opts = &BackendOpts{}
1571+
}
1572+
opts.Init = false // To be explicit- this method should not be used in init commands!
1573+
1574+
// Load the backend
1575+
be, beDiags := m.Backend(opts)
1576+
diags = diags.Append(beDiags)
1577+
if beDiags.HasErrors() {
1578+
return nil, diags
1579+
}
1580+
1581+
return be, diags
1582+
}
1583+
15281584
//-------------------------------------------------------------------
15291585
// Reusable helper functions for backend management
15301586
//-------------------------------------------------------------------

0 commit comments

Comments
 (0)