Skip to content

Commit 6ac5eb7

Browse files
modular-magicianEdward Sun
and
Edward Sun
authored
handle user not found (#7494) (#14098)
* handle user not found * update --------- Signed-off-by: Modular Magician <[email protected]> Co-authored-by: Edward Sun <[email protected]>
1 parent bef6c5a commit 6ac5eb7

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

.changelog/7494.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:bug
2+
cloudsql: fixed the error in any subsequent apply on `google_sql_user` after its `google_sql_database_instance` is deleted
3+
```

google/resource_sql_user.go

+16-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"strings"
77
"time"
88

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

27+
func handleUserNotFoundError(err error, d *schema.ResourceData, resource string) error {
28+
if IsGoogleApiErrorWithCode(err, 404) || IsGoogleApiErrorWithCode(err, 403) {
29+
log.Printf("[WARN] Removing %s because it's gone", resource)
30+
// The resource doesn't exist anymore
31+
d.SetId("")
32+
33+
return nil
34+
}
35+
36+
return errwrap.Wrapf(
37+
fmt.Sprintf("Error when reading or editing %s: {{err}}", resource), err)
38+
}
39+
2640
func ResourceSqlUser() *schema.Resource {
2741
return &schema.Resource{
2842
Create: resourceSqlUserCreate,
@@ -306,7 +320,8 @@ func resourceSqlUserRead(d *schema.ResourceData, meta interface{}) error {
306320
return err
307321
}, 5)
308322
if err != nil {
309-
return handleNotFoundError(err, d, fmt.Sprintf("SQL User %q in instance %q", name, instance))
323+
// move away from handleNotFoundError() as we need to handle both 404 and 403
324+
return handleUserNotFoundError(err, d, fmt.Sprintf("SQL User %q in instance %q", name, instance))
310325
}
311326

312327
var user *sqladmin.User

0 commit comments

Comments
 (0)