github.com/tushar-armorcode/terraform@v0.11.12-beta1/helper/customdiff/compose.go (about)

     1  package customdiff
     2  
     3  import (
     4  	"github.com/hashicorp/go-multierror"
     5  	"github.com/hashicorp/terraform/helper/schema"
     6  )
     7  
     8  // All returns a CustomizeDiffFunc that runs all of the given
     9  // CustomizeDiffFuncs and returns all of the errors produced.
    10  //
    11  // If one function produces an error, functions after it are still run.
    12  // If this is not desirable, use function Sequence instead.
    13  //
    14  // If multiple functions returns errors, the result is a multierror.
    15  //
    16  // For example:
    17  //
    18  //     &schema.Resource{
    19  //         // ...
    20  //         CustomizeDiff: customdiff.All(
    21  //             customdiff.ValidateChange("size", func (old, new, meta interface{}) error {
    22  //                 // If we are increasing "size" then the new value must be
    23  //                 // a multiple of the old value.
    24  //                 if new.(int) <= old.(int) {
    25  //                     return nil
    26  //                 }
    27  //                 if (new.(int) % old.(int)) != 0 {
    28  //                     return fmt.Errorf("new size value must be an integer multiple of old value %d", old.(int))
    29  //                 }
    30  //                 return nil
    31  //             }),
    32  //             customdiff.ForceNewIfChange("size", func (old, new, meta interface{}) bool {
    33  //                 // "size" can only increase in-place, so we must create a new resource
    34  //                 // if it is decreased.
    35  //                 return new.(int) < old.(int)
    36  //             }),
    37  //             customdiff.ComputedIf("version_id", func (d *schema.ResourceDiff, meta interface{}) bool {
    38  //                 // Any change to "content" causes a new "version_id" to be allocated.
    39  //                 return d.HasChange("content")
    40  //             }),
    41  //         ),
    42  //     }
    43  //
    44  func All(funcs ...schema.CustomizeDiffFunc) schema.CustomizeDiffFunc {
    45  	return func(d *schema.ResourceDiff, meta interface{}) error {
    46  		var err error
    47  		for _, f := range funcs {
    48  			thisErr := f(d, meta)
    49  			if thisErr != nil {
    50  				err = multierror.Append(err, thisErr)
    51  			}
    52  		}
    53  		return err
    54  	}
    55  }
    56  
    57  // Sequence returns a CustomizeDiffFunc that runs all of the given
    58  // CustomizeDiffFuncs in sequence, stopping at the first one that returns
    59  // an error and returning that error.
    60  //
    61  // If all functions succeed, the combined function also succeeds.
    62  func Sequence(funcs ...schema.CustomizeDiffFunc) schema.CustomizeDiffFunc {
    63  	return func(d *schema.ResourceDiff, meta interface{}) error {
    64  		for _, f := range funcs {
    65  			err := f(d, meta)
    66  			if err != nil {
    67  				return err
    68  			}
    69  		}
    70  		return nil
    71  	}
    72  }