Skip to content

Conversation

@tvdaptible
Copy link
Contributor

@tvdaptible tvdaptible commented Dec 23, 2025

We want to display the organizations a user has access to in the cli, along with their roles for each org. This is roughly the structure of what we have without this change:

user = Aptible::Auth::Token.current_token(token: token).user
user.roles.each do |role| # http request to list all roles for the user
  org = role.organization # http request for this organization
  puts "#{org.name}: #{role.name} (#{role.id})"
end

Note that this is an N+1 operation, because each role.organization call in the loop does another http call to retrieve the organization. That happens even if the same organization has already been seen.

We add user.roles_with_organizations, which enables the same looping operations and only make 2 http requests. First it gets all the organizations and keeps them in a map, then it does the call to roles and preloads each role with the organization from the preloaded map. The above code can be converted to:

user = Aptible::Auth::Token.current_token(token: token).user
user.roles_with_organizations.each do |role| # 2 http requests
  org = role.organization                    # does _not_ make an http request 🎉
  puts "#{org.name}: #{role.name} (#{role.id})"
end

This will help with: aptible/aptible-cli#403

This makes it simple to get the current user from the current token:

```ruby
whoami = Aptible::Auth::Token.current_token.user
```

This will help with: aptible/aptible-cli#403
We want to display the organizations a user has access to in the cli, along with their roles for each org. This is roughly the structure of what we have without this change:

```ruby
user = Aptible::Auth::Token.current_token(token: token).user
user.roles.each do |role| # http request to list all roles for the user
  org = role.organization # http request for this organization
  puts "#{org.name}: #{role.name} (#{role.id})"
end
```

Note that this is an N+1 operation, because each `role.organization` call in the loop does another http call to retrieve the organization. That happens even if the same organization has already been seen.

We add `user.roles_with_organizations`, which enables the same looping operations and only make 2 http requests. First it gets all the organizations and keeps them in a map, then it does the call to roles and preloads each role with the organization from the preloaded map. The above code can be converted to:

```ruby
user = Aptible::Auth::Token.current_token(token: token).user
user.roles_with_organizations.each do |role| # 2 http requests
  org = role.organization                    # does _not_ make an http request 🎉
  puts "#{org.name}: #{role.name} (#{role.id})"
end
```
@tvdaptible tvdaptible changed the base branch from whoami to master December 23, 2025 08:21
madhuravius
madhuravius previously approved these changes Dec 23, 2025
@tvdaptible tvdaptible changed the base branch from whoami to master December 23, 2025 17:25
@tvdaptible tvdaptible dismissed madhuravius’s stale review December 23, 2025 17:25

The base branch was changed.

@tvdaptible tvdaptible merged commit 090cce9 into master Dec 24, 2025
12 checks passed
@tvdaptible tvdaptible deleted the all-roles-for-a-user branch December 24, 2025 17:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants