11package google
22
33import (
4- "context"
54 "fmt"
6- "log"
75 "regexp"
8- "strings"
96 "testing"
107 "time"
118
@@ -30,13 +27,6 @@ var ignoredReplicaConfigurationFields = []string{
3027 "deletion_protection" ,
3128}
3229
33- func init () {
34- resource .AddTestSweepers ("gcp_sql_db_instance" , & resource.Sweeper {
35- Name : "gcp_sql_db_instance" ,
36- F : testSweepDatabases ,
37- })
38- }
39-
4030func TestMaintenanceVersionDiffSuppress (t * testing.T ) {
4131 cases := map [string ]struct {
4232 Old , New string
@@ -70,122 +60,6 @@ func TestMaintenanceVersionDiffSuppress(t *testing.T) {
7060 }
7161}
7262
73- func testSweepDatabases (region string ) error {
74- config , err := sharedConfigForRegion (region )
75- if err != nil {
76- return fmt .Errorf ("error getting shared config for region: %s" , err )
77- }
78-
79- err = config .LoadAndValidate (context .Background ())
80- if err != nil {
81- log .Fatalf ("error loading: %s" , err )
82- }
83-
84- found , err := config .NewSqlAdminClient (config .userAgent ).Instances .List (config .Project ).Do ()
85- if err != nil {
86- log .Printf ("error listing databases: %s" , err )
87- return nil
88- }
89-
90- if len (found .Items ) == 0 {
91- log .Printf ("No databases found" )
92- return nil
93- }
94-
95- running := map [string ]struct {}{}
96-
97- for _ , d := range found .Items {
98- var testDbInstance bool
99- for _ , testName := range []string {"tf-lw-" , "sqldatabasetest" } {
100- // only destroy instances we know to fit our test naming pattern
101- if strings .HasPrefix (d .Name , testName ) {
102- testDbInstance = true
103- }
104- }
105-
106- if ! testDbInstance {
107- continue
108- }
109- if d .State != "RUNNABLE" {
110- continue
111- }
112- running [d .Name ] = struct {}{}
113- }
114-
115- for _ , d := range found .Items {
116- // don't delete replicas, we'll take care of that
117- // when deleting the database they replicate
118- if d .ReplicaConfiguration != nil {
119- continue
120- }
121- log .Printf ("Destroying SQL Instance (%s)" , d .Name )
122-
123- // replicas need to be stopped and destroyed before destroying a master
124- // instance. The ordering slice tracks replica databases for a given master
125- // and we call destroy on them before destroying the master
126- var ordering []string
127- for _ , replicaName := range d .ReplicaNames {
128- // don't try to stop replicas that aren't running
129- if _ , ok := running [replicaName ]; ! ok {
130- ordering = append (ordering , replicaName )
131- continue
132- }
133-
134- // need to stop replication before being able to destroy a database
135- op , err := config .NewSqlAdminClient (config .userAgent ).Instances .StopReplica (config .Project , replicaName ).Do ()
136-
137- if err != nil {
138- log .Printf ("error, failed to stop replica instance (%s) for instance (%s): %s" , replicaName , d .Name , err )
139- return nil
140- }
141-
142- err = sqlAdminOperationWaitTime (config , op , config .Project , "Stop Replica" , config .userAgent , 10 * time .Minute )
143- if err != nil {
144- if strings .Contains (err .Error (), "does not exist" ) {
145- log .Printf ("Replication operation not found" )
146- } else {
147- log .Printf ("Error waiting for sqlAdmin operation: %s" , err )
148- return nil
149- }
150- }
151-
152- ordering = append (ordering , replicaName )
153- }
154-
155- // ordering has a list of replicas (or none), now add the primary to the end
156- ordering = append (ordering , d .Name )
157-
158- for _ , db := range ordering {
159- // destroy instances, replicas first
160- op , err := config .NewSqlAdminClient (config .userAgent ).Instances .Delete (config .Project , db ).Do ()
161-
162- if err != nil {
163- if strings .Contains (err .Error (), "409" ) {
164- // the GCP api can return a 409 error after the delete operation
165- // reaches a successful end
166- log .Printf ("Operation not found, got 409 response" )
167- continue
168- }
169-
170- log .Printf ("Error, failed to delete instance %s: %s" , db , err )
171- return nil
172- }
173-
174- err = sqlAdminOperationWaitTime (config , op , config .Project , "Delete Instance" , config .userAgent , 10 * time .Minute )
175- if err != nil {
176- if strings .Contains (err .Error (), "does not exist" ) {
177- log .Printf ("SQL instance not found" )
178- continue
179- }
180- log .Printf ("Error, failed to delete instance %s: %s" , db , err )
181- return nil
182- }
183- }
184- }
185-
186- return nil
187- }
188-
18963func TestAccSqlDatabaseInstance_basicInferredName (t * testing.T ) {
19064 // Randomness
19165 skipIfVcr (t )
@@ -472,7 +346,7 @@ func TestAccSqlDatabaseInstance_replica(t *testing.T) {
472346 {
473347 Config : fmt .Sprintf (
474348 testGoogleSqlDatabaseInstance_replica , databaseID , databaseID , databaseID , "true" ),
475- ExpectError : regexp .MustCompile ("Error, failed to create instance tf-lw -\\ d+-2: googleapi: Error 400: Invalid request: Invalid flag for instance role: Backups cannot be enabled for read replica instance.., invalid" ),
349+ ExpectError : regexp .MustCompile ("Error, failed to create instance tf-test -\\ d+-2: googleapi: Error 400: Invalid request: Invalid flag for instance role: Backups cannot be enabled for read replica instance.., invalid" ),
476350 },
477351 {
478352 Config : fmt .Sprintf (
@@ -2009,7 +1883,7 @@ resource "google_sql_database_instance" "instance" {
20091883
20101884var testGoogleSqlDatabaseInstance_replica = `
20111885resource "google_sql_database_instance" "instance_master" {
2012- name = "tf-lw -%d"
1886+ name = "tf-test -%d"
20131887 database_version = "MYSQL_5_7"
20141888 region = "us-central1"
20151889 deletion_protection = false
@@ -2026,7 +1900,7 @@ resource "google_sql_database_instance" "instance_master" {
20261900}
20271901
20281902resource "google_sql_database_instance" "replica1" {
2029- name = "tf-lw -%d-1"
1903+ name = "tf-test -%d-1"
20301904 database_version = "MYSQL_5_7"
20311905 region = "us-central1"
20321906 deletion_protection = false
@@ -2052,7 +1926,7 @@ resource "google_sql_database_instance" "replica1" {
20521926}
20531927
20541928resource "google_sql_database_instance" "replica2" {
2055- name = "tf-lw -%d-2"
1929+ name = "tf-test -%d-2"
20561930 database_version = "MYSQL_5_7"
20571931 region = "us-central1"
20581932 deletion_protection = false
@@ -2079,7 +1953,7 @@ resource "google_sql_database_instance" "replica2" {
20791953
20801954var testGoogleSqlDatabaseInstance_slave = `
20811955resource "google_sql_database_instance" "instance_master" {
2082- name = "tf-lw -%d"
1956+ name = "tf-test -%d"
20831957 region = "us-central1"
20841958 database_version = "MYSQL_5_7"
20851959 deletion_protection = false
@@ -2095,7 +1969,7 @@ resource "google_sql_database_instance" "instance_master" {
20951969}
20961970
20971971resource "google_sql_database_instance" "instance_slave" {
2098- name = "tf-lw -%d"
1972+ name = "tf-test -%d"
20991973 region = "us-central1"
21001974 database_version = "MYSQL_5_7"
21011975 deletion_protection = false
@@ -2110,7 +1984,7 @@ resource "google_sql_database_instance" "instance_slave" {
21101984
21111985var testGoogleSqlDatabaseInstance_highAvailability = `
21121986resource "google_sql_database_instance" "instance" {
2113- name = "tf-lw -%d"
1987+ name = "tf-test -%d"
21141988 region = "us-central1"
21151989 database_version = "POSTGRES_9_6"
21161990 deletion_protection = false
@@ -2130,7 +2004,7 @@ resource "google_sql_database_instance" "instance" {
21302004
21312005var testGoogleSqlDatabaseInstance_diskspecs = `
21322006resource "google_sql_database_instance" "instance" {
2133- name = "tf-lw -%d"
2007+ name = "tf-test -%d"
21342008 region = "us-central1"
21352009 database_version = "MYSQL_5_7"
21362010 deletion_protection = false
@@ -2147,7 +2021,7 @@ resource "google_sql_database_instance" "instance" {
21472021
21482022var testGoogleSqlDatabaseInstance_maintenance = `
21492023resource "google_sql_database_instance" "instance" {
2150- name = "tf-lw -%d"
2024+ name = "tf-test -%d"
21512025 region = "us-central1"
21522026 database_version = "MYSQL_5_7"
21532027 deletion_protection = false
@@ -2166,7 +2040,7 @@ resource "google_sql_database_instance" "instance" {
21662040
21672041var testGoogleSqlDatabaseInstance_authNets_step1 = `
21682042resource "google_sql_database_instance" "instance" {
2169- name = "tf-lw -%d"
2043+ name = "tf-test -%d"
21702044 region = "us-central1"
21712045 database_version = "MYSQL_5_7"
21722046 deletion_protection = false
@@ -2186,7 +2060,7 @@ resource "google_sql_database_instance" "instance" {
21862060
21872061var testGoogleSqlDatabaseInstance_authNets_step2 = `
21882062resource "google_sql_database_instance" "instance" {
2189- name = "tf-lw -%d"
2063+ name = "tf-test -%d"
21902064 region = "us-central1"
21912065 database_version = "MYSQL_5_7"
21922066 deletion_protection = false
0 commit comments