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 #6016

Merged
merged 10 commits into from
Jun 6, 2022
61 changes: 61 additions & 0 deletions mmv1/third_party/terraform/resources/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": {
jseriff marked this conversation as resolved.
Show resolved Hide resolved
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
51 changes: 47 additions & 4 deletions mmv1/third_party/terraform/tests/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,45 @@ func TestAccSqlUser_mysql(t *testing.T) {
})
}

func TestAccSqlUser_mysqlDisabled(t *testing.T) {
// Multiple fine-grained resources
skipIfVcr(t)
jseriff marked this conversation as resolved.
Show resolved Hide resolved
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),
Check: resource.ComposeTestCheckFunc(
jseriff marked this conversation as resolved.
Show resolved Hide resolved
testAccCheckGoogleSqlUserExists(t, "google_sql_user.user1"),
testAccCheckGoogleSqlUserExists(t, "google_sql_user.user2"),
resource.TestCheckResourceAttr("google_sql_user.user1", "sql_server_user_details.disabled", "true"),
),
},
{
// Update password
Config: testGoogleSqlUser_mysql(instance, "password", false),
Check: resource.ComposeTestCheckFunc(
testAccCheckGoogleSqlUserExists(t, "google_sql_user.user1"),
testAccCheckGoogleSqlUserExists(t, "google_sql_user.user2"),
resource.TestCheckResourceAttr("google_sql_user.user1", "sql_server_user_details.disabled", "false"),
),
},
{
ResourceName: "google_sql_user.user2",
jseriff marked this conversation as resolved.
Show resolved Hide resolved
ImportStateId: fmt.Sprintf("%s/%s/gmail.com/admin", getTestProjectFromEnv(), instance),
jseriff marked this conversation as resolved.
Show resolved Hide resolved
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 +284,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 +301,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 +313,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