Skip to content

Commit

Permalink
fix: delete resources & data sources if not found
Browse files Browse the repository at this point in the history
  • Loading branch information
ethanndickson committed Sep 16, 2024
1 parent a462665 commit 8e299b2
Show file tree
Hide file tree
Showing 10 changed files with 58 additions and 1 deletion.
8 changes: 8 additions & 0 deletions internal/provider/group_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,10 @@ func (d *GroupDataSource) Read(ctx context.Context, req datasource.ReadRequest,
groupID := data.ID.ValueUUID()
group, err = client.Group(ctx, groupID)
if err != nil {
if isNotFound(err) {
resp.State.RemoveResource(ctx)
return
}
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to get group by ID, got error: %s", err))
return
}
Expand All @@ -195,6 +199,10 @@ func (d *GroupDataSource) Read(ctx context.Context, req datasource.ReadRequest,
} else {
group, err = client.GroupByOrgAndName(ctx, data.OrganizationID.ValueUUID(), data.Name.ValueString())
if err != nil {
if isNotFound(err) {
resp.State.RemoveResource(ctx)
return
}
resp.Diagnostics.AddError("Failed to get group by name and org ID", err.Error())
return
}
Expand Down
4 changes: 4 additions & 0 deletions internal/provider/group_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,10 @@ func (r *GroupResource) Read(ctx context.Context, req resource.ReadRequest, resp

group, err := client.Group(ctx, groupID)
if err != nil {
if isNotFound(err) {
resp.State.RemoveResource(ctx)
return
}
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to get group, got error: %s", err))
return
}
Expand Down
4 changes: 3 additions & 1 deletion internal/provider/license_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,9 @@ func (r *LicenseResource) Read(ctx context.Context, req resource.ReadRequest, re
}
}
if !found {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("License with ID %d not found", data.ID.ValueInt32()))
resp.Diagnostics.AddWarning("Client Warning", fmt.Sprintf("License with ID %d not found", data.ID.ValueInt32()))
resp.State.RemoveResource(ctx)
return
}

// Save updated data into Terraform state
Expand Down
12 changes: 12 additions & 0 deletions internal/provider/organization_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,10 @@ func (d *OrganizationDataSource) Read(ctx context.Context, req datasource.ReadRe
orgID := data.ID.ValueUUID()
org, err = client.Organization(ctx, orgID)
if err != nil {
if isNotFound(err) {
resp.State.RemoveResource(ctx)
return
}
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to get organization by ID, got error: %s", err))
return
}
Expand All @@ -137,6 +141,10 @@ func (d *OrganizationDataSource) Read(ctx context.Context, req datasource.ReadRe
} else if data.IsDefault.ValueBool() { // Get Default
org, err = client.OrganizationByName(ctx, "default")
if err != nil {
if isNotFound(err) {
resp.State.RemoveResource(ctx)
return
}
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to get default organization, got error: %s", err))
return
}
Expand All @@ -147,6 +155,10 @@ func (d *OrganizationDataSource) Read(ctx context.Context, req datasource.ReadRe
} else { // By Name
org, err = client.OrganizationByName(ctx, data.Name.ValueString())
if err != nil {
if isNotFound(err) {
resp.State.RemoveResource(ctx)
return
}
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to get organization by name, got error: %s", err))
return
}
Expand Down
4 changes: 4 additions & 0 deletions internal/provider/template_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,10 @@ func (d *TemplateDataSource) Read(ctx context.Context, req datasource.ReadReques
template, err = client.TemplateByName(ctx, data.OrganizationID.ValueUUID(), data.Name.ValueString())
}
if err != nil {
if isNotFound(err) {
resp.State.RemoveResource(ctx)
return
}
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to get template, got error: %s", err))
return
}
Expand Down
4 changes: 4 additions & 0 deletions internal/provider/template_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -577,6 +577,10 @@ func (r *TemplateResource) Read(ctx context.Context, req resource.ReadRequest, r

template, err := client.Template(ctx, templateID)
if err != nil {
if isNotFound(err) {
resp.State.RemoveResource(ctx)
return
}
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Failed to get template: %s", err))
return
}
Expand Down
4 changes: 4 additions & 0 deletions internal/provider/user_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,10 @@ func (d *UserDataSource) Read(ctx context.Context, req datasource.ReadRequest, r
}
user, err := client.User(ctx, ident)
if err != nil {
if isNotFound(err) {
resp.State.RemoveResource(ctx)
return
}
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to get current user, got error: %s", err))
return
}
Expand Down
7 changes: 7 additions & 0 deletions internal/provider/user_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package provider

import (
"context"
"errors"
"fmt"
"net/http"
"strings"

"github.com/google/uuid"
Expand Down Expand Up @@ -251,6 +253,11 @@ func (r *UserResource) Read(ctx context.Context, req resource.ReadRequest, resp

user, err := client.User(ctx, data.ID.ValueString())
if err != nil {
var sdkErr *codersdk.Error
if errors.As(err, &sdkErr) && sdkErr.StatusCode() == http.StatusNotFound {
resp.State.RemoveResource(ctx)
return
}
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to get current user, got error: %s", err))
return
}
Expand Down
8 changes: 8 additions & 0 deletions internal/provider/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@ package provider
import (
"crypto/sha256"
"encoding/hex"
"errors"
"fmt"
"net/http"
"os"
"path/filepath"
"regexp"

"github.com/coder/coder/v2/codersdk"
"github.com/google/uuid"
)

Expand Down Expand Up @@ -113,3 +116,8 @@ func memberDiff(curMembers []uuid.UUID, plannedMembers []UUID) (add, remove []st
}
return add, remove
}

func isNotFound(err error) bool {
var sdkErr *codersdk.Error
return errors.As(err, &sdkErr) && sdkErr.StatusCode() == http.StatusNotFound
}
4 changes: 4 additions & 0 deletions internal/provider/workspace_proxy_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,10 @@ func (r *WorkspaceProxyResource) Read(ctx context.Context, req resource.ReadRequ
client := r.data.Client
wsp, err := client.WorkspaceProxyByID(ctx, data.ID.ValueUUID())
if err != nil {
if isNotFound(err) {
resp.State.RemoveResource(ctx)
return
}
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Failed to read workspace proxy: %v", err))
return
}
Expand Down

0 comments on commit 8e299b2

Please sign in to comment.