2016-01-13 21:33:08 +00:00
|
|
|
package google
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/helper/acctest"
|
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
)
|
|
|
|
|
2018-06-11 20:47:01 +00:00
|
|
|
func TestAccSqlUser_mysql(t *testing.T) {
|
2017-10-12 22:07:29 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
2018-02-09 21:43:49 +00:00
|
|
|
instance := acctest.RandomWithPrefix("i")
|
2016-01-13 21:33:08 +00:00
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
2018-02-23 23:14:24 +00:00
|
|
|
CheckDestroy: testAccSqlUserDestroy,
|
2016-01-13 21:33:08 +00:00
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
2018-06-11 20:47:01 +00:00
|
|
|
Config: testGoogleSqlUser_mysql(instance, "password"),
|
2016-01-13 21:33:08 +00:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
2018-02-09 21:43:49 +00:00
|
|
|
testAccCheckGoogleSqlUserExists("google_sql_user.user1"),
|
|
|
|
testAccCheckGoogleSqlUserExists("google_sql_user.user2"),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
resource.TestStep{
|
|
|
|
// Update password
|
2018-06-11 20:47:01 +00:00
|
|
|
Config: testGoogleSqlUser_mysql(instance, "new_password"),
|
2018-02-09 21:43:49 +00:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckGoogleSqlUserExists("google_sql_user.user1"),
|
|
|
|
testAccCheckGoogleSqlUserExists("google_sql_user.user2"),
|
2016-01-13 21:33:08 +00:00
|
|
|
),
|
|
|
|
},
|
2018-02-09 21:43:49 +00:00
|
|
|
resource.TestStep{
|
|
|
|
ResourceName: "google_sql_user.user2",
|
|
|
|
ImportStateId: instance + "/gmail.com/admin",
|
|
|
|
ImportState: true,
|
|
|
|
ImportStateVerify: true,
|
|
|
|
ImportStateVerifyIgnore: []string{"password"},
|
|
|
|
},
|
2016-01-13 21:33:08 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-06-11 20:47:01 +00:00
|
|
|
func TestAccSqlUser_postgres(t *testing.T) {
|
2017-10-12 22:07:29 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
2018-02-09 21:43:49 +00:00
|
|
|
instance := acctest.RandomWithPrefix("i")
|
2016-01-13 21:33:08 +00:00
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
2018-02-23 23:14:24 +00:00
|
|
|
CheckDestroy: testAccSqlUserDestroy,
|
2016-01-13 21:33:08 +00:00
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
2018-06-11 20:47:01 +00:00
|
|
|
Config: testGoogleSqlUser_postgres(instance, "password"),
|
2016-01-13 21:33:08 +00:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckGoogleSqlUserExists("google_sql_user.user"),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
resource.TestStep{
|
2018-02-09 21:43:49 +00:00
|
|
|
// Update password
|
2018-06-11 20:47:01 +00:00
|
|
|
Config: testGoogleSqlUser_postgres(instance, "new_password"),
|
2016-01-13 21:33:08 +00:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckGoogleSqlUserExists("google_sql_user.user"),
|
|
|
|
),
|
|
|
|
},
|
2018-02-09 21:43:49 +00:00
|
|
|
resource.TestStep{
|
|
|
|
ResourceName: "google_sql_user.user",
|
2018-03-23 18:19:39 +00:00
|
|
|
ImportStateId: instance + "/admin",
|
2018-02-09 21:43:49 +00:00
|
|
|
ImportState: true,
|
|
|
|
ImportStateVerify: true,
|
|
|
|
ImportStateVerifyIgnore: []string{"password"},
|
|
|
|
},
|
2016-01-13 21:33:08 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccCheckGoogleSqlUserExists(n string) resource.TestCheckFunc {
|
|
|
|
return func(s *terraform.State) error {
|
|
|
|
config := testAccProvider.Meta().(*Config)
|
|
|
|
rs, ok := s.RootModule().Resources[n]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Resource not found: %s", n)
|
|
|
|
}
|
|
|
|
|
|
|
|
name := rs.Primary.Attributes["name"]
|
|
|
|
instance := rs.Primary.Attributes["instance"]
|
|
|
|
host := rs.Primary.Attributes["host"]
|
|
|
|
users, err := config.clientSqlAdmin.Users.List(config.Project,
|
|
|
|
instance).Do()
|
|
|
|
|
2018-04-30 23:10:07 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-01-13 21:33:08 +00:00
|
|
|
for _, user := range users.Items {
|
|
|
|
if user.Name == name && user.Host == host {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Errorf("Not found: %s: %s", n, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-23 23:14:24 +00:00
|
|
|
func testAccSqlUserDestroy(s *terraform.State) error {
|
2016-01-13 21:33:08 +00:00
|
|
|
for _, rs := range s.RootModule().Resources {
|
|
|
|
config := testAccProvider.Meta().(*Config)
|
|
|
|
if rs.Type != "google_sql_database" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
name := rs.Primary.Attributes["name"]
|
|
|
|
instance := rs.Primary.Attributes["instance"]
|
|
|
|
host := rs.Primary.Attributes["host"]
|
|
|
|
users, err := config.clientSqlAdmin.Users.List(config.Project,
|
|
|
|
instance).Do()
|
|
|
|
|
|
|
|
for _, user := range users.Items {
|
|
|
|
if user.Name == name && user.Host == host {
|
|
|
|
return fmt.Errorf("User still %s exists %s", name, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-06-11 20:47:01 +00:00
|
|
|
func testGoogleSqlUser_mysql(instance, password string) string {
|
2016-01-13 21:33:08 +00:00
|
|
|
return fmt.Sprintf(`
|
|
|
|
resource "google_sql_database_instance" "instance" {
|
2018-02-09 21:43:49 +00:00
|
|
|
name = "%s"
|
2016-01-13 21:33:08 +00:00
|
|
|
region = "us-central"
|
|
|
|
settings {
|
|
|
|
tier = "D0"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-09 21:43:49 +00:00
|
|
|
resource "google_sql_user" "user1" {
|
|
|
|
name = "admin"
|
2016-01-13 21:33:08 +00:00
|
|
|
instance = "${google_sql_database_instance.instance.name}"
|
|
|
|
host = "google.com"
|
2018-02-09 21:43:49 +00:00
|
|
|
password = "%s"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "google_sql_user" "user2" {
|
|
|
|
name = "admin"
|
|
|
|
instance = "${google_sql_database_instance.instance.name}"
|
|
|
|
host = "gmail.com"
|
2016-01-13 21:33:08 +00:00
|
|
|
password = "hunter2"
|
|
|
|
}
|
2018-02-09 21:43:49 +00:00
|
|
|
`, instance, password)
|
2016-01-13 21:33:08 +00:00
|
|
|
}
|
|
|
|
|
2018-06-11 20:47:01 +00:00
|
|
|
func testGoogleSqlUser_postgres(instance, password string) string {
|
2016-01-13 21:33:08 +00:00
|
|
|
return fmt.Sprintf(`
|
|
|
|
resource "google_sql_database_instance" "instance" {
|
2018-02-09 21:43:49 +00:00
|
|
|
name = "%s"
|
|
|
|
region = "us-central1"
|
|
|
|
database_version = "POSTGRES_9_6"
|
|
|
|
|
2016-01-13 21:33:08 +00:00
|
|
|
settings {
|
2018-02-09 21:43:49 +00:00
|
|
|
tier = "db-f1-micro"
|
2016-01-13 21:33:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "google_sql_user" "user" {
|
2018-02-09 21:43:49 +00:00
|
|
|
name = "admin"
|
2016-01-13 21:33:08 +00:00
|
|
|
instance = "${google_sql_database_instance.instance.name}"
|
2018-02-09 21:43:49 +00:00
|
|
|
password = "%s"
|
2016-01-13 21:33:08 +00:00
|
|
|
}
|
2018-02-09 21:43:49 +00:00
|
|
|
`, instance, password)
|
2016-01-13 21:33:08 +00:00
|
|
|
}
|