Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c434450

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

10 files changed

+66
-1
lines changed

internal/provider/group_data_source.go

+10
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,11 @@ 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.Diagnostics.AddWarning("Client Warning", fmt.Sprintf("Group with ID %s not found. Marking as deleted.", groupID.String()))
192+
resp.State.RemoveResource(ctx)
193+
return
194+
}
190195
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to get group by ID, got error: %s", err))
191196
return
192197
}
@@ -195,6 +200,11 @@ func (d *GroupDataSource) Read(ctx context.Context, req datasource.ReadRequest,
195200
} else {
196201
group, err = client.GroupByOrgAndName(ctx, data.OrganizationID.ValueUUID(), data.Name.ValueString())
197202
if err != nil {
203+
if isNotFound(err) {
204+
resp.Diagnostics.AddWarning("Client Warning", fmt.Sprintf("Group with name %s not found in organization with ID %s. Marking as deleted.", data.Name.ValueString(), data.OrganizationID.ValueString()))
205+
resp.State.RemoveResource(ctx)
206+
return
207+
}
198208
resp.Diagnostics.AddError("Failed to get group by name and org ID", err.Error())
199209
return
200210
}

internal/provider/group_resource.go

+5
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,11 @@ 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.Diagnostics.AddWarning("Client Warning", fmt.Sprintf("Group with ID %s not found. Marking as deleted.", groupID.String()))
225+
resp.State.RemoveResource(ctx)
226+
return
227+
}
223228
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to get group, got error: %s", err))
224229
return
225230
}

internal/provider/license_resource.go

+3-1
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. Marking as deleted.", 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

+15
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,11 @@ 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.Diagnostics.AddWarning("Client Warning", fmt.Sprintf("Organization with ID %s not found. Marking as deleted.", data.ID.ValueString()))
132+
resp.State.RemoveResource(ctx)
133+
return
134+
}
130135
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to get organization by ID, got error: %s", err))
131136
return
132137
}
@@ -137,6 +142,11 @@ func (d *OrganizationDataSource) Read(ctx context.Context, req datasource.ReadRe
137142
} else if data.IsDefault.ValueBool() { // Get Default
138143
org, err = client.OrganizationByName(ctx, "default")
139144
if err != nil {
145+
if isNotFound(err) {
146+
resp.Diagnostics.AddWarning("Client Warning", "Default organization not found. Marking as deleted.")
147+
resp.State.RemoveResource(ctx)
148+
return
149+
}
140150
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to get default organization, got error: %s", err))
141151
return
142152
}
@@ -147,6 +157,11 @@ func (d *OrganizationDataSource) Read(ctx context.Context, req datasource.ReadRe
147157
} else { // By Name
148158
org, err = client.OrganizationByName(ctx, data.Name.ValueString())
149159
if err != nil {
160+
if isNotFound(err) {
161+
resp.Diagnostics.AddWarning("Client Warning", fmt.Sprintf("Organization with name %s not found. Marking as deleted.", data.Name))
162+
resp.State.RemoveResource(ctx)
163+
return
164+
}
150165
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to get organization by name, got error: %s", err))
151166
return
152167
}

internal/provider/template_data_source.go

+5
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,11 @@ 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.Diagnostics.AddWarning("Client Warning", "Template not found. Marking as deleted.")
267+
resp.State.RemoveResource(ctx)
268+
return
269+
}
265270
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to get template, got error: %s", err))
266271
return
267272
}

internal/provider/template_resource.go

+5
Original file line numberDiff line numberDiff line change
@@ -577,6 +577,11 @@ 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.Diagnostics.AddWarning("Client Warning", fmt.Sprintf("Template with ID %s not found. Marking as deleted.", templateID.String()))
582+
resp.State.RemoveResource(ctx)
583+
return
584+
}
580585
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Failed to get template: %s", err))
581586
return
582587
}

internal/provider/user_data_source.go

+5
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,11 @@ 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.Diagnostics.AddWarning("Client Warning", fmt.Sprintf("User with identifier %q not found. Marking as deleted.", ident))
154+
resp.State.RemoveResource(ctx)
155+
return
156+
}
152157
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to get current user, got error: %s", err))
153158
return
154159
}

internal/provider/user_resource.go

+5
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,11 @@ func (r *UserResource) Read(ctx context.Context, req resource.ReadRequest, resp
251251

252252
user, err := client.User(ctx, data.ID.ValueString())
253253
if err != nil {
254+
if isNotFound(err) {
255+
resp.Diagnostics.AddWarning("Client Warning", fmt.Sprintf("User with ID %q not found. Marking as deleted.", data.ID.ValueString()))
256+
resp.State.RemoveResource(ctx)
257+
return
258+
}
254259
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to get current user, got error: %s", err))
255260
return
256261
}

internal/provider/util.go

+8
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

+5
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,11 @@ 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.Diagnostics.AddWarning("Client Warning", fmt.Sprintf("Workspace proxy with ID %s not found. Marking as deleted.", data.ID.ValueString()))
147+
resp.State.RemoveResource(ctx)
148+
return
149+
}
145150
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Failed to read workspace proxy: %v", err))
146151
return
147152
}

0 commit comments

Comments
 (0)