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

Fixed group membership populating #284

Merged
merged 2 commits into from
Jan 14, 2021
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
6 changes: 4 additions & 2 deletions okta/data_source_okta_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,7 @@ func dataSourceUserRead(ctx context.Context, d *schema.ResourceData, m interface
}
user = users[0]
}

d.SetId(user.Id)

rawMap := flattenUser(user)
err = setNonPrimitives(d, rawMap)
if err != nil {
Expand All @@ -84,6 +82,10 @@ func dataSourceUserRead(ctx context.Context, d *schema.ResourceData, m interface
if err != nil {
return diag.Errorf("failed to set user's admin roles: %v", err)
}
err = setAllGroups(ctx, d, client)
if err != nil {
return diag.Errorf("failed to set user's groups: %v", err)
}
return nil
}

Expand Down
24 changes: 18 additions & 6 deletions okta/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,23 +286,35 @@ func setAdminRoles(ctx context.Context, d *schema.ResourceData, m interface{}) e
})
}

func setGroups(ctx context.Context, d *schema.ResourceData, c *okta.Client) error {
// set all groups currently attached to user in state
// set all groups currently attached to the user
func setAllGroups(ctx context.Context, d *schema.ResourceData, c *okta.Client) error {
groups, _, err := c.User.ListUserGroups(ctx, d.Id())
if err != nil {
return fmt.Errorf("failed to list user groups: %v", err)
}
groupIDs := make([]interface{}, len(groups))
for i := range groups {
groupIDs[i] = groups[i].Id
}
return setNonPrimitives(d, map[string]interface{}{
"group_memberships": schema.NewSet(schema.HashString, groupIDs),
})
}

// set groups attached to the user that can be changed
func setGroups(ctx context.Context, d *schema.ResourceData, c *okta.Client) error {
groups, _, err := c.User.ListUserGroups(ctx, d.Id())
if err != nil {
return fmt.Errorf("failed to list user groups: %v", err)
}
groupIDs := make([]interface{}, 0)

// ignore saving the Everyone group into state so we don't end up with perpetual diffs
// ignore saving build-in or app groups into state so we don't end up with perpetual diffs,
// because it's impossible to remove user from build-in or app group via API
for _, group := range groups {
if group.Type != "BUILT_IN" && group.Type != "APP_GROUP" {
groupIDs = append(groupIDs, group.Id)
}
}

// set the custom_profile_attributes values
return setNonPrimitives(d, map[string]interface{}{
"group_memberships": schema.NewSet(schema.HashString, groupIDs),
})
Expand Down