@@ -78,6 +78,40 @@ func TestAccSqlUser_postgres(t *testing.T) {
7878 })
7979}
8080
81+ func TestAccSqlUser_postgresAbandon (t * testing.T ) {
82+ t .Parallel ()
83+
84+ instance := fmt .Sprintf ("i-%d" , randInt (t ))
85+ userName := "admin"
86+ vcrTest (t , resource.TestCase {
87+ PreCheck : func () { testAccPreCheck (t ) },
88+ Providers : testAccProviders ,
89+ CheckDestroy : testAccSqlUserDestroyProducer (t ),
90+ Steps : []resource.TestStep {
91+ {
92+ Config : testGoogleSqlUser_postgresAbandon (instance , userName ),
93+ Check : resource .ComposeTestCheckFunc (
94+ testAccCheckGoogleSqlUserExists (t , "google_sql_user.user" ),
95+ ),
96+ },
97+ {
98+ ResourceName : "google_sql_user.user" ,
99+ ImportStateId : fmt .Sprintf ("%s/%s/admin" , getTestProjectFromEnv (), instance ),
100+ ImportState : true ,
101+ ImportStateVerify : true ,
102+ ImportStateVerifyIgnore : []string {"password" , "deletion_policy" },
103+ },
104+ {
105+ // Abandon user
106+ Config : testGoogleSqlUser_postgresNoUser (instance ),
107+ Check : resource .ComposeTestCheckFunc (
108+ testAccCheckGoogleSqlUserExistsWithName (t , instance , userName ),
109+ ),
110+ },
111+ },
112+ })
113+ }
114+
81115func testAccCheckGoogleSqlUserExists (t * testing.T , n string ) resource.TestCheckFunc {
82116 return func (s * terraform.State ) error {
83117 config := googleProviderConfig (t )
@@ -106,6 +140,27 @@ func testAccCheckGoogleSqlUserExists(t *testing.T, n string) resource.TestCheckF
106140 }
107141}
108142
143+ func testAccCheckGoogleSqlUserExistsWithName (t * testing.T , instance , name string ) resource.TestCheckFunc {
144+ return func (s * terraform.State ) error {
145+ config := googleProviderConfig (t )
146+
147+ users , err := config .NewSqlAdminClient (config .userAgent ).Users .List (config .Project ,
148+ instance ).Do ()
149+
150+ if err != nil {
151+ return err
152+ }
153+
154+ for _ , user := range users .Items {
155+ if user .Name == name {
156+ return nil
157+ }
158+ }
159+
160+ return fmt .Errorf ("Not found: User: %s in instance: %s: %s" , name , instance , err )
161+ }
162+ }
163+
109164func testAccSqlUserDestroyProducer (t * testing.T ) func (s * terraform.State ) error {
110165 return func (s * terraform.State ) error {
111166 for _ , rs := range s .RootModule ().Resources {
@@ -180,3 +235,40 @@ resource "google_sql_user" "user" {
180235}
181236` , instance , password )
182237}
238+
239+ func testGoogleSqlUser_postgresAbandon (instance , name string ) string {
240+ return fmt .Sprintf (`
241+ resource "google_sql_database_instance" "instance" {
242+ name = "%s"
243+ region = "us-central1"
244+ database_version = "POSTGRES_9_6"
245+ deletion_protection = false
246+
247+ settings {
248+ tier = "db-f1-micro"
249+ }
250+ }
251+
252+ resource "google_sql_user" "user" {
253+ name = "%s"
254+ instance = google_sql_database_instance.instance.name
255+ password = "password"
256+ deletion_policy = "ABANDON"
257+ }
258+ ` , instance , name )
259+ }
260+
261+ func testGoogleSqlUser_postgresNoUser (instance string ) string {
262+ return fmt .Sprintf (`
263+ resource "google_sql_database_instance" "instance" {
264+ name = "%s"
265+ region = "us-central1"
266+ database_version = "POSTGRES_9_6"
267+ deletion_protection = false
268+
269+ settings {
270+ tier = "db-f1-micro"
271+ }
272+ }
273+ ` , instance )
274+ }
0 commit comments