plan

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 6, 2026 License: MIT Imports: 7 Imported by: 0 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

Overview

Package plan parses a `terraform show -json` plan into grouped resource actions.

Index

Constants

This section is empty.

Variables

View Source
var ErrNotAPlan = errors.New("input is not a Terraform plan document: no " +
	`"resource_changes" or "planned_values" key found. Produce it with ` +
	"`terraform plan -out=plan.bin && terraform show -json plan.bin`, not " +
	"`terraform show -json` (which prints state)")

ErrNotAPlan is returned when the input parses as JSON but is not a Terraform plan document.

Order is the display order for actions.

Functions

func Glob added in v1.1.0

func Glob(pattern, s string) bool

Glob reports whether s matches pattern, where `*` matches any sequence of characters and `?` exactly one.

Types

type Action

type Action string

Action is the effective change kind for a resource.

const (
	Create  Action = "create"
	Update  Action = "update"
	Replace Action = "replace"
	Destroy Action = "destroy"
	// Forget is Terraform's `removed` block outcome: the object is dropped from
	// state but left running. Nothing is destroyed, yet the resource stops being
	// managed, so it is reported rather than hidden.
	Forget Action = "forget"
	// Unknown is any action combination this version does not recognise (an
	// empty array, or a verb from a newer Terraform). It is surfaced so the
	// summary can never silently under-report a change it did not understand.
	Unknown Action = "unknown"
	NoOp    Action = "no-op"
	Read    Action = "read"
)

type Policy added in v1.1.0

type Policy struct {
	Protect    []string
	IgnoreRisk []string
}

Policy marks some resources as protected and lets others out of the risk gate. Patterns are globs: `*` matches any run of characters (dots and brackets included) and `?` matches one character; everything else is literal, so `aws_instance.web[0]` needs no escaping.

A pattern applies to a change when it matches the full address (`module.db.aws_db_instance.main`), the resource type (`aws_db_instance`), or the address inside its module (`aws_db_instance.main`).

func (Policy) Validate added in v1.1.0

func (p Policy) Validate() error

Validate rejects empty patterns, which would otherwise match nothing and silently leave a protected resource unprotected.

type ResourceChange

type ResourceChange struct {
	Address string
	Type    string
	Action  Action
	// Reason is Terraform's action_reason code (for example
	// "replace_because_tainted"), empty when the plan gives none.
	Reason string
	// ReplacePaths are the attribute paths that forced a replacement, such as
	// "engine_version" or "ebs_block_device[0].volume_size". They name
	// attributes only; no value is ever read.
	ReplacePaths []string
	// Deposed is set when the change applies to a deposed object (left over
	// from a create_before_destroy replacement) rather than the current one.
	Deposed bool
	// ModuleAddress is the containing module instance (empty in the root
	// module); it lets a policy pattern match the address inside the module.
	ModuleAddress string
	// Protected and RiskIgnored are set by Summary.Apply from a Policy.
	Protected   bool
	RiskIgnored bool
}

ResourceChange is one planned change.

type Stats added in v1.0.0

type Stats struct {
	Create  int  `json:"create"`
	Update  int  `json:"update"`
	Replace int  `json:"replace"`
	Destroy int  `json:"destroy"`
	Forget  int  `json:"forget"`
	Unknown int  `json:"unknown"`
	NoOp    int  `json:"no_op"`
	Read    int  `json:"read"`
	Total   int  `json:"total"`
	HasRisk bool `json:"has_risk"`

	ProtectedDestroy int  `json:"protected_destroy"`
	ProtectedReplace int  `json:"protected_replace"`
	HasCriticalRisk  bool `json:"has_critical_risk"`
}

Stats is the machine-readable form of a Summary, for `--stats-json` and for GitHub Actions step outputs.

type Summary

type Summary struct {
	Changes []ResourceChange
	Counts  map[Action]int
}

Summary is the parsed plan grouped by action.

func Parse

func Parse(data []byte) (Summary, error)

Parse reads plan JSON produced by `terraform show -json plan.bin`.

Input that parses as JSON but carries neither "resource_changes" nor "planned_values" is rejected instead of being reported as an empty plan: a state file, a truncated download, or an unrelated JSON document must never render as "No changes".

func (*Summary) Apply added in v1.1.0

func (s *Summary) Apply(p Policy)

Apply marks the summary's changes according to the policy. Protection wins over --ignore-risk: a resource matched by both stays protected and risky.

func (Summary) HasCriticalRisk added in v1.1.0

func (s Summary) HasCriticalRisk() bool

HasCriticalRisk reports whether the plan destroys or replaces a protected resource.

func (Summary) HasRisk

func (s Summary) HasRisk() bool

HasRisk reports whether the plan destroys, replaces, or forgets anything that a policy has not exempted with --ignore-risk.

func (Summary) ProtectedCount added in v1.1.0

func (s Summary) ProtectedCount(a Action) int

ProtectedCount is the number of protected resources with action a.

func (Summary) RiskCount added in v1.1.0

func (s Summary) RiskCount(a Action) int

RiskCount is the number of changes with action a that still count as risk: the full count minus the changes a policy let out of the gate.

func (Summary) RiskIgnored added in v1.1.0

func (s Summary) RiskIgnored() int

RiskIgnored counts the risky changes a policy let out of the gate.

func (Summary) Stats added in v1.0.0

func (s Summary) Stats() Stats

Stats projects the summary into counts a workflow can gate on.

func (Summary) Total

func (s Summary) Total() int

Total counts the changes that actually modify infrastructure or state (excludes no-op and read).

Jump to

Keyboard shortcuts

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