tofu

package
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Aug 10, 2026 License: Apache-2.0 Imports: 22 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ExtractLockID

func ExtractLockID(errMsg string) (string, bool)

ExtractLockID extracts the OpenTofu state lock ID from a lock error message. Returns the lock ID and true if found, or empty string and false if not.

The canonical lock error embeds the ID in a "Lock Info:" block:

Error: Error acquiring the state lock
...
Lock Info:
  ID:        54802a0b-4db5-819a-4f02-2827bdcf02ba
  Path:      ...

We must extract the value from that "ID:" line specifically. A naive substring search for "ID:" matches "RequestID:" in the AWS error preamble first and returns the request ID instead of the lock ID.

func ResetInstance

func ResetInstance()

ResetInstance resets the singleton instance of TofuClient. This is useful for testing to ensure a fresh client is created.

Types

type StateResourceView

type StateResourceView struct {
	Address string                 // full state address, e.g. helm_release.quartz
	Mode    string                 // "managed" or "data"
	Type    string                 // resource type, e.g. helm_release
	Name    string                 // resource name, e.g. quartz
	Values  map[string]interface{} // attribute values with sensitive entries redacted
}

StateResourceView is a redacted, presentation-friendly snapshot of a single resource instance recorded in the OpenTofu state. Sensitive attribute values are replaced with a redaction marker so the view can be safely printed to a terminal or log without spilling secrets (tokens, passwords, helm values, etc.). It is returned by StateShow.

type TfExecLogger

type TfExecLogger interface {
	SetLogPath(string) error // Sets the log file path.
	SetLog(string) error     // Sets the log level.
}

TfExecLogger defines the interface for configuring OpenTofu logging.

type TfOpts

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

TfOpts represents options for configuring an OpenTofu instance.

type TofuApplyOpts

type TofuApplyOpts struct {
	AllowDeferral bool     // Enable OpenTofu deferred actions (-allow-deferral)
	Targets       []string // Restrict the apply to these resource addresses (-target)
}

TofuApplyOpts contains options for Apply operations.

type TofuClient

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

TofuClient is a wrapper for the OpenTofu CLI, providing functionality for managing OpenTofu operations.

func Instance

func Instance(ctx context.Context, cfg config.Settings) *TofuClient

Instance returns a singleton instance of TofuClient. It initializes the client if it has not been created already.

func NewTofuClient

func NewTofuClient(ctx context.Context, cfg config.Settings) (TofuClient, error)

NewTofuClient creates a new TofuClient and ensures the OpenTofu CLI binary is available.

func (*TofuClient) Apply

func (c *TofuClient) Apply(ctx context.Context, stage schema.StageConfig, opts ...TofuApplyOpts) error

Apply applies the OpenTofu configuration for the specified stage. It runs `tofu apply` with the configured input variables.

func (*TofuClient) Cleanup

func (c *TofuClient) Cleanup(ctx context.Context) error

Cleanup removes the downloaded OpenTofu CLI from the file system.

func (*TofuClient) Destroy

func (c *TofuClient) Destroy(ctx context.Context, stage schema.StageConfig) error

Destroy destroys the OpenTofu-managed infrastructure for the specified stage. It runs `tofu destroy` with the configured input variables and targets.

func (*TofuClient) ForceUnlock

func (c *TofuClient) ForceUnlock(ctx context.Context, stage schema.StageConfig, lockID string) error

ForceUnlock attempts to force-unlock a state lock for the specified stage. It extracts the lock ID from the error message and calls `tofu force-unlock`.

func (*TofuClient) Format

func (c *TofuClient) Format(ctx context.Context, stage schema.StageConfig) error

Format formats the OpenTofu configuration files in the specified stage directory. It runs `tofu fmt -recursive`.

func (*TofuClient) Import

func (c *TofuClient) Import(ctx context.Context, stage schema.StageConfig, address string, id string) error

Import brings an existing infrastructure object under OpenTofu management by associating the resource at the given configuration address with its real-world ID. It runs `tofu import <address> <id>` for the specified stage, applying the same stage input variables used by plan/apply so that any provider/config interpolation resolves identically.

func (*TofuClient) Init

func (c *TofuClient) Init(ctx context.Context, stage schema.StageConfig, opts TofuInitOpts) error

Init initializes the OpenTofu working directory for the specified stage. It runs `tofu init -upgrade -reconfigure` with the provided backend configuration options.

func (*TofuClient) Output

func (c *TofuClient) Output(ctx context.Context, stage schema.StageConfig) (map[string][]byte, error)

Output retrieves the OpenTofu output for the specified stage directory. It returns a map of output variable names to their values in JSON format.

It runs on a quiet (stdout-discarded) instance: terraform-exec tees the captured `tofu output -json` to stdout, which would leak sensitive stage outputs (e.g. credentials produced by an earlier stage and consumed as input by a later one) to the terminal/logs. Callers receive the structured map and are responsible for any intentional, redacted display.

func (*TofuClient) Plan

func (c *TofuClient) Plan(ctx context.Context, stage schema.StageConfig) (bool, error)

Plan creates an execution plan for the specified stage. It runs `tofu plan` with the configured input variables and returns whether changes are required.

func (*TofuClient) Refresh

func (c *TofuClient) Refresh(ctx context.Context, stage schema.StageConfig) error

Refresh updates the OpenTofu state for the specified stage. It runs `tofu refresh` with the configured input variables.

func (*TofuClient) StateList

func (c *TofuClient) StateList(ctx context.Context, stage schema.StageConfig, filters ...string) ([]string, error)

StateList returns the addresses of every resource instance recorded in the state for the given stage, walking the root module and all child modules. It is the structured equivalent of `tofu state list` (which terraform-exec does not expose directly) and is derived from `tofu show -json`.

An optional set of case-insensitive substring filters may be supplied; when non-empty, only addresses matching at least one filter are returned.

func (*TofuClient) StateRemove

func (c *TofuClient) StateRemove(ctx context.Context, stage schema.StageConfig, addresses ...string) error

StateRemove removes the named resource instances from the stage state without destroying the underlying infrastructure. It is the equivalent of `tofu state rm <address>...` and is primarily used to drop orphaned resources (e.g. a helm_release pointing at an already-deleted cluster) so that a subsequent destroy/clean can proceed. Each address is removed independently; the first failure is returned after attempting the remainder so a partial batch still makes progress.

func (*TofuClient) StateRemoveOrphanedClusterResources

func (c *TofuClient) StateRemoveOrphanedClusterResources(ctx context.Context, stage schema.StageConfig) (int, error)

StateRemoveOrphanedClusterResources removes only the in-cluster (Helm/Kubernetes) MANAGED resources from a stage's state, leaving AWS-provider resources intact.

This is a safe, surgical state cleanup for mixed stages (e.g. prereqs, core) that hold both cloud and in-cluster resources. When the EKS cluster has already been destroyed, its in-cluster objects are gone but remain recorded in state; OpenTofu can neither refresh nor destroy them because the Kubernetes/Helm providers can no longer reach an API server. Dropping just those orphaned records lets the remaining cloud resources destroy normally and unblocks state backend teardown — without ever orphaning real AWS resources.

It returns the number of resources removed. Data sources are never touched.

func (*TofuClient) StateShow

func (c *TofuClient) StateShow(ctx context.Context, stage schema.StageConfig, addresses ...string) ([]StateResourceView, error)

StateShow returns redacted views of resource instances in the stage state. When addresses are supplied, only resources whose address exactly matches one of them are returned; otherwise every resource is returned. Sensitive values are masked. It is the structured, secret-safe equivalent of `tofu state show <address>`.

func (*TofuClient) Validate

func (c *TofuClient) Validate(ctx context.Context, stage schema.StageConfig) (*tfjson.ValidateOutput, error)

Validate validates the OpenTofu configuration for the specified stage. It runs `tofu validate` and returns the validation output.

func (*TofuClient) Version

func (c *TofuClient) Version(ctx context.Context) (string, error)

Version retrieves the version of the OpenTofu CLI. It runs the `tofu version` command and returns the version string.

type TofuInitOpts

type TofuInitOpts struct {
	BackendConfig []string // The backend configuration options.
}

TofuInitOpts represents options for initializing OpenTofu with backend configuration.

Jump to

Keyboard shortcuts

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