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

Commit8e299b2

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

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
iferr!=nil {
190+
ifisNotFound(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
iferr!=nil {
202+
ifisNotFound(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
iferr!=nil {
223+
ifisNotFound(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
iferr!=nil {
130+
ifisNotFound(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
}elseifdata.IsDefault.ValueBool() {// Get Default
138142
org,err=client.OrganizationByName(ctx,"default")
139143
iferr!=nil {
144+
ifisNotFound(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
iferr!=nil {
158+
ifisNotFound(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
iferr!=nil {
265+
ifisNotFound(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
iferr!=nil {
580+
ifisNotFound(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
iferr!=nil {
152+
ifisNotFound(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
iferr!=nil {
256+
varsdkErr*codersdk.Error
257+
iferrors.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
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: 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
iferr!=nil {
145+
ifisNotFound(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)

[8]ページ先頭

©2009-2025 Movatter.jp