Skip to content

Commit

Permalink
Add NOT_FOUND error handling to resources [2/n]
Browse files Browse the repository at this point in the history
This should prevent terraform state divergence for the included
resources when the resource is deleted out of band.
  • Loading branch information
obs-gh-maxhahn committed Sep 9, 2024
1 parent 386d685 commit 5ca615f
Show file tree
Hide file tree
Showing 18 changed files with 74 additions and 1 deletion.
4 changes: 4 additions & 0 deletions observe/resource_app.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}

Expand Down
6 changes: 5 additions & 1 deletion observe/resource_app_datasource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions observe/resource_board.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
Expand Down
4 changes: 4 additions & 0 deletions observe/resource_bookmark.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
Expand Down
4 changes: 4 additions & 0 deletions observe/resource_bookmark_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
Expand Down
4 changes: 4 additions & 0 deletions observe/resource_channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}

Expand Down
4 changes: 4 additions & 0 deletions observe/resource_channel_action.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions observe/resource_dashboard_link.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}

Expand Down
4 changes: 4 additions & 0 deletions observe/resource_dataset.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
Expand Down
4 changes: 4 additions & 0 deletions observe/resource_datastream_token.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
Expand Down
5 changes: 5 additions & 0 deletions observe/resource_default_dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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())
}

Expand Down
4 changes: 4 additions & 0 deletions observe/resource_folder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}

Expand Down
4 changes: 4 additions & 0 deletions observe/resource_layered_setting_record.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
Expand Down
4 changes: 4 additions & 0 deletions observe/resource_link.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
Expand Down
4 changes: 4 additions & 0 deletions observe/resource_poller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}

Expand Down
4 changes: 4 additions & 0 deletions observe/resource_preferred_path.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
4 changes: 4 additions & 0 deletions observe/resource_source_dataset.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
Expand Down
4 changes: 4 additions & 0 deletions observe/resource_worksheet.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 5ca615f

Please sign in to comment.