Help us shape the future of Go! Take the 2026 Go Developer Survey

tfstate

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Oct 14, 2026 License: Apache-2.0 Imports: 26 Imported by: 0

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

View Source
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

View Source
var (
	ErrReadOnly    = errors.New("the state is read-only")
	ErrReadOnlyApi = errors.New("the meshStack API is read-only")
)
View Source
var ErrLockReplaced = errors.New("the state has another lock now, so the lock to release is released already")
View Source
var ErrNoLock = errors.New("no lock")
View Source
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

func ModuleDir(command []string) string

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 NoRunOf added in v0.4.0

func NoRunOf(ctx context.Context, raw *client.RawClient, buildingBlockUuid uuid.UUID) error

func WriteBackendOverride added in v0.4.0

func WriteBackendOverride(dir string) (remove func() error, err error)

WriteBackendOverride refuses to replace a file of the same name, which may be the user's own.

Types

type Backups

type Backups string

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

func (l Lock) NoUnfinishedRun(ctx context.Context, raw *client.RawClient) error

NoUnfinishedRun passes a lock that a person holds.

func (Lock) String added in v0.4.0

func (l Lock) String() string

type LockHolder added in v0.4.0

type LockHolder struct {
	// RunUuid is empty for a lock that a person holds.
	RunUuid   string `json:"runUuid"`
	Principal string `json:"principal"`
}

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.

func (*Proxy) Serve

func (p *Proxy) Serve(ctx context.Context, run func(env []string)) (requests int, err error)

func (*Proxy) ServeHTTP

func (p *Proxy) ServeHTTP(w gohttp.ResponseWriter, r *gohttp.Request)

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 NewStore

func NewStore(raw Requester, workspace string, buildingBlock uuid.UUID) Store

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) Delete

func (s Store) Delete(ctx context.Context, lockId string) error

func (Store) Get

func (s Store) Get(ctx context.Context) ([]byte, error)

func (Store) Lock added in v0.4.0

func (s Store) Lock(ctx context.Context, info []byte) error

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

func (s Store) Put(ctx context.Context, state []byte, lockId string) error

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.

func (Store) ReadLock added in v0.4.0

func (s Store) ReadLock(ctx context.Context) (lock Lock, err error)

func (Store) Release added in v0.4.0

func (s Store) Release(ctx context.Context, lock Lock) error

Release releases lock by the ID it was read with, so that a lock taken since stays.

func (Store) Unlock added in v0.4.0

func (s Store) Unlock(ctx context.Context, info []byte) error

Unlock fails with a 423 of meshStack where meshStack holds a lock of another ID than the one in info.

Directories

Path Synopsis
Package apiproxy passes requests on to the meshStack API.
Package apiproxy passes requests on to the meshStack API.

Jump to

Keyboard shortcuts

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