Skip to content

Commit

Permalink
handle user not found (#7494) (#5369)
Browse files Browse the repository at this point in the history
* handle user not found

* update

---------

Signed-off-by: Modular Magician <magic-modules@google.com>
Co-authored-by: Edward Sun <sunedward@google.com>
  • Loading branch information
modular-magician and Edward Sun authored Mar 27, 2023
1 parent b8e2c1c commit 6a987a0
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
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-beta/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

0 comments on commit 6a987a0

Please sign in to comment.