diff --git a/google/import_sql_user_test.go b/google/import_sql_user_test.go deleted file mode 100644 index 231dbe711fd..00000000000 --- a/google/import_sql_user_test.go +++ /dev/null @@ -1,34 +0,0 @@ -package google - -import ( - "testing" - - "github.com/hashicorp/terraform/helper/acctest" - "github.com/hashicorp/terraform/helper/resource" -) - -func TestAccGoogleSqlUser_importBasic(t *testing.T) { - t.Parallel() - - resourceName := "google_sql_user.user" - user := acctest.RandString(10) - instance := acctest.RandString(10) - - resource.Test(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - Providers: testAccProviders, - CheckDestroy: testAccGoogleSqlUserDestroy, - Steps: []resource.TestStep{ - resource.TestStep{ - Config: testGoogleSqlUser_basic(instance, user), - }, - - resource.TestStep{ - ResourceName: resourceName, - ImportState: true, - ImportStateVerify: true, - ImportStateVerifyIgnore: []string{"password"}, - }, - }, - }) -} diff --git a/google/resource_sql_user.go b/google/resource_sql_user.go index 045a85b1219..8289aa8644f 100644 --- a/google/resource_sql_user.go +++ b/google/resource_sql_user.go @@ -16,7 +16,7 @@ func resourceSqlUser() *schema.Resource { Update: resourceSqlUserUpdate, Delete: resourceSqlUserDelete, Importer: &schema.ResourceImporter{ - State: schema.ImportStatePassthrough, + State: resourceSqlUserImporter, }, SchemaVersion: 1, @@ -25,7 +25,7 @@ func resourceSqlUser() *schema.Resource { Schema: map[string]*schema.Schema{ "host": &schema.Schema{ Type: schema.TypeString, - Required: true, + Optional: true, ForceNew: true, }, @@ -107,16 +107,9 @@ func resourceSqlUserRead(d *schema.ResourceData, meta interface{}) error { return err } - instanceAndName := strings.SplitN(d.Id(), "/", 2) - if len(instanceAndName) != 2 { - return fmt.Errorf( - "Wrong number of arguments when specifying imported id. Expected: 2. Saw: %d. Expected Input: $INSTANCENAME/$SQLUSERNAME Input: %s", - len(instanceAndName), - d.Id()) - } - - instance := instanceAndName[0] - name := instanceAndName[1] + instance := d.Get("instance").(string) + name := d.Get("name").(string) + host := d.Get("host").(string) users, err := config.clientSqlAdmin.Users.List(project, instance).Do() @@ -126,7 +119,7 @@ func resourceSqlUserRead(d *schema.ResourceData, meta interface{}) error { var user *sqladmin.User for _, currentUser := range users.Items { - if currentUser.Name == name { + if currentUser.Name == name && currentUser.Host == host { user = currentUser break } @@ -221,3 +214,21 @@ func resourceSqlUserDelete(d *schema.ResourceData, meta interface{}) error { return nil } + +func resourceSqlUserImporter(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { + parts := strings.Split(d.Id(), "/") + + if len(parts) == 2 { + d.Set("instance", parts[0]) + d.Set("name", parts[1]) + } else if len(parts) == 3 { + d.Set("instance", parts[0]) + d.Set("host", parts[1]) + d.Set("name", parts[2]) + d.SetId(fmt.Sprintf("%s/%s", parts[0], parts[2])) + } else { + return nil, fmt.Errorf("Invalid specifier. Expecting {instance}/{name} for 2nd generation instance and {instance}/{host}/{name} for 1st generation instance") + } + + return []*schema.ResourceData{d}, nil +} diff --git a/google/resource_sql_user_test.go b/google/resource_sql_user_test.go index 718e5839267..aced574d4d7 100644 --- a/google/resource_sql_user_test.go +++ b/google/resource_sql_user_test.go @@ -9,51 +9,69 @@ import ( "github.com/hashicorp/terraform/terraform" ) -func TestAccGoogleSqlUser_basic(t *testing.T) { +func TestAccGoogleSqlUser_firstGen(t *testing.T) { t.Parallel() - user := acctest.RandString(10) - instance := acctest.RandString(10) - + instance := acctest.RandomWithPrefix("i") resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, Providers: testAccProviders, CheckDestroy: testAccGoogleSqlUserDestroy, Steps: []resource.TestStep{ resource.TestStep{ - Config: testGoogleSqlUser_basic(instance, user), + Config: testGoogleSqlUser_firstGen(instance, "password"), Check: resource.ComposeTestCheckFunc( - testAccCheckGoogleSqlUserExists("google_sql_user.user"), + testAccCheckGoogleSqlUserExists("google_sql_user.user1"), + testAccCheckGoogleSqlUserExists("google_sql_user.user2"), + ), + }, + resource.TestStep{ + // Update password + Config: testGoogleSqlUser_firstGen(instance, "new_password"), + Check: resource.ComposeTestCheckFunc( + testAccCheckGoogleSqlUserExists("google_sql_user.user1"), + testAccCheckGoogleSqlUserExists("google_sql_user.user2"), ), }, + resource.TestStep{ + ResourceName: "google_sql_user.user2", + ImportStateId: instance + "/gmail.com/admin", + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"password"}, + }, }, }) } -func TestAccGoogleSqlUser_update(t *testing.T) { +func TestAccGoogleSqlUser_secondGen(t *testing.T) { t.Parallel() - user := acctest.RandString(10) - instance := acctest.RandString(10) - + instance := acctest.RandomWithPrefix("i") resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, Providers: testAccProviders, CheckDestroy: testAccGoogleSqlUserDestroy, Steps: []resource.TestStep{ resource.TestStep{ - Config: testGoogleSqlUser_basic(instance, user), + Config: testGoogleSqlUser_secondGen(instance, "password"), Check: resource.ComposeTestCheckFunc( testAccCheckGoogleSqlUserExists("google_sql_user.user"), ), }, - resource.TestStep{ - Config: testGoogleSqlUser_basic2(instance, user), + // Update password + Config: testGoogleSqlUser_secondGen(instance, "new_password"), Check: resource.ComposeTestCheckFunc( testAccCheckGoogleSqlUserExists("google_sql_user.user"), ), }, + resource.TestStep{ + ResourceName: "google_sql_user.user", + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"password"}, + }, }, }) } @@ -107,39 +125,48 @@ func testAccGoogleSqlUserDestroy(s *terraform.State) error { return nil } -func testGoogleSqlUser_basic(instance, user string) string { +func testGoogleSqlUser_firstGen(instance, password string) string { return fmt.Sprintf(` resource "google_sql_database_instance" "instance" { - name = "i%s" + name = "%s" region = "us-central" settings { tier = "D0" } } - resource "google_sql_user" "user" { - name = "user%s" + resource "google_sql_user" "user1" { + name = "admin" instance = "${google_sql_database_instance.instance.name}" host = "google.com" + password = "%s" + } + + resource "google_sql_user" "user2" { + name = "admin" + instance = "${google_sql_database_instance.instance.name}" + host = "gmail.com" password = "hunter2" } - `, instance, user) + `, instance, password) } -func testGoogleSqlUser_basic2(instance, user string) string { +func testGoogleSqlUser_secondGen(instance, password string) string { return fmt.Sprintf(` resource "google_sql_database_instance" "instance" { - name = "i%s" - region = "us-central" + name = "%s" + region = "us-central1" + database_version = "POSTGRES_9_6" + settings { - tier = "D0" + tier = "db-f1-micro" } } resource "google_sql_user" "user" { - name = "user%s" + name = "admin" instance = "${google_sql_database_instance.instance.name}" - host = "google.com" + password = "%s" } - `, instance, user) + `, instance, password) } diff --git a/website/docs/r/sql_database_instance.html.markdown b/website/docs/r/sql_database_instance.html.markdown index 77dc4c52355..b9eb0e2c8dc 100644 --- a/website/docs/r/sql_database_instance.html.markdown +++ b/website/docs/r/sql_database_instance.html.markdown @@ -19,12 +19,15 @@ a restricted host and strong password. ## Example Usage -Example creating a SQL Database. +### SQL First Generation ```hcl resource "google_sql_database_instance" "master" { name = "master-instance" - database_version = "POSTGRES_9_6" + database_version = "MYSQL_5_6" + # First-generation instance regions are not the conventional + # Google Compute Engine regions. See argument reference below. + region = "us-central" settings { tier = "D0" @@ -32,6 +35,23 @@ resource "google_sql_database_instance" "master" { } ``` + +### SQL Second generation + +```hcl +resource "google_sql_database_instance" "master" { + name = "master-instance" + database_version = "POSTGRES_9_6" + region = "us-central1" + + settings { + # Second-generation instance tiers are based on the machine + # type. See argument reference below. + tier = "db-f1-micro" + } +} +``` + ## Argument Reference The following arguments are supported: diff --git a/website/docs/r/sql_user.html.markdown b/website/docs/r/sql_user.html.markdown index 8558515a238..65f41f238c6 100644 --- a/website/docs/r/sql_user.html.markdown +++ b/website/docs/r/sql_user.html.markdown @@ -39,9 +39,6 @@ resource "google_sql_user" "users" { The following arguments are supported: -* `host` - (Required) The host the user can connect from. Can be an IP address. - Changing this forces a new resource to be created. - * `instance` - (Required) The name of the Cloud SQL instance. Changing this forces a new resource to be created. @@ -52,6 +49,10 @@ The following arguments are supported: - - - +* `host` - (Optional) The host the user can connect from. This is only supported + for first generation SQL instances. Don't set this field for second generation + SQL instances. Can be an IP address. Changing this forces a new resource to be created. + * `project` - (Optional) The project in which the resource belongs. If it is not provided, the provider project is used. @@ -61,7 +62,13 @@ Only the arguments listed above are exposed as attributes. ## Import -SQL users can be imported using the `instance` and `name`, e.g. +SQL users for 1st generation databases can be imported using the `instance`, `host` and `name`, e.g. + +``` +$ terraform import google_sql_user.users master-instance/my-domain.com/me +``` + +SQL users for 2nd generation databases can be imported using the `instance` and `name`, e.g. ``` $ terraform import google_sql_user.users master-instance/me