From 5ca615f32c901fe5f8b4e3137b61d4d3a56db66e Mon Sep 17 00:00:00 2001 From: Max Hahn Date: Mon, 9 Sep 2024 09:23:03 -0700 Subject: [PATCH] Add NOT_FOUND error handling to resources [2/n] This should prevent terraform state divergence for the included resources when the resource is deleted out of band. --- observe/resource_app.go | 4 ++++ observe/resource_app_datasource.go | 6 +++++- observe/resource_board.go | 4 ++++ observe/resource_bookmark.go | 4 ++++ observe/resource_bookmark_group.go | 4 ++++ observe/resource_channel.go | 4 ++++ observe/resource_channel_action.go | 4 ++++ observe/resource_dashboard_link.go | 4 ++++ observe/resource_dataset.go | 4 ++++ observe/resource_datastream_token.go | 4 ++++ observe/resource_default_dashboard.go | 5 +++++ observe/resource_folder.go | 4 ++++ observe/resource_layered_setting_record.go | 4 ++++ observe/resource_link.go | 4 ++++ observe/resource_poller.go | 4 ++++ observe/resource_preferred_path.go | 4 ++++ observe/resource_source_dataset.go | 4 ++++ observe/resource_worksheet.go | 4 ++++ 18 files changed, 74 insertions(+), 1 deletion(-) diff --git a/observe/resource_app.go b/observe/resource_app.go index 63b93406..85698547 100644 --- a/observe/resource_app.go +++ b/observe/resource_app.go @@ -141,6 +141,10 @@ func resourceAppRead(ctx context.Context, data *schema.ResourceData, meta interf app, err := client.GetApp(ctx, data.Id()) if err != nil { + if gql.HasErrorCode(err, gql.ErrNotFound) { + data.SetId("") + return nil + } return diag.Errorf("failed to read app: %s", err.Error()) } diff --git a/observe/resource_app_datasource.go b/observe/resource_app_datasource.go index 2aa92f39..9c68eb15 100644 --- a/observe/resource_app_datasource.go +++ b/observe/resource_app_datasource.go @@ -115,7 +115,11 @@ func resourceAppDataSourceRead(ctx context.Context, data *schema.ResourceData, m appdatasource, err := client.GetAppDataSource(ctx, data.Id()) if err != nil { - return diag.Errorf("failed to read app: %s", err.Error()) + if gql.HasErrorCode(err, gql.ErrNotFound) { + data.SetId("") + return nil + } + return diag.Errorf("failed to read appdatasource: %s", err.Error()) } return appDataSourceToResourceData(appdatasource, data) diff --git a/observe/resource_board.go b/observe/resource_board.go index 39a0ceaa..eb8beb3c 100644 --- a/observe/resource_board.go +++ b/observe/resource_board.go @@ -139,6 +139,10 @@ func resourceBoardRead(ctx context.Context, data *schema.ResourceData, meta inte client := meta.(*observe.Client) result, err := client.GetBoard(ctx, data.Id()) if err != nil { + if gql.HasErrorCode(err, gql.ErrNotFound) { + data.SetId("") + return nil + } return append(diags, diag.Diagnostic{ Severity: diag.Error, Summary: fmt.Sprintf("failed to retrieve board [id=%s]", data.Id()), diff --git a/observe/resource_bookmark.go b/observe/resource_bookmark.go index e3d0b755..b5559645 100644 --- a/observe/resource_bookmark.go +++ b/observe/resource_bookmark.go @@ -149,6 +149,10 @@ func resourceBookmarkRead(ctx context.Context, data *schema.ResourceData, meta i client := meta.(*observe.Client) result, err := client.GetBookmark(ctx, data.Id()) if err != nil { + if gql.HasErrorCode(err, gql.ErrNotFound) { + data.SetId("") + return nil + } return append(diags, diag.Diagnostic{ Severity: diag.Error, Summary: fmt.Sprintf("failed to retrieve bookmark [id=%s]", data.Id()), diff --git a/observe/resource_bookmark_group.go b/observe/resource_bookmark_group.go index fdec0085..29507020 100644 --- a/observe/resource_bookmark_group.go +++ b/observe/resource_bookmark_group.go @@ -141,6 +141,10 @@ func resourceBookmarkGroupRead(ctx context.Context, data *schema.ResourceData, m client := meta.(*observe.Client) result, err := client.GetBookmarkGroup(ctx, data.Id()) if err != nil { + if gql.HasErrorCode(err, gql.ErrNotFound) { + data.SetId("") + return nil + } return append(diags, diag.Diagnostic{ Severity: diag.Error, Summary: fmt.Sprintf("failed to retrieve bookmark group [id=%s]", data.Id()), diff --git a/observe/resource_channel.go b/observe/resource_channel.go index 40377b3c..aa82afec 100644 --- a/observe/resource_channel.go +++ b/observe/resource_channel.go @@ -133,6 +133,10 @@ func resourceChannelRead(ctx context.Context, data *schema.ResourceData, meta in channel, err := client.GetChannel(ctx, data.Id()) if err != nil { + if gql.HasErrorCode(err, gql.ErrNotFound) { + data.SetId("") + return nil + } return diag.Errorf("failed to read channel: %s", err.Error()) } diff --git a/observe/resource_channel_action.go b/observe/resource_channel_action.go index dd4a9fa0..736d8c1e 100644 --- a/observe/resource_channel_action.go +++ b/observe/resource_channel_action.go @@ -286,6 +286,10 @@ func resourceChannelActionRead(ctx context.Context, data *schema.ResourceData, m channelActionPtr, err := client.GetChannelAction(ctx, data.Id()) if err != nil { + if gql.HasErrorCode(err, gql.ErrNotFound) { + data.SetId("") + return nil + } return diag.Errorf("failed to read channel action: %s", err.Error()) } channelAction := *channelActionPtr diff --git a/observe/resource_dashboard_link.go b/observe/resource_dashboard_link.go index 76bd84da..ea5db4a9 100644 --- a/observe/resource_dashboard_link.go +++ b/observe/resource_dashboard_link.go @@ -150,6 +150,10 @@ func resourceDashboardLinkRead(ctx context.Context, data *schema.ResourceData, m link, err := client.Meta.GetDashboardLink(ctx, data.Id()) if err != nil { + if gql.HasErrorCode(err, gql.ErrNotFound) { + data.SetId("") + return nil + } return diag.Errorf("failed to read dashboard link: %s", err.Error()) } diff --git a/observe/resource_dataset.go b/observe/resource_dataset.go index fd76d539..332d8a9f 100644 --- a/observe/resource_dataset.go +++ b/observe/resource_dataset.go @@ -342,6 +342,10 @@ func resourceDatasetRead(ctx context.Context, data *schema.ResourceData, meta in client := meta.(*observe.Client) result, err := client.GetDataset(ctx, data.Id()) if err != nil { + if gql.HasErrorCode(err, gql.ErrNotFound) { + data.SetId("") + return nil + } return append(diags, diag.Diagnostic{ Severity: diag.Error, Summary: fmt.Sprintf("failed to retrieve dataset [id=%s]", data.Id()), diff --git a/observe/resource_datastream_token.go b/observe/resource_datastream_token.go index 730fe256..e29198d7 100644 --- a/observe/resource_datastream_token.go +++ b/observe/resource_datastream_token.go @@ -152,6 +152,10 @@ func resourceDatastreamTokenRead(ctx context.Context, data *schema.ResourceData, client := meta.(*observe.Client) result, err := client.GetDatastreamToken(ctx, data.Id()) if err != nil { + if gql.HasErrorCode(err, gql.ErrNotFound) { + data.SetId("") + return nil + } return append(diags, diag.Diagnostic{ Severity: diag.Error, Summary: fmt.Sprintf("failed to retrieve datastream [id=%s]", data.Id()), diff --git a/observe/resource_default_dashboard.go b/observe/resource_default_dashboard.go index 0fe650a7..9465bea5 100644 --- a/observe/resource_default_dashboard.go +++ b/observe/resource_default_dashboard.go @@ -7,6 +7,7 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" observe "github.com/observeinc/terraform-provider-observe/client" + gql "github.com/observeinc/terraform-provider-observe/client/meta" "github.com/observeinc/terraform-provider-observe/client/oid" ) @@ -57,6 +58,10 @@ func resourceDefaultDashboardRead(ctx context.Context, data *schema.ResourceData dashid, err := client.GetDefaultDashboard(ctx, data.Id()) if err != nil { + if gql.HasErrorCode(err, gql.ErrNotFound) { + data.SetId("") + return nil + } return diag.Errorf("failed to read default dashboard: %s", err.Error()) } diff --git a/observe/resource_folder.go b/observe/resource_folder.go index 0cbc746e..a90eb46d 100644 --- a/observe/resource_folder.go +++ b/observe/resource_folder.go @@ -105,6 +105,10 @@ func resourceFolderRead(ctx context.Context, data *schema.ResourceData, meta int folder, err := client.GetFolder(ctx, data.Id()) if err != nil { + if gql.HasErrorCode(err, gql.ErrNotFound) { + data.SetId("") + return nil + } return diag.Errorf("failed to read folder: %s", err.Error()) } diff --git a/observe/resource_layered_setting_record.go b/observe/resource_layered_setting_record.go index faad3404..0b25d563 100644 --- a/observe/resource_layered_setting_record.go +++ b/observe/resource_layered_setting_record.go @@ -148,6 +148,10 @@ func resourceLayeredSettingRecordRead(ctx context.Context, data *schema.Resource client := meta.(*observe.Client) result, err := client.GetLayeredSettingRecord(ctx, data.Id()) if err != nil { + if gql.HasErrorCode(err, gql.ErrNotFound) { + data.SetId("") + return nil + } return append(diags, diag.Diagnostic{ Severity: diag.Error, Summary: fmt.Sprintf("failed to retrieve layeredsetting [id=%s]", data.Id()), diff --git a/observe/resource_link.go b/observe/resource_link.go index dcf8d8a3..06918400 100644 --- a/observe/resource_link.go +++ b/observe/resource_link.go @@ -129,6 +129,10 @@ func resourceLinkRead(ctx context.Context, data *schema.ResourceData, meta inter link, err := client.GetForeignKey(ctx, data.Id()) if err != nil { + if gql.HasErrorCode(err, gql.ErrNotFound) { + data.SetId("") + return nil + } if link == nil { return diag.Errorf("failed to read link: %s", err.Error()) } diff --git a/observe/resource_poller.go b/observe/resource_poller.go index ed5fae65..4187c1d1 100644 --- a/observe/resource_poller.go +++ b/observe/resource_poller.go @@ -744,6 +744,10 @@ func resourcePollerRead(ctx context.Context, data *schema.ResourceData, meta int poller, err := client.GetPoller(ctx, data.Id()) if err != nil { + if gql.HasErrorCode(err, gql.ErrNotFound) { + data.SetId("") + return nil + } return diag.Errorf("failed to read poller: %s", err.Error()) } diff --git a/observe/resource_preferred_path.go b/observe/resource_preferred_path.go index db194722..8fcda0ac 100644 --- a/observe/resource_preferred_path.go +++ b/observe/resource_preferred_path.go @@ -199,6 +199,10 @@ func resourcePreferredPathRead(ctx context.Context, data *schema.ResourceData, m path, err := client.GetPreferredPath(ctx, data.Id()) if err != nil { + if gql.HasErrorCode(err, gql.ErrNotFound) { + data.SetId("") + return nil + } if path == nil { return diag.Errorf("failed to read preferred path: %s", err) } diff --git a/observe/resource_source_dataset.go b/observe/resource_source_dataset.go index 3ecb5968..7d211d12 100644 --- a/observe/resource_source_dataset.go +++ b/observe/resource_source_dataset.go @@ -344,6 +344,10 @@ func resourceSourceDatasetRead(ctx context.Context, data *schema.ResourceData, m client := meta.(*observe.Client) result, err := client.GetSourceDataset(ctx, data.Id()) if err != nil { + if gql.HasErrorCode(err, gql.ErrNotFound) { + data.SetId("") + return nil + } return append(diags, diag.Diagnostic{ Severity: diag.Error, Summary: fmt.Sprintf("failed to retrieve dataset [id=%s]", data.Id()), diff --git a/observe/resource_worksheet.go b/observe/resource_worksheet.go index 28ed877c..248c55b4 100644 --- a/observe/resource_worksheet.go +++ b/observe/resource_worksheet.go @@ -137,6 +137,10 @@ func resourceWorksheetCreate(ctx context.Context, data *schema.ResourceData, met id, _ := oid.NewOID(data.Get("workspace").(string)) result, err := client.CreateWorksheet(ctx, id.Id, config) if err != nil { + if gql.HasErrorCode(err, gql.ErrNotFound) { + data.SetId("") + return nil + } diags = append(diags, diag.Diagnostic{ Severity: diag.Error, Summary: "failed to create worksheet",