Documentation
¶
Overview ¶
Package init contains the list of backends that can be initialized and basic helper functions for initializing those backends.
Index ¶
- Variables
- func Backend(name string) (backend.InitFn, string)
- func Init(services *disco.Disco)
- func RegisterTemp(name string, f backend.InitFn) func()
- func Set(name string, f backend.InitFn)
- type MockBackend
- func (m *MockBackend) ConfigSchema() *configschema.Block
- func (m *MockBackend) Configure(ctx context.Context, configObj cty.Value) tfdiags.Diagnostics
- func (m *MockBackend) DeleteWorkspace(_ context.Context, name string, force bool) error
- func (m *MockBackend) PrepareConfig(configObj cty.Value) (cty.Value, tfdiags.Diagnostics)
- func (m *MockBackend) StateMgr(_ context.Context, workspace string) (statemgr.Full, error)
- func (m *MockBackend) Workspaces(context.Context) ([]string, error)
Constants ¶
This section is empty.
Variables ¶
var RemovedBackends map[string]string
RemovedBackends is a record of previously supported backends which have since been deprecated and removed.
Functions ¶
func Backend ¶
Backend returns the initialization factory for the given backend, or nil if none exists.
The second return value is the canonical name for the selected backend, if any, which should be used in the UI and in OpenTofu's records of which backend is active in a particular working directory.
func RegisterTemp ¶
RegisterTemp adds a new entry to the table of backends and returns a function that will deregister it once called.
This is essentially a workaround for the fact that the Ghoten CLI layer expects backends to always come from a centrally-maintained table and doesn't have any way to directly pass an anonymous backend implementation.
The given name MUST start with an underscore, to ensure that it cannot collide with any "real" backend. If the given name does not start with an underscore then this function will panic. If we introduce plugin-based backends in future then we might consider reserving part of the plugin address namespace to represent temporary backends for test purposes only, which would then replace this special underscore prefix as the differentiator.
This is intended for unit tests that use MockBackend, or a derivative thereof. A typical usage pattern from a unit test would be:
// ("backendInit" represents _this_ package)
t.Cleanup(backendInit.RegisterTemp("_test", func (enc encryption.StateEncryption) Backend {
return &backendInit.MockBackend{
// (and then whichever mock settings your test case needs)
}
}))
Because this function modifies global state observable throughout the program, any test using this function MUST NOT use t.Parallel. If a single test needs to register multiple temporary backends for some reason then it must select a different name for each one.
func Set ¶
Set sets a new backend in the list of backends. If f is nil then the backend will be removed from the map. If this backend already exists then it will be overwritten.
This method sets this backend globally and care should be taken to do this only before OpenTofu is executing to prevent odd behavior of backends changing mid-execution.
NOTE: Underscore-prefixed named are reserved for unit testing use via the RegisterTemp function. Do not add any underscore-prefixed names using this function.
Types ¶
type MockBackend ¶
type MockBackend struct {
ConfigSchemaFn func() *configschema.Block
ConfigSchemaCalled bool
ConfigureFn func(configObj cty.Value) tfdiags.Diagnostics
ConfigureCalled bool
ConfigureConfigObj cty.Value
DeleteWorkspaceFn func(name string, force bool) error
DeleteWorkspaceCalled bool
DeleteWorkspaceName string
DeleteWorkspaceForce bool
PrepareConfigFn func(configObj cty.Value) (cty.Value, tfdiags.Diagnostics)
PrepareConfigCalled bool
PrepareConfigConfigObj cty.Value
StateMgrFn func(workspace string) (statemgr.Full, error)
StateMgrCalled bool
StateMgrWorkspace string
WorkspacesFn func() ([]string, error)
WorkspacesCalled bool
}
MockBackend is an implementation of Backend that largely just routes incoming calls to a set of closures provided by a caller.
This is included for testing purposes only. Use RegisterTemp to temporarily add a MockBackend instance to the table of available backends from a unit test function. Do not include MockBackend instances in the initial static backend table.
The mock automatically tracks the most recent call to each method for ease of writing assertions in simple cases. If you need more complex tracking such as a log of all calls then you can implement that inside your provided callback functions.
This implementation intentionally covers only the basic Backend interface, and not any extension interfaces like [CLI] and [Enhanced]. Consider embedding this into another type if you need to mock extension interfaces too, since Ghoten backend init uses type assertions to check for extension interfaces and so having this type implement them would prevent its use in testing situations that occur with non-extended backend implementations.
func (*MockBackend) ConfigSchema ¶
func (m *MockBackend) ConfigSchema() *configschema.Block
ConfigSchema implements Backend.
func (*MockBackend) Configure ¶
func (m *MockBackend) Configure(ctx context.Context, configObj cty.Value) tfdiags.Diagnostics
Configure implements Backend.
func (*MockBackend) DeleteWorkspace ¶
DeleteWorkspace implements Backend.
func (*MockBackend) PrepareConfig ¶
func (m *MockBackend) PrepareConfig(configObj cty.Value) (cty.Value, tfdiags.Diagnostics)
PrepareConfig implements Backend.
func (*MockBackend) Workspaces ¶
func (m *MockBackend) Workspaces(context.Context) ([]string, error)
Workspaces implements Backend.