Skip to content

Commit ef9d2e2

Browse files
authored
customdiff: Use warning log instead of returning errors in ComputedIf, ForceNewIf, and ForceNewIfChange (#909)
Reference: hashicorp/terraform-plugin-sdk#908
1 parent 51988f7 commit ef9d2e2

File tree

6 files changed

+162
-5
lines changed

6 files changed

+162
-5
lines changed

.changelog/909.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:bug
2+
customdiff: Prevented unexpected non-existent key errors in `ComputedIf`, `ForceNewIf`, and `ForceNewIfChange` since 2.11.0, using a warning log for backwards compatibility instead
3+
```

helper/customdiff/computed.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,30 @@ package customdiff
22

33
import (
44
"context"
5-
"fmt"
65

76
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/internal/logging"
88
)
99

1010
// ComputedIf returns a CustomizeDiffFunc that sets the given key's new value
1111
// as computed if the given condition function returns true.
12+
//
13+
// This function is best effort and will generate a warning log on any errors.
1214
func ComputedIf(key string, f ResourceConditionFunc) schema.CustomizeDiffFunc {
1315
return func(ctx context.Context, d *schema.ResourceDiff, meta interface{}) error {
1416
if f(ctx, d, meta) {
17+
// To prevent backwards compatibility issues, this logic only
18+
// generates a warning log instead of returning the error to
19+
// the provider and ultimately the practitioner. Providers may
20+
// not be aware of all situations in which the key may not be
21+
// present in the data, such as during resource creation, so any
22+
// further changes here should take that into account by
23+
// documenting how to prevent the error.
1524
if err := d.SetNewComputed(key); err != nil {
16-
return fmt.Errorf("unable to set %q to unknown: %w", key, err)
25+
logging.HelperSchemaWarn(ctx, "unable to set attribute value to unknown", map[string]interface{}{
26+
logging.KeyAttributePath: key,
27+
logging.KeyError: err,
28+
})
1729
}
1830
}
1931
return nil

helper/customdiff/computed_test.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import (
88
)
99

1010
func TestComputedIf(t *testing.T) {
11+
t.Parallel()
12+
1113
t.Run("true", func(t *testing.T) {
1214
var condCalls int
1315
var gotOld, gotNew string
@@ -69,6 +71,24 @@ func TestComputedIf(t *testing.T) {
6971
t.Error("Attribute 'comp' is not marked as NewComputed")
7072
}
7173
})
74+
t.Run("true-non-existent-attribute", func(t *testing.T) {
75+
provider := testProvider(
76+
map[string]*schema.Schema{},
77+
ComputedIf("non-existent", func(_ context.Context, d *schema.ResourceDiff, meta interface{}) bool {
78+
return true
79+
}),
80+
)
81+
82+
_, err := testDiff(
83+
provider,
84+
map[string]string{},
85+
map[string]string{},
86+
)
87+
88+
if err != nil {
89+
t.Fatalf("unexpected error: %s", err)
90+
}
91+
})
7292
t.Run("false", func(t *testing.T) {
7393
var condCalls int
7494
var gotOld, gotNew string
@@ -124,4 +144,22 @@ func TestComputedIf(t *testing.T) {
124144
t.Error("Attribute 'foo' is marked as NewComputed, but should not be")
125145
}
126146
})
147+
t.Run("false-non-existent-attribute", func(t *testing.T) {
148+
provider := testProvider(
149+
map[string]*schema.Schema{},
150+
ComputedIf("non-existent", func(_ context.Context, d *schema.ResourceDiff, meta interface{}) bool {
151+
return false
152+
}),
153+
)
154+
155+
_, err := testDiff(
156+
provider,
157+
map[string]string{},
158+
map[string]string{},
159+
)
160+
161+
if err != nil {
162+
t.Fatalf("unexpected error: %s", err)
163+
}
164+
})
127165
}

helper/customdiff/force_new.go

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ package customdiff
22

33
import (
44
"context"
5-
"fmt"
65

76
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/internal/logging"
88
)
99

1010
// ForceNewIf returns a CustomizeDiffFunc that flags the given key as
@@ -13,11 +13,23 @@ import (
1313
// The return value of the condition function is ignored if the old and new
1414
// values of the field compare equal, since no attribute diff is generated in
1515
// that case.
16+
//
17+
// This function is best effort and will generate a warning log on any errors.
1618
func ForceNewIf(key string, f ResourceConditionFunc) schema.CustomizeDiffFunc {
1719
return func(ctx context.Context, d *schema.ResourceDiff, meta interface{}) error {
1820
if f(ctx, d, meta) {
21+
// To prevent backwards compatibility issues, this logic only
22+
// generates a warning log instead of returning the error to
23+
// the provider and ultimately the practitioner. Providers may
24+
// not be aware of all situations in which the key may not be
25+
// present in the data, such as during resource creation, so any
26+
// further changes here should take that into account by
27+
// documenting how to prevent the error.
1928
if err := d.ForceNew(key); err != nil {
20-
return fmt.Errorf("unable to set %q to require replacement: %w", key, err)
29+
logging.HelperSchemaWarn(ctx, "unable to require attribute replacement", map[string]interface{}{
30+
logging.KeyAttributePath: key,
31+
logging.KeyError: err,
32+
})
2133
}
2234
}
2335
return nil
@@ -34,12 +46,24 @@ func ForceNewIf(key string, f ResourceConditionFunc) schema.CustomizeDiffFunc {
3446
// only the old and new values of the given key, which leads to more compact
3547
// and explicit code in the common case where the decision can be made with
3648
// only the specific field value.
49+
//
50+
// This function is best effort and will generate a warning log on any errors.
3751
func ForceNewIfChange(key string, f ValueChangeConditionFunc) schema.CustomizeDiffFunc {
3852
return func(ctx context.Context, d *schema.ResourceDiff, meta interface{}) error {
3953
oldValue, newValue := d.GetChange(key)
4054
if f(ctx, oldValue, newValue, meta) {
55+
// To prevent backwards compatibility issues, this logic only
56+
// generates a warning log instead of returning the error to
57+
// the provider and ultimately the practitioner. Providers may
58+
// not be aware of all situations in which the key may not be
59+
// present in the data, such as during resource creation, so any
60+
// further changes here should take that into account by
61+
// documenting how to prevent the error.
4162
if err := d.ForceNew(key); err != nil {
42-
return fmt.Errorf("unable to set %q to require replacement: %w", key, err)
63+
logging.HelperSchemaWarn(ctx, "unable to require attribute replacement", map[string]interface{}{
64+
logging.KeyAttributePath: key,
65+
logging.KeyError: err,
66+
})
4367
}
4468
}
4569
return nil

helper/customdiff/force_new_test.go

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import (
88
)
99

1010
func TestForceNewIf(t *testing.T) {
11+
t.Parallel()
12+
1113
t.Run("true", func(t *testing.T) {
1214
var condCalls int
1315
var gotOld1, gotNew1, gotOld2, gotNew2 string
@@ -77,6 +79,24 @@ func TestForceNewIf(t *testing.T) {
7779
t.Error("Attribute 'foo' is not marked as RequiresNew")
7880
}
7981
})
82+
t.Run("true-non-existent-attribute", func(t *testing.T) {
83+
provider := testProvider(
84+
map[string]*schema.Schema{},
85+
ForceNewIf("non-existent", func(_ context.Context, d *schema.ResourceDiff, meta interface{}) bool {
86+
return true
87+
}),
88+
)
89+
90+
_, err := testDiff(
91+
provider,
92+
map[string]string{},
93+
map[string]string{},
94+
)
95+
96+
if err != nil {
97+
t.Fatalf("unexpected error: %s", err)
98+
}
99+
})
80100
t.Run("false", func(t *testing.T) {
81101
var condCalls int
82102
var gotOld, gotNew string
@@ -127,9 +147,29 @@ func TestForceNewIf(t *testing.T) {
127147
t.Error("Attribute 'foo' is marked as RequiresNew, but should not be")
128148
}
129149
})
150+
t.Run("false-non-existent-attribute", func(t *testing.T) {
151+
provider := testProvider(
152+
map[string]*schema.Schema{},
153+
ForceNewIf("non-existent", func(_ context.Context, d *schema.ResourceDiff, meta interface{}) bool {
154+
return false
155+
}),
156+
)
157+
158+
_, err := testDiff(
159+
provider,
160+
map[string]string{},
161+
map[string]string{},
162+
)
163+
164+
if err != nil {
165+
t.Fatalf("unexpected error: %s", err)
166+
}
167+
})
130168
}
131169

132170
func TestForceNewIfChange(t *testing.T) {
171+
t.Parallel()
172+
133173
t.Run("true", func(t *testing.T) {
134174
var condCalls int
135175
var gotOld1, gotNew1, gotOld2, gotNew2 string
@@ -198,6 +238,24 @@ func TestForceNewIfChange(t *testing.T) {
198238
t.Error("Attribute 'foo' is not marked as RequiresNew")
199239
}
200240
})
241+
t.Run("true-non-existent-attribute", func(t *testing.T) {
242+
provider := testProvider(
243+
map[string]*schema.Schema{},
244+
ForceNewIfChange("foo", func(_ context.Context, oldValue, newValue, meta interface{}) bool {
245+
return true
246+
}),
247+
)
248+
249+
_, err := testDiff(
250+
provider,
251+
map[string]string{},
252+
map[string]string{},
253+
)
254+
255+
if err != nil {
256+
t.Fatalf("unexpected error: %s", err)
257+
}
258+
})
201259
t.Run("false", func(t *testing.T) {
202260
var condCalls int
203261
var gotOld, gotNew string
@@ -247,4 +305,22 @@ func TestForceNewIfChange(t *testing.T) {
247305
t.Error("Attribute 'foo' is marked as RequiresNew, but should not be")
248306
}
249307
})
308+
t.Run("false-non-existent-attribute", func(t *testing.T) {
309+
provider := testProvider(
310+
map[string]*schema.Schema{},
311+
ForceNewIfChange("foo", func(_ context.Context, oldValue, newValue, meta interface{}) bool {
312+
return true
313+
}),
314+
)
315+
316+
_, err := testDiff(
317+
provider,
318+
map[string]string{},
319+
map[string]string{},
320+
)
321+
322+
if err != nil {
323+
t.Fatalf("unexpected error: %s", err)
324+
}
325+
})
250326
}

internal/logging/keys.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ package logging
88
// Refer to the terraform-plugin-go logging keys as well, which should be
99
// equivalent to these when possible.
1010
const (
11+
// Attribute path representation, which is typically in flatmap form such
12+
// as parent.0.child in this project.
13+
KeyAttributePath = "tf_attribute_path"
14+
1115
// The type of data source being operated on, such as "archive_file"
1216
KeyDataSourceType = "tf_data_source_type"
1317

0 commit comments

Comments
 (0)