-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Signed-off-by: Modular Magician <magic-modules@google.com> Signed-off-by: Modular Magician <magic-modules@google.com>
- Loading branch information
1 parent
a989875
commit 9688bd7
Showing
4 changed files
with
144 additions
and
141 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
```release-note:none | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
package google | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log" | ||
"strings" | ||
"time" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
) | ||
|
||
func init() { | ||
resource.AddTestSweepers("SQLDatabaseInstance", &resource.Sweeper{ | ||
Name: "SQLDatabaseInstance", | ||
F: testSweepSQLDatabaseInstance, | ||
}) | ||
} | ||
|
||
func testSweepSQLDatabaseInstance(region string) error { | ||
config, err := sharedConfigForRegion(region) | ||
if err != nil { | ||
return fmt.Errorf("error getting shared config for region: %s", err) | ||
} | ||
|
||
err = config.LoadAndValidate(context.Background()) | ||
if err != nil { | ||
log.Fatalf("error loading: %s", err) | ||
} | ||
|
||
found, err := config.NewSqlAdminClient(config.userAgent).Instances.List(config.Project).Do() | ||
if err != nil { | ||
log.Printf("error listing databases: %s", err) | ||
return nil | ||
} | ||
|
||
if len(found.Items) == 0 { | ||
log.Printf("No databases found") | ||
return nil | ||
} | ||
|
||
running := map[string]struct{}{} | ||
|
||
for _, d := range found.Items { | ||
if !isSweepableTestResource(d.Name) { | ||
continue | ||
} | ||
|
||
if d.State != "RUNNABLE" { | ||
continue | ||
} | ||
running[d.Name] = struct{}{} | ||
} | ||
|
||
for _, d := range found.Items { | ||
// don't delete replicas, we'll take care of that | ||
// when deleting the database they replicate | ||
if d.ReplicaConfiguration != nil { | ||
continue | ||
} | ||
log.Printf("Destroying SQL Instance (%s)", d.Name) | ||
|
||
// replicas need to be stopped and destroyed before destroying a master | ||
// instance. The ordering slice tracks replica databases for a given master | ||
// and we call destroy on them before destroying the master | ||
var ordering []string | ||
for _, replicaName := range d.ReplicaNames { | ||
// don't try to stop replicas that aren't running | ||
if _, ok := running[replicaName]; !ok { | ||
ordering = append(ordering, replicaName) | ||
continue | ||
} | ||
|
||
// need to stop replication before being able to destroy a database | ||
op, err := config.NewSqlAdminClient(config.userAgent).Instances.StopReplica(config.Project, replicaName).Do() | ||
|
||
if err != nil { | ||
log.Printf("error, failed to stop replica instance (%s) for instance (%s): %s", replicaName, d.Name, err) | ||
return nil | ||
} | ||
|
||
err = sqlAdminOperationWaitTime(config, op, config.Project, "Stop Replica", config.userAgent, 10*time.Minute) | ||
if err != nil { | ||
if strings.Contains(err.Error(), "does not exist") { | ||
log.Printf("Replication operation not found") | ||
} else { | ||
log.Printf("Error waiting for sqlAdmin operation: %s", err) | ||
return nil | ||
} | ||
} | ||
|
||
ordering = append(ordering, replicaName) | ||
} | ||
|
||
// ordering has a list of replicas (or none), now add the primary to the end | ||
ordering = append(ordering, d.Name) | ||
|
||
for _, db := range ordering { | ||
// destroy instances, replicas first | ||
op, err := config.NewSqlAdminClient(config.userAgent).Instances.Delete(config.Project, db).Do() | ||
|
||
if err != nil { | ||
if strings.Contains(err.Error(), "409") { | ||
// the GCP api can return a 409 error after the delete operation | ||
// reaches a successful end | ||
log.Printf("Operation not found, got 409 response") | ||
continue | ||
} | ||
|
||
log.Printf("Error, failed to delete instance %s: %s", db, err) | ||
return nil | ||
} | ||
|
||
err = sqlAdminOperationWaitTime(config, op, config.Project, "Delete Instance", config.userAgent, 10*time.Minute) | ||
if err != nil { | ||
if strings.Contains(err.Error(), "does not exist") { | ||
log.Printf("SQL instance not found") | ||
continue | ||
} | ||
log.Printf("Error, failed to delete instance %s: %s", db, err) | ||
return nil | ||
} | ||
} | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.