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

handle user not found #14098

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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/7494.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
cloudsql: fixed the error in any subsequent apply on `google_sql_user` after its `google_sql_database_instance` is deleted
```
17 changes: 16 additions & 1 deletion google/resource_sql_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"strings"
"time"

"github.com/hashicorp/errwrap"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
sqladmin "google.golang.org/api/sqladmin/v1beta4"
Expand All @@ -23,6 +24,19 @@ func diffSuppressIamUserName(_, old, new string, d *schema.ResourceData) bool {
return false
}

func handleUserNotFoundError(err error, d *schema.ResourceData, resource string) error {
if IsGoogleApiErrorWithCode(err, 404) || IsGoogleApiErrorWithCode(err, 403) {
log.Printf("[WARN] Removing %s because it's gone", resource)
// The resource doesn't exist anymore
d.SetId("")

return nil
}

return errwrap.Wrapf(
fmt.Sprintf("Error when reading or editing %s: {{err}}", resource), err)
}

func ResourceSqlUser() *schema.Resource {
return &schema.Resource{
Create: resourceSqlUserCreate,
Expand Down Expand Up @@ -306,7 +320,8 @@ func resourceSqlUserRead(d *schema.ResourceData, meta interface{}) error {
return err
}, 5)
if err != nil {
return handleNotFoundError(err, d, fmt.Sprintf("SQL User %q in instance %q", name, instance))
// move away from handleNotFoundError() as we need to handle both 404 and 403
return handleUserNotFoundError(err, d, fmt.Sprintf("SQL User %q in instance %q", name, instance))
}

var user *sqladmin.User
Expand Down