Skip to content

Commit

Permalink
Bigtable: Remove resources on NOT_FOUND error only (#6735) (#12953)
Browse files Browse the repository at this point in the history
fixes #10086

Signed-off-by: Modular Magician <magic-modules@google.com>

Signed-off-by: Modular Magician <magic-modules@google.com>
  • Loading branch information
modular-magician committed Nov 7, 2022
1 parent d837a16 commit 0ff9ba0
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 9 deletions.
3 changes: 3 additions & 0 deletions .changelog/6735.txt
@@ -0,0 +1,3 @@
```release-note:bug
bigtable: updated the error handling logic to remove the resource on resource not found error only
```
9 changes: 6 additions & 3 deletions google/resource_bigtable_gc_policy.go
Expand Up @@ -256,9 +256,12 @@ func resourceBigtableGCPolicyRead(d *schema.ResourceData, meta interface{}) erro
columnFamily := d.Get("column_family").(string)
ti, err := c.TableInfo(ctx, name)
if err != nil {
log.Printf("[WARN] Removing %s because it's gone", name)
d.SetId("")
return nil
if isNotFoundGrpcError(err) {
log.Printf("[WARN] Removing the GC policy because the parent table %s is gone", name)
d.SetId("")
return nil
}
return err
}

for _, fi := range ti.FamilyInfos {
Expand Down
9 changes: 6 additions & 3 deletions google/resource_bigtable_instance.go
Expand Up @@ -246,9 +246,12 @@ func resourceBigtableInstanceRead(d *schema.ResourceData, meta interface{}) erro

instance, err := c.InstanceInfo(ctx, instanceName)
if err != nil {
log.Printf("[WARN] Removing %s because it's gone", instanceName)
d.SetId("")
return nil
if isNotFoundGrpcError(err) {
log.Printf("[WARN] Removing %s because it's gone", instanceName)
d.SetId("")
return nil
}
return err
}

if err := d.Set("project", project); err != nil {
Expand Down
9 changes: 6 additions & 3 deletions google/resource_bigtable_table.go
Expand Up @@ -173,9 +173,12 @@ func resourceBigtableTableRead(d *schema.ResourceData, meta interface{}) error {
name := d.Get("name").(string)
table, err := c.TableInfo(ctx, name)
if err != nil {
log.Printf("[WARN] Removing %s because it's gone", name)
d.SetId("")
return nil
if isNotFoundGrpcError(err) {
log.Printf("[WARN] Removing %s because it's gone", name)
d.SetId("")
return nil
}
return err
}

if err := d.Set("project", project); err != nil {
Expand Down
11 changes: 11 additions & 0 deletions google/utils.go
Expand Up @@ -16,6 +16,8 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
"google.golang.org/api/googleapi"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)

type TerraformResourceDataChange interface {
Expand Down Expand Up @@ -160,6 +162,15 @@ func isConflictError(err error) bool {
return false
}

// gRPC does not return errors of type *googleapi.Error. Instead the errors returned are *status.Error.
// See the types of codes returned here (https://pkg.go.dev/google.golang.org/grpc/codes#Code).
func isNotFoundGrpcError(err error) bool {
if errorStatus, ok := status.FromError(err); ok && errorStatus.Code() == codes.NotFound {
return true
}
return false
}

// expandLabels pulls the value of "labels" out of a TerraformResourceData as a map[string]string.
func expandLabels(d TerraformResourceData) map[string]string {
return expandStringMap(d, "labels")
Expand Down
17 changes: 17 additions & 0 deletions google/utils_test.go
Expand Up @@ -11,6 +11,8 @@ import (
"github.com/hashicorp/errwrap"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"google.golang.org/api/googleapi"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)

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

func TestIsNotFoundGrpcErrort(t *testing.T) {
error_status := status.New(codes.FailedPrecondition, "FailedPrecondition error")
if isNotFoundGrpcError(error_status.Err()) {
t.Error("found FailedPrecondition as a NotFound error")
}
error_status = status.New(codes.OK, "OK")
if isNotFoundGrpcError(error_status.Err()) {
t.Error("found OK as a NotFound error")
}
error_status = status.New(codes.NotFound, "NotFound error")
if !isNotFoundGrpcError(error_status.Err()) {
t.Error("expect a NotFound error")
}
}

func TestSnakeToPascalCase(t *testing.T) {
input := "boot_disk"
expected := "BootDisk"
Expand Down

0 comments on commit 0ff9ba0

Please sign in to comment.