@@ -2,6 +2,8 @@ package schemafunc_test
2
2
3
3
import (
4
4
"context"
5
+ "fmt"
6
+ "slices"
5
7
"testing"
6
8
7
9
"github.com/hashicorp/terraform-plugin-framework/attr"
@@ -85,6 +87,20 @@ func TestHasUnknown(t *testing.T) {
85
87
}
86
88
}
87
89
90
+ type TFAutoScalingModel struct {
91
+ ComputeMinInstanceSize types.String `tfsdk:"compute_min_instance_size"`
92
+ ComputeMaxInstanceSize types.String `tfsdk:"compute_max_instance_size"`
93
+ ComputeEnabled types.Bool `tfsdk:"compute_enabled"`
94
+ DiskGBEnabled types.Bool `tfsdk:"disk_gb_enabled"`
95
+ }
96
+
97
+ var AutoScalingObjType = types.ObjectType {AttrTypes : map [string ]attr.Type {
98
+ "compute_enabled" : types .BoolType ,
99
+ "disk_gb_enabled" : types .BoolType ,
100
+ "compute_min_instance_size" : types .StringType ,
101
+ "compute_max_instance_size" : types .StringType ,
102
+ }}
103
+
88
104
type TFSpec struct {
89
105
InstanceSize types.String `tfsdk:"instance_size"`
90
106
NodeCount types.Int64 `tfsdk:"node_count"`
@@ -96,12 +112,14 @@ var SpecObjType = types.ObjectType{AttrTypes: map[string]attr.Type{
96
112
}}
97
113
98
114
type TFRegionConfig struct {
115
+ AutoScaling types.Object `tfsdk:"auto_scaling"`
99
116
ProviderName types.String `tfsdk:"provider_name"`
100
117
RegionName types.String `tfsdk:"region_name"`
101
118
Spec types.Object `tfsdk:"spec"`
102
119
}
103
120
104
121
var RegionConfigsObjType = types.ObjectType {AttrTypes : map [string ]attr.Type {
122
+ "auto_scaling" : AutoScalingObjType ,
105
123
"provider_name" : types .StringType ,
106
124
"region_name" : types .StringType ,
107
125
"spec" : SpecObjType ,
@@ -136,35 +154,85 @@ type TFSimpleModel struct {
136
154
var (
137
155
ctx = context .Background ()
138
156
regionConfigSrc = TFRegionConfig {
157
+ AutoScaling : autoScalingFalseAndNull ,
139
158
ProviderName : types .StringValue ("aws" ),
140
159
RegionName : types .StringValue ("US_EAST_1" ),
141
160
Spec : asObjectValue (ctx , TFSpec {InstanceSize : types .StringValue ("M10" ), NodeCount : types .Int64Value (3 )}, SpecObjType .AttrTypes ),
142
161
}
162
+ regionConfigNodeCount0 = TFRegionConfig {
163
+ AutoScaling : autoScalingFalseAndNull ,
164
+ ProviderName : types .StringValue ("aws" ),
165
+ RegionName : types .StringValue ("US_EAST_1" ),
166
+ Spec : asObjectValue (ctx , TFSpec {InstanceSize : types .StringValue ("M10" ), NodeCount : types .Int64Value (0 )}, SpecObjType .AttrTypes ),
167
+ }
143
168
regionConfigDest = TFRegionConfig {
169
+ AutoScaling : autoScalingFalseAndNull ,
144
170
ProviderName : types .StringUnknown (),
145
171
RegionName : types .StringValue ("US_EAST_1" ),
146
172
Spec : types .ObjectUnknown (SpecObjType .AttrTypes ),
147
173
}
148
174
regionConfigNodeCountUnknown = TFRegionConfig {
175
+ AutoScaling : autoScalingFalseAndNull ,
149
176
ProviderName : types .StringValue ("aws" ),
150
177
RegionName : types .StringValue ("US_EAST_1" ),
151
178
Spec : asObjectValue (ctx , TFSpec {InstanceSize : types .StringValue ("M10" ), NodeCount : types .Int64Unknown ()}, SpecObjType .AttrTypes ),
152
179
}
153
180
regionConfigSpecUnknown = TFRegionConfig {
181
+ AutoScaling : autoScalingFalseAndNull ,
154
182
ProviderName : types .StringValue ("aws" ),
155
183
RegionName : types .StringValue ("US_EAST_1" ),
156
184
Spec : types .ObjectUnknown (SpecObjType .AttrTypes ),
157
185
}
158
- advancedConfigTrue = asObjectValue (ctx , TFAdvancedConfig {JavascriptEnabled : types .BoolValue (true )}, AdvancedConfigObjType .AttrTypes )
186
+ advancedConfigTrue = asObjectValue (ctx , TFAdvancedConfig {JavascriptEnabled : types .BoolValue (true )}, AdvancedConfigObjType .AttrTypes )
187
+ autoScalingFalseAndEmpty = asObjectValue (ctx , TFAutoScalingModel {
188
+ ComputeEnabled : types .BoolValue (false ),
189
+ DiskGBEnabled : types .BoolValue (false ),
190
+ ComputeMinInstanceSize : types .StringValue ("" ),
191
+ ComputeMaxInstanceSize : types .StringValue ("" ),
192
+ }, AutoScalingObjType .AttrTypes )
193
+
194
+ autoScalingFalseAndNull = asObjectValue (ctx , TFAutoScalingModel {
195
+ ComputeEnabled : types .BoolValue (false ),
196
+ DiskGBEnabled : types .BoolValue (false ),
197
+ ComputeMinInstanceSize : types .StringNull (),
198
+ ComputeMaxInstanceSize : types .StringNull (),
199
+ }, AutoScalingObjType .AttrTypes )
200
+ autoScalingTrueAndNonEmpty = asObjectValue (ctx , TFAutoScalingModel {
201
+ ComputeEnabled : types .BoolValue (true ),
202
+ DiskGBEnabled : types .BoolValue (true ),
203
+ ComputeMinInstanceSize : types .StringValue ("M10" ),
204
+ ComputeMaxInstanceSize : types .StringValue ("M20" ),
205
+ }, AutoScalingObjType .AttrTypes )
206
+ autoScalingUnknown = types .ObjectUnknown (AutoScalingObjType .AttrTypes )
207
+ autoScalingLeafsUnknown = asObjectValue (ctx , TFAutoScalingModel {
208
+ ComputeEnabled : types .BoolUnknown (),
209
+ DiskGBEnabled : types .BoolUnknown (),
210
+ ComputeMinInstanceSize : types .StringUnknown (),
211
+ ComputeMaxInstanceSize : types .StringUnknown (),
212
+ }, AutoScalingObjType .AttrTypes )
213
+ keepProjectIDUnknown = func (name string , value attr.Value ) bool {
214
+ return name == "project_id"
215
+ }
216
+ useStateOnlyWhenNodeCount0 = func (name string , value attr.Value ) bool {
217
+ return name == "node_count" && ! value .Equal (types .Int64Value (0 ))
218
+ }
219
+
220
+ autoScalingBoolsKeepUnknown = func (name string , value attr.Value ) bool {
221
+ return slices .Contains ([]string {"compute_enabled" , "disk_gb_enabled" }, name ) && value .Equal (types .BoolValue (true ))
222
+ }
223
+ autoScalingStringsKeepUnknown = func (name string , value attr.Value ) bool {
224
+ return slices .Contains ([]string {"compute_min_instance_size" , "compute_max_instance_size" }, name ) && value .(types.String ).ValueString () != ""
225
+ }
159
226
)
160
227
161
228
func TestCopyUnknowns (t * testing.T ) {
162
229
tests := map [string ]struct {
163
- src * TFSimpleModel
164
- dest * TFSimpleModel
165
- expectedDest * TFSimpleModel
166
- panicMessage string
167
- keepUnknown []string
230
+ src * TFSimpleModel
231
+ dest * TFSimpleModel
232
+ expectedDest * TFSimpleModel
233
+ keepUnknownCalls schemafunc.KeepUnknownFunc
234
+ panicMessage string
235
+ keepUnknown []string
168
236
}{
169
237
"copy unknown basic fields" : {
170
238
src : & TFSimpleModel {
@@ -225,6 +293,93 @@ func TestCopyUnknowns(t *testing.T) {
225
293
},
226
294
keepUnknown : []string {"spec" },
227
295
},
296
+ "respect keepUnknownCall root" : {
297
+ src : & TFSimpleModel {
298
+ ProjectID : types .StringValue ("src-project" ),
299
+ Name : types .StringValue ("src-name" ),
300
+ },
301
+ dest : & TFSimpleModel {
302
+ ProjectID : types .StringUnknown (),
303
+ Name : types .StringUnknown (),
304
+ },
305
+ expectedDest : & TFSimpleModel {
306
+ ProjectID : types .StringUnknown (),
307
+ Name : types .StringValue ("src-name" ),
308
+ },
309
+ keepUnknownCalls : keepProjectIDUnknown ,
310
+ },
311
+ "respect keepUnknownCall nested" : {
312
+ src : & TFSimpleModel {
313
+ ProjectID : types .StringValue ("src-project" ),
314
+ Name : types .StringValue ("src-name" ),
315
+ ReplicationSpecs : newReplicationSpecs (ctx , types .StringValue ("Zone 1" ), []TFRegionConfig {regionConfigSrc }),
316
+ },
317
+ dest : & TFSimpleModel {
318
+ ProjectID : types .StringUnknown (),
319
+ Name : types .StringUnknown (),
320
+ ReplicationSpecs : newReplicationSpecs (ctx , types .StringUnknown (), []TFRegionConfig {regionConfigNodeCountUnknown }),
321
+ },
322
+ expectedDest : & TFSimpleModel {
323
+ ProjectID : types .StringValue ("src-project" ),
324
+ Name : types .StringValue ("src-name" ),
325
+ ReplicationSpecs : newReplicationSpecs (ctx , types .StringValue ("Zone 1" ), []TFRegionConfig {regionConfigNodeCountUnknown }),
326
+ },
327
+ keepUnknownCalls : useStateOnlyWhenNodeCount0 ,
328
+ },
329
+ "respect multiple keepUnknownCall" : {
330
+ src : & TFSimpleModel {
331
+ ProjectID : types .StringValue ("src-project" ),
332
+ Name : types .StringValue ("src-name" ),
333
+ ReplicationSpecs : newReplicationSpecs (ctx , types .StringValue ("Zone 1" ), []TFRegionConfig {regionConfigSrc }),
334
+ },
335
+ dest : & TFSimpleModel {
336
+ ProjectID : types .StringUnknown (),
337
+ Name : types .StringUnknown (),
338
+ ReplicationSpecs : newReplicationSpecs (ctx , types .StringUnknown (), []TFRegionConfig {regionConfigNodeCountUnknown }),
339
+ },
340
+ expectedDest : & TFSimpleModel {
341
+ ProjectID : types .StringUnknown (),
342
+ Name : types .StringValue ("src-name" ),
343
+ ReplicationSpecs : newReplicationSpecs (ctx , types .StringValue ("Zone 1" ), []TFRegionConfig {regionConfigNodeCountUnknown }),
344
+ },
345
+ keepUnknownCalls : schemafunc .KeepUnknownFuncOr (keepProjectIDUnknown , useStateOnlyWhenNodeCount0 ),
346
+ },
347
+ "copy node_count 0" : {
348
+ src : & TFSimpleModel {
349
+ ReplicationSpecs : newReplicationSpecs (ctx , types .StringValue ("Zone 1" ), []TFRegionConfig {regionConfigNodeCount0 }),
350
+ },
351
+ dest : & TFSimpleModel {
352
+ ReplicationSpecs : newReplicationSpecs (ctx , types .StringUnknown (), []TFRegionConfig {regionConfigNodeCountUnknown }),
353
+ },
354
+ expectedDest : & TFSimpleModel {
355
+ ReplicationSpecs : newReplicationSpecs (ctx , types .StringValue ("Zone 1" ), []TFRegionConfig {regionConfigNodeCount0 }),
356
+ },
357
+ keepUnknownCalls : useStateOnlyWhenNodeCount0 ,
358
+ },
359
+ "keepUnknownCall on string" : {
360
+ src : & TFSimpleModel {
361
+ ReplicationSpecs : newReplicationSpecs (ctx , types .StringValue ("Zone 1" ), []TFRegionConfig {
362
+ regionConfigWithAutoScaling (autoScalingFalseAndEmpty ),
363
+ regionConfigWithAutoScaling (autoScalingFalseAndNull ),
364
+ regionConfigWithAutoScaling (autoScalingTrueAndNonEmpty ),
365
+ }),
366
+ },
367
+ dest : & TFSimpleModel {
368
+ ReplicationSpecs : newReplicationSpecs (ctx , types .StringUnknown (), []TFRegionConfig {
369
+ regionConfigWithAutoScaling (autoScalingUnknown ),
370
+ regionConfigWithAutoScaling (autoScalingUnknown ),
371
+ regionConfigWithAutoScaling (autoScalingUnknown ),
372
+ }),
373
+ },
374
+ expectedDest : & TFSimpleModel {
375
+ ReplicationSpecs : newReplicationSpecs (ctx , types .StringValue ("Zone 1" ), []TFRegionConfig {
376
+ regionConfigWithAutoScaling (autoScalingFalseAndEmpty ),
377
+ regionConfigWithAutoScaling (autoScalingFalseAndNull ),
378
+ regionConfigWithAutoScaling (autoScalingLeafsUnknown ),
379
+ }),
380
+ },
381
+ keepUnknownCalls : schemafunc .KeepUnknownFuncOr (autoScalingStringsKeepUnknown , autoScalingBoolsKeepUnknown ),
382
+ },
228
383
"non-pointer input" : {
229
384
src : & TFSimpleModel {},
230
385
dest : nil ,
@@ -301,11 +456,11 @@ func TestCopyUnknowns(t *testing.T) {
301
456
t .Run (name , func (t * testing.T ) {
302
457
if tc .panicMessage != "" {
303
458
assert .PanicsWithValue (t , tc .panicMessage , func () {
304
- schemafunc .CopyUnknowns (ctx , tc .src , tc .dest , tc .keepUnknown )
459
+ schemafunc .CopyUnknowns (ctx , tc .src , tc .dest , tc .keepUnknown , nil )
305
460
})
306
461
return
307
462
}
308
- schemafunc .CopyUnknowns (ctx , tc .src , tc .dest , tc .keepUnknown )
463
+ schemafunc .CopyUnknowns (ctx , tc .src , tc .dest , tc .keepUnknown , tc . keepUnknownCalls )
309
464
assert .Equal (t , * tc .expectedDest , * tc .dest )
310
465
})
311
466
}
@@ -324,7 +479,7 @@ func newReplicationSpecs(ctx context.Context, zoneName types.String, regionConfi
324
479
for i , config := range regionConfigs {
325
480
configObject , diags := types .ObjectValueFrom (ctx , RegionConfigsObjType .AttrTypes , config )
326
481
if diags .HasError () {
327
- panic ("failed to create region config object" )
482
+ panic (fmt . Sprintf ( "failed to create region config object %v" , diags ) )
328
483
}
329
484
regionConfigsObjects [i ] = configObject
330
485
}
@@ -346,3 +501,12 @@ func combineReplicationSpecs(specs ...types.List) types.List {
346
501
}
347
502
return types .ListValueMust (ReplicationSpecsObjType , combined )
348
503
}
504
+
505
+ func regionConfigWithAutoScaling (autoScaling types.Object ) TFRegionConfig {
506
+ return TFRegionConfig {
507
+ AutoScaling : autoScaling ,
508
+ ProviderName : types .StringValue ("aws" ),
509
+ RegionName : types .StringValue ("US_EAST_1" ),
510
+ Spec : asObjectValue (ctx , TFSpec {InstanceSize : types .StringValue ("M10" ), NodeCount : types .Int64Value (3 )}, SpecObjType .AttrTypes ),
511
+ }
512
+ }
0 commit comments