From c2fa1479eaaccb0e414f9db5805a264db3350796 Mon Sep 17 00:00:00 2001 From: Omar Ismail Date: Mon, 14 Jun 2021 14:25:30 -0400 Subject: [PATCH] Add Admin Organizations to data source organizations (#323) * Add Admin Organizations to data source organizations ** Add `admin` field to data source organizations * Update docs --- tfe/data_source_organizations.go | 96 +++++++++++++++++++--- website/docs/d/organizations.html.markdown | 7 +- 2 files changed, 90 insertions(+), 13 deletions(-) diff --git a/tfe/data_source_organizations.go b/tfe/data_source_organizations.go index a879bf13d..5ad75d621 100644 --- a/tfe/data_source_organizations.go +++ b/tfe/data_source_organizations.go @@ -23,6 +23,12 @@ func dataSourceTFEOrganizations() *schema.Resource { Type: schema.TypeMap, Computed: true, }, + + "admin": { + Type: schema.TypeBool, + Default: false, + Optional: true, + }, }, } } @@ -30,20 +36,18 @@ func dataSourceTFEOrganizations() *schema.Resource { func dataSourceTFEOrganizationList(d *schema.ResourceData, meta interface{}) error { tfeClient := meta.(*tfe.Client) - log.Printf("[DEBUG] Listing all organizations") - orgs, err := tfeClient.Organizations.List(ctx, tfe.OrganizationListOptions{}) - if err != nil { - if err == tfe.ErrResourceNotFound { - return fmt.Errorf("Could not list organizations.") - } - return fmt.Errorf("Error retrieving organizations: %v.", err) + var names []string + var ids map[string]string + var err error + + if isAdmin(d) { + names, ids, err = adminOrgsPopulateFields(tfeClient, d) + } else { + names, ids, err = orgsPopulateFields(tfeClient) } - names := []string{} - ids := map[string]string{} - for _, org := range orgs.Items { - ids[org.Name] = org.ExternalID - names = append(names, org.Name) + if err != nil { + return err } log.Printf("[DEBUG] Setting Organizations Attributes") @@ -53,3 +57,71 @@ func dataSourceTFEOrganizationList(d *schema.ResourceData, meta interface{}) err return nil } + +func adminOrgsPopulateFields(client *tfe.Client, d *schema.ResourceData) ([]string, map[string]string, error) { + names := []string{} + ids := map[string]string{} + log.Printf("[DEBUG] Listing all organizations (admin)") + options := tfe.AdminOrganizationListOptions{ + ListOptions: tfe.ListOptions{ + PageSize: 100, + }, + } + for { + orgList, err := client.Admin.Organizations.List(ctx, options) + if err != nil { + return nil, nil, fmt.Errorf("Error retrieving Admin Organizations: %v", err) + } + + for _, org := range orgList.Items { + ids[org.Name] = org.ExternalID + names = append(names, org.Name) + } + + // Exit the loop when we've seen all pages. + if orgList.CurrentPage >= orgList.TotalPages { + break + } + + // Update the page number to get the next page. + options.PageNumber = orgList.NextPage + } + + return names, ids, nil +} + +func orgsPopulateFields(client *tfe.Client) ([]string, map[string]string, error) { + names := []string{} + ids := map[string]string{} + log.Printf("[DEBUG] Listing all organizations (non-admin)") + options := tfe.OrganizationListOptions{ + ListOptions: tfe.ListOptions{ + PageSize: 100, + }, + } + for { + orgList, err := client.Organizations.List(ctx, options) + if err != nil { + return nil, nil, fmt.Errorf("Error retrieving Organizations: %v", err) + } + + for _, org := range orgList.Items { + ids[org.Name] = org.ExternalID + names = append(names, org.Name) + } + + // Exit the loop when we've seen all pages. + if orgList.CurrentPage >= orgList.TotalPages { + break + } + + // Update the page number to get the next page. + options.PageNumber = orgList.NextPage + } + + return names, ids, nil +} + +func isAdmin(d *schema.ResourceData) bool { + return d.Get("admin").(bool) +} diff --git a/website/docs/d/organizations.html.markdown b/website/docs/d/organizations.html.markdown index c70f2e217..f8bc39d50 100644 --- a/website/docs/d/organizations.html.markdown +++ b/website/docs/d/organizations.html.markdown @@ -19,7 +19,12 @@ data "tfe_organizations" "foo" { ## Argument Reference -No arguments are required. This retrieves the names and IDs of all the organizations readable by the provided token. +The following argument(s) are supported: + +* `admin` - This field is for Terraform Enterprise only. It is a boolean field that determines + the list of organizations that should be retrieved. If it is true, then it will retrieve all + the organizations for the entire installation. If it is false, then it will retrieve the + organizations available as per permissions of the API Token. ## Attributes Reference