Skip to content

Commit 49806ea

Browse files
committed
expose diagnostics when parsing model
1 parent c0c6a00 commit 49806ea

File tree

1 file changed

+43
-11
lines changed

1 file changed

+43
-11
lines changed

internal/provider/resource_tfe_team_notification_configuration.go

Lines changed: 43 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/hashicorp/terraform-plugin-framework-validators/setvalidator"
1212
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
1313
"github.com/hashicorp/terraform-plugin-framework/attr"
14+
"github.com/hashicorp/terraform-plugin-framework/diag"
1415
"github.com/hashicorp/terraform-plugin-framework/path"
1516
"github.com/hashicorp/terraform-plugin-framework/resource"
1617
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
@@ -56,7 +57,7 @@ type modelTFETeamNotificationConfiguration struct {
5657

5758
// modelFromTFETeamNotificationConfiguration builds a modelTFETeamNotificationConfiguration
5859
// struct from a tfe.TeamNotificationConfiguration value.
59-
func modelFromTFETeamNotificationConfiguration(v *tfe.NotificationConfiguration) modelTFETeamNotificationConfiguration {
60+
func modelFromTFETeamNotificationConfiguration(v *tfe.NotificationConfiguration) (*modelTFETeamNotificationConfiguration, *diag.Diagnostics) {
6061
result := modelTFETeamNotificationConfiguration{
6162
ID: types.StringValue(v.ID),
6263
Name: types.StringValue(v.Name),
@@ -65,21 +66,38 @@ func modelFromTFETeamNotificationConfiguration(v *tfe.NotificationConfiguration)
6566
TeamID: types.StringValue(v.SubscribableChoice.Team.ID),
6667
}
6768

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+
}
6976
result.EmailAddresses = emailAddresses
7077
}
7178

7279
if len(v.Triggers) == 0 {
7380
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+
7587
result.Triggers = triggers
88+
7689
}
7790

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)
81100
}
82-
result.EmailUserIDs = types.SetValueMust(types.StringType, emailUserIDs)
83101

84102
if v.Token != "" {
85103
result.Token = types.StringValue(v.Token)
@@ -89,7 +107,7 @@ func modelFromTFETeamNotificationConfiguration(v *tfe.NotificationConfiguration)
89107
result.URL = types.StringValue(v.URL)
90108
}
91109

92-
return result
110+
return &result, nil
93111
}
94112

95113
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
298316
return
299317
} else if len(tnc.EmailUsers) != len(plan.EmailUserIDs.Elements()) {
300318
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
301320
}
302321

303322
// Restore token from plan because it is write only
304323
if !plan.Token.IsNull() {
305324
tnc.Token = plan.Token.ValueString()
306325
}
307326

308-
result := modelFromTFETeamNotificationConfiguration(tnc)
327+
result, diags := modelFromTFETeamNotificationConfiguration(tnc)
328+
if diags != nil && diags.HasError() {
329+
resp.Diagnostics.Append((*diags)...)
330+
return
331+
}
309332

310333
// Save data into Terraform state
311334
resp.Diagnostics.Append(resp.State.Set(ctx, &result)...)
@@ -333,7 +356,11 @@ func (r *resourceTFETeamNotificationConfiguration) Read(ctx context.Context, req
333356
tnc.Token = state.Token.ValueString()
334357
}
335358

336-
result := modelFromTFETeamNotificationConfiguration(tnc)
359+
result, diags := modelFromTFETeamNotificationConfiguration(tnc)
360+
if diags != nil && diags.HasError() {
361+
resp.Diagnostics.Append((*diags)...)
362+
return
363+
}
337364

338365
// Save updated data into Terraform state
339366
resp.Diagnostics.Append(resp.State.Set(ctx, &result)...)
@@ -401,14 +428,19 @@ func (r *resourceTFETeamNotificationConfiguration) Update(ctx context.Context, r
401428
return
402429
} else if len(tnc.EmailUsers) != len(plan.EmailUserIDs.Elements()) {
403430
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
404432
}
405433

406434
// Restore token from plan because it is write only
407435
if !plan.Token.IsNull() {
408436
tnc.Token = plan.Token.ValueString()
409437
}
410438

411-
result := modelFromTFETeamNotificationConfiguration(tnc)
439+
result, diags := modelFromTFETeamNotificationConfiguration(tnc)
440+
if diags != nil && diags.HasError() {
441+
resp.Diagnostics.Append((*diags)...)
442+
return
443+
}
412444

413445
// Save data into Terraform state
414446
resp.Diagnostics.Append(resp.State.Set(ctx, &result)...)

0 commit comments

Comments
 (0)