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

Allow setting SqlServerUserDetails from google_sql_user resource #11834

Merged
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 3 additions & 0 deletions .changelog/6016.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
sql: added `sql_server_user_details` field to `google_sql_user` resource
```
61 changes: 61 additions & 0 deletions google/resource_sql_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,27 @@ func resourceSqlUser() *schema.Resource {
The default is the database's built-in user type. Flags include "BUILT_IN", "CLOUD_IAM_USER", or "CLOUD_IAM_SERVICE_ACCOUNT".`,
ValidateFunc: validation.StringInSlice([]string{"BUILT_IN", "CLOUD_IAM_USER", "CLOUD_IAM_SERVICE_ACCOUNT", ""}, false),
},
"sql_server_user_details": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"disabled": {
Type: schema.TypeBool,
Optional: true,
Default: false,
Description: `If the user has been disabled.`,
},
"server_roles": {
Type: schema.TypeList,
Optional: true,
Description: `The server roles for this user in the database.`,
Elem: &schema.Schema{Type: schema.TypeString},
},
},
},
},

"project": {
Type: schema.TypeString,
Expand All @@ -105,6 +126,22 @@ func resourceSqlUser() *schema.Resource {
}
}

func expandSqlServerUserDetails(cfg interface{}) (*sqladmin.SqlServerUserDetails, error) {
raw := cfg.([]interface{})[0].(map[string]interface{})

ssud := &sqladmin.SqlServerUserDetails{}

if v, ok := raw["disabled"]; ok {
ssud.Disabled = v.(bool)
}
if v, ok := raw["server_roles"]; ok {
ssud.ServerRoles = expandStringArray(v)
}

return ssud, nil

}

func resourceSqlUserCreate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
userAgent, err := generateUserAgentString(d, config.userAgent)
Expand All @@ -131,6 +168,14 @@ func resourceSqlUserCreate(d *schema.ResourceData, meta interface{}) error {
Type: typ,
}

if v, ok := d.GetOk("sql_server_user_details"); ok {
ssud, err := expandSqlServerUserDetails(v)
if err != nil {
return err
}
user.SqlserverUserDetails = ssud
}

mutexKV.Lock(instanceMutexKey(project, instance))
defer mutexKV.Unlock(instanceMutexKey(project, instance))
var op *sqladmin.Operation
Expand Down Expand Up @@ -229,6 +274,14 @@ func resourceSqlUserRead(d *schema.ResourceData, meta interface{}) error {
if err := d.Set("project", project); err != nil {
return fmt.Errorf("Error setting project: %s", err)
}
if user.SqlserverUserDetails != nil {
if err := d.Set("disabled", user.SqlserverUserDetails.Disabled); err != nil {
return fmt.Errorf("Error setting disabled: %s", err)
}
if err := d.Set("server_roles", user.SqlserverUserDetails.ServerRoles); err != nil {
return fmt.Errorf("Error setting server_roles: %s", err)
}
}
d.SetId(fmt.Sprintf("%s/%s/%s", user.Name, user.Host, user.Instance))
return nil
}
Expand Down Expand Up @@ -257,6 +310,14 @@ func resourceSqlUserUpdate(d *schema.ResourceData, meta interface{}) error {
Password: password,
}

if v, ok := d.GetOk("sql_server_user_details"); ok {
ssud, err := expandSqlServerUserDetails(v)
if err != nil {
return err
}
user.SqlserverUserDetails = ssud
}

mutexKV.Lock(instanceMutexKey(project, instance))
defer mutexKV.Unlock(instanceMutexKey(project, instance))
var op *sqladmin.Operation
Expand Down
46 changes: 42 additions & 4 deletions google/resource_sql_user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ func TestAccSqlUser_mysql(t *testing.T) {
CheckDestroy: testAccSqlUserDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testGoogleSqlUser_mysql(instance, "password"),
Config: testGoogleSqlUser_mysql(instance, "password", false),
Check: resource.ComposeTestCheckFunc(
testAccCheckGoogleSqlUserExists(t, "google_sql_user.user1"),
testAccCheckGoogleSqlUserExists(t, "google_sql_user.user2"),
),
},
{
// Update password
Config: testGoogleSqlUser_mysql(instance, "new_password"),
Config: testGoogleSqlUser_mysql(instance, "new_password", false),
Check: resource.ComposeTestCheckFunc(
testAccCheckGoogleSqlUserExists(t, "google_sql_user.user1"),
testAccCheckGoogleSqlUserExists(t, "google_sql_user.user2"),
Expand All @@ -45,6 +45,40 @@ func TestAccSqlUser_mysql(t *testing.T) {
})
}

func TestAccSqlUser_mysqlDisabled(t *testing.T) {
t.Parallel()

instance := fmt.Sprintf("i-%d", randInt(t))
vcrTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccSqlUserDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testGoogleSqlUser_mysql(instance, "password", true),
},
{
ResourceName: "google_sql_user.user1",
ImportStateId: fmt.Sprintf("%s/%s/gmail.com/admin", getTestProjectFromEnv(), instance),
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"password"},
},
{
// Update password
Config: testGoogleSqlUser_mysql(instance, "password", false),
},
{
ResourceName: "google_sql_user.user1",
ImportStateId: fmt.Sprintf("%s/%s/gmail.com/admin", getTestProjectFromEnv(), instance),
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"password"},
},
},
})
}

func TestAccSqlUser_iamUser(t *testing.T) {
// Multiple fine-grained resources
skipIfVcr(t)
Expand Down Expand Up @@ -245,7 +279,7 @@ func testAccSqlUserDestroyProducer(t *testing.T) func(s *terraform.State) error
}
}

func testGoogleSqlUser_mysql(instance, password string) string {
func testGoogleSqlUser_mysql(instance, password string, disabled bool) string {
return fmt.Sprintf(`
resource "google_sql_database_instance" "instance" {
name = "%s"
Expand All @@ -262,6 +296,10 @@ resource "google_sql_user" "user1" {
instance = google_sql_database_instance.instance.name
host = "google.com"
password = "%s"
sql_server_user_details {
disabled = "%t"
server_roles = [ "admin" ]
}
}

resource "google_sql_user" "user2" {
Expand All @@ -270,7 +308,7 @@ resource "google_sql_user" "user2" {
host = "gmail.com"
password = "hunter2"
}
`, instance, password)
`, instance, password, disabled)
}

func testGoogleSqlUser_postgres(instance, password string) string {
Expand Down