Skip to content

Commit b797341

Browse files
committed
Deal with changes to 'attr.Value.ToTerraformValue' (hashicorp/terraform-plugin-framework#231).
1 parent 4f3b6b9 commit b797341

File tree

5 files changed

+57
-21
lines changed

5 files changed

+57
-21
lines changed

internal/generic/default_value.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ func (attributePlanModifier defaultValueAttributePlanModifier) Modify(ctx contex
3838
))
3939

4040
return
41-
} else if v == nil && request.AttributeState != nil && request.AttributeState.Equal(attributePlanModifier.val) {
41+
} else if v.IsNull() && request.AttributeState != nil && request.AttributeState.Equal(attributePlanModifier.val) {
4242
response.AttributePlan = request.AttributeState
4343
} else {
4444
response.AttributePlan = request.AttributePlan

internal/types/duration.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,14 +128,18 @@ func (d Duration) Type(_ context.Context) attr.Type {
128128
// ToTerraformValue returns the data contained in the *String as a string. If
129129
// Unknown is true, it returns a tftypes.UnknownValue. If Null is true, it
130130
// returns nil.
131-
func (d Duration) ToTerraformValue(_ context.Context) (interface{}, error) {
131+
func (d Duration) ToTerraformValue(ctx context.Context) (tftypes.Value, error) {
132+
t := DurationType.TerraformType(ctx)
132133
if d.Null {
133-
return nil, nil
134+
return tftypes.NewValue(t, nil), nil
134135
}
135136
if d.Unknown {
136-
return tftypes.UnknownValue, nil
137+
return tftypes.NewValue(t, tftypes.UnknownValue), nil
137138
}
138-
return d.Value, nil
139+
if err := tftypes.ValidateValue(tftypes.Number, d.Value); err != nil {
140+
return tftypes.NewValue(t, tftypes.UnknownValue), err
141+
}
142+
return tftypes.NewValue(t, d.Value), nil
139143
}
140144

141145
// Equal returns true if `other` is a *Duration and has the same value as `d`.

internal/validate/array.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ func setKeyer(ctx context.Context, path *tftypes.AttributePath, i int, v attr.Va
159159
return nil, ccdiag.NewUnableToObtainValueAttributeError(path, err)
160160
}
161161

162-
return path.WithElementKeyValue(tftypes.NewValue(v.Type(ctx).TerraformType(ctx), val)), nil
162+
return path.WithElementKeyValue(val), nil
163163
}
164164

165165
func validateArray(request tfsdk.ValidateAttributeRequest, response *tfsdk.ValidateAttributeResponse) ([]attr.Value, arrayKeyer, bool) {

internal/validate/required.go

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,21 +129,26 @@ func (validator requiredAttributesValidator) Validate(ctx context.Context, reque
129129
// * Object (SingleNestedAttribute)
130130
// * List (ListNestedAttribute)
131131
// * Set (SetNestedAttribute)
132+
isMap := false
133+
isSlice := false
132134
switch v := request.AttributeConfig.(type) {
133135
case types.Object:
134136
if v.Null || v.Unknown {
135137
return
136138
}
139+
isMap = true
137140

138141
case types.List:
139142
if v.Null || v.Unknown {
140143
return
141144
}
145+
isSlice = true
142146

143147
case types.Set:
144148
if v.Null || v.Unknown {
145149
return
146150
}
151+
isSlice = true
147152

148153
default:
149154
response.Diagnostics.Append(ccdiag.NewIncorrectValueTypeAttributeError(
@@ -166,8 +171,17 @@ func (validator requiredAttributesValidator) Validate(ctx context.Context, reque
166171
}
167172

168173
var diags tfdiag.Diagnostics
169-
switch v := val.(type) {
170-
case map[string]tftypes.Value:
174+
if isMap {
175+
var v map[string]tftypes.Value
176+
if err := val.As(&v); err != nil {
177+
response.Diagnostics.Append(ccdiag.NewUnableToConvertValueTypeAttributeError(
178+
request.AttributePath,
179+
err,
180+
))
181+
182+
return
183+
}
184+
171185
// Ensure that the object is fully known.
172186
for _, val := range v {
173187
if !val.IsFullyKnown() {
@@ -176,8 +190,17 @@ func (validator requiredAttributesValidator) Validate(ctx context.Context, reque
176190
}
177191

178192
diags = evaluateRequiredAttributesFuncs(specifiedAttributes(v), validator.fs...)
193+
} else if isSlice {
194+
var v []tftypes.Value
195+
if err := val.As(&v); err != nil {
196+
response.Diagnostics.Append(ccdiag.NewUnableToConvertValueTypeAttributeError(
197+
request.AttributePath,
198+
err,
199+
))
200+
201+
return
202+
}
179203

180-
case []tftypes.Value:
181204
// Ensure that the array is fully known.
182205
for _, val := range v {
183206
if !val.IsFullyKnown() {

internal/validate/unique_items.go

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55

66
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
77
"github.com/hashicorp/terraform-plugin-framework/types"
8-
"github.com/hashicorp/terraform-plugin-go/tftypes"
98
"github.com/hashicorp/terraform-provider-awscc/internal/diag"
109
)
1110

@@ -41,28 +40,38 @@ func (v uniqueItemsValidator) Validate(ctx context.Context, request tfsdk.Valida
4140
return
4241
}
4342

44-
val, err := list.ToTerraformValue(ctx)
43+
for i1, val1 := range list.Elems {
44+
val1, err := val1.ToTerraformValue(ctx)
4545

46-
if err != nil {
47-
response.Diagnostics.Append(diag.NewUnableToObtainValueAttributeError(
48-
request.AttributePath,
49-
err,
50-
))
46+
if err != nil {
47+
response.Diagnostics.Append(diag.NewUnableToObtainValueAttributeError(
48+
request.AttributePath,
49+
err,
50+
))
5151

52-
return
53-
}
52+
return
53+
}
5454

55-
vals := val.([]tftypes.Value)
56-
for i1, val1 := range vals {
5755
if !val1.IsFullyKnown() {
5856
continue
5957
}
6058

61-
for i2, val2 := range vals {
59+
for i2, val2 := range list.Elems {
6260
if i2 == i1 {
6361
continue
6462
}
6563

64+
val2, err := val2.ToTerraformValue(ctx)
65+
66+
if err != nil {
67+
response.Diagnostics.Append(diag.NewUnableToObtainValueAttributeError(
68+
request.AttributePath,
69+
err,
70+
))
71+
72+
return
73+
}
74+
6675
if !val2.IsFullyKnown() {
6776
continue
6877
}

0 commit comments

Comments
 (0)