Skip to content

Commit 74be207

Browse files
authored
fix: resource read should remove from state on resource not found (#100)
1 parent 3c994e7 commit 74be207

File tree

12 files changed

+39
-14
lines changed

12 files changed

+39
-14
lines changed

internal/resource/cloud_connection.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,11 @@ func (r *CloudConnectionResource) Read(ctx context.Context, req resource.ReadReq
163163
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to read cloud connection, got error: %s", err))
164164
return
165165
}
166+
if response == nil || response.CloudConnection == nil {
167+
// Resource not found, remove from state
168+
resp.State.RemoveResource(ctx)
169+
return
170+
}
166171

167172
data.From(response.CloudConnection, ctx, &resp.Diagnostics)
168173
resp.Diagnostics.Append(resp.State.Set(ctx, data)...)

internal/resource/cluster.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,8 @@ func (r *clusterResource) Read(ctx context.Context, req resource.ReadRequest, re
9898
return
9999
}
100100
if result == nil || result.Cluster == nil {
101-
resp.Diagnostics.AddError("Not Found", "Unable to find cluster, it looks like it was deleted manually")
101+
// Resource not found, remove from state
102+
resp.State.RemoveResource(ctx)
102103
return
103104
}
104105

internal/resource/git_repository.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,9 +173,9 @@ func (r *GitRepositoryResource) Read(ctx context.Context, req resource.ReadReque
173173
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to get GitRepository, got error: %s", err))
174174
return
175175
}
176-
177176
if response == nil || response.GitRepository == nil {
178-
resp.Diagnostics.AddError("Client Error", "Unable to find GitRepository")
177+
// Resource not found, remove from state
178+
resp.State.RemoveResource(ctx)
179179
return
180180
}
181181

internal/resource/group.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,9 @@ func (r *GroupResource) Read(ctx context.Context, req resource.ReadRequest, resp
115115
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to get group, got error: %s", err))
116116
return
117117
}
118-
119118
if response == nil || response.Group == nil {
120-
resp.Diagnostics.AddError("Client Error", "Unable to find group")
119+
// Resource not found, remove from state
120+
resp.State.RemoveResource(ctx)
121121
return
122122
}
123123

internal/resource/infrastructure_stack.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,11 @@ func (r *InfrastructureStackResource) Read(ctx context.Context, req resource.Rea
8686
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to read infrastructure stack, got error: %s", err))
8787
return
8888
}
89+
if response == nil || response.InfrastructureStack == nil {
90+
// Resource not found, remove from state
91+
resp.State.RemoveResource(ctx)
92+
return
93+
}
8994

9095
data.From(response.InfrastructureStack, ctx, &resp.Diagnostics)
9196
resp.Diagnostics.Append(resp.State.Set(ctx, data)...)

internal/resource/observability_webhook.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,9 @@ func (r *ObservabilityWebhookResource) Read(ctx context.Context, req resource.Re
125125
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to read observability webhook, got error: %s", err))
126126
return
127127
}
128-
if response.ObservabilityWebhook == nil {
129-
resp.Diagnostics.AddError("Client Error", "Unable to find observability webhook, got no error")
128+
if response == nil || response.ObservabilityWebhook == nil {
129+
// Resource not found, remove from state
130+
resp.State.RemoveResource(ctx)
130131
return
131132
}
132133

internal/resource/project.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,11 @@ func (r *ProjectResource) Read(ctx context.Context, req resource.ReadRequest, re
158158
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to read project, got error: %s", err))
159159
return
160160
}
161+
if response == nil || response.Project == nil {
162+
// Resource not found, remove from state
163+
resp.State.RemoveResource(ctx)
164+
return
165+
}
161166

162167
data.From(response.Project, ctx, &resp.Diagnostics)
163168
resp.Diagnostics.Append(resp.State.Set(ctx, data)...)

internal/resource/provider.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,8 @@ func (r *providerResource) Read(ctx context.Context, req resource.ReadRequest, r
227227
return
228228
}
229229
if result == nil || result.ClusterProvider == nil {
230-
resp.Diagnostics.AddError("Not Found", "Unable to find provider, it looks like it was deleted manually")
230+
// Resource not found, remove from state
231+
resp.State.RemoveResource(ctx)
231232
return
232233
}
233234

internal/resource/scm_webhook.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,9 @@ func (r *SCMWebhookResource) Read(ctx context.Context, req resource.ReadRequest,
131131
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to read SCM webhook, got error: %s", err))
132132
return
133133
}
134-
if response.ScmWebhook == nil {
135-
resp.Diagnostics.AddError("Client Error", "Unable to find SCM webhook, got no error")
134+
if response == nil || response.ScmWebhook == nil {
135+
// Resource not found, remove from state
136+
resp.State.RemoveResource(ctx)
136137
return
137138
}
138139

internal/resource/service_context.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,9 @@ func (r *ServiceContextResource) Read(ctx context.Context, req resource.ReadRequ
112112
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to read service context, got error: %s", err))
113113
return
114114
}
115-
if response.ServiceContext == nil {
116-
resp.Diagnostics.AddError("Client Error", "Unable to find service context, got no error")
115+
if response == nil || response.ServiceContext == nil {
116+
// Resource not found, remove from state
117+
resp.State.RemoveResource(ctx)
117118
return
118119
}
119120

0 commit comments

Comments
 (0)