customdiff
packageThis package is not in the latest version of its module.
Details
Validgo.mod file
The Go module system was introduced in Go 1.11 and is the official dependency management solution for Go.
Redistributable license
Redistributable licenses place minimal restrictions on how software can be used, modified, and redistributed.
Tagged version
Modules with tagged versions give importers more predictable builds.
Stable version
When a project reaches major version v1 it is considered stable.
- Learn more about best practices
Repository
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¶
- func All(funcs ...schema.CustomizeDiffFunc) schema.CustomizeDiffFunc
- func ComputedIf(key string, f ResourceConditionFunc) schema.CustomizeDiffFunc
- func ForceNewIf(key string, f ResourceConditionFunc) schema.CustomizeDiffFunc
- func ForceNewIfChange(key string, f ValueChangeConditionFunc) schema.CustomizeDiffFunc
- func If(cond ResourceConditionFunc, f schema.CustomizeDiffFunc) schema.CustomizeDiffFunc
- func IfValue(key string, cond ValueConditionFunc, f schema.CustomizeDiffFunc) schema.CustomizeDiffFunc
- func IfValueChange(key string, cond ValueChangeConditionFunc, f schema.CustomizeDiffFunc) schema.CustomizeDiffFunc
- func Sequence(funcs ...schema.CustomizeDiffFunc) schema.CustomizeDiffFunc
- func ValidateChange(key string, f ValueChangeValidationFunc) schema.CustomizeDiffFunc
- func ValidateValue(key string, f ValueValidationFunc) schema.CustomizeDiffFunc
- type ResourceConditionFunc
- type ValueChangeConditionFunc
- type ValueChangeValidationFunc
- type ValueConditionFunc
- type ValueValidationFunc
Constants¶
This section is empty.
Variables¶
This section is empty.
Functions¶
funcAll¶
func All(funcs ...schema.CustomizeDiffFunc)schema.CustomizeDiffFunc
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¶
func ComputedIf(keystring, fResourceConditionFunc)schema.CustomizeDiffFunc
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¶
func ForceNewIf(keystring, fResourceConditionFunc)schema.CustomizeDiffFunc
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¶
func ForceNewIfChange(keystring, fValueChangeConditionFunc)schema.CustomizeDiffFunc
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¶
func If(condResourceConditionFunc, fschema.CustomizeDiffFunc)schema.CustomizeDiffFunc
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¶
func IfValue(keystring, condValueConditionFunc, fschema.CustomizeDiffFunc)schema.CustomizeDiffFunc
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¶
func IfValueChange(keystring, condValueChangeConditionFunc, fschema.CustomizeDiffFunc)schema.CustomizeDiffFunc
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¶
func Sequence(funcs ...schema.CustomizeDiffFunc)schema.CustomizeDiffFunc
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¶
func ValidateChange(keystring, fValueChangeValidationFunc)schema.CustomizeDiffFunc
ValidateChange returns a CustomizeDiffFunc that applies the given validationfunction to the change for the given key, returning any error produced.
funcValidateValue¶
func ValidateValue(keystring, fValueValidationFunc)schema.CustomizeDiffFunc
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¶
ValueChangeConditionFunc is a function type that makes a boolean decisionby comparing two values.
typeValueChangeValidationFunc¶
ValueChangeValidationFunc is a function type that validates the difference(or lack thereof) between two values, returning an error if the changeis invalid.
typeValueConditionFunc¶
ValueConditionFunc is a function type that makes a boolean decision basedon a given value.
typeValueValidationFunc¶
ValueValidationFunc is a function type that validates a particular value,returning an error if the value is invalid.