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

Add Admin Organizations to data source organizations #323

Merged
merged 10 commits into from
Jun 14, 2021

Conversation

omarismail
Copy link
Contributor

@omarismail omarismail commented Jun 3, 2021

Description

In #320, two a data source for listing all organizations was added. That data source returns the organizations that the user (token) has access to.

This PR augments that data source. We add a boolean field admin that determines if we should retrieve the organizations from using the Admin Organizations endpoint or the Organizationsendpoint will be used.

Added docs.

Testing plan

This can only be properly tested with access to Site Admin on your Terraform Enterprise instance.

The testing plan is as follows:

Create a terraform config using the tfe provider, and add this bit:

data "tfe_organizations" "orgs" {
  admin = true 
}

output "many_orgs" {
  value = data.tfe_organizations.orgs
}
  1. Using your API token (assuming you have Site Admin access), add admin = true to the data source in the config, run terraform apply and you will see the output of all the organizations.
  2. Create a new organization, and create an Organization token from this new organization.
  3. Remove the admin=true from the data source, and run terraform apply using this organization token.
  4. Confirm that you only see one organization, and not all of the organizations as in step 1.

External links

Closes #281
Closes #272

Followup

We will add the ability to query specific organizations as a follow up to this task.

@omarismail omarismail marked this pull request as ready for review June 3, 2021 12:42
@omarismail omarismail requested review from a team June 3, 2021 12:42
Copy link
Member

@chrisarcand chrisarcand left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lookin' nice! Two main points:

❌ This was a problem in the first version of this (Before admin stuff), so I'm glad we found this this time around! The result doesn't follow pagination. That is, even if you have more than 20 organizations available to you, the default page size of 20 is all that shows up. You'll need to add pagination concerns here; see how workspaces does it as an example.

❌ Needs documentation


Aside from that, the 'uses admin unless you aren't an admin' thing works but we may want to consider the alternative - an optional argument could be utilized, where you explicitly opt-in to using the admin API for installation-wide names/IDs. At it's simplest: installation_wide = true or admin = true (TFE only). Besides being clear from a usage perspective, that would avoid the needless request out to the Admin API that will happen every time for non-admin users; thoughts?

@omarismail
Copy link
Contributor Author

omarismail commented Jun 8, 2021

@chrisarcand

Aside from that, the 'uses admin unless you aren't an admin' thing works but we may want to consider the alternative - an optional argument could be utilized, where you explicitly opt-in to using the admin API for installation-wide names/IDs. At it's simplest: installation_wide = true or admin = true (TFE only). Besides being clear from a usage perspective, that would avoid the needless request out to the Admin API that will happen every time for non-admin users; thoughts?

I think this is a good idea. I prefer admin = true. I also failed to notice that, while we don't have any query paramters for OrganizationListOptions, we do have them for AdminOrganizationListOptions. I'll add them here while we are at it so as to complete this data source and not have to come back to it later.

Having one data source for both regular Organizations and Admin Organizations, having the prefix admin_ would clarify.

data "tfe_organizations" "orgs" {
  admin = true
  admin_query = <query>
}

One could also say that "if admin=true is set, and we see a query field, then we can assume that the user is intending the do add the query for admin orgs." But I think someone might just remove the admin=true field and keep the query and think it'll work. I think the verbosity here is clearer.

Also, we can be a bit intelligent — while not making things confusing — by saying that "if admin_query is present but admin=true is not, then we can assume they are querying for admin orgs".

@omarismail omarismail marked this pull request as draft June 9, 2021 13:12
@omarismail omarismail marked this pull request as ready for review June 10, 2021 16:23
Copy link
Member

@chrisarcand chrisarcand left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, moving along! Biggie problem with the admin side of things sadly; with any larger amount of organizations this datasource takes a reeeeaaallly long time. That's probably already the case with d/tfe_workspace if you query for all workspaces in an org, but without some filtering provided here we should at least squeeze as much as we can out of minimizing the request load.

tfe/data_source_organizations.go Show resolved Hide resolved
website/docs/d/organizations.html.markdown Outdated Show resolved Hide resolved
log.Printf("[DEBUG] Listing all organizations (admin)")
options := tfe.AdminOrganizationListOptions{}
for {
orgList, err := client.Admin.Organizations.List(ctx, options)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is problematic at any larger scale, sadly. I hooked this up to an installation with several thousand organizations and it took so long I canceled it after 15 minutes.

Short of a specialized endpoint for making this really performant, I think the most practical thing we can do here is to use sparse fieldsets to minimize serialization and max out the page size, which is currently twenty 😱. That is, you'll need to make this URL what is used here (and after that, might as well add it to the other endpoint, too):

/api/v2/admin/organizations?fields[organization]=name&page[size]=100

This also tells us that query filtering is undoubtedly going to be the next step here to limit results (similar to d/tfe_workspace.names, but smarter), but we can save that for another day.

Copy link
Member

@chrisarcand chrisarcand left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Works great! This datasource worked very reasonably with ~700 organizations on the TFE instance I tried it against. 🚀

@omarismail omarismail merged commit c2fa147 into main Jun 14, 2021
@omarismail omarismail deleted the omarismail/admin-orgs branch June 14, 2021 18:25
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.

tfe_organization data source missing data source to list all organizations in tfe
2 participants