terraform

package
v1.7.1 Latest Latest
Warning

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

Go to latest
Published: Jul 2, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package terraform parses "terraform show -json" plan output and renders it as human-readable, terraform plan-style text.

It is the shared engine behind unum's diff command and the tf-plan-summary GitHub Action. Decode a plan once with Parse; then count changes with Plan.AddCount, Plan.ChangeCount, Plan.DestroyCount and Plan.ReplaceCount, list the changed resources with Plan.Changed, or render the diff body with Plan.RenderDiff.

The package emits plain, uncoloured text and imports only the standard library: a caller adds any heading, footer and colour it wants.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Action

type Action int

Action is the primary change Terraform will apply to a resource. The zero value is ActionNoOp.

const (
	// ActionNoOp means the resource is unchanged.
	ActionNoOp Action = iota
	// ActionCreate means the resource will be created.
	ActionCreate
	// ActionRead means the resource will be read during apply.
	ActionRead
	// ActionUpdate means the resource will be updated in place.
	ActionUpdate
	// ActionDelete means the resource will be destroyed.
	ActionDelete
	// ActionReplace means the resource will be destroyed and recreated.
	ActionReplace
)

func (Action) String

func (a Action) String() string

String returns the lower-case name of the action, such as "create" or "replace".

type Plan

type Plan struct {
	// Changes holds every resource change in the plan, in file order.
	Changes []ResourceChange
}

Plan is a parsed Terraform plan, as returned by Parse.

func Parse

func Parse(data []byte) (*Plan, error)

Parse decodes "terraform show -json" output into a Plan. It returns an error only when the top-level JSON is malformed; individual resources with an unexpected shape degrade gracefully rather than failing the whole parse.

func (*Plan) AddCount

func (p *Plan) AddCount() int

AddCount returns the number of resources to create; it is shorthand for Count(ActionCreate).

func (*Plan) ChangeCount

func (p *Plan) ChangeCount() int

ChangeCount returns the number of resources to update in place; it is shorthand for Count(ActionUpdate).

func (*Plan) Changed

func (p *Plan) Changed() []ResourceChange

Changed returns the resources with a visible change — create, update, delete or replace — in plan order. No-op and read resources are omitted.

func (*Plan) Count

func (p *Plan) Count(action Action) int

Count returns the number of resources whose primary Action is action.

func (*Plan) DestroyCount

func (p *Plan) DestroyCount() int

DestroyCount returns the number of resources to destroy; it is shorthand for Count(ActionDelete).

func (*Plan) HasChanges

func (p *Plan) HasChanges() bool

HasChanges reports whether the plan contains at least one create, update, delete or replace action. It is equivalent to len(Plan.Changed) > 0.

func (*Plan) RenderDiff

func (p *Plan) RenderDiff(opts RenderOptions) string

RenderDiff renders only the per-resource changes as plain, uncoloured terraform plan-style text: per-resource headers, ~/+/- markers, "old -> new" transitions, "(known after apply)" values, nested blocks and "# (N unchanged … hidden)" counts.

It emits no "Terraform will perform…" preamble and no "Plan:" footer, and it applies no colour; a caller composes the heading and footer (using the count accessors such as Plan.AddCount) and colours the output itself. RenderDiff returns the empty string when the plan has no changes. See RenderOptions for layout control.

func (*Plan) ReplaceCount

func (p *Plan) ReplaceCount() int

ReplaceCount returns the number of resources to destroy and recreate; it is shorthand for Count(ActionReplace).

type RenderOptions

type RenderOptions struct {
	// MarkerFirst places each +/-/~ change marker in column 0 instead of an
	// indented gutter, so that a GitHub-flavoured diff code block colours the
	// added and removed lines. The zero value keeps terraform's indentation.
	MarkerFirst bool

	// BangUpdates, in [RenderOptions.MarkerFirst] mode, marks update-in-place
	// lines with "!" instead of "~". A "!" prefix is the context-diff marker for
	// a changed line, which diff highlighters (Pygments, Rouge, GitHub) colour
	// while leaving "~" untouched. It has no effect unless MarkerFirst is set.
	BangUpdates bool
}

RenderOptions configures Plan.RenderDiff. The zero value renders authentic, indented terraform plan output; callers apply their own colour.

type ResourceChange

type ResourceChange struct {
	// Address is the full resource address, such as
	// "module.web.aws_instance.this".
	Address string
	// Type is the resource type, such as "aws_instance".
	Type string
	// Name is the resource name, such as "this".
	Name string
	// Action is the primary change Terraform will apply to the resource.
	Action Action
	// Before is the prior state, or nil when the resource is being created.
	Before map[string]any
	// After is the planned state, or nil when the resource is being destroyed.
	After map[string]any
	// AfterUnknown mirrors the shape of After, holding true where a value is
	// only known after apply.
	AfterUnknown map[string]any
	// BeforeSensitive mirrors the shape of Before, holding true where a value
	// is sensitive.
	BeforeSensitive map[string]any
	// AfterSensitive mirrors the shape of After, holding true where a value is
	// sensitive.
	AfterSensitive map[string]any
}

ResourceChange is a single resource's planned change, as found in the resource_changes array of a plan.

Jump to

Keyboard shortcuts

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