state

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Sep 14, 2025 License: MIT Imports: 16 Imported by: 0 modules, 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

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Backend

type Backend interface {
	Get(ctx context.Context, key string) ([]byte, error)
	Put(ctx context.Context, key string, data []byte) error
	Delete(ctx context.Context, key string) error
	List(ctx context.Context, prefix string) ([]string, error)
	Lock(ctx context.Context, key string) error
	Unlock(ctx context.Context, key string) error
	ListStates(ctx context.Context) ([]string, error)
	ListStateVersions(ctx context.Context, key string) ([]StateVersion, error)
	GetStateVersion(ctx context.Context, key string, version int) ([]byte, error)
}

Backend interface for state storage backends

func NewLocalBackend

func NewLocalBackend(basePath string) Backend

NewLocalBackend creates a new local file backend

type BackupManager

type BackupManager struct {
	// contains filtered or unexported fields
}

func NewBackupManager

func NewBackupManager(backupDir string) *BackupManager

func (*BackupManager) CreateBackup

func (bm *BackupManager) CreateBackup(backupID string, state interface{}) error

func (*BackupManager) DeleteBackup

func (bm *BackupManager) DeleteBackup(backupID string) error

func (*BackupManager) ListBackups

func (bm *BackupManager) ListBackups() ([]*BackupMetadata, error)

func (*BackupManager) LoadMetadata

func (bm *BackupManager) LoadMetadata() error

func (*BackupManager) RestoreBackup

func (bm *BackupManager) RestoreBackup(backupID string) error

func (*BackupManager) SaveMetadata

func (bm *BackupManager) SaveMetadata() error

func (*BackupManager) SetCompression

func (bm *BackupManager) SetCompression(enabled bool)

func (*BackupManager) SetEncryption

func (bm *BackupManager) SetEncryption(enabled bool, key []byte)

func (*BackupManager) SetMaxBackups

func (bm *BackupManager) SetMaxBackups(max int)

type BackupMetadata

type BackupMetadata struct {
	ID           string            `json:"id"`
	Timestamp    time.Time         `json:"timestamp"`
	Size         int64             `json:"size"`
	Compressed   bool              `json:"compressed"`
	Encrypted    bool              `json:"encrypted"`
	StateVersion int               `json:"state_version"`
	Description  string            `json:"description"`
	Tags         map[string]string `json:"tags"`
}

type BackupOptions

type BackupOptions struct {
	Compress    bool              `json:"compress"`
	Encrypt     bool              `json:"encrypt"`
	Description string            `json:"description"`
	Tags        map[string]string `json:"tags"`
}

type CacheItem

type CacheItem struct {
	State      *TerraformState
	ExpiresAt  time.Time
	AccessedAt time.Time
	Size       int64
}

CacheItem represents a cached state

type CacheStats

type CacheStats struct {
	ItemCount  int       `json:"item_count"`
	TotalSize  int64     `json:"total_size"`
	OldestItem time.Time `json:"oldest_item"`
	NewestItem time.Time `json:"newest_item"`
}

CacheStats contains cache statistics

type CheckObject

type CheckObject struct {
	ObjectAddr string `json:"object_addr"`
	Status     string `json:"status"`
}

CheckObject represents an object in a check result

type CheckResult

type CheckResult struct {
	ObjectKind string        `json:"object_kind"`
	ConfigAddr string        `json:"config_addr"`
	Status     string        `json:"status"`
	Objects    []CheckObject `json:"objects,omitempty"`
}

CheckResult represents a check result in the state

type Instance

type Instance struct {
	SchemaVersion       int                    `json:"schema_version"`
	Attributes          map[string]interface{} `json:"attributes,omitempty"`
	AttributesFlat      map[string]string      `json:"attributes_flat,omitempty"`
	SensitiveAttributes []interface{}          `json:"sensitive_attributes,omitempty"`
	Private             string                 `json:"private,omitempty"`
	Dependencies        []string               `json:"dependencies,omitempty"`
	CreateBeforeDestroy bool                   `json:"create_before_destroy,omitempty"`
	IndexKey            interface{}            `json:"index_key,omitempty"`
	Status              string                 `json:"status,omitempty"`
	StatusReason        string                 `json:"status_reason,omitempty"`
}

Instance represents an instance of a resource

type LocalBackend

type LocalBackend struct {
	// contains filtered or unexported fields
}

LocalBackend implements Backend interface for local file storage

func (*LocalBackend) Delete

func (lb *LocalBackend) Delete(ctx context.Context, key string) error

Delete removes data for a key

func (*LocalBackend) Get

func (lb *LocalBackend) Get(ctx context.Context, key string) ([]byte, error)

Get retrieves data for a key

func (*LocalBackend) GetStateVersion

func (lb *LocalBackend) GetStateVersion(ctx context.Context, key string, version int) ([]byte, error)

GetStateVersion retrieves a specific version of a state

func (*LocalBackend) List

func (lb *LocalBackend) List(ctx context.Context, prefix string) ([]string, error)

List returns keys with a given prefix

func (*LocalBackend) ListStateVersions

func (lb *LocalBackend) ListStateVersions(ctx context.Context, key string) ([]StateVersion, error)

ListStateVersions returns versions of a state

func (*LocalBackend) ListStates

func (lb *LocalBackend) ListStates(ctx context.Context) ([]string, error)

ListStates returns all state keys

func (*LocalBackend) Lock

func (lb *LocalBackend) Lock(ctx context.Context, key string) error

Lock acquires a lock for a key

func (*LocalBackend) Put

func (lb *LocalBackend) Put(ctx context.Context, key string, data []byte) error

Put stores data for a key

func (*LocalBackend) Unlock

func (lb *LocalBackend) Unlock(ctx context.Context, key string) error

Unlock releases a lock for a key

type Module

type Module struct {
	Path      []string               `json:"path"`
	Outputs   map[string]OutputValue `json:"outputs,omitempty"`
	Resources map[string]Resource    `json:"resources,omitempty"`
}

Module represents a module in the state

type OutputValue

type OutputValue struct {
	Value     interface{} `json:"value"`
	Type      interface{} `json:"type,omitempty"`
	Sensitive bool        `json:"sensitive,omitempty"`
}

OutputValue represents an output value in the state

type Parser

type Parser struct {
	// contains filtered or unexported fields
}

Parser handles parsing of Terraform state files

func NewParser

func NewParser() *Parser

NewParser creates a new state parser

func (*Parser) ExtractProviders

func (p *Parser) ExtractProviders(state *TerraformState) []string

ExtractProviders extracts unique providers from the state

func (*Parser) GetResourceByAddress

func (p *Parser) GetResourceByAddress(state *TerraformState, address string) (*Resource, *Instance, error)

GetResourceByAddress returns a resource by its address

func (*Parser) GetResourceCount

func (p *Parser) GetResourceCount(state *TerraformState) int

GetResourceCount returns the total number of resource instances

func (*Parser) GetResourcesByProvider

func (p *Parser) GetResourcesByProvider(state *TerraformState, provider string) []Resource

GetResourcesByProvider returns all resources for a specific provider

func (*Parser) GetResourcesByType

func (p *Parser) GetResourcesByType(state *TerraformState, resourceType string) []Resource

GetResourcesByType returns all resources of a specific type

func (*Parser) MergeStates

func (p *Parser) MergeStates(states ...*TerraformState) (*TerraformState, error)

MergeStates merges multiple state files into one

func (*Parser) Parse

func (p *Parser) Parse(data []byte) (*TerraformState, error)

Parse parses raw state data into a TerraformState structure

func (*Parser) ParseLegacy

func (p *Parser) ParseLegacy(data []byte) (*TerraformState, error)

ParseLegacy parses legacy Terraform state formats (< 0.11)

func (*Parser) ValidateState

func (p *Parser) ValidateState(state *TerraformState) error

ValidateState performs basic validation on the state

type Resource

type Resource struct {
	ID        string     `json:"id,omitempty"`
	Module    string     `json:"module,omitempty"`
	Mode      string     `json:"mode"`
	Type      string     `json:"type"`
	Name      string     `json:"name"`
	Provider  string     `json:"provider"`
	Instances []Instance `json:"instances"`
	DependsOn []string   `json:"depends_on,omitempty"`
	EachMode  string     `json:"each,omitempty"`
}

Resource represents a resource in the state

type Severity

type Severity int

Severity defines the severity of a validation issue

const (
	SeverityWarning Severity = iota
	SeverityError
	SeverityCritical
)

type State

type State = TerraformState

State is an alias for TerraformState

type StateCache

type StateCache struct {
	// contains filtered or unexported fields
}

StateCache provides caching for parsed Terraform states

func NewStateCache

func NewStateCache(ttl time.Duration) *StateCache

NewStateCache creates a new state cache

func (*StateCache) Clear

func (c *StateCache) Clear()

Clear removes all items from the cache

func (*StateCache) Delete

func (c *StateCache) Delete(key string)

Delete removes a state from the cache

func (*StateCache) Get

func (c *StateCache) Get(key string) *TerraformState

Get retrieves a state from the cache

func (*StateCache) GetKeys

func (c *StateCache) GetKeys() []string

GetKeys returns all cache keys

func (*StateCache) GetStats

func (c *StateCache) GetStats() CacheStats

GetStats returns cache statistics

func (*StateCache) Has

func (c *StateCache) Has(key string) bool

Has checks if a key exists in the cache

func (*StateCache) Set

func (c *StateCache) Set(key string, state *TerraformState)

Set stores a state in the cache

func (*StateCache) SetMaxSize

func (c *StateCache) SetMaxSize(size int)

SetMaxSize updates the maximum cache size

func (*StateCache) SetTTL

func (c *StateCache) SetTTL(ttl time.Duration)

SetTTL updates the TTL for the cache

func (*StateCache) Size

func (c *StateCache) Size() int

Size returns the number of items in the cache

func (*StateCache) Touch

func (c *StateCache) Touch(key string)

Touch updates the access time of a cached item

type StateComparison added in v1.1.0

type StateComparison struct {
	AreEqual          bool       `json:"are_equal"`
	SerialDiff        int        `json:"serial_diff"`
	AddedResources    []Resource `json:"added_resources"`
	RemovedResources  []Resource `json:"removed_resources"`
	ModifiedResources []Resource `json:"modified_resources"`
}

StateComparison represents the comparison between two states

type StateDifference

type StateDifference struct {
	Type        string      `json:"type"` // added, removed, modified
	Resource    string      `json:"resource"`
	OldValue    interface{} `json:"old_value,omitempty"`
	NewValue    interface{} `json:"new_value,omitempty"`
	Description string      `json:"description"`
}

StateDifference represents a difference between two states

type StateFile

type StateFile struct {
	*TerraformState
	Path string `json:"path,omitempty"`
}

StateFile represents a Terraform state file

type StateManager

type StateManager struct {
	// contains filtered or unexported fields
}

StateManager manages Terraform state operations

func NewStateManager

func NewStateManager(backend Backend) *StateManager

NewStateManager creates a new state manager

func (*StateManager) CompareStates

func (sm *StateManager) CompareStates(state1, state2 *TerraformState) *StateComparison

CompareStates compares two state objects and returns their differences

func (*StateManager) DeleteState

func (sm *StateManager) DeleteState(ctx context.Context, key string) error

DeleteState removes a state file

func (*StateManager) GetState

func (sm *StateManager) GetState(ctx context.Context, key string) (*TerraformState, error)

GetState retrieves and parses a state file

func (*StateManager) GetStateHistory

func (sm *StateManager) GetStateHistory(ctx context.Context, key string) ([]StateVersion, error)

GetStateHistory retrieves the version history of a state

func (*StateManager) GetStateMetadata

func (sm *StateManager) GetStateMetadata(ctx context.Context, key string) (*StateMetadata, error)

GetStateMetadata retrieves metadata about a state without parsing the full state

func (*StateManager) ImportResource

func (sm *StateManager) ImportResource(ctx context.Context, key string, resource Resource) error

ImportResource adds a new resource to the state

func (*StateManager) ListStateVersions added in v1.1.0

func (sm *StateManager) ListStateVersions(ctx context.Context, key string) ([]StateVersion, error)

ListStateVersions lists all versions of a state

func (*StateManager) ListStates

func (sm *StateManager) ListStates(ctx context.Context) ([]string, error)

ListStates lists all available states

func (*StateManager) MoveResource

func (sm *StateManager) MoveResource(state *TerraformState, fromAddress, toAddress string) error

MoveResource moves a resource within a single state

func (*StateManager) PutState added in v1.1.0

func (sm *StateManager) PutState(ctx context.Context, key string, state *TerraformState) error

PutState saves a state file (alias for UpdateState with proper locking)

func (*StateManager) RefreshState

func (sm *StateManager) RefreshState(ctx context.Context, key string, actualResources map[string]interface{}) error

RefreshState updates the state with actual cloud resource data

func (*StateManager) RemoveResource

func (sm *StateManager) RemoveResource(state *TerraformState, address string) error

RemoveResource removes a resource from a state

func (*StateManager) RestoreStateVersion

func (sm *StateManager) RestoreStateVersion(ctx context.Context, key string, version int) (*TerraformState, error)

RestoreStateVersion restores a specific version and returns the restored state

func (*StateManager) UpdateState

func (sm *StateManager) UpdateState(ctx context.Context, key string, state *TerraformState) error

UpdateState updates a state file

type StateMetadata

type StateMetadata struct {
	Key           string    `json:"key"`
	Version       int       `json:"version"`
	Serial        int       `json:"serial"`
	Lineage       string    `json:"lineage"`
	LastModified  time.Time `json:"last_modified"`
	Size          int64     `json:"size"`
	Checksum      string    `json:"checksum"`
	ResourceCount int       `json:"resource_count"`
}

StateMetadata contains metadata about a state

type StateParser

type StateParser struct{}

StateParser handles parsing of Terraform state files

func NewStateParser

func NewStateParser() *StateParser

NewStateParser creates a new state parser

func (*StateParser) CountResources added in v1.1.0

func (sp *StateParser) CountResources(state *StateFile) map[string]int

CountResources counts resources by type

func (*StateParser) ExtractProviderFromResource added in v1.1.0

func (sp *StateParser) ExtractProviderFromResource(resource Resource) string

ExtractProviderFromResource extracts the provider name from a resource

func (*StateParser) ExtractResourceID added in v1.1.0

func (sp *StateParser) ExtractResourceID(resource Resource) string

ExtractResourceID extracts the ID from a resource

func (*StateParser) FilterResourcesByType added in v1.1.0

func (sp *StateParser) FilterResourcesByType(state *StateFile, resourceType string) []Resource

FilterResourcesByType filters resources by type

func (*StateParser) Parse

func (p *StateParser) Parse(data []byte) (*StateFile, error)

Parse parses Terraform state data

func (*StateParser) ParseFile

func (p *StateParser) ParseFile(path string) (*StateFile, error)

ParseFile parses a Terraform state file

func (*StateParser) ValidateStateVersion added in v1.1.0

func (sp *StateParser) ValidateStateVersion(state *StateFile) error

ValidateStateVersion validates the state version

type StateVersion

type StateVersion struct {
	Version   int       `json:"version"`
	Serial    int       `json:"serial"`
	Timestamp time.Time `json:"timestamp"`
	Checksum  string    `json:"checksum"`
}

StateVersion represents a version of a state file

type TerraformState

type TerraformState struct {
	Version          int                    `json:"version"`
	TerraformVersion string                 `json:"terraform_version"`
	Serial           int                    `json:"serial"`
	Lineage          string                 `json:"lineage"`
	Outputs          map[string]OutputValue `json:"outputs,omitempty"`
	Resources        []Resource             `json:"resources"`
	CheckResults     []CheckResult          `json:"check_results,omitempty"`
	Modules          []Module               `json:"modules,omitempty"`
}

TerraformState represents a complete Terraform state file

type ValidationError

type ValidationError struct {
	Rule     string   `json:"rule"`
	Message  string   `json:"message"`
	Severity Severity `json:"severity"`
	Resource string   `json:"resource,omitempty"`
	Field    string   `json:"field,omitempty"`
}

ValidationError represents a validation error

type ValidationResult

type ValidationResult struct {
	Valid    bool
	Errors   []ValidationError
	Warnings []ValidationError
}

ValidationResult contains the results of validation

type ValidationRule

type ValidationRule struct {
	Name        string
	Description string
	Validate    func(*TerraformState) error
	Severity    Severity
}

ValidationRule defines a validation rule

type Validator

type Validator struct {
	// contains filtered or unexported fields
}

Validator validates Terraform state files

func NewValidator

func NewValidator() *Validator

NewValidator creates a new state validator

func (*Validator) AddRule

func (v *Validator) AddRule(rule ValidationRule)

AddRule adds a custom validation rule

func (*Validator) ClearRules

func (v *Validator) ClearRules()

ClearRules removes all validation rules

func (*Validator) GetRules

func (v *Validator) GetRules() []ValidationRule

GetRules returns all validation rules

func (*Validator) RemoveRule

func (v *Validator) RemoveRule(name string)

RemoveRule removes a validation rule by name

func (*Validator) SetStrictMode

func (v *Validator) SetStrictMode(strict bool)

SetStrictMode enables or disables strict validation mode

func (*Validator) Validate

func (v *Validator) Validate(state *TerraformState) error

Validate validates a Terraform state

func (*Validator) ValidateJSON

func (v *Validator) ValidateJSON(data []byte) error

ValidateJSON validates that state data is valid JSON

func (*Validator) ValidateResourceAddress

func (v *Validator) ValidateResourceAddress(address string) error

ValidateResourceAddress validates a resource address format

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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