@@ -5,6 +5,7 @@ package provider
55
66import (
77 "context"
8+ "errors"
89 "fmt"
910
1011 tfe "github.com/hashicorp/go-tfe"
@@ -328,28 +329,29 @@ func (r *resourceTFENotificationConfiguration) Create(ctx context.Context, req r
328329
329330 // Add email_addresses set to the options struct
330331 emailAddresses := make ([]types.String , len (plan .EmailAddresses .Elements ()))
331- if diags := plan .EmailAddresses .ElementsAs (ctx , & emailAddresses , true ); diags != nil && diags . HasError () {
332- resp .Diagnostics .Append ( diags ... )
332+ resp . Diagnostics . Append ( plan .EmailAddresses .ElementsAs (ctx , & emailAddresses , true )... )
333+ if resp .Diagnostics .HasError () {
333334 return
334335 }
335-
336336 options .EmailAddresses = []string {}
337337 for _ , emailAddress := range emailAddresses {
338338 options .EmailAddresses = append (options .EmailAddresses , emailAddress .ValueString ())
339339 }
340340
341341 // Add email_user_ids set to the options struct
342342 emailUserIDs := make ([]types.String , len (plan .EmailUserIDs .Elements ()))
343- if diags := plan .EmailUserIDs .ElementsAs (ctx , & emailUserIDs , true ); diags != nil && diags . HasError () {
344- resp .Diagnostics .Append ( diags ... )
343+ resp . Diagnostics . Append ( plan .EmailUserIDs .ElementsAs (ctx , & emailUserIDs , true )... )
344+ if resp .Diagnostics .HasError () {
345345 return
346346 }
347+
347348 options .EmailUsers = []* tfe.User {}
348349 for _ , emailUserID := range emailUserIDs {
349350 options .EmailUsers = append (options .EmailUsers , & tfe.User {ID : emailUserID .ValueString ()})
350351 }
351352
352353 tflog .Debug (ctx , "Creating notification configuration" )
354+
353355 nc , err := r .config .Client .NotificationConfigurations .Create (ctx , workspaceID , options )
354356 if err != nil {
355357 resp .Diagnostics .AddError ("Unable to create notification configuration" , err .Error ())
@@ -366,8 +368,8 @@ func (r *resourceTFENotificationConfiguration) Create(ctx context.Context, req r
366368
367369 // We got a notification, so set state to new values
368370 result , diags := modelFromTFENotificationConfiguration (nc , ! config .TokenWO .IsNull ())
369- if diags != nil && diags . HasError () {
370- resp .Diagnostics .Append (( diags ) ... )
371+ resp . Diagnostics . Append ( diags ... )
372+ if resp .Diagnostics .HasError () {
371373 return
372374 }
373375
@@ -395,7 +397,12 @@ func (r *resourceTFENotificationConfiguration) Read(ctx context.Context, req res
395397 tflog .Debug (ctx , fmt .Sprintf ("Reading notification configuration %q" , state .ID .ValueString ()))
396398 nc , err := r .config .Client .NotificationConfigurations .Read (ctx , state .ID .ValueString ())
397399 if err != nil {
398- resp .Diagnostics .AddError ("Unable to read notification configuration" , err .Error ())
400+ if errors .Is (err , tfe .ErrResourceNotFound ) {
401+ tflog .Debug (ctx , fmt .Sprintf ("`Notification configuration %s no longer exists" , state .ID ))
402+ resp .State .RemoveResource (ctx )
403+ } else {
404+ resp .Diagnostics .AddError ("Error reading notification configuration" , "Could not read notification configuration, unexpected error: " + err .Error ())
405+ }
399406 return
400407 }
401408
@@ -406,13 +413,13 @@ func (r *resourceTFENotificationConfiguration) Read(ctx context.Context, req res
406413
407414 isWriteOnly , diags := r .writeOnlyValueStore (resp .Private ).PriorValueExists (ctx )
408415 resp .Diagnostics .Append (diags ... )
409- if diags .HasError () {
416+ if resp . Diagnostics .HasError () {
410417 return
411418 }
412419
413420 result , diags := modelFromTFENotificationConfiguration (nc , isWriteOnly )
414- if diags != nil && diags . HasError () {
415- resp .Diagnostics .Append (( diags ) ... )
421+ resp . Diagnostics . Append ( diags ... )
422+ if resp .Diagnostics .HasError () {
416423 return
417424 }
418425
@@ -449,32 +456,35 @@ func (r *resourceTFENotificationConfiguration) Update(ctx context.Context, req r
449456
450457 // Add triggers set to the options struct
451458 triggers := make ([]types.String , len (plan .Triggers .Elements ()))
452- if diags := plan .Triggers .ElementsAs (ctx , & triggers , true ); diags != nil && diags . HasError () {
453- resp .Diagnostics .Append ( diags ... )
459+ resp . Diagnostics . Append ( plan .Triggers .ElementsAs (ctx , & triggers , true )... )
460+ if resp .Diagnostics .HasError () {
454461 return
455462 }
463+
456464 options .Triggers = []tfe.NotificationTriggerType {}
457465 for _ , trigger := range triggers {
458466 options .Triggers = append (options .Triggers , tfe .NotificationTriggerType (trigger .ValueString ()))
459467 }
460468
461469 // Add email_addresses set to the options struct
462470 emailAddresses := make ([]types.String , len (plan .EmailAddresses .Elements ()))
463- if diags := plan .EmailAddresses .ElementsAs (ctx , & emailAddresses , true ); diags != nil && diags . HasError () {
464- resp .Diagnostics .Append ( diags ... )
471+ resp . Diagnostics . Append ( plan .EmailAddresses .ElementsAs (ctx , & emailAddresses , true )... )
472+ if resp .Diagnostics .HasError () {
465473 return
466474 }
475+
467476 options .EmailAddresses = []string {}
468477 for _ , emailAddress := range emailAddresses {
469478 options .EmailAddresses = append (options .EmailAddresses , emailAddress .ValueString ())
470479 }
471480
472481 // Add email_user_ids set to the options struct
473482 emailUserIDs := make ([]types.String , len (plan .EmailUserIDs .Elements ()))
474- if diags := plan .EmailUserIDs .ElementsAs (ctx , & emailUserIDs , true ); diags != nil && diags . HasError () {
475- resp .Diagnostics .Append ( diags ... )
483+ resp . Diagnostics . Append ( plan .EmailUserIDs .ElementsAs (ctx , & emailUserIDs , true )... )
484+ if resp .Diagnostics .HasError () {
476485 return
477486 }
487+
478488 options .EmailUsers = []* tfe.User {}
479489 for _ , emailUserID := range emailUserIDs {
480490 options .EmailUsers = append (options .EmailUsers , & tfe.User {ID : emailUserID .ValueString ()})
@@ -503,8 +513,8 @@ func (r *resourceTFENotificationConfiguration) Update(ctx context.Context, req r
503513 }
504514
505515 result , diags := modelFromTFENotificationConfiguration (nc , ! config .TokenWO .IsNull ())
506- if diags != nil && diags . HasError () {
507- resp .Diagnostics .Append (( diags ) ... )
516+ resp . Diagnostics . Append ( diags ... )
517+ if resp .Diagnostics .HasError () {
508518 return
509519 }
510520
0 commit comments