Skip to content

Commit 2ba76e4

Browse files
Handle CLOUD_IAM_GROUP username edgecases
1 parent 193b08b commit 2ba76e4

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

google/services/sql/resource_sql_user.go

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,25 @@ import (
1919
)
2020

2121
func diffSuppressIamUserName(_, old, new string, d *schema.ResourceData) bool {
22-
strippedName := strings.Split(new, "@")[0]
22+
// IAM users of type `CLOUD_IAM_USER` and `CLOUD_IAM_SERVICE_ACCOUNT` are created based on
23+
// email addresses, but do not include the domain in the generated user. So we need
24+
// to strip the domain in order to compare incoming values with old values.
25+
// Group users of type `CLOUD_IAM_GROUP`, however, retain their domains as a part of their username,
26+
// so we need to compare these directly
27+
truncated_iam_types := []string{"CLOUD_IAM_USER", "CLOUD_IAM_SERVICE_ACCOUNT"}
28+
untruncated_iam_types := []string{"CLOUD_IAM_GROUP"}
2329

2430
userType := d.Get("type").(string)
2531

26-
if old == strippedName && strings.Contains(userType, "IAM") {
27-
return true
32+
if slices.Contains(untruncated_iam_types, userType) {
33+
// We compare old and new directly for untruncated entries
34+
return old == new
35+
}
36+
37+
if slices.Contains(truncated_iam_types, userType) {
38+
// For truncated types, We strip the domain from the new value use it for comparison
39+
strippedName := strings.Split(new, "@")[0]
40+
return old == strippedName
2841
}
2942

3043
return false
@@ -351,6 +364,8 @@ func resourceSqlUserRead(d *schema.ResourceData, meta interface{}) error {
351364
}
352365

353366
for _, currentUser := range users.Items {
367+
// `CLOUD_IAM_GROUP` users are created with the domain name in the username, unlike
368+
// the other `CLOUD_IAM_*` user types.
354369
if !(strings.Contains(databaseInstance.DatabaseVersion, "POSTGRES") || currentUser.Type == "CLOUD_IAM_GROUP") {
355370
name = strings.Split(name, "@")[0]
356371
}

0 commit comments

Comments
 (0)