Skip to content

Commit e7df6a1

Browse files
fix: prevent config drift for OIDC users in Read function
Update the Read function to not populate roles from server response for OIDC users. This prevents Terraform from detecting config drift when OIDC users have roles assigned by the OIDC provider but an empty roles list in the Terraform config. Addresses review comment about config drift in PR #247. Co-authored-by: angrycub <464492+angrycub@users.noreply.github.com>
1 parent d8d4d6f commit e7df6a1

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

internal/provider/user_resource.go

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -278,11 +278,21 @@ func (r *UserResource) Read(ctx context.Context, req resource.ReadRequest, resp
278278
data.Email = types.StringValue(user.Email)
279279
data.Name = types.StringValue(user.Name)
280280
data.Username = types.StringValue(user.Username)
281-
roles := make([]attr.Value, 0, len(user.Roles))
282-
for _, role := range user.Roles {
283-
roles = append(roles, types.StringValue(role.Name))
281+
282+
// For OIDC users, don't populate roles from server to avoid config drift
283+
// OIDC users get their roles from the OIDC provider's role mapping
284+
if user.LoginType == codersdk.LoginTypeOIDC {
285+
// Keep roles empty for OIDC users to match the expected Terraform config
286+
data.Roles = types.SetValueMust(types.StringType, []attr.Value{})
287+
} else {
288+
// For non-OIDC users, populate roles from server response
289+
roles := make([]attr.Value, 0, len(user.Roles))
290+
for _, role := range user.Roles {
291+
roles = append(roles, types.StringValue(role.Name))
292+
}
293+
data.Roles = types.SetValueMust(types.StringType, roles)
284294
}
285-
data.Roles = types.SetValueMust(types.StringType, roles)
295+
286296
data.LoginType = types.StringValue(string(user.LoginType))
287297
data.Suspended = types.BoolValue(user.Status == codersdk.UserStatusSuspended)
288298

@@ -446,4 +456,4 @@ func (r *UserResource) ImportState(ctx context.Context, req resource.ImportState
446456
return
447457
}
448458
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("id"), user.ID.String())...)
449-
}
459+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy