provider/google: use a mutex to prevent concurrent sql instance operations (#14424)

This commit is contained in:
Dana Hoffman 2017-05-31 15:19:27 -04:00 committed by GitHub
parent b463bec369
commit 0d1718fa83
4 changed files with 55 additions and 0 deletions

View File

@ -57,6 +57,8 @@ func resourceSqlDatabaseCreate(d *schema.ResourceData, meta interface{}) error {
Instance: instance_name,
}
mutexKV.Lock(instanceMutexKey(project, instance_name))
defer mutexKV.Unlock(instanceMutexKey(project, instance_name))
op, err := config.clientSqlAdmin.Databases.Insert(project, instance_name,
db).Do()
@ -111,6 +113,8 @@ func resourceSqlDatabaseDelete(d *schema.ResourceData, meta interface{}) error {
database_name := d.Get("name").(string)
instance_name := d.Get("instance").(string)
mutexKV.Lock(instanceMutexKey(project, instance_name))
defer mutexKV.Unlock(instanceMutexKey(project, instance_name))
op, err := config.clientSqlAdmin.Databases.Delete(project, instance_name,
database_name).Do()

View File

@ -1172,3 +1172,7 @@ func validateNumericRange(v interface{}, k string, min int, max int) (ws []strin
}
return
}
func instanceMutexKey(project, instance_name string) string {
return fmt.Sprintf("google-sql-database-instance-%s-%s", project, instance_name)
}

View File

@ -277,6 +277,24 @@ func TestAccGoogleSqlDatabaseInstance_authNets(t *testing.T) {
})
}
// Tests that a SQL instance can be referenced from more than one other resource without
// throwing an error during provisioning, see #9018.
func TestAccGoogleSqlDatabaseInstance_multipleOperations(t *testing.T) {
databaseID, instanceID, userID := acctest.RandString(8), acctest.RandString(8), acctest.RandString(8)
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccGoogleSqlDatabaseInstanceDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: fmt.Sprintf(
testGoogleSqlDatabaseInstance_multipleOperations, databaseID, instanceID, userID),
},
},
})
}
func testAccCheckGoogleSqlDatabaseInstanceEquals(n string,
instance *sqladmin.DatabaseInstance) resource.TestCheckFunc {
return func(s *terraform.State) error {
@ -678,3 +696,26 @@ resource "google_sql_database_instance" "instance" {
}
}
`
var testGoogleSqlDatabaseInstance_multipleOperations = `
resource "google_sql_database_instance" "instance" {
name = "tf-test-%s"
region = "us-central"
settings {
tier = "D0"
crash_safe_replication = false
}
}
resource "google_sql_database" "database" {
name = "tf-test-%s"
instance = "${google_sql_database_instance.instance.name}"
}
resource "google_sql_user" "user" {
name = "tf-test-%s"
instance = "${google_sql_database_instance.instance.name}"
host = "google.com"
password = "hunter2"
}
`

View File

@ -76,6 +76,8 @@ func resourceSqlUserCreate(d *schema.ResourceData, meta interface{}) error {
Host: host,
}
mutexKV.Lock(instanceMutexKey(project, instance))
defer mutexKV.Unlock(instanceMutexKey(project, instance))
op, err := config.clientSqlAdmin.Users.Insert(project, instance,
user).Do()
@ -163,6 +165,8 @@ func resourceSqlUserUpdate(d *schema.ResourceData, meta interface{}) error {
Host: host,
}
mutexKV.Lock(instanceMutexKey(project, instance))
defer mutexKV.Unlock(instanceMutexKey(project, instance))
op, err := config.clientSqlAdmin.Users.Update(project, instance, host, name,
user).Do()
@ -196,6 +200,8 @@ func resourceSqlUserDelete(d *schema.ResourceData, meta interface{}) error {
instance := d.Get("instance").(string)
host := d.Get("host").(string)
mutexKV.Lock(instanceMutexKey(project, instance))
defer mutexKV.Unlock(instanceMutexKey(project, instance))
op, err := config.clientSqlAdmin.Users.Delete(project, instance, host, name).Do()
if err != nil {