6
6
"strings"
7
7
"time"
8
8
9
+ "github.com/hashicorp/errwrap"
9
10
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
10
11
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
11
12
sqladmin "google.golang.org/api/sqladmin/v1beta4"
@@ -23,6 +24,19 @@ func diffSuppressIamUserName(_, old, new string, d *schema.ResourceData) bool {
23
24
return false
24
25
}
25
26
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
+
26
40
func ResourceSqlUser () * schema.Resource {
27
41
return & schema.Resource {
28
42
Create : resourceSqlUserCreate ,
@@ -306,7 +320,8 @@ func resourceSqlUserRead(d *schema.ResourceData, meta interface{}) error {
306
320
return err
307
321
}, 5 )
308
322
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 ))
310
325
}
311
326
312
327
var user * sqladmin.User
0 commit comments