Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitc434450

Browse files
committed
fix: delete resources & data sources if not found
1 parenta462665 commitc434450

10 files changed

+66
-1
lines changed

‎internal/provider/group_data_source.go

Lines changed: 10 additions & 0 deletions
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
iferr!=nil {
190+
ifisNotFound(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
iferr!=nil {
203+
ifisNotFound(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

Lines changed: 5 additions & 0 deletions
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
iferr!=nil {
223+
ifisNotFound(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

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. 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

Lines changed: 15 additions & 0 deletions
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
iferr!=nil {
130+
ifisNotFound(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
}elseifdata.IsDefault.ValueBool() {// Get Default
138143
org,err=client.OrganizationByName(ctx,"default")
139144
iferr!=nil {
145+
ifisNotFound(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
iferr!=nil {
160+
ifisNotFound(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

Lines changed: 5 additions & 0 deletions
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
iferr!=nil {
265+
ifisNotFound(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

Lines changed: 5 additions & 0 deletions
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
iferr!=nil {
580+
ifisNotFound(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

Lines changed: 5 additions & 0 deletions
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
iferr!=nil {
152+
ifisNotFound(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

Lines changed: 5 additions & 0 deletions
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
iferr!=nil {
254+
ifisNotFound(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

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
returnadd,remove
115118
}
119+
120+
funcisNotFound(errerror)bool {
121+
varsdkErr*codersdk.Error
122+
returnerrors.As(err,&sdkErr)&&sdkErr.StatusCode()==http.StatusNotFound
123+
}

‎internal/provider/workspace_proxy_resource.go

Lines changed: 5 additions & 0 deletions
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
iferr!=nil {
145+
ifisNotFound(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)

[8]ページ先頭

©2009-2025 Movatter.jp