Skip to content

Commit 9feb45b

Browse files
add support for IAM Group authentication to google_sql_user (#9685) (#6787)
* add support for IAM Group authentication to google_sql_user * Update resource_sql_user_test.go Minor formatting changes * Update documentation and minor formatting changes [upstream:4d160cb257e40ad9228a94766acfa3b77becb14e] Signed-off-by: Modular Magician <[email protected]>
1 parent f7a618e commit 9feb45b

File tree

5 files changed

+87
-10
lines changed

5 files changed

+87
-10
lines changed

.changelog/9685.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:enhancement
2+
sql: added support for IAM GROUP authentication in the `type` field of `google_sql_user`
3+
```

.teamcity/components/generated/packages.kt

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,6 @@ var packages = mapOf(
1212
"displayName" to "Environment Variables",
1313
"path" to "./google-beta/envvar"
1414
),
15-
"fwmodels" to mapOf(
16-
"name" to "fwmodels",
17-
"displayName" to "Framework Models",
18-
"path" to "./google-beta/fwmodels"
19-
),
2015
"fwprovider" to mapOf(
2116
"name" to "fwprovider",
2217
"displayName" to "Framework Provider",

google-beta/services/sql/resource_sql_user.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,8 @@ func ResourceSqlUser() *schema.Resource {
104104
ForceNew: true,
105105
DiffSuppressFunc: tpgresource.EmptyOrDefaultStringSuppress("BUILT_IN"),
106106
Description: `The user type. It determines the method to authenticate the user during login.
107-
The default is the database's built-in user type. Flags include "BUILT_IN", "CLOUD_IAM_USER", or "CLOUD_IAM_SERVICE_ACCOUNT".`,
108-
ValidateFunc: validation.StringInSlice([]string{"BUILT_IN", "CLOUD_IAM_USER", "CLOUD_IAM_SERVICE_ACCOUNT", ""}, false),
107+
The default is the database's built-in user type. Flags include "BUILT_IN", "CLOUD_IAM_USER", "CLOUD_IAM_GROUP" or "CLOUD_IAM_SERVICE_ACCOUNT".`,
108+
ValidateFunc: validation.StringInSlice([]string{"BUILT_IN", "CLOUD_IAM_USER", "CLOUD_IAM_GROUP", "CLOUD_IAM_SERVICE_ACCOUNT", ""}, false),
109109
},
110110
"sql_server_user_details": {
111111
Type: schema.TypeList,
@@ -351,10 +351,9 @@ func resourceSqlUserRead(d *schema.ResourceData, meta interface{}) error {
351351
}
352352

353353
for _, currentUser := range users.Items {
354-
if !strings.Contains(databaseInstance.DatabaseVersion, "POSTGRES") {
354+
if !(strings.Contains(databaseInstance.DatabaseVersion, "POSTGRES") || currentUser.Type == "CLOUD_IAM_GROUP") {
355355
name = strings.Split(name, "@")[0]
356356
}
357-
358357
if currentUser.Name == name {
359358
// Host can only be empty for postgres instances,
360359
// so don't compare the host if the API host is empty.

google-beta/services/sql/resource_sql_user_test.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,34 @@ func TestAccSqlUser_iamUser(t *testing.T) {
7676
})
7777
}
7878

79+
func TestAccSqlUser_iamGroupUser(t *testing.T) {
80+
// Multiple fine-grained resources
81+
acctest.SkipIfVcr(t)
82+
t.Parallel()
83+
84+
instance := fmt.Sprintf("tf-test-%d", acctest.RandInt(t))
85+
acctest.VcrTest(t, resource.TestCase{
86+
PreCheck: func() { acctest.AccTestPreCheck(t) },
87+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
88+
CheckDestroy: testAccSqlUserDestroyProducer(t),
89+
Steps: []resource.TestStep{
90+
{
91+
Config: testGoogleSqlUser_iamGroupUser(instance),
92+
Check: resource.ComposeTestCheckFunc(
93+
testAccCheckGoogleSqlUserExists(t, "google_sql_user.user"),
94+
),
95+
},
96+
{
97+
ResourceName: "google_sql_user.user",
98+
ImportStateId: fmt.Sprintf("%s/%s/[email protected]", envvar.GetTestProjectFromEnv(), instance),
99+
ImportState: true,
100+
ImportStateVerify: true,
101+
ImportStateVerifyIgnore: []string{"password"},
102+
},
103+
},
104+
})
105+
}
106+
79107
func TestAccSqlUser_postgres(t *testing.T) {
80108
t.Parallel()
81109

@@ -507,3 +535,27 @@ resource "google_project_iam_member" "sa_user" {
507535
}
508536
`, instance, instance, instance, instance)
509537
}
538+
539+
func testGoogleSqlUser_iamGroupUser(instance string) string {
540+
return fmt.Sprintf(`
541+
resource "google_sql_database_instance" "instance" {
542+
name = "%s"
543+
region = "us-central1"
544+
database_version = "MYSQL_8_0"
545+
deletion_protection = false
546+
settings {
547+
tier = "db-f1-micro"
548+
database_flags {
549+
name = "cloudsql_iam_authentication"
550+
value = "on"
551+
}
552+
}
553+
}
554+
555+
resource "google_sql_user" "user" {
556+
557+
instance = google_sql_database_instance.instance.name
558+
type = "CLOUD_IAM_GROUP"
559+
}
560+
`, instance)
561+
}

website/docs/r/sql_user.html.markdown

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,34 @@ resource "google_sql_user" "iam_service_account_user" {
7474
}
7575
```
7676

77+
Example using [Cloud SQL IAM Group authentication](https://cloud.google.com/sql/docs/mysql/iam-authentication#iam-group-auth).
78+
79+
```hcl
80+
resource "random_id" "db_name_suffix" {
81+
byte_length = 4
82+
}
83+
84+
resource "google_sql_database_instance" "main" {
85+
name = "main-instance-${random_id.db_name_suffix.hex}"
86+
database_version = "MYSQL_8_0"
87+
88+
settings {
89+
tier = "db-f1-micro"
90+
91+
database_flags {
92+
name = "cloudsql.iam_authentication"
93+
value = "on"
94+
}
95+
}
96+
}
97+
98+
resource "google_sql_user" "iam_group_user" {
99+
100+
instance = google_sql_database_instance.main.name
101+
type = "CLOUD_IAM_GROUP"
102+
}
103+
```
104+
77105
## Argument Reference
78106

79107
The following arguments are supported:
@@ -91,7 +119,7 @@ The following arguments are supported:
91119

92120
* `type` - (Optional) The user type. It determines the method to authenticate the
93121
user during login. The default is the database's built-in user type. Flags
94-
include "BUILT_IN", "CLOUD_IAM_USER", or "CLOUD_IAM_SERVICE_ACCOUNT".
122+
include "BUILT_IN", "CLOUD_IAM_USER", "CLOUD_IAM_GROUP" or "CLOUD_IAM_SERVICE_ACCOUNT".
95123

96124
* `deletion_policy` - (Optional) The deletion policy for the user.
97125
Setting `ABANDON` allows the resource to be abandoned rather than deleted. This is useful

0 commit comments

Comments
 (0)