stateio

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Apr 7, 2026 License: Apache-2.0 Imports: 29 Imported by: 0

Documentation

Index

Constants

View Source
const (
	StorageEngineMemfile  = "memfile"
	StorageEnginePostgres = "postgres"
)

Variables

This section is empty.

Functions

func BuildPostgresDatabaseURL

func BuildPostgresDatabaseURL(config *StateConfig) string

BuildPostgresDatabaseURL constructs a postgres connection URL from config.

func CreateTestTarGz

func CreateTestTarGz(t *testing.T, files map[string][]byte) []byte

CreateTestTarGz creates a valid tar.gz archive with the given file contents. This is exported for use by tests in other packages.

func DownloadRemoteFile

func DownloadRemoteFile(ctx context.Context, filePath string, opts *RemoteDownloadOptions) ([]byte, error)

DownloadRemoteFile downloads a file from a remote storage location. Supports s3://, gcs://, and azureblob:// URL schemes.

func GetDefaultEngineConfigPath

func GetDefaultEngineConfigPath() string

GetDefaultEngineConfigPath returns the default engine config file path.

func GetDefaultStateDir

func GetDefaultStateDir() string

GetDefaultStateDir returns the default state directory based on the OS.

func IsRemoteFile

func IsRemoteFile(filePath string) bool

IsRemoteFile returns true if the file path refers to a remote storage location.

func ParseInstancesJSON

func ParseInstancesJSON(data []byte) ([]state.InstanceState, error)

ParseInstancesJSON parses a JSON array of instances from raw bytes.

func SerializeInstancesJSON

func SerializeInstancesJSON(instances []state.InstanceState) ([]byte, error)

SerializeInstancesJSON serializes instances to a JSON byte array.

func UploadRemoteFile

func UploadRemoteFile(ctx context.Context, filePath string, data []byte, opts *RemoteUploadOptions) error

UploadRemoteFile uploads a file to a remote storage location. Supports s3://, gcs://, and azureblob:// URL schemes.

Types

type ContainerStateExporter

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

ContainerStateExporter implements StateExporter using a state.Container. This works with any backend that implements the state.Container interface.

func NewContainerStateExporter

func NewContainerStateExporter(container state.Container) *ContainerStateExporter

NewContainerStateExporter creates a new exporter that uses the given container.

func (*ContainerStateExporter) ExportInstances

func (e *ContainerStateExporter) ExportInstances(
	ctx context.Context,
	instanceFilters []string,
) ([]state.InstanceState, error)

ExportInstances retrieves instances using the container's batch get operation. If instanceFilters is empty, all instances are exported.

type ContainerStateImporter

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

ContainerStateImporter implements StateImporter using a state.Container. This works with any backend that implements the state.Container interface. It uses the container's SaveBatch method for efficient bulk operations.

func NewContainerStateImporter

func NewContainerStateImporter(container state.Container) *ContainerStateImporter

NewContainerStateImporter creates a new importer that uses the given container.

func (*ContainerStateImporter) ImportInstances

func (i *ContainerStateImporter) ImportInstances(ctx context.Context, instances []state.InstanceState) error

ImportInstances saves instances using the container's batch save operation. This is efficient for both memfile and postgres backends.

type EngineConfig

type EngineConfig struct {
	State StateConfig `json:"state"`
}

EngineConfig represents the deploy engine configuration file structure. Only the state-related fields are parsed.

func LoadEngineConfig

func LoadEngineConfig(configFilePath string) (*EngineConfig, error)

LoadEngineConfig loads and parses a deploy engine configuration file.

type ExportError

type ExportError struct {
	Code    ExportErrorCode
	Message string
	Err     error
}

ExportError represents an error that occurred during export.

func (*ExportError) Error

func (e *ExportError) Error() string

func (*ExportError) Unwrap

func (e *ExportError) Unwrap() error

type ExportErrorCode

type ExportErrorCode string

ExportErrorCode represents the type of export error.

const (
	// ErrCodeExportFailed indicates a general export failure.
	ErrCodeExportFailed ExportErrorCode = "export_failed"
	// ErrCodeInstanceNotFound indicates one or more instances were not found.
	ErrCodeInstanceNotFound ExportErrorCode = "not_found"
	// ErrCodeRemoteUploadFailed indicates a remote upload failed.
	ErrCodeRemoteUploadFailed ExportErrorCode = "remote_upload_failed"
)

type ExportInstancesResult

type ExportInstancesResult struct {
	InstancesCount int
	Data           []byte
}

ExportInstancesResult contains the result of an instances export.

func ExecuteInstancesExport

func ExecuteInstancesExport(
	ctx context.Context,
	exporter StateExporter,
	instanceFilters []string,
) (*ExportInstancesResult, error)

ExecuteInstancesExport performs the instances export using the provided exporter.

type ExportParams

type ExportParams struct {
	// FilePath is the path to the output file (local or remote URL).
	FilePath string
	// InstanceFilters is a list of instance IDs or names to export.
	// If empty, all instances are exported.
	InstanceFilters []string
	// EngineConfig contains the deploy engine configuration.
	// Used to determine the storage backend (memfile or postgres).
	EngineConfig *EngineConfig
	// FileSystem is the filesystem to use for local file operations.
	FileSystem afero.Fs
	// Logger is the logger to use for logging.
	Logger core.Logger
	// RemoteOptions contains options for uploading to remote storage.
	RemoteOptions *RemoteUploadOptions
	// Exporter is an optional StateExporter for export.
	// If not provided, a default exporter will be created based on EngineConfig.
	Exporter StateExporter
}

ExportParams contains the parameters for an export operation.

type ExportResult

type ExportResult struct {
	Success        bool   `json:"success"`
	InstancesCount int    `json:"instancesCount,omitempty"`
	FilePath       string `json:"filePath,omitempty"`
	Message        string `json:"message"`
}

ExportResult contains the result of an export operation.

func Export

func Export(params ExportParams) (*ExportResult, error)

Export performs a state export operation based on the provided parameters. The output is a JSON array of blueprint instances.

type ImportError

type ImportError struct {
	Code    ImportErrorCode
	Message string
	Err     error
}

ImportError represents an error that occurred during import.

func (*ImportError) Error

func (e *ImportError) Error() string

func (*ImportError) Unwrap

func (e *ImportError) Unwrap() error

type ImportErrorCode

type ImportErrorCode string

ImportErrorCode represents the type of import error.

const (
	// ErrCodeInvalidJSON indicates the JSON input is malformed.
	ErrCodeInvalidJSON ImportErrorCode = "invalid_json"
	// ErrCodeFileNotFound indicates the input file was not found.
	ErrCodeFileNotFound ImportErrorCode = "file_not_found"
	// ErrCodeRemoteAccessFail indicates a remote file could not be accessed.
	ErrCodeRemoteAccessFail ImportErrorCode = "remote_access_failed"
)

type ImportInstancesResult

type ImportInstancesResult struct {
	InstancesCount int
}

ImportInstancesResult contains the result of an instances import.

func ExecuteInstancesImport

func ExecuteInstancesImport(
	ctx context.Context,
	importer StateImporter,
	data []byte,
) (*ImportInstancesResult, error)

ExecuteInstancesImport performs the instances import using the provided importer.

type ImportParams

type ImportParams struct {
	// FilePath is the path to the input file (local or remote URL).
	FilePath string
	// EngineConfig contains the deploy engine configuration.
	// Used to determine the storage backend (memfile or postgres).
	EngineConfig *EngineConfig
	// FileSystem is the filesystem to use for operations.
	FileSystem afero.Fs
	// Logger is the logger to use for logging.
	Logger core.Logger
	// FileData contains the raw file data to import.
	// If provided, FilePath is ignored for reading (but may be used for logging).
	FileData []byte
	// RemoteOptions contains options for downloading from remote storage.
	RemoteOptions *RemoteDownloadOptions
	// Importer is an optional StateImporter for import.
	// If not provided, a default importer will be created based on EngineConfig.
	Importer StateImporter
}

ImportParams contains the parameters for an import operation.

type ImportResult

type ImportResult struct {
	Success        bool   `json:"success"`
	InstancesCount int    `json:"instancesCount,omitempty"`
	Message        string `json:"message"`
}

ImportResult contains the result of an import operation.

func Import

func Import(params ImportParams) (*ImportResult, error)

Import performs a state import operation based on the provided parameters. The input file must be a JSON array of blueprint instances.

type RemoteDownloadOptions

type RemoteDownloadOptions struct {
	// S3Endpoint overrides the default S3 endpoint (useful for testing with LocalStack).
	S3Endpoint string
	// S3UsePathStyle enables path-style addressing for S3 (required for LocalStack).
	S3UsePathStyle bool
	// GCSEndpoint overrides the default GCS endpoint (useful for testing with fake-gcs-server).
	GCSEndpoint string
	// AzureConnectionString is the connection string for Azure Blob Storage.
	// If empty, DefaultAzureCredential will be used.
	AzureConnectionString string
}

RemoteDownloadOptions contains options for downloading files from remote storage.

type RemoteUploadOptions

type RemoteUploadOptions struct {
	// S3Endpoint overrides the default S3 endpoint (useful for testing with LocalStack).
	S3Endpoint string
	// S3UsePathStyle enables path-style addressing for S3 (required for LocalStack).
	S3UsePathStyle bool
	// GCSEndpoint overrides the default GCS endpoint (useful for testing with fake-gcs-server).
	GCSEndpoint string
	// AzureConnectionString is the connection string for Azure Blob Storage.
	// If empty, DefaultAzureCredential will be used.
	AzureConnectionString string
}

RemoteUploadOptions contains options for uploading files to remote storage.

type StateConfig

type StateConfig struct {
	StorageEngine               string `json:"storage_engine"`
	MemFileStateDir             string `json:"memfile_state_dir"`
	PostgresUser                string `json:"postgres_user"`
	PostgresPassword            string `json:"postgres_password"`
	PostgresHost                string `json:"postgres_host"`
	PostgresPort                int    `json:"postgres_port"`
	PostgresDatabase            string `json:"postgres_database"`
	PostgresSSLMode             string `json:"postgres_ssl_mode"`
	PostgresPoolMaxConns        int    `json:"postgres_pool_max_conns"`
	PostgresPoolMaxConnLifetime string `json:"postgres_pool_max_conn_lifetime"`
}

StateConfig holds the state-related configuration from an engine config file. This is a subset of the deploy engine's StateConfig, containing only the fields needed for state import/export operations.

type StateExporter

type StateExporter interface {
	// ExportInstances exports instances by their IDs or names.
	// If instanceFilters is empty, exports all instances.
	// Returns the full instance state including child blueprints.
	ExportInstances(ctx context.Context, instanceFilters []string) ([]state.InstanceState, error)
}

StateExporter defines the interface for exporting instance state data. This abstraction allows different storage backends to implement their own export logic with optimal performance characteristics.

type StateImporter

type StateImporter interface {
	// ImportInstances imports a slice of instance states.
	// Implementations should handle batch operations efficiently where possible.
	ImportInstances(ctx context.Context, instances []state.InstanceState) error
}

StateImporter defines the interface for importing instance state data. This abstraction allows different storage backends to implement their own import logic with optimal performance characteristics.

Jump to

Keyboard shortcuts

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