Skip to content

Commit 45adf0d

Browse files
Update SQL Instance sweeper to check common prefixes (#6934) (#4973)
Signed-off-by: Modular Magician <[email protected]> Signed-off-by: Modular Magician <[email protected]>
1 parent 61d6d86 commit 45adf0d

File tree

4 files changed

+144
-141
lines changed

4 files changed

+144
-141
lines changed

.changelog/6934.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
```release-note:none
2+
```
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
package google
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"log"
7+
"strings"
8+
"time"
9+
10+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
11+
)
12+
13+
func init() {
14+
resource.AddTestSweepers("SQLDatabaseInstance", &resource.Sweeper{
15+
Name: "SQLDatabaseInstance",
16+
F: testSweepSQLDatabaseInstance,
17+
})
18+
}
19+
20+
func testSweepSQLDatabaseInstance(region string) error {
21+
config, err := sharedConfigForRegion(region)
22+
if err != nil {
23+
return fmt.Errorf("error getting shared config for region: %s", err)
24+
}
25+
26+
err = config.LoadAndValidate(context.Background())
27+
if err != nil {
28+
log.Fatalf("error loading: %s", err)
29+
}
30+
31+
found, err := config.NewSqlAdminClient(config.userAgent).Instances.List(config.Project).Do()
32+
if err != nil {
33+
log.Printf("error listing databases: %s", err)
34+
return nil
35+
}
36+
37+
if len(found.Items) == 0 {
38+
log.Printf("No databases found")
39+
return nil
40+
}
41+
42+
running := map[string]struct{}{}
43+
44+
for _, d := range found.Items {
45+
if !isSweepableTestResource(d.Name) {
46+
continue
47+
}
48+
49+
if d.State != "RUNNABLE" {
50+
continue
51+
}
52+
running[d.Name] = struct{}{}
53+
}
54+
55+
for _, d := range found.Items {
56+
// don't delete replicas, we'll take care of that
57+
// when deleting the database they replicate
58+
if d.ReplicaConfiguration != nil {
59+
continue
60+
}
61+
log.Printf("Destroying SQL Instance (%s)", d.Name)
62+
63+
// replicas need to be stopped and destroyed before destroying a master
64+
// instance. The ordering slice tracks replica databases for a given master
65+
// and we call destroy on them before destroying the master
66+
var ordering []string
67+
for _, replicaName := range d.ReplicaNames {
68+
// don't try to stop replicas that aren't running
69+
if _, ok := running[replicaName]; !ok {
70+
ordering = append(ordering, replicaName)
71+
continue
72+
}
73+
74+
// need to stop replication before being able to destroy a database
75+
op, err := config.NewSqlAdminClient(config.userAgent).Instances.StopReplica(config.Project, replicaName).Do()
76+
77+
if err != nil {
78+
log.Printf("error, failed to stop replica instance (%s) for instance (%s): %s", replicaName, d.Name, err)
79+
return nil
80+
}
81+
82+
err = sqlAdminOperationWaitTime(config, op, config.Project, "Stop Replica", config.userAgent, 10*time.Minute)
83+
if err != nil {
84+
if strings.Contains(err.Error(), "does not exist") {
85+
log.Printf("Replication operation not found")
86+
} else {
87+
log.Printf("Error waiting for sqlAdmin operation: %s", err)
88+
return nil
89+
}
90+
}
91+
92+
ordering = append(ordering, replicaName)
93+
}
94+
95+
// ordering has a list of replicas (or none), now add the primary to the end
96+
ordering = append(ordering, d.Name)
97+
98+
for _, db := range ordering {
99+
// destroy instances, replicas first
100+
op, err := config.NewSqlAdminClient(config.userAgent).Instances.Delete(config.Project, db).Do()
101+
102+
if err != nil {
103+
if strings.Contains(err.Error(), "409") {
104+
// the GCP api can return a 409 error after the delete operation
105+
// reaches a successful end
106+
log.Printf("Operation not found, got 409 response")
107+
continue
108+
}
109+
110+
log.Printf("Error, failed to delete instance %s: %s", db, err)
111+
return nil
112+
}
113+
114+
err = sqlAdminOperationWaitTime(config, op, config.Project, "Delete Instance", config.userAgent, 10*time.Minute)
115+
if err != nil {
116+
if strings.Contains(err.Error(), "does not exist") {
117+
log.Printf("SQL instance not found")
118+
continue
119+
}
120+
log.Printf("Error, failed to delete instance %s: %s", db, err)
121+
return nil
122+
}
123+
}
124+
}
125+
126+
return nil
127+
}

google-beta/resource_sql_database_instance_test.go

Lines changed: 11 additions & 137 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
package google
22

33
import (
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-
4030
func 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-
18963
func 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

20101884
var testGoogleSqlDatabaseInstance_replica = `
20111885
resource "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
20281902
resource "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
20541928
resource "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

20801954
var testGoogleSqlDatabaseInstance_slave = `
20811955
resource "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
20971971
resource "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

21111985
var testGoogleSqlDatabaseInstance_highAvailability = `
21121986
resource "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

21312005
var testGoogleSqlDatabaseInstance_diskspecs = `
21322006
resource "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

21482022
var testGoogleSqlDatabaseInstance_maintenance = `
21492023
resource "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

21672041
var testGoogleSqlDatabaseInstance_authNets_step1 = `
21682042
resource "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

21872061
var testGoogleSqlDatabaseInstance_authNets_step2 = `
21882062
resource "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

Comments
 (0)