@@ -112,6 +112,9 @@ func TestServerApplyResourceChange(t *testing.T) {
112
112
if data .TestRequired .Value != "test-config-value" {
113
113
resp .Diagnostics .AddError ("unexpected req.Config value: %s" , data .TestRequired .Value )
114
114
}
115
+
116
+ // Prevent missing resource state error diagnostic
117
+ resp .Diagnostics .Append (resp .State .Set (ctx , & data )... )
115
118
},
116
119
DeleteMethod : func (_ context.Context , _ tfsdk.DeleteResourceRequest , resp * tfsdk.DeleteResourceResponse ) {
117
120
resp .Diagnostics .AddError ("Unexpected Method Call" , "Expected: Create, Got: Delete" )
@@ -124,8 +127,13 @@ func TestServerApplyResourceChange(t *testing.T) {
124
127
},
125
128
},
126
129
expectedResponse : & fwserver.ApplyResourceChangeResponse {
127
- // Intentionally empty, Create implementation does not call resp.State.Set()
128
- NewState : testEmptyState ,
130
+ NewState : & tfsdk.State {
131
+ Raw : tftypes .NewValue (testSchemaType , map [string ]tftypes.Value {
132
+ "test_computed" : tftypes .NewValue (tftypes .String , nil ),
133
+ "test_required" : tftypes .NewValue (tftypes .String , "test-config-value" ),
134
+ }),
135
+ Schema : testSchema ,
136
+ },
129
137
},
130
138
},
131
139
"create-request-plannedstate" : {
@@ -156,6 +164,9 @@ func TestServerApplyResourceChange(t *testing.T) {
156
164
if data .TestRequired .Value != "test-plannedstate-value" {
157
165
resp .Diagnostics .AddError ("unexpected req.Plan value: %s" , data .TestRequired .Value )
158
166
}
167
+
168
+ // Prevent missing resource state error diagnostic
169
+ resp .Diagnostics .Append (resp .State .Set (ctx , & data )... )
159
170
},
160
171
DeleteMethod : func (_ context.Context , _ tfsdk.DeleteResourceRequest , resp * tfsdk.DeleteResourceResponse ) {
161
172
resp .Diagnostics .AddError ("Unexpected Method Call" , "Expected: Create, Got: Delete" )
@@ -168,15 +179,27 @@ func TestServerApplyResourceChange(t *testing.T) {
168
179
},
169
180
},
170
181
expectedResponse : & fwserver.ApplyResourceChangeResponse {
171
- // Intentionally empty, Create implementation does not call resp.State.Set()
172
- NewState : testEmptyState ,
182
+ NewState : & tfsdk.State {
183
+ Raw : tftypes .NewValue (testSchemaType , map [string ]tftypes.Value {
184
+ "test_computed" : tftypes .NewValue (tftypes .String , nil ),
185
+ "test_required" : tftypes .NewValue (tftypes .String , "test-plannedstate-value" ),
186
+ }),
187
+ Schema : testSchema ,
188
+ },
173
189
},
174
190
},
175
191
"create-request-providermeta" : {
176
192
server : & fwserver.Server {
177
193
Provider : & testprovider.Provider {},
178
194
},
179
195
request : & fwserver.ApplyResourceChangeRequest {
196
+ PlannedState : & tfsdk.Plan {
197
+ Raw : tftypes .NewValue (testSchemaType , map [string ]tftypes.Value {
198
+ "test_computed" : tftypes .NewValue (tftypes .String , nil ),
199
+ "test_required" : tftypes .NewValue (tftypes .String , "test-plannedstate-value" ),
200
+ }),
201
+ Schema : testSchema ,
202
+ },
180
203
PriorState : testEmptyState ,
181
204
ResourceSchema : testSchema ,
182
205
ResourceType : & testprovider.ResourceType {
@@ -186,13 +209,19 @@ func TestServerApplyResourceChange(t *testing.T) {
186
209
NewResourceMethod : func (_ context.Context , _ tfsdk.Provider ) (tfsdk.Resource , diag.Diagnostics ) {
187
210
return & testprovider.Resource {
188
211
CreateMethod : func (ctx context.Context , req tfsdk.CreateResourceRequest , resp * tfsdk.CreateResourceResponse ) {
189
- var data testProviderMetaData
212
+ var metadata testProviderMetaData
190
213
191
- resp .Diagnostics .Append (req .ProviderMeta .Get (ctx , & data )... )
214
+ resp .Diagnostics .Append (req .ProviderMeta .Get (ctx , & metadata )... )
192
215
193
- if data .TestProviderMetaAttribute .Value != "test-provider-meta-value" {
194
- resp .Diagnostics .AddError ("unexpected req.ProviderMeta value: %s " , data .TestProviderMetaAttribute .Value )
216
+ if metadata .TestProviderMetaAttribute .Value != "test-provider-meta-value" {
217
+ resp .Diagnostics .AddError ("Unexpected req.ProviderMeta Value " , "Got: " + metadata .TestProviderMetaAttribute .Value )
195
218
}
219
+
220
+ // Prevent missing resource state error diagnostic
221
+ var data testSchemaData
222
+
223
+ resp .Diagnostics .Append (req .Plan .Get (ctx , & data )... )
224
+ resp .Diagnostics .Append (resp .State .Set (ctx , & data )... )
196
225
},
197
226
DeleteMethod : func (_ context.Context , _ tfsdk.DeleteResourceRequest , resp * tfsdk.DeleteResourceResponse ) {
198
227
resp .Diagnostics .AddError ("Unexpected Method Call" , "Expected: Create, Got: Delete" )
@@ -206,8 +235,13 @@ func TestServerApplyResourceChange(t *testing.T) {
206
235
ProviderMeta : testProviderMetaConfig ,
207
236
},
208
237
expectedResponse : & fwserver.ApplyResourceChangeResponse {
209
- // Intentionally empty, Create implementation does not call resp.State.Set()
210
- NewState : testEmptyState ,
238
+ NewState : & tfsdk.State {
239
+ Raw : tftypes .NewValue (testSchemaType , map [string ]tftypes.Value {
240
+ "test_computed" : tftypes .NewValue (tftypes .String , nil ),
241
+ "test_required" : tftypes .NewValue (tftypes .String , "test-plannedstate-value" ),
242
+ }),
243
+ Schema : testSchema ,
244
+ },
211
245
},
212
246
},
213
247
"create-response-diagnostics" : {
@@ -305,6 +339,59 @@ func TestServerApplyResourceChange(t *testing.T) {
305
339
},
306
340
},
307
341
},
342
+ "create-response-newstate-null" : {
343
+ server : & fwserver.Server {
344
+ Provider : & testprovider.Provider {},
345
+ },
346
+ request : & fwserver.ApplyResourceChangeRequest {
347
+ Config : & tfsdk.Config {
348
+ Raw : tftypes .NewValue (testSchemaType , map [string ]tftypes.Value {
349
+ "test_computed" : tftypes .NewValue (tftypes .String , nil ),
350
+ "test_required" : tftypes .NewValue (tftypes .String , "test-config-value" ),
351
+ }),
352
+ Schema : testSchema ,
353
+ },
354
+ PlannedState : & tfsdk.Plan {
355
+ Raw : tftypes .NewValue (testSchemaType , map [string ]tftypes.Value {
356
+ "test_computed" : tftypes .NewValue (tftypes .String , "test-plannedstate-value" ),
357
+ "test_required" : tftypes .NewValue (tftypes .String , "test-config-value" ),
358
+ }),
359
+ Schema : testSchema ,
360
+ },
361
+ PriorState : testEmptyState ,
362
+ ResourceSchema : testSchema ,
363
+ ResourceType : & testprovider.ResourceType {
364
+ GetSchemaMethod : func (_ context.Context ) (tfsdk.Schema , diag.Diagnostics ) {
365
+ return testSchema , nil
366
+ },
367
+ NewResourceMethod : func (_ context.Context , _ tfsdk.Provider ) (tfsdk.Resource , diag.Diagnostics ) {
368
+ return & testprovider.Resource {
369
+ CreateMethod : func (ctx context.Context , req tfsdk.CreateResourceRequest , resp * tfsdk.CreateResourceResponse ) {
370
+ // Intentionally missing resp.State.Set()
371
+ },
372
+ DeleteMethod : func (_ context.Context , _ tfsdk.DeleteResourceRequest , resp * tfsdk.DeleteResourceResponse ) {
373
+ resp .Diagnostics .AddError ("Unexpected Method Call" , "Expected: Create, Got: Delete" )
374
+ },
375
+ UpdateMethod : func (_ context.Context , _ tfsdk.UpdateResourceRequest , resp * tfsdk.UpdateResourceResponse ) {
376
+ resp .Diagnostics .AddError ("Unexpected Method Call" , "Expected: Create, Got: Update" )
377
+ },
378
+ }, nil
379
+ },
380
+ },
381
+ },
382
+ expectedResponse : & fwserver.ApplyResourceChangeResponse {
383
+ Diagnostics : diag.Diagnostics {
384
+ diag .NewErrorDiagnostic (
385
+ "Missing Resource State After Create" ,
386
+ "The Terraform Provider unexpectedly returned no resource state after having no errors in the resource creation. " +
387
+ "This is always an issue in the Terraform Provider and should be reported to the provider developers.\n \n " +
388
+ "The resource may have been successfully created, but Terraform is not tracking it. " +
389
+ "Applying the configuration again with no other action may result in duplicate resource errors." ,
390
+ ),
391
+ },
392
+ NewState : testEmptyState ,
393
+ },
394
+ },
308
395
"delete-request-priorstate" : {
309
396
server : & fwserver.Server {
310
397
Provider : & testprovider.Provider {},
@@ -862,6 +949,63 @@ func TestServerApplyResourceChange(t *testing.T) {
862
949
},
863
950
},
864
951
},
952
+ "update-response-newstate-null" : {
953
+ server : & fwserver.Server {
954
+ Provider : & testprovider.Provider {},
955
+ },
956
+ request : & fwserver.ApplyResourceChangeRequest {
957
+ Config : & tfsdk.Config {
958
+ Raw : tftypes .NewValue (testSchemaType , map [string ]tftypes.Value {
959
+ "test_computed" : tftypes .NewValue (tftypes .String , nil ),
960
+ "test_required" : tftypes .NewValue (tftypes .String , "test-new-value" ),
961
+ }),
962
+ Schema : testSchema ,
963
+ },
964
+ PlannedState : & tfsdk.Plan {
965
+ Raw : tftypes .NewValue (testSchemaType , map [string ]tftypes.Value {
966
+ "test_computed" : tftypes .NewValue (tftypes .String , "test-plannedstate-value" ),
967
+ "test_required" : tftypes .NewValue (tftypes .String , "test-new-value" ),
968
+ }),
969
+ Schema : testSchema ,
970
+ },
971
+ PriorState : & tfsdk.State {
972
+ Raw : tftypes .NewValue (testSchemaType , map [string ]tftypes.Value {
973
+ "test_computed" : tftypes .NewValue (tftypes .String , nil ),
974
+ "test_required" : tftypes .NewValue (tftypes .String , "test-old-value" ),
975
+ }),
976
+ Schema : testSchema ,
977
+ },
978
+ ResourceSchema : testSchema ,
979
+ ResourceType : & testprovider.ResourceType {
980
+ GetSchemaMethod : func (_ context.Context ) (tfsdk.Schema , diag.Diagnostics ) {
981
+ return testSchema , nil
982
+ },
983
+ NewResourceMethod : func (_ context.Context , _ tfsdk.Provider ) (tfsdk.Resource , diag.Diagnostics ) {
984
+ return & testprovider.Resource {
985
+ CreateMethod : func (_ context.Context , _ tfsdk.CreateResourceRequest , resp * tfsdk.CreateResourceResponse ) {
986
+ resp .Diagnostics .AddError ("Unexpected Method Call" , "Expected: Update, Got: Create" )
987
+ },
988
+ DeleteMethod : func (_ context.Context , _ tfsdk.DeleteResourceRequest , resp * tfsdk.DeleteResourceResponse ) {
989
+ resp .Diagnostics .AddError ("Unexpected Method Call" , "Expected: Update, Got: Delete" )
990
+ },
991
+ UpdateMethod : func (ctx context.Context , req tfsdk.UpdateResourceRequest , resp * tfsdk.UpdateResourceResponse ) {
992
+ resp .State .RemoveResource (ctx )
993
+ },
994
+ }, nil
995
+ },
996
+ },
997
+ },
998
+ expectedResponse : & fwserver.ApplyResourceChangeResponse {
999
+ Diagnostics : diag.Diagnostics {
1000
+ diag .NewErrorDiagnostic (
1001
+ "Missing Resource State After Update" ,
1002
+ "The Terraform Provider unexpectedly returned no resource state after having no errors in the resource update. " +
1003
+ "This is always an issue in the Terraform Provider and should be reported to the provider developers." ,
1004
+ ),
1005
+ },
1006
+ NewState : testEmptyState ,
1007
+ },
1008
+ },
865
1009
}
866
1010
867
1011
for name , testCase := range testCases {
0 commit comments