Skip to content

Commit 0ff9ba0

Browse files
Bigtable: Remove resources on NOT_FOUND error only (#6735) (#12953)
fixes #10086 Signed-off-by: Modular Magician <[email protected]> Signed-off-by: Modular Magician <[email protected]>
1 parent d837a16 commit 0ff9ba0

6 files changed

+49
-9
lines changed

.changelog/6735.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:bug
2+
bigtable: updated the error handling logic to remove the resource on resource not found error only
3+
```

google/resource_bigtable_gc_policy.go

+6-3
Original file line numberDiff line numberDiff line change
@@ -256,9 +256,12 @@ func resourceBigtableGCPolicyRead(d *schema.ResourceData, meta interface{}) erro
256256
columnFamily := d.Get("column_family").(string)
257257
ti, err := c.TableInfo(ctx, name)
258258
if err != nil {
259-
log.Printf("[WARN] Removing %s because it's gone", name)
260-
d.SetId("")
261-
return nil
259+
if isNotFoundGrpcError(err) {
260+
log.Printf("[WARN] Removing the GC policy because the parent table %s is gone", name)
261+
d.SetId("")
262+
return nil
263+
}
264+
return err
262265
}
263266

264267
for _, fi := range ti.FamilyInfos {

google/resource_bigtable_instance.go

+6-3
Original file line numberDiff line numberDiff line change
@@ -246,9 +246,12 @@ func resourceBigtableInstanceRead(d *schema.ResourceData, meta interface{}) erro
246246

247247
instance, err := c.InstanceInfo(ctx, instanceName)
248248
if err != nil {
249-
log.Printf("[WARN] Removing %s because it's gone", instanceName)
250-
d.SetId("")
251-
return nil
249+
if isNotFoundGrpcError(err) {
250+
log.Printf("[WARN] Removing %s because it's gone", instanceName)
251+
d.SetId("")
252+
return nil
253+
}
254+
return err
252255
}
253256

254257
if err := d.Set("project", project); err != nil {

google/resource_bigtable_table.go

+6-3
Original file line numberDiff line numberDiff line change
@@ -173,9 +173,12 @@ func resourceBigtableTableRead(d *schema.ResourceData, meta interface{}) error {
173173
name := d.Get("name").(string)
174174
table, err := c.TableInfo(ctx, name)
175175
if err != nil {
176-
log.Printf("[WARN] Removing %s because it's gone", name)
177-
d.SetId("")
178-
return nil
176+
if isNotFoundGrpcError(err) {
177+
log.Printf("[WARN] Removing %s because it's gone", name)
178+
d.SetId("")
179+
return nil
180+
}
181+
return err
179182
}
180183

181184
if err := d.Set("project", project); err != nil {

google/utils.go

+11
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ import (
1616
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1717
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
1818
"google.golang.org/api/googleapi"
19+
"google.golang.org/grpc/codes"
20+
"google.golang.org/grpc/status"
1921
)
2022

2123
type TerraformResourceDataChange interface {
@@ -160,6 +162,15 @@ func isConflictError(err error) bool {
160162
return false
161163
}
162164

165+
// gRPC does not return errors of type *googleapi.Error. Instead the errors returned are *status.Error.
166+
// See the types of codes returned here (https://pkg.go.dev/google.golang.org/grpc/codes#Code).
167+
func isNotFoundGrpcError(err error) bool {
168+
if errorStatus, ok := status.FromError(err); ok && errorStatus.Code() == codes.NotFound {
169+
return true
170+
}
171+
return false
172+
}
173+
163174
// expandLabels pulls the value of "labels" out of a TerraformResourceData as a map[string]string.
164175
func expandLabels(d TerraformResourceData) map[string]string {
165176
return expandStringMap(d, "labels")

google/utils_test.go

+17
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import (
1111
"github.com/hashicorp/errwrap"
1212
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1313
"google.golang.org/api/googleapi"
14+
"google.golang.org/grpc/codes"
15+
"google.golang.org/grpc/status"
1416
)
1517

1618
func TestConvertStringArr(t *testing.T) {
@@ -694,6 +696,21 @@ func TestConflictError(t *testing.T) {
694696
// skipping negative tests as other cases may be added later.
695697
}
696698

699+
func TestIsNotFoundGrpcErrort(t *testing.T) {
700+
error_status := status.New(codes.FailedPrecondition, "FailedPrecondition error")
701+
if isNotFoundGrpcError(error_status.Err()) {
702+
t.Error("found FailedPrecondition as a NotFound error")
703+
}
704+
error_status = status.New(codes.OK, "OK")
705+
if isNotFoundGrpcError(error_status.Err()) {
706+
t.Error("found OK as a NotFound error")
707+
}
708+
error_status = status.New(codes.NotFound, "NotFound error")
709+
if !isNotFoundGrpcError(error_status.Err()) {
710+
t.Error("expect a NotFound error")
711+
}
712+
}
713+
697714
func TestSnakeToPascalCase(t *testing.T) {
698715
input := "boot_disk"
699716
expected := "BootDisk"

0 commit comments

Comments
 (0)