objchange

package
v0.34.1 Latest Latest
Warning

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

Go to latest
Published: Nov 12, 2026 License: MPL-2.0 Imports: 10 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 objchange deals with the business logic of taking a prior state value and a config value and producing a proposed new merged value, along with other related rules in this domain.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AssertObjectCompatible

func AssertObjectCompatible(schema *configschema.Block, planned, actual cty.Value) []error

AssertObjectCompatible checks whether the given "actual" value is a valid completion of the possibly-partially-unknown "planned" value.

This means that any known leaf value in "planned" must be equal to the corresponding value in "actual", and various other similar constraints.

Any inconsistencies are reported by returning a non-zero number of errors. These errors are usually (but not necessarily) cty.PathError values referring to a particular nested value within the "actual" value.

The two values must have types that conform to the given schema's implied type, or this function will panic.

func AssertPlanValid

func AssertPlanValid(schema *configschema.Block, priorState, config, plannedState cty.Value) []error

AssertPlanValid checks checks whether a planned new state returned by a provider's PlanResourceChange method is suitable to achieve a change from priorState to config. It returns a slice with nonzero length if any problems are detected. Because problems here indicate bugs in the provider that generated the plannedState, they are written with provider developers as an audience, rather than end-users.

All of the given values must have the same type and must conform to the implied type of the given schema, or this function may panic or produce garbage results.

During planning, a provider may only make changes to attributes that are null (unset) in the configuration and are marked as "computed" in the resource type schema, in order to insert any default values the provider may know about. If the default value cannot be determined until apply time, the provider can return an unknown value. Providers are forbidden from planning a change that disagrees with any non-null argument in the configuration.

As a special exception, providers _are_ allowed to provide attribute values conflicting with configuration if and only if the planned value exactly matches the corresponding attribute value in the prior state. The provider can use this to signal that the new value is functionally equivalent to the old and thus no change is required.

func LongestCommonSubsequence

func LongestCommonSubsequence[V any](xs, ys []V, equals func(x, y V) bool) []V

LongestCommonSubsequence finds a sequence of values that are common to both x and y, with the same relative ordering as in both collections. This result is useful as a first step towards computing a diff showing added/removed elements in a sequence.

The approached used here is a "naive" one, assuming that both xs and ys will generally be small in most reasonable OpenTofu configurations. For larger lists the time/space usage may be sub-optimal.

A pair of lists may have multiple longest common subsequences. In that case, the one selected by this function is undefined.

func MarkChanges added in v0.34.0

func MarkChanges(schema *configschema.Block, before, after cty.Value, marker Marker) cty.Value

MarkChanges returns a value with the same content as "after" except that it has additional marks on any part of the value that differs from "before".

The "marker" function is given the path of the discovered change and can use that during the mark construction process. NOTE: For performance reasons the slice underlying the path may be re-used for subsequent calls and should not stored directly, unless path.Copy() is explicitly called in the marking function.

The given values must both conform to the given schema and must be consistent with the rules enforced by AssertPlanValid, or the behavior is unspecified.

func MarkPendingChanges added in v0.34.0

func MarkPendingChanges(schema *configschema.Block, prior, planned cty.Value, instAddr addrs.AbsResourceInstance) cty.Value

MarkPendingChanges returns a value with the same content as "planned" except that it has additional PendingChange marks on any part of the value that differs from "prior".

Use this with the prior and planned values for a resource instance to produce a value that can be used for downstream analysis of what else in the configuration is derived from changes that have not been applied yet.

The given values must both conform to the given schema and must be consistent with the rules enforced by AssertPlanValid, or the behavior is unspecified.

func MarkUnknownValuesAsPending added in v0.34.0

func MarkUnknownValuesAsPending(v cty.Value, instAddr addrs.AbsResourceInstance) cty.Value

MarkUnknownValuesAsPending returns a value that is equivalent to the given value except that any unknown values within it are marked as having pending changes associated with the given resource instance address.

This is intended for when preparing the approximate planned value for a data resource instance whose read is being delayed until the apply phase to await some upstream changes. In that case, we want to treat the unknown parts of that placeholder as also being pending changes so that downstream uses of those values in other data resource instances can get attributed to the data resource instance in question.

Ideally we'd merge PlannedUnknownObject and this function together to do both jobs at once instead of doing this in two passes, but that'll wait until we're ready to start changing code that the older OpenTofu runtime depends on, rather than having new-runtime-specific needs intentionally separated.

func NewPendingChange added in v0.34.0

func NewPendingChange(instAddr addrs.AbsResourceInstance) any

func NormalizeObjectFromLegacySDK

func NormalizeObjectFromLegacySDK(val cty.Value, schema *configschema.Block) cty.Value

NormalizeObjectFromLegacySDK takes an object that may have been generated by the legacy Terraform SDK (i.e. returned from a provider with the LegacyTypeSystem opt-out set) and does its best to normalize it for the assumptions we would normally enforce if the provider had not opted out.

In particular, this function guarantees that a value representing a nested block will never itself be unknown or null, instead representing that as a non-null value that may contain null/unknown values.

The input value must still conform to the implied type of the given schema, or else this function may produce garbage results or panic. This is usually okay because type consistency is enforced when deserializing the value returned from the provider over the RPC wire protocol anyway.

func PlannedUnknownObject

func PlannedUnknownObject(schema *configschema.Block, config cty.Value) cty.Value

PlannedUnknownObject is similar to proposedNewBlock but tailored for planning data resources in particular. Specifically, it replaces the values of any Computed attributes not set in the configuration with an unknown value, which serves as a placeholder for a value to be filled in by the provider when the data resource is finally read.

Data resources are different because the planning of them is handled entirely within OpenTofu Core and not subject to customization by the provider. This function is, in effect, producing an equivalent result to passing the proposedNewBlock result into a provider's PlanResourceChange function, assuming a fixed implementation of PlanResourceChange that just fills in unknown values as needed.

This is also used for generating the planned value of an ephemeral resource when that is deferred. By design, ephemeral resources opening requires the configuration of the block to be fully known. Therefore, when there is at least one unknown attribute, we defer the execution until we resolve all the unknowns, and only then we open the ephemeral. Until then, this particular function helps with generating a placeholder for the ephemeral resource based on its schema. Ephemeral resources are not stored into the state, so every newly planned value is based only on the configuration and its schema.

func PrereqChangesForValue added in v0.34.0

func PrereqChangesForValue(v cty.Value) iter.Seq[addrs.AbsResourceInstance]

PrereqChangesForValue takes a value derived from a result of MarkPendingChanges and returns a sequence of all of the resource instance addresses that have pending changes that must be applied before the remote system should agree with the given value.

This is intended for deciding whether it's okay to read a data resource instance during the planning phase or if it must wait until some other changes have been applied during the apply phase. If this returns one or more addresses then the action of reading the data resource instance should happen during the apply phase and should happen only after the changes for the returned resource instances have been successfully applied.

The same resource instance could potentially appear more than once in the sequence if its changed values were used in more than one place in the given value. Callers should ignore duplicate reports, such as by adding each of the results to an addrs.Set.

It's okay but useless to call this function with a value that wasn't derived from a MarkPendingChanges result: in that case it'll just return an empty sequence.

func ProposedNew

func ProposedNew(schema *configschema.Block, prior, config cty.Value) cty.Value

ProposedNew constructs a proposed new object value by combining the computed attribute values from "prior" with the configured attribute values from "config".

Both value must conform to the given schema's implied type, or this function will panic.

The prior value must be wholly known, but the config value may be unknown or have nested unknown values.

The merging of the two objects includes the attributes of any nested blocks, which will be correlated in a manner appropriate for their nesting mode. Note in particular that the correlation for blocks backed by sets is a heuristic based on matching non-computed attribute values and so it may produce strange results with more "extreme" cases, such as a nested set block where _all_ attributes are computed.

func RemovePendingChangesMark added in v0.34.0

func RemovePendingChangesMark(val cty.Value) cty.Value

func ValueEqual

func ValueEqual(x, y cty.Value) bool

ValueEqual provides an implementation of the equals function that can be passed into LongestCommonSubsequence when comparing cty.Value types.

func ValuePendingChange added in v0.34.0

func ValuePendingChange(v cty.Value, instAddr addrs.AbsResourceInstance) cty.Value

ValuePendingChange returns the given value marked as being derived from the result of a pending change to the resource instance given in instAddr.

Types

type Marker added in v0.34.0

type Marker func(cty.Path) any

type PendingChange added in v0.34.0

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

PendingChange is used to "mark" cty values which are derived from the result of a planned change that hasn't yet been applied to the remote object it belongs to.

MarkPendingChanges produces values marked in this way. We can use this in situations where a pending change upstream must cause something downstream to be delayed until that change has been applied, such as when the configuration for a data resource instance refers to a value that is expected to change during the apply phase and therefore the data resource instance must not be read until the apply phase.

Jump to

Keyboard shortcuts

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