Skip to content

Commit 06871c2

Browse files
committed
numbers, floats, and ints
1 parent dc31133 commit 06871c2

File tree

10 files changed

+310
-225
lines changed

10 files changed

+310
-225
lines changed

resource/schema/float32planmodifier/use_state_for_unknown.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ func (m useStateForUnknownModifier) MarkdownDescription(_ context.Context) strin
3636

3737
// PlanModifyFloat32 implements the plan modification logic.
3838
func (m useStateForUnknownModifier) PlanModifyFloat32(_ context.Context, req planmodifier.Float32Request, resp *planmodifier.Float32Response) {
39-
// Do nothing if there is no state value.
40-
if req.StateValue.IsNull() {
39+
// Do nothing if there is no state (resource is being created).
40+
if req.State.Raw.IsNull() {
4141
return
4242
}
4343

resource/schema/float32planmodifier/use_state_for_unknown_test.go

Lines changed: 60 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ import (
88
"testing"
99

1010
"github.com/google/go-cmp/cmp"
11-
"github.com/hashicorp/terraform-plugin-framework/attr"
12-
"github.com/hashicorp/terraform-plugin-framework/path"
1311
"github.com/hashicorp/terraform-plugin-framework/resource/schema/float32planmodifier"
1412
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
13+
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
1514
"github.com/hashicorp/terraform-plugin-framework/types"
15+
"github.com/hashicorp/terraform-plugin-go/tftypes"
1616
)
1717

1818
func TestUseStateForUnknownModifierPlanModifyFloat32(t *testing.T) {
@@ -26,6 +26,16 @@ func TestUseStateForUnknownModifierPlanModifyFloat32(t *testing.T) {
2626
// when we first create the resource, use the unknown
2727
// value
2828
request: planmodifier.Float32Request{
29+
State: tfsdk.State{
30+
Raw: tftypes.NewValue(
31+
tftypes.Object{
32+
AttributeTypes: map[string]tftypes.Type{
33+
"attr": tftypes.Number,
34+
},
35+
},
36+
nil,
37+
),
38+
},
2939
StateValue: types.Float32Null(),
3040
PlanValue: types.Float32Unknown(),
3141
ConfigValue: types.Float32Null(),
@@ -42,6 +52,18 @@ func TestUseStateForUnknownModifierPlanModifyFloat32(t *testing.T) {
4252
// but we still want to preserve that value, in this
4353
// case
4454
request: planmodifier.Float32Request{
55+
State: tfsdk.State{
56+
Raw: tftypes.NewValue(
57+
tftypes.Object{
58+
AttributeTypes: map[string]tftypes.Type{
59+
"attr": tftypes.Number,
60+
},
61+
},
62+
map[string]tftypes.Value{
63+
"attr": tftypes.NewValue(tftypes.Number, 2.4),
64+
},
65+
),
66+
},
4567
StateValue: types.Float32Value(2.4),
4668
PlanValue: types.Float32Value(1.2),
4769
ConfigValue: types.Float32Null(),
@@ -50,10 +72,22 @@ func TestUseStateForUnknownModifierPlanModifyFloat32(t *testing.T) {
5072
PlanValue: types.Float32Value(1.2),
5173
},
5274
},
53-
"non-null-state-unknown-plan": {
75+
"non-null-state-value-unknown-plan": {
5476
// this is the situation we want to preserve the state
5577
// in
5678
request: planmodifier.Float32Request{
79+
State: tfsdk.State{
80+
Raw: tftypes.NewValue(
81+
tftypes.Object{
82+
AttributeTypes: map[string]tftypes.Type{
83+
"attr": tftypes.Number,
84+
},
85+
},
86+
map[string]tftypes.Value{
87+
"attr": tftypes.NewValue(tftypes.Number, 1.2),
88+
},
89+
),
90+
},
5791
StateValue: types.Float32Value(1.2),
5892
PlanValue: types.Float32Unknown(),
5993
ConfigValue: types.Float32Null(),
@@ -62,6 +96,29 @@ func TestUseStateForUnknownModifierPlanModifyFloat32(t *testing.T) {
6296
PlanValue: types.Float32Value(1.2),
6397
},
6498
},
99+
"null-state-value-unknown-plan": {
100+
// Null state values are still known, so we should preserve this as well.
101+
request: planmodifier.Float32Request{
102+
State: tfsdk.State{
103+
Raw: tftypes.NewValue(
104+
tftypes.Object{
105+
AttributeTypes: map[string]tftypes.Type{
106+
"attr": tftypes.Number,
107+
},
108+
},
109+
map[string]tftypes.Value{
110+
"attr": tftypes.NewValue(tftypes.Number, nil),
111+
},
112+
),
113+
},
114+
StateValue: types.Float32Null(),
115+
PlanValue: types.Float32Unknown(),
116+
ConfigValue: types.Float32Null(),
117+
},
118+
expected: &planmodifier.Float32Response{
119+
PlanValue: types.Float32Null(),
120+
},
121+
},
65122
"unknown-config": {
66123
// this is the situation in which a user is
67124
// interpolating into a field. We want that to still
@@ -78,46 +135,6 @@ func TestUseStateForUnknownModifierPlanModifyFloat32(t *testing.T) {
78135
PlanValue: types.Float32Unknown(),
79136
},
80137
},
81-
"under-list": {
82-
request: planmodifier.Float32Request{
83-
ConfigValue: types.Float32Null(),
84-
Path: path.Root("test").AtListIndex(0).AtName("nested_test"),
85-
PlanValue: types.Float32Unknown(),
86-
StateValue: types.Float32Null(),
87-
},
88-
expected: &planmodifier.Float32Response{
89-
PlanValue: types.Float32Unknown(),
90-
},
91-
},
92-
"under-set": {
93-
request: planmodifier.Float32Request{
94-
ConfigValue: types.Float32Null(),
95-
Path: path.Root("test").AtSetValue(
96-
types.SetValueMust(
97-
types.ObjectType{
98-
AttrTypes: map[string]attr.Type{
99-
"nested_test": types.Float32Type,
100-
},
101-
},
102-
[]attr.Value{
103-
types.ObjectValueMust(
104-
map[string]attr.Type{
105-
"nested_test": types.Float32Type,
106-
},
107-
map[string]attr.Value{
108-
"nested_test": types.Float32Unknown(),
109-
},
110-
),
111-
},
112-
),
113-
).AtName("nested_test"),
114-
PlanValue: types.Float32Unknown(),
115-
StateValue: types.Float32Null(),
116-
},
117-
expected: &planmodifier.Float32Response{
118-
PlanValue: types.Float32Unknown(),
119-
},
120-
},
121138
}
122139

123140
for name, testCase := range testCases {

resource/schema/float64planmodifier/use_state_for_unknown.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ func (m useStateForUnknownModifier) MarkdownDescription(_ context.Context) strin
3636

3737
// PlanModifyFloat64 implements the plan modification logic.
3838
func (m useStateForUnknownModifier) PlanModifyFloat64(_ context.Context, req planmodifier.Float64Request, resp *planmodifier.Float64Response) {
39-
// Do nothing if there is no state value.
40-
if req.StateValue.IsNull() {
39+
// Do nothing if there is no state (resource is being created).
40+
if req.State.Raw.IsNull() {
4141
return
4242
}
4343

resource/schema/float64planmodifier/use_state_for_unknown_test.go

Lines changed: 60 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ import (
88
"testing"
99

1010
"github.com/google/go-cmp/cmp"
11-
"github.com/hashicorp/terraform-plugin-framework/attr"
12-
"github.com/hashicorp/terraform-plugin-framework/path"
1311
"github.com/hashicorp/terraform-plugin-framework/resource/schema/float64planmodifier"
1412
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
13+
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
1514
"github.com/hashicorp/terraform-plugin-framework/types"
15+
"github.com/hashicorp/terraform-plugin-go/tftypes"
1616
)
1717

1818
func TestUseStateForUnknownModifierPlanModifyFloat64(t *testing.T) {
@@ -26,6 +26,16 @@ func TestUseStateForUnknownModifierPlanModifyFloat64(t *testing.T) {
2626
// when we first create the resource, use the unknown
2727
// value
2828
request: planmodifier.Float64Request{
29+
State: tfsdk.State{
30+
Raw: tftypes.NewValue(
31+
tftypes.Object{
32+
AttributeTypes: map[string]tftypes.Type{
33+
"attr": tftypes.Number,
34+
},
35+
},
36+
nil,
37+
),
38+
},
2939
StateValue: types.Float64Null(),
3040
PlanValue: types.Float64Unknown(),
3141
ConfigValue: types.Float64Null(),
@@ -42,6 +52,18 @@ func TestUseStateForUnknownModifierPlanModifyFloat64(t *testing.T) {
4252
// but we still want to preserve that value, in this
4353
// case
4454
request: planmodifier.Float64Request{
55+
State: tfsdk.State{
56+
Raw: tftypes.NewValue(
57+
tftypes.Object{
58+
AttributeTypes: map[string]tftypes.Type{
59+
"attr": tftypes.Number,
60+
},
61+
},
62+
map[string]tftypes.Value{
63+
"attr": tftypes.NewValue(tftypes.Number, 2.4),
64+
},
65+
),
66+
},
4567
StateValue: types.Float64Value(2.4),
4668
PlanValue: types.Float64Value(1.2),
4769
ConfigValue: types.Float64Null(),
@@ -50,10 +72,22 @@ func TestUseStateForUnknownModifierPlanModifyFloat64(t *testing.T) {
5072
PlanValue: types.Float64Value(1.2),
5173
},
5274
},
53-
"non-null-state-unknown-plan": {
75+
"non-null-state-value-unknown-plan": {
5476
// this is the situation we want to preserve the state
5577
// in
5678
request: planmodifier.Float64Request{
79+
State: tfsdk.State{
80+
Raw: tftypes.NewValue(
81+
tftypes.Object{
82+
AttributeTypes: map[string]tftypes.Type{
83+
"attr": tftypes.Number,
84+
},
85+
},
86+
map[string]tftypes.Value{
87+
"attr": tftypes.NewValue(tftypes.Number, 1.2),
88+
},
89+
),
90+
},
5791
StateValue: types.Float64Value(1.2),
5892
PlanValue: types.Float64Unknown(),
5993
ConfigValue: types.Float64Null(),
@@ -62,6 +96,29 @@ func TestUseStateForUnknownModifierPlanModifyFloat64(t *testing.T) {
6296
PlanValue: types.Float64Value(1.2),
6397
},
6498
},
99+
"null-state-value-unknown-plan": {
100+
// Null state values are still known, so we should preserve this as well.
101+
request: planmodifier.Float64Request{
102+
State: tfsdk.State{
103+
Raw: tftypes.NewValue(
104+
tftypes.Object{
105+
AttributeTypes: map[string]tftypes.Type{
106+
"attr": tftypes.Number,
107+
},
108+
},
109+
map[string]tftypes.Value{
110+
"attr": tftypes.NewValue(tftypes.Number, nil),
111+
},
112+
),
113+
},
114+
StateValue: types.Float64Null(),
115+
PlanValue: types.Float64Unknown(),
116+
ConfigValue: types.Float64Null(),
117+
},
118+
expected: &planmodifier.Float64Response{
119+
PlanValue: types.Float64Null(),
120+
},
121+
},
65122
"unknown-config": {
66123
// this is the situation in which a user is
67124
// interpolating into a field. We want that to still
@@ -78,46 +135,6 @@ func TestUseStateForUnknownModifierPlanModifyFloat64(t *testing.T) {
78135
PlanValue: types.Float64Unknown(),
79136
},
80137
},
81-
"under-list": {
82-
request: planmodifier.Float64Request{
83-
ConfigValue: types.Float64Null(),
84-
Path: path.Root("test").AtListIndex(0).AtName("nested_test"),
85-
PlanValue: types.Float64Unknown(),
86-
StateValue: types.Float64Null(),
87-
},
88-
expected: &planmodifier.Float64Response{
89-
PlanValue: types.Float64Unknown(),
90-
},
91-
},
92-
"under-set": {
93-
request: planmodifier.Float64Request{
94-
ConfigValue: types.Float64Null(),
95-
Path: path.Root("test").AtSetValue(
96-
types.SetValueMust(
97-
types.ObjectType{
98-
AttrTypes: map[string]attr.Type{
99-
"nested_test": types.Float64Type,
100-
},
101-
},
102-
[]attr.Value{
103-
types.ObjectValueMust(
104-
map[string]attr.Type{
105-
"nested_test": types.Float64Type,
106-
},
107-
map[string]attr.Value{
108-
"nested_test": types.Float64Unknown(),
109-
},
110-
),
111-
},
112-
),
113-
).AtName("nested_test"),
114-
PlanValue: types.Float64Unknown(),
115-
StateValue: types.Float64Null(),
116-
},
117-
expected: &planmodifier.Float64Response{
118-
PlanValue: types.Float64Unknown(),
119-
},
120-
},
121138
}
122139

123140
for name, testCase := range testCases {

resource/schema/int32planmodifier/use_state_for_unknown.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ func (m useStateForUnknownModifier) MarkdownDescription(_ context.Context) strin
3636

3737
// PlanModifyInt32 implements the plan modification logic.
3838
func (m useStateForUnknownModifier) PlanModifyInt32(_ context.Context, req planmodifier.Int32Request, resp *planmodifier.Int32Response) {
39-
// Do nothing if there is no state value.
40-
if req.StateValue.IsNull() {
39+
// Do nothing if there is no state (resource is being created).
40+
if req.State.Raw.IsNull() {
4141
return
4242
}
4343

0 commit comments

Comments
 (0)