Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bigtable: Remove resources on NOT_FOUND error only #12953

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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