mirror of
https://github.com/letic/terraform-provider-google.git
synced 2024-07-01 07:42:40 +00:00
provider/aws: Add Sweeper setup, Sweepers for DB Option Group, Key Pair (#14773)
* provider/aws: Add Sweeper setup, Sweepers for DB Option Group, Key Pair * provider/google: Add sweeper for any leaked databases * more recursion and added LC sweeper, to test out the Dependency path * implement a dependency example * implement sweep-run flag to filter runs * stub a test for TestMain * test for multiple -sweep-run list
This commit is contained in:
parent
7c9158e454
commit
a543e456e0
35
gcp_sweeper_test.go
Normal file
35
gcp_sweeper_test.go
Normal file
|
@ -0,0 +1,35 @@
|
|||
package google
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/hashicorp/terraform/helper/resource"
|
||||
)
|
||||
|
||||
func TestMain(m *testing.M) {
|
||||
resource.TestMain(m)
|
||||
}
|
||||
|
||||
// sharedConfigForRegion returns a common config setup needed for the sweeper
|
||||
// functions for a given region
|
||||
func sharedConfigForRegion(region string) (*Config, error) {
|
||||
project := os.Getenv("GOOGLE_PROJECT")
|
||||
if project == "" {
|
||||
return nil, fmt.Errorf("empty GOOGLE_PROJECT")
|
||||
}
|
||||
|
||||
creds := os.Getenv("GOOGLE_CREDENTIALS")
|
||||
if creds == "" {
|
||||
return nil, fmt.Errorf("empty GOOGLE_CREDENTIALS")
|
||||
}
|
||||
|
||||
conf := &Config{
|
||||
Credentials: creds,
|
||||
Region: region,
|
||||
Project: project,
|
||||
}
|
||||
|
||||
return conf, nil
|
||||
}
|
|
@ -9,6 +9,7 @@ package google
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"strconv"
|
||||
"strings"
|
||||
"testing"
|
||||
|
@ -20,6 +21,105 @@ import (
|
|||
"google.golang.org/api/sqladmin/v1beta4"
|
||||
)
|
||||
|
||||
func init() {
|
||||
resource.AddTestSweepers("gcp_sql_db_instance", &resource.Sweeper{
|
||||
Name: "gcp_sql_db_instance",
|
||||
F: testSweepDatabases,
|
||||
})
|
||||
}
|
||||
|
||||
func testSweepDatabases(region string) error {
|
||||
config, err := sharedConfigForRegion(region)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error getting shared config for region: %s", err)
|
||||
}
|
||||
|
||||
err = config.loadAndValidate()
|
||||
if err != nil {
|
||||
log.Fatalf("error loading: %s", err)
|
||||
}
|
||||
|
||||
found, err := config.clientSqlAdmin.Instances.List(config.Project).Do()
|
||||
if err != nil {
|
||||
log.Fatalf("error listing databases: %s", err)
|
||||
}
|
||||
|
||||
if len(found.Items) == 0 {
|
||||
log.Printf("No databases found")
|
||||
return nil
|
||||
}
|
||||
|
||||
for _, d := range found.Items {
|
||||
var testDbInstance bool
|
||||
for _, testName := range []string{"tf-lw-", "sqldatabasetest"} {
|
||||
// only destroy instances we know to fit our test naming pattern
|
||||
if strings.HasPrefix(d.Name, testName) {
|
||||
testDbInstance = true
|
||||
}
|
||||
}
|
||||
|
||||
if !testDbInstance {
|
||||
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 {
|
||||
// need to stop replication before being able to destroy a database
|
||||
op, err := config.clientSqlAdmin.Instances.StopReplica(config.Project, replicaName).Do()
|
||||
|
||||
if err != nil {
|
||||
return fmt.Errorf("error, failed to stop replica instance (%s) for instance (%s): %s", replicaName, d.Name, err)
|
||||
}
|
||||
|
||||
err = sqladminOperationWait(config, op, "Stop Replica")
|
||||
if err != nil {
|
||||
if strings.Contains(err.Error(), "does not exist") {
|
||||
log.Printf("Replication operation not found")
|
||||
} else {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
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.clientSqlAdmin.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
|
||||
}
|
||||
|
||||
return fmt.Errorf("Error, failed to delete instance %s: %s", db, err)
|
||||
}
|
||||
|
||||
err = sqladminOperationWait(config, op, "Delete Instance")
|
||||
if err != nil {
|
||||
if strings.Contains(err.Error(), "does not exist") {
|
||||
log.Printf("SQL instance not found")
|
||||
continue
|
||||
}
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func TestAccGoogleSqlDatabaseInstance_basic(t *testing.T) {
|
||||
var instance sqladmin.DatabaseInstance
|
||||
databaseID := acctest.RandInt()
|
||||
|
|
Loading…
Reference in New Issue
Block a user