Documentation
¶
Overview ¶
Package tfstate reads and writes the OpenTofu state meshStack keeps for a building block, and serves it to tofu's http backend.
Index ¶
- Constants
- Variables
- func ModuleDir(command []string) string
- func NoRunOf(ctx context.Context, raw *client.RawClient, buildingBlockUuid uuid.UUID) error
- func WriteBackendOverride(dir string) (remove func() error, err error)
- type Backups
- type Lock
- type LockHolder
- type LockInfo
- type Proxy
- type Requester
- type Store
- func (s Store) Delete(ctx context.Context, lockId string) error
- func (s Store) Get(ctx context.Context) ([]byte, error)
- func (s Store) Lock(ctx context.Context, info []byte) error
- func (s Store) Put(ctx context.Context, state []byte, lockId string) error
- func (s Store) ReadLock(ctx context.Context) (lock Lock, err error)
- func (s Store) Release(ctx context.Context, lock Lock) error
- func (s Store) Unlock(ctx context.Context, info []byte) error
Constants ¶
const BackendOverrideFile = "zz_meshstack_override.tf"
BackendOverrideFile ends in _override.tf, so tofu adds its backend to a module without one, and replaces the backend of a module with one. OpenTofu takes no backend type from the environment (opentofu/opentofu#2058). tofu reads override files in name order, so zz_ makes this one win.
Variables ¶
var ( ErrReadOnly = errors.New("the state is read-only") ErrReadOnlyApi = errors.New("the meshStack API is read-only") )
var ErrLockReplaced = errors.New("the state has another lock now, so the lock to release is released already")
var ErrNoLock = errors.New("no lock")
var ErrNoState = errors.New("no state")
ErrNoState is meshStack's 404, which tofu's http backend takes for a state not written yet.
Functions ¶
func ModuleDir ¶ added in v0.5.0
ModuleDir is where command runs its module: the directory of tofu's or terraform's -chdir, or else the working directory. It must stay in step with extractChdirOption of OpenTofu's internal/command/workdir/chdir.go, which takes -chdir only as -chdir=DIR before the subcommand.
func WriteBackendOverride ¶ added in v0.4.0
WriteBackendOverride refuses to replace a file of the same name, which may be the user's own.
Types ¶
type Lock ¶ added in v0.4.0
type Lock struct {
Info LockInfo `json:"lockInfo"`
Holder LockHolder `json:"holder"`
CreatedOn time.Time `json:"createdOn"`
}
func (Lock) NoUnfinishedRun ¶ added in v0.4.0
NoUnfinishedRun passes a lock that a person holds.
type LockHolder ¶ added in v0.4.0
type LockInfo ¶ added in v0.4.0
type LockInfo struct {
ID string `json:"ID"`
Operation string `json:"Operation"`
Who string `json:"Who"`
}
LockInfo is the part of tofu's lock info that a person needs to judge a lock. The field names are tofu's, which meshStack stores as tofu sent them.
type Proxy ¶
type Proxy struct {
Store Store
Endpoint *url.URL
Api http.AuthorizedClient
// Writable passes tofu's lock calls on to meshStack. A proxy that is not writable answers them
// itself, so that a command that stores nothing never makes a run of the building block wait.
Writable bool
BeforeWrite func(ctx context.Context) error
// Force stores a state that does not follow the stored one, such as the one of a
// tofu state push -force.
Force bool
// Backups keeps a copy of the stored state before each write. Empty keeps none.
Backups Backups
// LockWarning is how long tofu may hold meshStack's lock before the proxy warns that the runs of
// the building block wait for it. Zero warns never.
LockWarning time.Duration
// OnProblem gets the error of each request that the proxy refused, because tofu prints only the
// status code of the response. A 5xx of meshStack, which tofu retries, is logged as a warning
// instead.
OnProblem func(ctx context.Context, err error)
// contains filtered or unexported fields
}
Proxy stands between tofu and meshStack, rather than giving tofu the session's token as TF_HTTP_PASSWORD, which meshfed's TfStateRunTokenBasicAuthFilter accepts. That token can expire in the middle of an apply, and nothing would check tofu's writes against the stored state.
For the same reason it passes every other request on to the meshStack API with the session's token, so that tofu's meshstack provider works with the CLI's login.
type Requester ¶
type Requester interface {
DoRequest(ctx context.Context, method, path string, opts ...http.RequestOption) ([]byte, error)
}
Requester is implemented by client.RawClient.
type Store ¶
type Store struct {
BuildingBlock uuid.UUID
Workspace string
// contains filtered or unexported fields
}
func OpenStore ¶ added in v0.4.0
func OpenStore(ctx context.Context, raw *client.RawClient, workspace string, buildingBlockUuid uuid.UUID) (Store, error)
OpenStore opens the state under the building block's own workspace where workspace is empty, because that is where the runner stores it.
func (Store) Lock ¶ added in v0.4.0
Lock and Unlock take tofu's lock info as tofu sends it. A Lock that meshStack refuses with 423 is an http.Error that carries the lock info of the holder.
func (Store) Put ¶
Put and Delete send the ID of tofu's lock as tofu's http backend does, because meshStack refuses a write without the ID of the lock it holds.