mirror of
https://github.com/opentofu/opentofu.git
synced 2024-12-27 09:21:14 -06:00
c647b22d97
The CustomizeDiff functionality in helper/schema is powerful, but directly writing single CustomizeDiff functions can obscure the intent when a number of different, orthogonal diff-customization behaviors are required. This new library provides some building blocks that aim to allow a more declarative form of CustomizeDiff implementation, by composing a number of smaller operations. 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") }), ), } The goal is to allow the various separate operations to be quickly seen and to ensure that each of them runs independently of the others. These functions all create closures on the call parameters, so the result is still just a normal CustomizeDiffFunc and so the helpers in this package can be combined with hand-written functions as needed. As we get more experience writing CustomizeDiff functions we may wish to expand the repertoire of functions here in future; this initial set attempts to cover some common cases we've seen so far. We may also investigate some helper functions that are entirely declarative and so don't take callback functions at all, but want to learn what the relevant use-cases are before going in too deep here.
111 lines
2.2 KiB
Go
111 lines
2.2 KiB
Go
package customdiff
|
|
|
|
import (
|
|
"errors"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
|
)
|
|
|
|
func TestAll(t *testing.T) {
|
|
var aCalled, bCalled, cCalled bool
|
|
|
|
provider := testProvider(
|
|
map[string]*schema.Schema{},
|
|
All(
|
|
func(d *schema.ResourceDiff, meta interface{}) error {
|
|
aCalled = true
|
|
return errors.New("A bad")
|
|
},
|
|
func(d *schema.ResourceDiff, meta interface{}) error {
|
|
bCalled = true
|
|
return nil
|
|
},
|
|
func(d *schema.ResourceDiff, meta interface{}) error {
|
|
cCalled = true
|
|
return errors.New("C bad")
|
|
},
|
|
),
|
|
)
|
|
|
|
_, err := testDiff(
|
|
provider,
|
|
map[string]string{
|
|
"foo": "bar",
|
|
},
|
|
map[string]string{
|
|
"foo": "baz",
|
|
},
|
|
)
|
|
|
|
if err == nil {
|
|
t.Fatal("Diff succeeded; want error")
|
|
}
|
|
if s, sub := err.Error(), "* A bad"; !strings.Contains(s, sub) {
|
|
t.Errorf("Missing substring %q in error message %q", sub, s)
|
|
}
|
|
if s, sub := err.Error(), "* C bad"; !strings.Contains(s, sub) {
|
|
t.Errorf("Missing substring %q in error message %q", sub, s)
|
|
}
|
|
|
|
if !aCalled {
|
|
t.Error("customize callback A was not called")
|
|
}
|
|
if !bCalled {
|
|
t.Error("customize callback B was not called")
|
|
}
|
|
if !cCalled {
|
|
t.Error("customize callback C was not called")
|
|
}
|
|
}
|
|
|
|
func TestSequence(t *testing.T) {
|
|
var aCalled, bCalled, cCalled bool
|
|
|
|
provider := testProvider(
|
|
map[string]*schema.Schema{},
|
|
Sequence(
|
|
func(d *schema.ResourceDiff, meta interface{}) error {
|
|
aCalled = true
|
|
return nil
|
|
},
|
|
func(d *schema.ResourceDiff, meta interface{}) error {
|
|
bCalled = true
|
|
return errors.New("B bad")
|
|
},
|
|
func(d *schema.ResourceDiff, meta interface{}) error {
|
|
cCalled = true
|
|
return errors.New("C bad")
|
|
},
|
|
),
|
|
)
|
|
|
|
_, err := testDiff(
|
|
provider,
|
|
map[string]string{
|
|
"foo": "bar",
|
|
},
|
|
map[string]string{
|
|
"foo": "baz",
|
|
},
|
|
)
|
|
|
|
if err == nil {
|
|
t.Fatal("Diff succeeded; want error")
|
|
}
|
|
if got, want := err.Error(), "B bad"; got != want {
|
|
t.Errorf("Wrong error message %q; want %q", got, want)
|
|
}
|
|
|
|
if !aCalled {
|
|
t.Error("customize callback A was not called")
|
|
}
|
|
if !bCalled {
|
|
t.Error("customize callback B was not called")
|
|
}
|
|
if cCalled {
|
|
t.Error("customize callback C was called (should not have been)")
|
|
}
|
|
}
|