Skip to content

Commit 8e299b2

Browse files
committed
fix: delete resources & data sources if not found
1 parent a462665 commit 8e299b2

10 files changed

+58
-1
lines changed

internal/provider/group_data_source.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,10 @@ func (d *GroupDataSource) Read(ctx context.Context, req datasource.ReadRequest,
187187
groupID := data.ID.ValueUUID()
188188
group, err = client.Group(ctx, groupID)
189189
if err != nil {
190+
if isNotFound(err) {
191+
resp.State.RemoveResource(ctx)
192+
return
193+
}
190194
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to get group by ID, got error: %s", err))
191195
return
192196
}
@@ -195,6 +199,10 @@ func (d *GroupDataSource) Read(ctx context.Context, req datasource.ReadRequest,
195199
} else {
196200
group, err = client.GroupByOrgAndName(ctx, data.OrganizationID.ValueUUID(), data.Name.ValueString())
197201
if err != nil {
202+
if isNotFound(err) {
203+
resp.State.RemoveResource(ctx)
204+
return
205+
}
198206
resp.Diagnostics.AddError("Failed to get group by name and org ID", err.Error())
199207
return
200208
}

internal/provider/group_resource.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,10 @@ func (r *GroupResource) Read(ctx context.Context, req resource.ReadRequest, resp
220220

221221
group, err := client.Group(ctx, groupID)
222222
if err != nil {
223+
if isNotFound(err) {
224+
resp.State.RemoveResource(ctx)
225+
return
226+
}
223227
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to get group, got error: %s", err))
224228
return
225229
}

internal/provider/license_resource.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,9 @@ func (r *LicenseResource) Read(ctx context.Context, req resource.ReadRequest, re
150150
}
151151
}
152152
if !found {
153-
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("License with ID %d not found", data.ID.ValueInt32()))
153+
resp.Diagnostics.AddWarning("Client Warning", fmt.Sprintf("License with ID %d not found", data.ID.ValueInt32()))
154+
resp.State.RemoveResource(ctx)
155+
return
154156
}
155157

156158
// Save updated data into Terraform state

internal/provider/organization_data_source.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,10 @@ func (d *OrganizationDataSource) Read(ctx context.Context, req datasource.ReadRe
127127
orgID := data.ID.ValueUUID()
128128
org, err = client.Organization(ctx, orgID)
129129
if err != nil {
130+
if isNotFound(err) {
131+
resp.State.RemoveResource(ctx)
132+
return
133+
}
130134
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to get organization by ID, got error: %s", err))
131135
return
132136
}
@@ -137,6 +141,10 @@ func (d *OrganizationDataSource) Read(ctx context.Context, req datasource.ReadRe
137141
} else if data.IsDefault.ValueBool() { // Get Default
138142
org, err = client.OrganizationByName(ctx, "default")
139143
if err != nil {
144+
if isNotFound(err) {
145+
resp.State.RemoveResource(ctx)
146+
return
147+
}
140148
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to get default organization, got error: %s", err))
141149
return
142150
}
@@ -147,6 +155,10 @@ func (d *OrganizationDataSource) Read(ctx context.Context, req datasource.ReadRe
147155
} else { // By Name
148156
org, err = client.OrganizationByName(ctx, data.Name.ValueString())
149157
if err != nil {
158+
if isNotFound(err) {
159+
resp.State.RemoveResource(ctx)
160+
return
161+
}
150162
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to get organization by name, got error: %s", err))
151163
return
152164
}

internal/provider/template_data_source.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,10 @@ func (d *TemplateDataSource) Read(ctx context.Context, req datasource.ReadReques
262262
template, err = client.TemplateByName(ctx, data.OrganizationID.ValueUUID(), data.Name.ValueString())
263263
}
264264
if err != nil {
265+
if isNotFound(err) {
266+
resp.State.RemoveResource(ctx)
267+
return
268+
}
265269
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to get template, got error: %s", err))
266270
return
267271
}

internal/provider/template_resource.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -577,6 +577,10 @@ func (r *TemplateResource) Read(ctx context.Context, req resource.ReadRequest, r
577577

578578
template, err := client.Template(ctx, templateID)
579579
if err != nil {
580+
if isNotFound(err) {
581+
resp.State.RemoveResource(ctx)
582+
return
583+
}
580584
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Failed to get template: %s", err))
581585
return
582586
}

internal/provider/user_data_source.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,10 @@ func (d *UserDataSource) Read(ctx context.Context, req datasource.ReadRequest, r
149149
}
150150
user, err := client.User(ctx, ident)
151151
if err != nil {
152+
if isNotFound(err) {
153+
resp.State.RemoveResource(ctx)
154+
return
155+
}
152156
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to get current user, got error: %s", err))
153157
return
154158
}

internal/provider/user_resource.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ package provider
22

33
import (
44
"context"
5+
"errors"
56
"fmt"
7+
"net/http"
68
"strings"
79

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

252254
user, err := client.User(ctx, data.ID.ValueString())
253255
if err != nil {
256+
var sdkErr *codersdk.Error
257+
if errors.As(err, &sdkErr) && sdkErr.StatusCode() == http.StatusNotFound {
258+
resp.State.RemoveResource(ctx)
259+
return
260+
}
254261
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to get current user, got error: %s", err))
255262
return
256263
}

internal/provider/util.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,14 @@ package provider
33
import (
44
"crypto/sha256"
55
"encoding/hex"
6+
"errors"
67
"fmt"
8+
"net/http"
79
"os"
810
"path/filepath"
911
"regexp"
1012

13+
"github.com/coder/coder/v2/codersdk"
1114
"github.com/google/uuid"
1215
)
1316

@@ -113,3 +116,8 @@ func memberDiff(curMembers []uuid.UUID, plannedMembers []UUID) (add, remove []st
113116
}
114117
return add, remove
115118
}
119+
120+
func isNotFound(err error) bool {
121+
var sdkErr *codersdk.Error
122+
return errors.As(err, &sdkErr) && sdkErr.StatusCode() == http.StatusNotFound
123+
}

internal/provider/workspace_proxy_resource.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,10 @@ func (r *WorkspaceProxyResource) Read(ctx context.Context, req resource.ReadRequ
142142
client := r.data.Client
143143
wsp, err := client.WorkspaceProxyByID(ctx, data.ID.ValueUUID())
144144
if err != nil {
145+
if isNotFound(err) {
146+
resp.State.RemoveResource(ctx)
147+
return
148+
}
145149
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Failed to read workspace proxy: %v", err))
146150
return
147151
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy