state

package
v0.14.0-a.2 Latest Latest
Warning

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

Go to latest
Published: Nov 29, 2026 License: MIT Imports: 18 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package state defines the contract a state backend implements.

A backend stores snapshots, advances a current pointer, and arbitrates a per-stack lock. A backend implementation satisfies the Backend interface and joins the fixed set in pkg/backends, where an operator selects it by name. Encryption is a separate concern; see the sibling pkg/sdk/encrypt package.

Index

Constants

View Source
const (
	MaxProjectionDepth = 64
	MaxProjectionNodes = 10000
)
View Source
const (
	// LegacyFormatVersion is the prior snapshot schema version imported via
	// compatibility decoding.
	LegacyFormatVersion = 2

	// CurrentFormatVersion is the schema version this package writes for
	// snapshots. Older versions below LegacyFormatVersion error on read.
	CurrentFormatVersion = 3
)
View Source
const EnvelopeVersion = 1

EnvelopeVersion is the on-disk version of the envelope that wraps a plan or a state snapshot. Bump it when the envelope itself changes; the inner body keeps its own format version, which moves independently.

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 stack yet.

Functions

func CompareProjections

func CompareProjections(planned, actual Projection) error

func EncodeSnapshot

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

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

func Open added in v0.5.0

func Open(
	b []byte,
	expectedPayloadType PayloadType,
	resolveEnc func(*Ref) (encrypt.Encrypter, error),
) ([]byte, error)

Open reads an envelope and returns the decrypted inner body. resolveEnc builds or selects an encrypter from the envelope's ref; it receives nil when the envelope has no encrypter ref.

func Seal added in v0.5.0

func Seal(body []byte, payloadType PayloadType, enc encrypt.Encrypter) ([]byte, error)

Seal encrypts body with enc and wraps the result in an Envelope ready for atomic write. The envelope records enc's description, taken after encrypting so it includes facts resolved on first use, like the kms encrypter's key ARN.

func SortRevisions added in v0.11.0

func SortRevisions(revisions []string) []string

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 returns snapshot revisions from oldest to newest.
	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 stack's lock through Lock and release it through the returned Lock value. Plan never locks or writes state, but backend initialization may create a bucket when bootstrap is configured. ForceUnlock is the escape hatch for a leaked lock.

type BackendType

type BackendType struct {
	Name          string
	Description   string
	Configuration cfg.Registration
	New           func(config any, factory, stack 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 Binding added in v0.8.0

type Binding struct {
	Alias       string `json:"alias"`
	LibraryPath string `json:"library-path,omitempty"`
	Export      string `json:"kind,omitempty"`
}

Binding identifies the implementation selected for an entry or scope.

type Classification

type Classification string
const (
	ClassificationPublic    Classification = "public"
	ClassificationTemporary Classification = "temporary"
)

type Entry

type Entry struct {
	Address   string `json:"address"`
	Category  string `json:"category"`
	Composite bool   `json:"composite"`

	Binding          *Binding `json:"binding,omitempty"`
	SchemaVersion    int      `json:"schema-version,omitempty"`
	SensitiveInputs  []string `json:"sensitive-inputs,omitempty"`
	SensitiveOutputs []string `json:"sensitive-outputs,omitempty"`

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

	Inputs  map[string]any `json:"inputs,omitempty"`
	Outputs map[string]any `json:"outputs,omitempty"`

	// Configuration contains the durable projection of library settings reviewed
	// for this resource. Public fields identify the managed target; temporary
	// fields are marked temporary without storing their payloads.
	Configuration *Projection `json:"configuration,omitempty"`
	DependsOn     []string    `json:"depends-on,omitempty"`
}

Entry describes a resource, data observation, action, or composite call. Category identifies the address namespace. Composite marks a call boundary independently of its category. Binding identifies its implementation.

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

func (*Entry) MarshalJSON added in v0.8.0

func (e *Entry) MarshalJSON() ([]byte, error)

func (*Entry) UnmarshalJSON added in v0.8.0

func (e *Entry) UnmarshalJSON(b []byte) error

type Envelope added in v0.5.0

type Envelope struct {
	EnvelopeVersion int         `json:"envelope-version"`
	PayloadType     PayloadType `json:"payload-type,omitempty"`
	Encrypter       *Ref        `json:"encrypter,omitempty"`
	Ciphertext      []byte      `json:"ciphertext"`
}

Envelope is the on-disk container for a plan or a state snapshot. The envelope is plaintext; its Encrypter ref records which key source sealed the inner body and the non-secret configuration (env-var name, KMS key ARN, region) needed to decrypt it, so a state file found long after the config that wrote it still says how to get back to plaintext. Key material is never on disk; the operator must have it available through the encrypter's own channel.

The envelope is not authenticated, so a reader with its own configuration must not let the file choose the key: state backends decrypt with the encrypter resolved from the stack file and treat the recorded ref as information for operators and error messages.

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 stack. Callers must invoke Unlock; a leaked lock blocks future apply and refresh runs until an operator calls ForceUnlock.

type PayloadType added in v0.8.0

type PayloadType string

PayloadType labels the plaintext body sealed inside an Envelope.

const (
	// PayloadTypePlan marks a sealed plan file body.
	PayloadTypePlan PayloadType = "plan"
	// PayloadTypeState marks a sealed state snapshot body.
	PayloadTypeState PayloadType = "state"
)

type Prerequisite

type Prerequisite struct {
	Address    string `json:"address"`
	View       string `json:"view,omitempty"`
	Generation uint64 `json:"generation,omitempty"`
}

type Projection

type Projection struct {
	Kind          ProjectionKind        `json:"kind"`
	Type          string                `json:"type,omitempty"`
	Value         any                   `json:"value,omitempty"`
	Members       map[string]Projection `json:"members,omitempty"`
	Items         []Projection          `json:"items,omitempty"`
	Prerequisites []Prerequisite        `json:"prerequisites,omitempty"`
	Allowed       []Classification      `json:"allowed,omitempty"`
}

func ProjectPublic

func ProjectPublic(value any) (Projection, error)

func ProjectPublicMap

func ProjectPublicMap(values map[string]any) (*Projection, error)

func (*Projection) Clone

func (p *Projection) Clone() *Projection

func (Projection) ContainsPending

func (p Projection) ContainsPending() bool

func (Projection) ContainsTemporary

func (p Projection) ContainsTemporary() bool

func (Projection) MarshalJSON

func (p Projection) MarshalJSON() ([]byte, error)

func (*Projection) PublicMap

func (p *Projection) PublicMap() (map[string]any, error)

func (Projection) PublicValue

func (p Projection) PublicValue() (any, error)

func (*Projection) UnmarshalJSON

func (p *Projection) UnmarshalJSON(data []byte) error

func (Projection) Validate

func (p Projection) Validate() error

type ProjectionKind

type ProjectionKind string
const (
	ProjectionKnown     ProjectionKind = "known"
	ProjectionObject    ProjectionKind = "object"
	ProjectionList      ProjectionKind = "list"
	ProjectionAbsent    ProjectionKind = "absent"
	ProjectionTemporary ProjectionKind = "temporary"
	ProjectionPending   ProjectionKind = "pending"
)

type Ref added in v0.5.0

type Ref struct {
	Name string         `json:"name"`
	Body map[string]any `json:"body,omitempty"`
}

Ref names an entry in the fixed backend or encrypter registry. Name is the bare state backend or encryption key-source from the stack file; Body is the configuration the resolver decodes against that entry's schema.

A plan file records a Backend ref so apply can reconstruct the same backend without re-reading the stack file, and every envelope records an Encrypter ref naming the key source that sealed it.

type Scope

type Scope struct {
	Address   string      `json:"address"`
	Parent    string      `json:"parent,omitempty"`
	Category  string      `json:"category"`
	Binding   *Binding    `json:"binding"`
	Arguments *Projection `json:"arguments,omitempty"`
}

Scope describes an enclosing composite call scope stored independently from observation entries so surviving child resources retain their scope metadata even when a composite call is unsaved or removed from source.

type Snapshot

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

	ImportedFormatVersion int `json:"-"`
}

Snapshot is the in-memory contents 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 format-3 JSON snapshot or imports a format-2 snapshot through the legacy adapter, restoring integer values as int64.

func NewSnapshot

func NewSnapshot(factory FactoryInfo, stack 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) FindScope

func (s *Snapshot) FindScope(address string) *Scope

FindScope returns the scope at address, or nil.

func (*Snapshot) UpsertScope

func (s *Snapshot) UpsertScope(scope *Scope)

UpsertScope replaces an existing scope with the same address or appends a new scope, keeping scopes sorted by address.

func (*Snapshot) Validate

func (s *Snapshot) Validate() error

Validate checks format version, scope catalog closure, entry categories, required bindings, durable configuration projections, and duplicate addresses.

Jump to

Keyboard shortcuts

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