Movatterモバイル変換


[0]ホーム

URL:


customdiff

package
v2.38.1Latest Latest
Warning

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

Go to latest
Published: Sep 22, 2025 License:MPL-2.0Imports:4Imported by:549

Details

Repository

github.com/hashicorp/terraform-plugin-sdk

Links

Documentation

Overview

Package customdiff provides a set of reusable and composable functionsto enable more "declarative" use of the CustomizeDiff mechanism availablefor resources in package helper/schema.

The intent of these helpers is to make the intent of a set of diffcustomizations easier to see, rather than lost in a sea of Go functionboilerplate. They should _not_ be used in situations where they _obscure_intent, e.g. by over-using the composition functions where a singlefunction containing normal Go control flow statements would be morestraightforward.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

funcAll

All returns a CustomizeDiffFunc that runs all of the givenCustomizeDiffFuncs and returns all of the errors produced.

If one function produces an error, functions after it are still run.If this is not desirable, use function Sequence instead.

If multiple functions returns errors, the result is a multierror.

For example:

&schema.Resource{    // ...    CustomizeDiff: customdiff.All(        customdiff.ValidateChange("size", func (ctx context.Context, old, new, meta interface{}) error {            // If we are increasing "size" then the new value must be            // a multiple of the old value.            if new.(int) <= old.(int) {                return nil            }            if (new.(int) % old.(int)) != 0 {                return fmt.Errorf("new size value must be an integer multiple of old value %d", old.(int))            }            return nil        }),        customdiff.ForceNewIfChange("size", func (ctx context.Context, old, new, meta interface{}) bool {            // "size" can only increase in-place, so we must create a new resource            // if it is decreased.            return new.(int) < old.(int)        }),        customdiff.ComputedIf("version_id", func (ctx context.Context, d *schema.ResourceDiff, meta interface{}) bool {            // Any change to "content" causes a new "version_id" to be allocated.            return d.HasChange("content")        }),    ),}

funcComputedIf

ComputedIf returns a CustomizeDiffFunc that sets the given key's new valueas computed if the given condition function returns true.

This function is best effort and will generate a warning log on any errors.

funcForceNewIf

ForceNewIf returns a CustomizeDiffFunc that flags the given key asrequiring a new resource if the given condition function returns true.

The return value of the condition function is ignored if the old and newvalues of the field compare equal, since no attribute diff is generated inthat case.

This function is best effort and will generate a warning log on any errors.

funcForceNewIfChange

ForceNewIfChange returns a CustomizeDiffFunc that flags the given key asrequiring a new resource if the given condition function returns true.

The return value of the condition function is ignored if the old and newvalues compare equal, since no attribute diff is generated in that case.

This function is similar to ForceNewIf but provides the condition functiononly the old and new values of the given key, which leads to more compactand explicit code in the common case where the decision can be made withonly the specific field value.

This function is best effort and will generate a warning log on any errors.

funcIf

If returns a CustomizeDiffFunc that calls the given conditionfunction and then calls the given CustomizeDiffFunc only if the conditionfunction returns true.

This can be used to include conditional customizations when composingcustomizations using All and Sequence, but should generally be used only insimple scenarios. Prefer directly writing a CustomizeDiffFunc containinga conditional branch if the given CustomizeDiffFunc is already alocally-defined function, since this avoids obscuring the control flow.

funcIfValue

IfValue returns a CustomizeDiffFunc that calls the given conditionfunction with the new values of the given key and then calls thegiven CustomizeDiffFunc only if the condition function returns true.

funcIfValueChange

IfValueChange returns a CustomizeDiffFunc that calls the given conditionfunction with the old and new values of the given key and then calls thegiven CustomizeDiffFunc only if the condition function returns true.

funcSequence

Sequence returns a CustomizeDiffFunc that runs all of the givenCustomizeDiffFuncs in sequence, stopping at the first one that returnsan error and returning that error.

If all functions succeed, the combined function also succeeds.

funcValidateChange

ValidateChange returns a CustomizeDiffFunc that applies the given validationfunction to the change for the given key, returning any error produced.

funcValidateValue

ValidateValue returns a CustomizeDiffFunc that applies the given validationfunction to value of the given key, returning any error produced.

This should generally not be used since it is functionally equivalent toa validation function applied directly to the schema attribute in question,but is provided for situations where composing multiple CustomizeDiffFuncstogether makes intent clearer than spreading that validation across theschema.

Types

typeResourceConditionFunc

type ResourceConditionFunc func(ctxcontext.Context, d *schema.ResourceDiff, meta interface{})bool

ResourceConditionFunc is a function type that makes a boolean decision basedon an entire resource diff.

typeValueChangeConditionFunc

type ValueChangeConditionFunc func(ctxcontext.Context, oldValue, newValue, meta interface{})bool

ValueChangeConditionFunc is a function type that makes a boolean decisionby comparing two values.

typeValueChangeValidationFunc

type ValueChangeValidationFunc func(ctxcontext.Context, oldValue, newValue, meta interface{})error

ValueChangeValidationFunc is a function type that validates the difference(or lack thereof) between two values, returning an error if the changeis invalid.

typeValueConditionFunc

type ValueConditionFunc func(ctxcontext.Context, value, meta interface{})bool

ValueConditionFunc is a function type that makes a boolean decision basedon a given value.

typeValueValidationFunc

type ValueValidationFunc func(ctxcontext.Context, value, meta interface{})error

ValueValidationFunc is a function type that validates a particular value,returning an error if the value is invalid.

Source Files

View all Source files

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f orF : Jump to
y orY : Canonical URL
go.dev uses cookies from Google to deliver and enhance the quality of its services and to analyze traffic.Learn more.

[8]ページ先頭

©2009-2025 Movatter.jp