Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support multiple users with the same name for different host for 1st gen. #1066

Merged
merged 2 commits into from
Feb 9, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 0 additions & 34 deletions google/import_sql_user_test.go

This file was deleted.

37 changes: 24 additions & 13 deletions google/resource_sql_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func resourceSqlUser() *schema.Resource {
Update: resourceSqlUserUpdate,
Delete: resourceSqlUserDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
State: resourceSqlUserImporter,
},

SchemaVersion: 1,
Expand All @@ -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,
},

Expand Down Expand Up @@ -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()

Expand All @@ -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
}
Expand Down Expand Up @@ -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
}
77 changes: 52 additions & 25 deletions google/resource_sql_user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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/john",
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"},
},
},
})
}
Expand Down Expand Up @@ -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 = "john"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm sorry to be That Person but can we use a gender-neutral name here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure. Changed for admin.

instance = "${google_sql_database_instance.instance.name}"
host = "google.com"
password = "%s"
}

resource "google_sql_user" "user2" {
name = "john"
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 = "user"
instance = "${google_sql_database_instance.instance.name}"
host = "google.com"
password = "%s"
}
`, instance, user)
`, instance, password)
}
24 changes: 22 additions & 2 deletions website/docs/r/sql_database_instance.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,39 @@ 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 region. See argument reference below.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: regions

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

region = "us-central"

settings {
tier = "D0"
}
}
```


### 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 tier are based on the machine
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: tiers

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

# type. See argument reference below.
tier = "db-f1-micro"
}
}
```

## Argument Reference

The following arguments are supported:
Expand Down
15 changes: 11 additions & 4 deletions website/docs/r/sql_user.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -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 instance. Don't set this field for second generation
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: instances

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

SQL instance. Can be an IP address. Changing this forces a new resource to be created.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: instances

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done


* `project` - (Optional) The project in which the resource belongs. If it
is not provided, the provider project is used.

Expand All @@ -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
Expand Down