-
Notifications
You must be signed in to change notification settings - Fork 9.7k
/
Copy pathcompose.go
72 lines (69 loc) · 2.43 KB
/
compose.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package customdiff
import (
"github.com/hashicorp/go-multierror"
"github.com/hashicorp/terraform/helper/schema"
)
// All returns a CustomizeDiffFunc that runs all of the given
// CustomizeDiffFuncs 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 (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 (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 (d *schema.ResourceDiff, meta interface{}) bool {
// // Any change to "content" causes a new "version_id" to be allocated.
// return d.HasChange("content")
// }),
// ),
// }
//
func All(funcs ...schema.CustomizeDiffFunc) schema.CustomizeDiffFunc {
return func(d *schema.ResourceDiff, meta interface{}) error {
var err error
for _, f := range funcs {
thisErr := f(d, meta)
if thisErr != nil {
err = multierror.Append(err, thisErr)
}
}
return err
}
}
// Sequence returns a CustomizeDiffFunc that runs all of the given
// CustomizeDiffFuncs in sequence, stopping at the first one that returns
// an error and returning that error.
//
// If all functions succeed, the combined function also succeeds.
func Sequence(funcs ...schema.CustomizeDiffFunc) schema.CustomizeDiffFunc {
return func(d *schema.ResourceDiff, meta interface{}) error {
for _, f := range funcs {
err := f(d, meta)
if err != nil {
return err
}
}
return nil
}
}