state

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: May 30, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package state defines the contract a state backend implements.

A backend stores snapshots, advances a current pointer, and arbitrates a per-deployment lock. Provider libraries import this package, satisfy the Backend interface, and register their implementations under runtime.Library.StateBackends. Encryption is a separate concern; see the sibling pkg/sdk/encrypt package.

Index

Constants

View Source
const CurrentFormatVersion = 1

CurrentFormatVersion is the schema version this package reads and writes for snapshots. Older versions error on read.

Variables

View Source
var ErrNoCurrent = errors.New("no current snapshot")

ErrNoCurrent is returned by Backend.Current and Backend.CurrentRev when no snapshot has been written for the deployment yet.

Functions

func EncodeSnapshot

func EncodeSnapshot(s *Snapshot) ([]byte, error)

EncodeSnapshot serializes s as pretty-printed JSON with a trailing newline. Map keys are sorted by encoding/json so two encodes of the same snapshot produce identical bytes.

Types

type Backend

type Backend interface {
	Stack() string
	Current() (*Snapshot, error)
	CurrentRev() (string, error)
	Get(rev string) (*Snapshot, error)
	Write(snap *Snapshot) (string, error)
	SetCurrent(rev string) error
	List() ([]string, error)
	Delete(rev string) error
	Lock(ctx context.Context) (Lock, error)
	ForceUnlock() error
}

Backend is the contract a state backend satisfies. The runtime reads and writes snapshots through it; concrete implementations decide where the bytes live. Apply and refresh acquire the deployment's lock through Lock and release it through the returned Lock value. Plan is read-only and never locks. ForceUnlock is the escape hatch for a leaked lock.

type BackendType

type BackendType struct {
	Name          string
	Description   string
	Configuration *cfg.ConfigurationType
	New           func(config any, stack, deploymentID string, enc encrypt.Encrypter) (Backend, error)
}

BackendType registers a state backend a provider library ships. Configuration describes the schema for the `state:` block fields the operator writes (e.g., path for the local backend, bucket and region for an S3 backend). New is the factory the runtime invokes once it has decoded the configuration against that schema; it returns a ready-to-use Backend.

type Entry

type Entry struct {
	Address string    `json:"address"`
	Type    EntryType `json:"type"`

	Kind             string   `json:"kind,omitempty"`
	SchemaVersion    int      `json:"schema-version,omitempty"`
	SensitiveInputs  []string `json:"sensitive-inputs,omitempty"`
	SensitiveOutputs []string `json:"sensitive-outputs,omitempty"`

	Library     string `json:"library,omitempty"`
	LibraryType string `json:"library-type,omitempty"`

	// Configuration names the library configuration the resource was
	// created against, as "<alias>.<configuration>". It is recorded only
	// when that differs from the import's own default, since destroy and
	// refresh need it to find the right credentials once the resource is
	// no longer described in source.
	Configuration string `json:"configuration,omitempty"`

	TriggerHash string `json:"trigger-hash,omitempty"`

	Inputs    map[string]any `json:"inputs,omitempty"`
	Outputs   map[string]any `json:"outputs,omitempty"`
	DependsOn []string       `json:"depends-on,omitempty"`
}

Entry is one record in a snapshot. Type discriminates the fields used: leaf entries hold a primitive resource's Kind, SchemaVersion, Inputs, and Outputs; library-call entries hold a composite type's Library, LibraryType, and call-site Inputs/Outputs.

SensitiveInputs and SensitiveOutputs name the kebab-case fields whose values came from a sensitive source. Renderers mask the matching entries when printing.

type EntryType

type EntryType string

EntryType discriminates the three records a snapshot can hold.

const (
	EntryLeaf        EntryType = "leaf"
	EntryLibraryCall EntryType = "library-call"
	EntryAction      EntryType = "action"
)

type FactoryInfo

type FactoryInfo struct {
	Name            string `json:"name"`
	Version         string `json:"version"`
	ContentRevision string `json:"content-revision"`
}

FactoryInfo identifies the stack a snapshot belongs to. ContentRevision is the content-addressable hash the binary was compiled with.

type Lock

type Lock interface {
	Unlock() error
}

Lock is a held exclusion on one deployment. Callers must invoke Unlock; a leaked lock blocks future apply and refresh runs until an operator calls ForceUnlock.

type Snapshot

type Snapshot struct {
	FormatVersion int            `json:"format-version"`
	Factory       FactoryInfo    `json:"stack"`
	Stack         string         `json:"deployment-id"`
	GeneratedAt   time.Time      `json:"generated-at"`
	Entries       []*Entry       `json:"entries"`
	Outputs       map[string]any `json:"outputs,omitempty"`
}

Snapshot is the in-memory record of one state file. The runtime reads the current snapshot at the start of plan or apply, and writes a fresh one after each successful resource action.

func DecodeSnapshot

func DecodeSnapshot(b []byte) (*Snapshot, error)

DecodeSnapshot parses a snapshot from JSON bytes.

func NewSnapshot

func NewSnapshot(stack FactoryInfo, deploymentID string) *Snapshot

NewSnapshot returns an empty snapshot at the current schema version.

func (*Snapshot) Find

func (s *Snapshot) Find(address string) *Entry

Find returns the entry at address, or nil.

func (*Snapshot) Validate

func (s *Snapshot) Validate() error

Validate checks every entry's discriminator and required fields, and rejects duplicate addresses within a snapshot.

Jump to

Keyboard shortcuts

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