Skip to content

Commit

Permalink
Add Admin Organizations to data source organizations (#323)
Browse files Browse the repository at this point in the history
* Add Admin Organizations to data source organizations
** Add `admin` field to data source organizations
* Update docs
  • Loading branch information
omarismail committed Jun 14, 2021
1 parent ad1faca commit c2fa147
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 13 deletions.
96 changes: 84 additions & 12 deletions tfe/data_source_organizations.go
Expand Up @@ -23,27 +23,31 @@ func dataSourceTFEOrganizations() *schema.Resource {
Type: schema.TypeMap,
Computed: true,
},

"admin": {
Type: schema.TypeBool,
Default: false,
Optional: true,
},
},
}
}

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")
Expand All @@ -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)
}
7 changes: 6 additions & 1 deletion website/docs/d/organizations.html.markdown
Expand Up @@ -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

Expand Down

0 comments on commit c2fa147

Please sign in to comment.