Skip to content

Commit 5105d40

Browse files
authored
handle resource not found (#24)
if a resource was created and exists in a plan, but was deleted outside of terraform, it would return an error during `Read`. this PR handles NotFound response and removes the resource from plan, so that it would get re-created. Fixes https://linear.app/pomerium/issue/ENG-1782/provider-doesnt-handle-route-deletion-correctly
1 parent 388685f commit 5105d40

File tree

6 files changed

+36
-0
lines changed

6 files changed

+36
-0
lines changed

internal/provider/keychain.go

+6
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import (
1111
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
1212
"github.com/hashicorp/terraform-plugin-framework/types"
1313
"github.com/hashicorp/terraform-plugin-log/tflog"
14+
"google.golang.org/grpc/codes"
15+
"google.golang.org/grpc/status"
1416

1517
client "github.com/pomerium/enterprise-client-go"
1618
"github.com/pomerium/enterprise-client-go/pb"
@@ -131,6 +133,10 @@ func (r *KeyChainResource) Read(ctx context.Context, req resource.ReadRequest, r
131133
Id: state.ID.ValueString(),
132134
})
133135
if err != nil {
136+
if status.Code(err) == codes.NotFound {
137+
resp.State.RemoveResource(ctx)
138+
return
139+
}
134140
resp.Diagnostics.AddError("Error reading key pair", err.Error())
135141
return
136142
}

internal/provider/namespace.go

+6
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import (
99
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
1010
"github.com/hashicorp/terraform-plugin-framework/types"
1111
"github.com/hashicorp/terraform-plugin-log/tflog"
12+
"google.golang.org/grpc/codes"
13+
"google.golang.org/grpc/status"
1214

1315
client "github.com/pomerium/enterprise-client-go"
1416
"github.com/pomerium/enterprise-client-go/pb"
@@ -108,6 +110,10 @@ func (r *NamespaceResource) Read(ctx context.Context, req resource.ReadRequest,
108110
Id: state.ID.ValueString(),
109111
})
110112
if err != nil {
113+
if status.Code(err) == codes.NotFound {
114+
resp.State.RemoveResource(ctx)
115+
return
116+
}
111117
resp.Diagnostics.AddError("Error reading namespace", err.Error())
112118
return
113119
}

internal/provider/namespace_permission.go

+6
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ import (
1414
"github.com/hashicorp/terraform-plugin-log/tflog"
1515
client "github.com/pomerium/enterprise-client-go"
1616
"github.com/pomerium/enterprise-client-go/pb"
17+
"google.golang.org/grpc/codes"
18+
"google.golang.org/grpc/status"
1719
)
1820

1921
//go:embed help/namespace_permissions.md
@@ -118,6 +120,10 @@ func (r *NamespacePermissionResource) Read(ctx context.Context, req resource.Rea
118120
Id: state.ID.ValueString(),
119121
})
120122
if err != nil {
123+
if status.Code(err) == codes.NotFound {
124+
resp.State.RemoveResource(ctx)
125+
return
126+
}
121127
resp.Diagnostics.AddError("failed to read namespace permission", err.Error())
122128
return
123129
}

internal/provider/policy.go

+6
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import (
1111
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
1212
"github.com/hashicorp/terraform-plugin-framework/types"
1313
"github.com/hashicorp/terraform-plugin-log/tflog"
14+
"google.golang.org/grpc/codes"
15+
"google.golang.org/grpc/status"
1416

1517
client "github.com/pomerium/enterprise-client-go"
1618
"github.com/pomerium/enterprise-client-go/pb"
@@ -150,6 +152,10 @@ func (r *PolicyResource) Read(ctx context.Context, req resource.ReadRequest, res
150152
Id: state.ID.ValueString(),
151153
})
152154
if err != nil {
155+
if status.Code(err) == codes.NotFound {
156+
resp.State.RemoveResource(ctx)
157+
return
158+
}
153159
resp.Diagnostics.AddError("Error reading policy", err.Error())
154160
return
155161
}

internal/provider/route.go

+6
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import (
1313
"github.com/hashicorp/terraform-plugin-log/tflog"
1414
client "github.com/pomerium/enterprise-client-go"
1515
"github.com/pomerium/enterprise-client-go/pb"
16+
"google.golang.org/grpc/codes"
17+
"google.golang.org/grpc/status"
1618
)
1719

1820
// Ensure provider defined types fully satisfy framework interfaces.
@@ -256,6 +258,10 @@ func (r *RouteResource) Read(ctx context.Context, req resource.ReadRequest, resp
256258
Id: state.ID.ValueString(),
257259
})
258260
if err != nil {
261+
if status.Code(err) == codes.NotFound {
262+
resp.State.RemoveResource(ctx)
263+
return
264+
}
259265
resp.Diagnostics.AddError("get route", err.Error())
260266
return
261267
}

internal/provider/service_account.go

+6
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import (
1111
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
1212
"github.com/hashicorp/terraform-plugin-framework/types"
1313
"github.com/hashicorp/terraform-plugin-log/tflog"
14+
"google.golang.org/grpc/codes"
15+
"google.golang.org/grpc/status"
1416

1517
client "github.com/pomerium/enterprise-client-go"
1618
"github.com/pomerium/enterprise-client-go/pb"
@@ -131,6 +133,10 @@ func (r *ServiceAccountResource) Read(ctx context.Context, req resource.ReadRequ
131133
Id: state.ID.ValueString(),
132134
})
133135
if err != nil {
136+
if status.Code(err) == codes.NotFound {
137+
resp.State.RemoveResource(ctx)
138+
return
139+
}
134140
resp.Diagnostics.AddError("Error reading service account", err.Error())
135141
return
136142
}

0 commit comments

Comments
 (0)