terraform

package
v0.10.0 Latest Latest
Warning

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

Go to latest
Published: Nov 23, 2026 License: MPL-2.0 Imports: 21 Imported by: ? 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 DefaultTerraformProvider added in v0.10.0

type DefaultTerraformProvider struct {
	Shims *Shims // Exported for testing
	// contains filtered or unexported fields
}

DefaultTerraformProvider provides all terraform-specific operations with session caching.

func (*DefaultTerraformProvider) CacheOutputs added in v0.10.0

func (p *DefaultTerraformProvider) CacheOutputs(componentID string) error

CacheOutputs proactively fetches and caches all outputs for a component. This is called after terraform apply to warm the cache, so subsequent terraform_output() calls for this component will hit the cache instead of running terraform output again.

func (*DefaultTerraformProvider) ClearCache added in v0.10.0

func (p *DefaultTerraformProvider) ClearCache()

ClearCache clears the session cache for all components and the config scope. This invalidates cached Terraform components, output values, and scope used for input evaluation, forcing them to be reloaded from the blueprint and re-set by the next composition. This is useful when the blueprint has been modified or when outputs need to be refreshed.

func (*DefaultTerraformProvider) FindRelativeProjectPath added in v0.10.0

func (p *DefaultTerraformProvider) FindRelativeProjectPath(directory ...string) (string, error)

FindRelativeProjectPath returns the relative path to a Terraform project from the current working directory or the specified directory. It locates Terraform files (*.tf) and resolves the path relative to "terraform" or "contexts" directory markers in the path structure. Returns an empty string if no Terraform files are found, or an error if file system inspection fails.

func (*DefaultTerraformProvider) FormatArgsForEnv added in v0.10.0

func (p *DefaultTerraformProvider) FormatArgsForEnv(args []string) string

FormatArgsForEnv formats CLI arguments for use in environment variables. It adds quotes around file paths and values that need them for shell compatibility, ensuring that paths with spaces or special characters are properly escaped. Handles Unix absolute paths (/path), relative paths (./path), and Windows drive letter paths (both D:\path and D:/path formats). This is necessary because Terraform CLI arguments passed via TF_CLI_ARGS_* environment variables must be properly quoted to work correctly across different shell environments.

func (*DefaultTerraformProvider) GenerateBackendOverride added in v0.10.0

func (p *DefaultTerraformProvider) GenerateBackendOverride(directory string) error

GenerateBackendOverride writes or removes the backend_override.tf file in directory for the configured backend. For the none backend, it removes the file. For local, s3, kubernetes, azurerm, and gcs, it writes the backend block. Under <project>/terraform/, it also adds the file to terraform/.gitignore. A failure there is only a warning. An unsupported backend type returns an error with the valid types as the hint.

func (*DefaultTerraformProvider) GenerateTerraformArgs added in v0.10.0

func (p *DefaultTerraformProvider) GenerateTerraformArgs(componentID string, interactive bool) (*TerraformArgs, error)

GenerateTerraformArgs returns the Terraform arguments of a component for the configured backend. It finds the var files, adds the -backend-config arguments, and builds the arguments of each subcommand. It adds -lock-timeout to init and to each subcommand that changes state. The default is 5m. It checks terraform.lock.timeout first, so a typo gets a Windsor error, not a Terraform parse failure. The init arguments hold only backend config. Each caller of runTerraformInit adds its own policy flags.

func (*DefaultTerraformProvider) GetEnvVars added in v0.10.0

func (p *DefaultTerraformProvider) GetEnvVars(componentID string, interactive bool) (map[string]string, []string, *TerraformArgs, error)

GetEnvVars returns the environment variables for a Terraform run of componentID. The set holds the base variables, the contexts/<context>/terraform/.env content, and a TF_VAR_* per input. Outputs of other components only evaluate inputs. They are not emitted as separate variables. An input that resolves to nil is skipped, so Terraform uses its declared default and not "null". It also returns the .env key names, for callers that filter or track them, and the TerraformArgs.

func (*DefaultTerraformProvider) GetStatePath added in v0.10.0

func (p *DefaultTerraformProvider) GetStatePath(componentID string) (string, error)

GetStatePath returns the path to the Terraform state file for the specified component ID. The state file is stored in .tfstate/<componentID>/terraform.tfstate within the Windsor scratch path. If a backend prefix is configured, it is included in the path to support multi-tenant or multi-environment deployments where state files need to be namespaced.

func (*DefaultTerraformProvider) GetTFDataDir added in v0.10.0

func (p *DefaultTerraformProvider) GetTFDataDir(componentID string) (string, error)

GetTFDataDir calculates the TF_DATA_DIR path for a given component ID. It resolves the component ID to the actual component ID (using GetID if component exists), then constructs the path as ${windsorScratchPath}/.terraform/${componentID}. This path is used by Terraform to store its working directory data and state. Returns the path with forward slashes or an error if scratch path lookup fails.

func (*DefaultTerraformProvider) GetTerraformComponent added in v0.10.0

func (p *DefaultTerraformProvider) GetTerraformComponent(componentID string) *blueprintv1alpha1.TerraformComponent

GetTerraformComponent finds a terraform component by its path or name from the loaded blueprint components. It searches through all Terraform components and matches against both the Path and Name fields. Returns the component if found, or nil if not found.

func (*DefaultTerraformProvider) GetTerraformComponentForPath added in v0.10.0

func (p *DefaultTerraformProvider) GetTerraformComponentForPath(directory string) *blueprintv1alpha1.TerraformComponent

GetTerraformComponentForPath finds the loaded blueprint component whose resolved FullPath matches directory. Terraform module directories are shared on disk across contexts, so this confirms directory actually belongs to the active context's blueprint before a caller treats it as one of its components. Returns nil if no component resolves to directory.

func (*DefaultTerraformProvider) GetTerraformComponents added in v0.10.0

func (p *DefaultTerraformProvider) GetTerraformComponents() []blueprintv1alpha1.TerraformComponent

GetTerraformComponents loads and parses Terraform components from blueprint.yaml. It retrieves the blueprint configuration root, reads the blueprint.yaml file, and computes the FullPath for each Terraform component based on the presence of a Source field and the resolved project root. Returns all TerraformComponent structs from blueprint.yaml with FullPath fields set. Components are cached after first load to avoid repeated file I/O operations. If components have been set via SetTerraformComponents, those are returned instead.

func (*DefaultTerraformProvider) GetTerraformOutputs added in v0.10.0

func (p *DefaultTerraformProvider) GetTerraformOutputs(componentID string) (map[string]any, error)

GetTerraformOutputs returns the output values of a component from 'terraform output -json'. It prepares TF_DATA_DIR and the backend override, and it runs init and retries once when output fails. It returns an empty map and no error when the component has no outputs, so the ?? operator can fall back. It returns an error only when the JSON does not parse, which shows corrupt state. It runs the command on the capture path, so --verbose never prints sensitive outputs.

func (*DefaultTerraformProvider) IsInTerraformProject added in v0.10.0

func (p *DefaultTerraformProvider) IsInTerraformProject() bool

IsInTerraformProject checks if the current working directory is within a Terraform project. A Terraform project is identified by the presence of .tf files in the current directory or parent directories. Returns true if .tf files are found and a valid project path can be resolved, false otherwise.

func (*DefaultTerraformProvider) ListLocalStateComponentIDs added in v0.10.0

func (p *DefaultTerraformProvider) ListLocalStateComponentIDs() ([]string, error)

ListLocalStateComponentIDs returns each component ID that has a local Terraform state file. It includes IDs that the active blueprint no longer declares. The bootstrap recovery sweep uses it to find state left under the old ID of a renamed component. It walks the directory tree that GetStatePath writes into. A missing .tfstate root is not an error, because no local apply has run.

func (*DefaultTerraformProvider) SetConfigScope added in v0.10.0

func (p *DefaultTerraformProvider) SetConfigScope(scope map[string]any)

SetConfigScope sets the config scope (facet config blocks) used when evaluating component inputs in GetEnvVars, so expressions like docker_desktop_common_patches.patchVars.common_config_patches resolve.

func (*DefaultTerraformProvider) SetTerraformComponents added in v0.10.0

func (p *DefaultTerraformProvider) SetTerraformComponents(components []blueprintv1alpha1.TerraformComponent)

SetTerraformComponents sets the terraform components directly, bypassing file loading. This allows the provider to use in-memory components with inputs preserved from blueprint composition. Components set via this method take precedence over components loaded from blueprint.yaml.

func (*DefaultTerraformProvider) SharedBackendContexts added in v0.10.0

func (p *DefaultTerraformProvider) SharedBackendContexts() []string

SharedBackendContexts returns warnings for contexts that share the backend of the active context and write the same state keys. It returns nothing when terraform.backend.auto_prefix is on, or the backend is local or none. A context matches when it has the same backend type, the same target (bucket, storage account and container, or namespace), the same prefix, and auto_prefix off. A target that comes from backend.tfvars or a facet is not visible here, so the check can miss a match.

type MockTerraformProvider

type MockTerraformProvider struct {
	FindRelativeProjectPathFunc      func(directory ...string) (string, error)
	IsInTerraformProjectFunc         func() bool
	GenerateBackendOverrideFunc      func(directory string) error
	GenerateTerraformArgsFunc        func(componentID string, interactive bool) (*TerraformArgs, error)
	GetTerraformComponentFunc        func(componentID string) *blueprintv1alpha1.TerraformComponent
	GetTerraformComponentsFunc       func() []blueprintv1alpha1.TerraformComponent
	GetTerraformComponentForPathFunc func(directory string) *blueprintv1alpha1.TerraformComponent
	SetTerraformComponentsFunc       func(components []blueprintv1alpha1.TerraformComponent)
	SetConfigScopeFunc               func(scope map[string]any)
	GetTerraformOutputsFunc          func(componentID string) (map[string]any, error)
	CacheOutputsFunc                 func(componentID string) error
	GetTFDataDirFunc                 func(componentID string) (string, error)
	GetStatePathFunc                 func(componentID string) (string, error)
	ListLocalStateComponentIDsFunc   func() ([]string, error)
	GetEnvVarsFunc                   func(componentID string, interactive bool) (map[string]string, []string, *TerraformArgs, error)
	FormatArgsForEnvFunc             func(args []string) string
	SharedBackendContextsFunc        func() []string
	ClearCacheFunc                   func()
}

MockTerraformProvider is a mock implementation of TerraformProvider for testing.

func (*MockTerraformProvider) CacheOutputs

func (m *MockTerraformProvider) CacheOutputs(componentID string) error

CacheOutputs implements TerraformProvider.

func (*MockTerraformProvider) ClearCache

func (m *MockTerraformProvider) ClearCache()

ClearCache implements TerraformProvider.

func (*MockTerraformProvider) FindRelativeProjectPath

func (m *MockTerraformProvider) FindRelativeProjectPath(directory ...string) (string, error)

FindRelativeProjectPath implements TerraformProvider.

func (*MockTerraformProvider) FormatArgsForEnv

func (m *MockTerraformProvider) FormatArgsForEnv(args []string) string

FormatArgsForEnv implements TerraformProvider.

func (*MockTerraformProvider) GenerateBackendOverride

func (m *MockTerraformProvider) GenerateBackendOverride(directory string) error

GenerateBackendOverride implements TerraformProvider.

func (*MockTerraformProvider) GenerateTerraformArgs

func (m *MockTerraformProvider) GenerateTerraformArgs(componentID string, interactive bool) (*TerraformArgs, error)

GenerateTerraformArgs implements TerraformProvider.

func (*MockTerraformProvider) GetEnvVars

func (m *MockTerraformProvider) GetEnvVars(componentID string, interactive bool) (map[string]string, []string, *TerraformArgs, error)

GetEnvVars implements TerraformProvider.

func (*MockTerraformProvider) GetStatePath

func (m *MockTerraformProvider) GetStatePath(componentID string) (string, error)

GetStatePath implements TerraformProvider.

func (*MockTerraformProvider) GetTFDataDir

func (m *MockTerraformProvider) GetTFDataDir(componentID string) (string, error)

GetTFDataDir implements TerraformProvider.

func (*MockTerraformProvider) GetTerraformComponent

func (m *MockTerraformProvider) GetTerraformComponent(componentID string) *blueprintv1alpha1.TerraformComponent

GetTerraformComponent implements TerraformProvider.

func (*MockTerraformProvider) GetTerraformComponentForPath added in v0.9.1

func (m *MockTerraformProvider) GetTerraformComponentForPath(directory string) *blueprintv1alpha1.TerraformComponent

GetTerraformComponentForPath implements TerraformProvider.

func (*MockTerraformProvider) GetTerraformComponents

func (m *MockTerraformProvider) GetTerraformComponents() []blueprintv1alpha1.TerraformComponent

GetTerraformComponents implements TerraformProvider.

func (*MockTerraformProvider) GetTerraformOutputs

func (m *MockTerraformProvider) GetTerraformOutputs(componentID string) (map[string]any, error)

GetTerraformOutputs implements TerraformProvider.

func (*MockTerraformProvider) IsInTerraformProject

func (m *MockTerraformProvider) IsInTerraformProject() bool

IsInTerraformProject implements TerraformProvider.

func (*MockTerraformProvider) ListLocalStateComponentIDs added in v0.9.1

func (m *MockTerraformProvider) ListLocalStateComponentIDs() ([]string, error)

ListLocalStateComponentIDs implements TerraformProvider.

func (*MockTerraformProvider) SetConfigScope

func (m *MockTerraformProvider) SetConfigScope(scope map[string]any)

SetConfigScope implements TerraformProvider.

func (*MockTerraformProvider) SetTerraformComponents

func (m *MockTerraformProvider) SetTerraformComponents(components []blueprintv1alpha1.TerraformComponent)

SetTerraformComponents implements TerraformProvider.

func (*MockTerraformProvider) SharedBackendContexts added in v0.10.0

func (m *MockTerraformProvider) SharedBackendContexts() []string

SharedBackendContexts implements TerraformProvider.

type ModuleSource added in v0.10.0

type ModuleSource struct {
	Name    string
	Source  string
	Literal bool
}

ModuleSource is the source of one module block. Literal is false when the source is not a literal string, and then Source is empty.

func ParseModuleSources added in v0.10.0

func ParseModuleSources(filename string, content []byte) ([]ModuleSource, error)

ParseModuleSources returns the source of each module block in the content of a Terraform file. A module block with no source gives no entry. It returns an error for content that does not parse.

type Shims

type Shims struct {
	shims.Shims
	Unsetenv func(key string) error
}

Shims holds the shared system call primitives and the terraform-specific shims.

func NewShims

func NewShims() *Shims

NewShims returns a Shims with every field set to its real implementation.

type TerraformArgs

type TerraformArgs struct {
	TFDataDir           string
	InitArgs            []string
	PlanArgs            []string
	ApplyArgs           []string
	RefreshArgs         []string
	ImportArgs          []string
	DestroyArgs         []string
	PlanDestroyArgs     []string
	BackendConfig       string
	KubernetesNamespace string
	LegacyKeyField      string
	LegacyKeyValue      string
}

TerraformArgs contains all the CLI arguments needed for terraform operations. It does not include environment variable formatting - that is handled by the env printer. KubernetesNamespace is the namespace the config sets for the kubernetes backend, and empty when none is set. LegacyKeyField and LegacyKeyValue name the backend key without the auto-prefix segment. Both are empty when terraform.backend.auto_prefix is off.

type TerraformProvider

type TerraformProvider interface {
	FindRelativeProjectPath(directory ...string) (string, error)
	IsInTerraformProject() bool
	GenerateBackendOverride(directory string) error
	GenerateTerraformArgs(componentID string, interactive bool) (*TerraformArgs, error)
	GetTerraformComponent(componentID string) *blueprintv1alpha1.TerraformComponent
	GetTerraformComponents() []blueprintv1alpha1.TerraformComponent
	GetTerraformComponentForPath(directory string) *blueprintv1alpha1.TerraformComponent
	SetTerraformComponents(components []blueprintv1alpha1.TerraformComponent)
	SetConfigScope(scope map[string]any)
	GetTerraformOutputs(componentID string) (map[string]any, error)
	CacheOutputs(componentID string) error
	GetTFDataDir(componentID string) (string, error)
	GetStatePath(componentID string) (string, error)
	ListLocalStateComponentIDs() ([]string, error)
	GetEnvVars(componentID string, interactive bool) (map[string]string, []string, *TerraformArgs, error)
	FormatArgsForEnv(args []string) string
	SharedBackendContexts() []string
	ClearCache()
}

TerraformProvider defines the interface for Terraform operations

func NewTerraformProvider

func NewTerraformProvider(
	configHandler config.ConfigHandler,
	shell shell.Shell,
	toolsManager tools.ToolsManager,
	evaluator evaluator.ExpressionEvaluator,
) TerraformProvider

NewTerraformProvider creates a new TerraformProvider instance and registers its helper functions with the evaluator. Panics if configHandler, shell, toolsManager, or evaluator are nil.

Jump to

Keyboard shortcuts

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