4
4
"context"
5
5
"testing"
6
6
7
+ "github.com/hashicorp/terraform-plugin-framework/attr"
7
8
"github.com/hashicorp/terraform-plugin-framework/resource"
8
9
rschema "github.com/hashicorp/terraform-plugin-framework/resource/schema"
9
10
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
@@ -151,7 +152,80 @@ func TestDetailedDiffSet(t *testing.T) {
151
152
},
152
153
})
153
154
154
- computedCreateFunc := func (ctx context.Context , req resource.CreateRequest , resp * resource.CreateResponse ) {
155
+ computedAttributeCreateFunc := func (ctx context.Context , req resource.CreateRequest , resp * resource.CreateResponse ) {
156
+ type ObjectModel struct {
157
+ ID types.String `tfsdk:"id"`
158
+ Keys types.Set `tfsdk:"key"`
159
+ }
160
+ reqVal := ObjectModel {}
161
+ diags := req .Plan .Get (ctx , & reqVal )
162
+ contract .Assertf (diags .ErrorsCount () == 0 , "failed to get attribute: %v" , diags )
163
+
164
+ respVal := ObjectModel {
165
+ ID : types .StringValue ("test-id" ),
166
+ }
167
+ if reqVal .Keys .IsUnknown () {
168
+ respVal .Keys = types .SetValueMust (types .StringType , []attr.Value {
169
+ types .StringValue ("value" ),
170
+ })
171
+ } else {
172
+ respVal .Keys = reqVal .Keys
173
+ }
174
+
175
+ diags = resp .State .Set (ctx , & respVal )
176
+ contract .Assertf (diags .ErrorsCount () == 0 , "failed to set attribute: %v" , diags )
177
+ }
178
+
179
+ computedAttributeUpdateFunc := func (ctx context.Context , req resource.UpdateRequest , resp * resource.UpdateResponse ) {
180
+ createResp := resource.CreateResponse {
181
+ State : resp .State ,
182
+ Diagnostics : resp .Diagnostics ,
183
+ }
184
+ computedAttributeCreateFunc (ctx , resource.CreateRequest {
185
+ Plan : req .Plan ,
186
+ Config : req .Config ,
187
+ ProviderMeta : req .ProviderMeta ,
188
+ }, & createResp )
189
+ resp .State = createResp .State
190
+ resp .Diagnostics = createResp .Diagnostics
191
+ }
192
+
193
+ computedSetAttributeSchema := pb .NewResource (pb.NewResourceArgs {
194
+ ResourceSchema : rschema.Schema {
195
+ Attributes : map [string ]rschema.Attribute {
196
+ "key" : rschema.SetAttribute {
197
+ Optional : true ,
198
+ ElementType : types .StringType ,
199
+ Computed : true ,
200
+ PlanModifiers : []planmodifier.Set {
201
+ setplanmodifier .UseStateForUnknown (),
202
+ },
203
+ },
204
+ },
205
+ },
206
+ CreateFunc : computedAttributeCreateFunc ,
207
+ UpdateFunc : computedAttributeUpdateFunc ,
208
+ })
209
+
210
+ computedSetAttributeReplaceSchema := pb .NewResource (pb.NewResourceArgs {
211
+ ResourceSchema : rschema.Schema {
212
+ Attributes : map [string ]rschema.Attribute {
213
+ "key" : rschema.SetAttribute {
214
+ Optional : true ,
215
+ ElementType : types .StringType ,
216
+ Computed : true ,
217
+ PlanModifiers : []planmodifier.Set {
218
+ setplanmodifier .RequiresReplace (),
219
+ setplanmodifier .UseStateForUnknown (),
220
+ },
221
+ },
222
+ },
223
+ },
224
+ CreateFunc : computedAttributeCreateFunc ,
225
+ UpdateFunc : computedAttributeUpdateFunc ,
226
+ })
227
+
228
+ computedBlockCreateFunc := func (ctx context.Context , req resource.CreateRequest , resp * resource.CreateResponse ) {
155
229
type Nested struct {
156
230
Nested types.String `tfsdk:"nested"`
157
231
Computed types.String `tfsdk:"computed"`
@@ -192,12 +266,12 @@ func TestDetailedDiffSet(t *testing.T) {
192
266
contract .Assertf (diags .ErrorsCount () == 0 , "failed to set attribute: %v" , diags )
193
267
}
194
268
195
- computedUpdateFunc := func (ctx context.Context , req resource.UpdateRequest , resp * resource.UpdateResponse ) {
269
+ computedBlockUpdateFunc := func (ctx context.Context , req resource.UpdateRequest , resp * resource.UpdateResponse ) {
196
270
createResp := resource.CreateResponse {
197
271
State : resp .State ,
198
272
Diagnostics : resp .Diagnostics ,
199
273
}
200
- computedCreateFunc (ctx , resource.CreateRequest {
274
+ computedBlockCreateFunc (ctx , resource.CreateRequest {
201
275
Plan : req .Plan ,
202
276
Config : req .Config ,
203
277
ProviderMeta : req .ProviderMeta ,
@@ -226,8 +300,8 @@ func TestDetailedDiffSet(t *testing.T) {
226
300
},
227
301
},
228
302
},
229
- CreateFunc : computedCreateFunc ,
230
- UpdateFunc : computedUpdateFunc ,
303
+ CreateFunc : computedBlockCreateFunc ,
304
+ UpdateFunc : computedBlockUpdateFunc ,
231
305
})
232
306
233
307
blockSchemaWithComputedNoStateForUnknown := pb .NewResource (pb.NewResourceArgs {
@@ -246,8 +320,8 @@ func TestDetailedDiffSet(t *testing.T) {
246
320
},
247
321
},
248
322
},
249
- CreateFunc : computedCreateFunc ,
250
- UpdateFunc : computedUpdateFunc ,
323
+ CreateFunc : computedBlockCreateFunc ,
324
+ UpdateFunc : computedBlockUpdateFunc ,
251
325
})
252
326
253
327
blockSchemaWithComputedReplace := pb .NewResource (pb.NewResourceArgs {
@@ -272,8 +346,8 @@ func TestDetailedDiffSet(t *testing.T) {
272
346
},
273
347
},
274
348
},
275
- CreateFunc : computedCreateFunc ,
276
- UpdateFunc : computedUpdateFunc ,
349
+ CreateFunc : computedBlockCreateFunc ,
350
+ UpdateFunc : computedBlockUpdateFunc ,
277
351
})
278
352
279
353
blockSchemaWithComputedNestedReplace := pb .NewResource (pb.NewResourceArgs {
@@ -300,8 +374,8 @@ func TestDetailedDiffSet(t *testing.T) {
300
374
},
301
375
},
302
376
},
303
- CreateFunc : computedCreateFunc ,
304
- UpdateFunc : computedUpdateFunc ,
377
+ CreateFunc : computedBlockCreateFunc ,
378
+ UpdateFunc : computedBlockUpdateFunc ,
305
379
})
306
380
307
381
blockSchemaWithComputedComputedRequiresReplace := pb .NewResource (pb.NewResourceArgs {
@@ -324,8 +398,8 @@ func TestDetailedDiffSet(t *testing.T) {
324
398
},
325
399
},
326
400
},
327
- CreateFunc : computedCreateFunc ,
328
- UpdateFunc : computedUpdateFunc ,
401
+ CreateFunc : computedBlockCreateFunc ,
402
+ UpdateFunc : computedBlockUpdateFunc ,
329
403
})
330
404
331
405
attrList := func (arr * []string ) cty.Value {
@@ -393,7 +467,11 @@ func TestDetailedDiffSet(t *testing.T) {
393
467
{"block requires replace" , blockReplaceSchema , nestedAttrList },
394
468
{"block nested requires replace" , blockNestedReplaceSchema , nestedAttrList },
395
469
396
- // Computed, each state we test both the behaviour when the computed value is specified in the program and when it is not.
470
+ // Computed attributes
471
+ {"attribute with computed no replace" , computedSetAttributeSchema , attrList },
472
+ {"attribute with computed requires replace" , computedSetAttributeReplaceSchema , attrList },
473
+
474
+ // Computed blocks, each state we test both the behaviour when the computed value is specified in the program and when it is not.
397
475
{"block with computed no replace computed" , blockSchemaWithComputed , nestedAttrList },
398
476
{"block with computed no replace computed specified in program" , blockSchemaWithComputed , nestedAttrListWithComputedSpecified },
399
477
{"block with computed requires replace" , blockSchemaWithComputedReplace , nestedAttrList },
0 commit comments