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 )
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 ¶
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 ¶
AddCount returns the number of resources to create; it is shorthand for Count(ActionCreate).
func (*Plan) ChangeCount ¶
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) DestroyCount ¶
DestroyCount returns the number of resources to destroy; it is shorthand for Count(ActionDelete).
func (*Plan) HasChanges ¶
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 ¶
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
// AmberUpdates, in [RenderOptions.MarkerFirst] mode, marks update-in-place
// lines with "!" instead of "~". GitHub's diff highlighter colours "!"
// lines amber but leaves "~" uncoloured, so set this to make updates stand
// out. It has no effect unless MarkerFirst is also set.
AmberUpdates 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.