init

package
v1.9.0-beta2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 9, 2024 License: MPL-2.0 Imports: 23 Imported by: 0

Documentation

Overview

Package init contains the list of backends that can be initialized and basic helper functions for initializing those backends.

Index

Constants

This section is empty.

Variables

View Source
var RemovedBackends map[string]string

RemovedBackends is a record of previously supported backends which have since been deprecated and removed.

Functions

func Backend

func Backend(name string) backend.InitFn

Backend returns the initialization factory for the given backend, or nil if none exists.

func Init

func Init(services *disco.Disco)

Init initializes the backends map with all our hardcoded backends.

func RegisterTemp added in v1.8.5

func RegisterTemp(name string, f backend.InitFn) func()

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 OpenTofu 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

func Set(name string, f backend.InitFn)

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 added in v1.8.5

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 OpenTofu 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 added in v1.8.5

func (m *MockBackend) ConfigSchema() *configschema.Block

ConfigSchema implements Backend.

func (*MockBackend) Configure added in v1.8.5

func (m *MockBackend) Configure(configObj cty.Value) tfdiags.Diagnostics

Configure implements Backend.

func (*MockBackend) DeleteWorkspace added in v1.8.5

func (m *MockBackend) DeleteWorkspace(name string, force bool) error

DeleteWorkspace implements Backend.

func (*MockBackend) PrepareConfig added in v1.8.5

func (m *MockBackend) PrepareConfig(configObj cty.Value) (cty.Value, tfdiags.Diagnostics)

PrepareConfig implements Backend.

func (*MockBackend) StateMgr added in v1.8.5

func (m *MockBackend) StateMgr(workspace string) (statemgr.Full, error)

StateMgr implements Backend.

func (*MockBackend) Workspaces added in v1.8.5

func (m *MockBackend) Workspaces() ([]string, error)

Workspaces implements Backend.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL