Skip to content

Commit

Permalink
Merge pull request #1544 from okta/hot-fix
Browse files Browse the repository at this point in the history
add fix for terraform acc test
  • Loading branch information
duytiennguyen-okta authored May 2, 2023
2 parents e71e159 + 5945c1b commit 130f9f1
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 10 deletions.
2 changes: 1 addition & 1 deletion okta/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func (c *Config) loadAndValidate(ctx context.Context) error {
// NOTE: validate credentials during initial config with a call to
// /api/v1/users/me
if c.apiToken != "" {
if _, _, err := c.oktaClient.User.GetUser(ctx, "me"); err != nil {
if _, _, err := client.User.GetUser(ctx, "me"); err != nil {
return err
}
}
Expand Down
35 changes: 32 additions & 3 deletions okta/data_source_okta_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,18 @@ func dataSourceUser() *schema.Resource {
Optional: true,
Description: "Force delay of the user read by N seconds. Useful when eventual consistency of user information needs to be allowed for.",
},
"skip_groups": {
Type: schema.TypeBool,
Optional: true,
Default: false,
Description: "Do not populate user groups information (prevents additional API call)",
},
"skip_roles": {
Type: schema.TypeBool,
Optional: true,
Default: false,
Description: "Do not populate user roles information (prevents additional API call)",
},
}),
}
}
Expand Down Expand Up @@ -115,9 +127,26 @@ func dataSourceUserRead(ctx context.Context, d *schema.ResourceData, m interface
if err != nil {
return diag.Errorf("failed to set user's properties: %v", err)
}
err = setRoles(ctx, d, m)
if err != nil {
return diag.Errorf("failed to set user's roles: %v", err)
if val := d.Get("skip_roles"); val != nil {
if skip, ok := val.(bool); ok && !skip {
err = setAdminRoles(ctx, d, m)
if err != nil {
return diag.Errorf("failed to set user's admin roles: %v", err)
}
err = setRoles(ctx, d, m)
if err != nil {
return diag.Errorf("failed to set user's roles: %v", err)
}
}
}

if val := d.Get("skip_groups"); val != nil {
if skip, ok := val.(bool); ok && !skip {
err = setAllGroups(ctx, d, client)
if err != nil {
return diag.Errorf("failed to set user's groups: %v", err)
}
}
}

return nil
Expand Down
3 changes: 0 additions & 3 deletions okta/data_source_okta_user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,6 @@ resource "okta_user" "testAcc-replace_with_uuid" {
last_name = "Smith"
login = "testAcc-replace_with_uuid@example.com"
email = "testAcc-replace_with_uuid@example.com"
lifecycle {
ignore_changes = [admin_roles]
}
}
resource "okta_user_admin_roles" "test" {
user_id = okta_user.testAcc-replace_with_uuid.id
Expand Down
3 changes: 0 additions & 3 deletions okta/data_source_okta_users_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,9 +218,6 @@ resource "okta_user" "testAcc-replace_with_uuid" {
last_name = "Smith"
login = "testAcc-replace_with_uuid@example.com"
email = "testAcc-replace_with_uuid@example.com"
lifecycle {
ignore_changes = [admin_roles]
}
}
resource "okta_user_admin_roles" "test" {
user_id = okta_user.testAcc-replace_with_uuid.id
Expand Down
12 changes: 12 additions & 0 deletions okta/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,18 @@ func listUserOnlyRoles(ctx context.Context, c *sdk.Client, userID string) (userO
return
}

// set all groups currently attached to the user
func setAllGroups(ctx context.Context, d *schema.ResourceData, c *sdk.Client) error {
groupIDs, err := getGroupsForUser(ctx, d.Id(), c)
if err != nil {
return err
}
gids := convertStringSliceToInterfaceSlice(groupIDs)
return setNonPrimitives(d, map[string]interface{}{
"group_memberships": schema.NewSet(schema.HashString, gids),
})
}

func getAdminRoles(ctx context.Context, id string, c *sdk.Client) ([]interface{}, *sdk.Response, error) {
roleTypes := make([]interface{}, 0)
roles, resp, err := listUserOnlyRoles(ctx, c, id)
Expand Down

0 comments on commit 130f9f1

Please sign in to comment.