@@ -11,6 +11,7 @@ import (
11
11
"github.com/hashicorp/terraform-plugin-framework-validators/setvalidator"
12
12
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
13
13
"github.com/hashicorp/terraform-plugin-framework/attr"
14
+ "github.com/hashicorp/terraform-plugin-framework/diag"
14
15
"github.com/hashicorp/terraform-plugin-framework/path"
15
16
"github.com/hashicorp/terraform-plugin-framework/resource"
16
17
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
@@ -56,7 +57,7 @@ type modelTFETeamNotificationConfiguration struct {
56
57
57
58
// modelFromTFETeamNotificationConfiguration builds a modelTFETeamNotificationConfiguration
58
59
// struct from a tfe.TeamNotificationConfiguration value.
59
- func modelFromTFETeamNotificationConfiguration (v * tfe.NotificationConfiguration ) modelTFETeamNotificationConfiguration {
60
+ func modelFromTFETeamNotificationConfiguration (v * tfe.NotificationConfiguration ) ( * modelTFETeamNotificationConfiguration , * diag. Diagnostics ) {
60
61
result := modelTFETeamNotificationConfiguration {
61
62
ID : types .StringValue (v .ID ),
62
63
Name : types .StringValue (v .Name ),
@@ -65,21 +66,38 @@ func modelFromTFETeamNotificationConfiguration(v *tfe.NotificationConfiguration)
65
66
TeamID : types .StringValue (v .SubscribableChoice .Team .ID ),
66
67
}
67
68
68
- if emailAddresses , err := types .SetValueFrom (ctx , types .StringType , v .EmailAddresses ); err == nil {
69
+ if len (v .EmailAddresses ) == 0 {
70
+ result .EmailAddresses = types .SetNull (types .StringType )
71
+ } else {
72
+ emailAddresses , diags := types .SetValueFrom (ctx , types .StringType , v .EmailAddresses )
73
+ if diags != nil && diags .HasError () {
74
+ return nil , & diags
75
+ }
69
76
result .EmailAddresses = emailAddresses
70
77
}
71
78
72
79
if len (v .Triggers ) == 0 {
73
80
result .Triggers = types .SetNull (types .StringType )
74
- } else if triggers , err := types .SetValueFrom (ctx , types .StringType , v .Triggers ); err == nil {
81
+ } else {
82
+ triggers , diags := types .SetValueFrom (ctx , types .StringType , v .Triggers )
83
+ if diags != nil && diags .HasError () {
84
+ return nil , & diags
85
+ }
86
+
75
87
result .Triggers = triggers
88
+
76
89
}
77
90
78
- emailUserIDs := make ([]attr.Value , len (v .EmailUsers ))
79
- for i , emailUser := range v .EmailUsers {
80
- emailUserIDs [i ] = types .StringValue (emailUser .ID )
91
+ if len (v .EmailUsers ) == 0 {
92
+ result .EmailUserIDs = types .SetNull (types .StringType )
93
+ } else {
94
+ emailUserIDs := make ([]attr.Value , len (v .EmailUsers ))
95
+ for i , emailUser := range v .EmailUsers {
96
+ emailUserIDs [i ] = types .StringValue (emailUser .ID )
97
+ }
98
+
99
+ result .EmailUserIDs = types .SetValueMust (types .StringType , emailUserIDs )
81
100
}
82
- result .EmailUserIDs = types .SetValueMust (types .StringType , emailUserIDs )
83
101
84
102
if v .Token != "" {
85
103
result .Token = types .StringValue (v .Token )
@@ -89,7 +107,7 @@ func modelFromTFETeamNotificationConfiguration(v *tfe.NotificationConfiguration)
89
107
result .URL = types .StringValue (v .URL )
90
108
}
91
109
92
- return result
110
+ return & result , nil
93
111
}
94
112
95
113
func (r * resourceTFETeamNotificationConfiguration ) Schema (ctx context.Context , req resource.SchemaRequest , resp * resource.SchemaResponse ) {
@@ -298,14 +316,19 @@ func (r *resourceTFETeamNotificationConfiguration) Create(ctx context.Context, r
298
316
return
299
317
} else if len (tnc .EmailUsers ) != len (plan .EmailUserIDs .Elements ()) {
300
318
resp .Diagnostics .AddError ("Email user IDs produced an inconsistent result" , "API returned a different number of email user IDs than were provided in the plan." )
319
+ return
301
320
}
302
321
303
322
// Restore token from plan because it is write only
304
323
if ! plan .Token .IsNull () {
305
324
tnc .Token = plan .Token .ValueString ()
306
325
}
307
326
308
- result := modelFromTFETeamNotificationConfiguration (tnc )
327
+ result , diags := modelFromTFETeamNotificationConfiguration (tnc )
328
+ if diags != nil && diags .HasError () {
329
+ resp .Diagnostics .Append ((* diags )... )
330
+ return
331
+ }
309
332
310
333
// Save data into Terraform state
311
334
resp .Diagnostics .Append (resp .State .Set (ctx , & result )... )
@@ -333,7 +356,11 @@ func (r *resourceTFETeamNotificationConfiguration) Read(ctx context.Context, req
333
356
tnc .Token = state .Token .ValueString ()
334
357
}
335
358
336
- result := modelFromTFETeamNotificationConfiguration (tnc )
359
+ result , diags := modelFromTFETeamNotificationConfiguration (tnc )
360
+ if diags != nil && diags .HasError () {
361
+ resp .Diagnostics .Append ((* diags )... )
362
+ return
363
+ }
337
364
338
365
// Save updated data into Terraform state
339
366
resp .Diagnostics .Append (resp .State .Set (ctx , & result )... )
@@ -401,14 +428,19 @@ func (r *resourceTFETeamNotificationConfiguration) Update(ctx context.Context, r
401
428
return
402
429
} else if len (tnc .EmailUsers ) != len (plan .EmailUserIDs .Elements ()) {
403
430
resp .Diagnostics .AddError ("Email user IDs produced an inconsistent result" , "API returned a different number of email user IDs than were provided in the plan." )
431
+ return
404
432
}
405
433
406
434
// Restore token from plan because it is write only
407
435
if ! plan .Token .IsNull () {
408
436
tnc .Token = plan .Token .ValueString ()
409
437
}
410
438
411
- result := modelFromTFETeamNotificationConfiguration (tnc )
439
+ result , diags := modelFromTFETeamNotificationConfiguration (tnc )
440
+ if diags != nil && diags .HasError () {
441
+ resp .Diagnostics .Append ((* diags )... )
442
+ return
443
+ }
412
444
413
445
// Save data into Terraform state
414
446
resp .Diagnostics .Append (resp .State .Set (ctx , & result )... )
0 commit comments