Last active
April 11, 2022 08:16
-
-
Save Olgoetz/c273f1ddba5e015b45e2438ed80e066d to your computer and use it in GitHub Desktop.
Example how to consume pagination with the go-tfe-cleint
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"context" | |
"fmt" | |
"log" | |
tfe "github.com/hashicorp/go-tfe" | |
) | |
func main() { | |
// Confige the tfe client | |
config := &tfe.Config{ | |
Token: "your-token", | |
} | |
// Get a new client | |
client, err := tfe.NewClient(config) | |
if err != nil { | |
log.Fatal(err) | |
} | |
// Define a slice []*tfe.Organization for adding retrieved organizations | |
var all_orgas []*tfe.Organization | |
// Start at page 1 of the pagination | |
page := 1 | |
// Loop through the pagination result and exit if the property NextPage == 0 | |
for { | |
options := &tfe.OrganizationListOptions{ | |
ListOptions: tfe.ListOptions{ | |
PageSize: 15, | |
PageNumber: page, | |
}, | |
} | |
// Get organizations | |
orgs, err := client.Organizations.List(context.Background(), options) | |
if err != nil { | |
log.Fatal(err) | |
} | |
// Add organizations to all_orgas slice | |
all_orgas = append(all_orgas, orgs.Items...) | |
// Exit the loop when no new page exists | |
if orgs.NextPage == 0 { | |
break | |
} | |
// Start the api call again with the next page | |
page = orgs.NextPage | |
} | |
// Print organization names | |
for _, val := range all_orgas { | |
fmt.Println(val.Name) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment