Created
March 15, 2023 20:50
-
-
Save rwhelan/3380ff9785c77d4107f99eaa026c7233 to your computer and use it in GitHub Desktop.
List Cloudflare List Items
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" | |
"errors" | |
"fmt" | |
"log" | |
"os" | |
"github.com/cloudflare/cloudflare-go" | |
) | |
func main() { | |
ctx := context.Background() | |
apiToken, err := getCloudflareToken() | |
if err != nil { | |
log.Fatal(err) | |
} | |
client, err := cloudflare.NewWithAPIToken(apiToken) | |
if err != nil { | |
log.Fatal(err) | |
} | |
accounts, _, err := client.Accounts(ctx, cloudflare.AccountsListParams{}) | |
if err != nil { | |
log.Fatal(err) | |
} | |
for _, acc := range accounts { | |
lists, err := client.ListLists(ctx, cloudflare.AccountIdentifier(acc.ID), cloudflare.ListListsParams{}) | |
if err != nil { | |
log.Fatal(err) | |
} | |
for _, list := range lists { | |
listItems, err := client.ListListItems(ctx, cloudflare.AccountIdentifier(acc.ID), cloudflare.ListListItemsParams{ID: list.ID}) | |
if err != nil { | |
log.Fatal(err) | |
} | |
for _, l := range listItems { | |
fmt.Printf("%s/%s/%s %s\n", acc.ID, list.ID, l.ID, l.Comment) | |
} | |
} | |
} | |
//fmt.Printf("%+v\n", client.ListListItems(context.TODO(), cloud)) | |
} | |
func getCloudflareToken() (string, error) { | |
possibleTokenEnvs := []string{ | |
"TF_VAR_cloudflare_api_token", | |
"CLOUDFLARE_API_TOKEN", | |
} | |
for _, envName := range possibleTokenEnvs { | |
value, ok := os.LookupEnv(envName) | |
if ok { | |
return value, nil | |
} | |
} | |
return "", errors.New("unable to locate cloudflare api token in available env vars") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment