Skip to content

Commit

Permalink
Merge pull request #1706 from AkemiDavisson/akemi/addPaginationTeamsList
Browse files Browse the repository at this point in the history
Adds recently supported per_page/pagination teams_list
  • Loading branch information
jacobbednarz committed Oct 31, 2022
2 parents 1db7aa6 + e98fa40 commit 3709167
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 2 deletions.
3 changes: 3 additions & 0 deletions .changelog/1706.txt
@@ -0,0 +1,3 @@
```release-note:enhancement
resource/cloudflare_teams_list: handle pagination for larger Team List accounts
```
4 changes: 4 additions & 0 deletions internal/provider/resource_cloudflare_teams_list.go
Expand Up @@ -60,6 +60,7 @@ func resourceCloudflareTeamsListRead(ctx context.Context, d *schema.ResourceData

identifier := cloudflare.AccountIdentifier(accountID)
list, err := client.GetTeamsList(ctx, identifier, d.Id())

if err != nil {
var notFoundError *cloudflare.NotFoundError
if errors.As(err, &notFoundError) {
Expand All @@ -80,6 +81,7 @@ func resourceCloudflareTeamsListRead(ctx context.Context, d *schema.ResourceData
if err != nil {
return diag.FromErr(fmt.Errorf("error finding Teams List %q: %w", d.Id(), err))
}

d.Set("items", convertListItemsToSchema(listItems))

return nil
Expand Down Expand Up @@ -114,7 +116,9 @@ func resourceCloudflareTeamsListUpdate(ctx context.Context, d *schema.ResourceDa
newItems := newItemsIface.(*schema.Set).List()
patchTeamsList := cloudflare.PatchTeamsListParams{ID: d.Id()}
setListItemDiff(&patchTeamsList, oldItems, newItems)

l, err := client.PatchTeamsList(ctx, identifier, patchTeamsList)

if err != nil {
return diag.FromErr(fmt.Errorf("error updating Teams List for account %q: %w", accountID, err))
}
Expand Down
56 changes: 54 additions & 2 deletions internal/provider/resource_cloudflare_teams_list_test.go
Expand Up @@ -4,14 +4,16 @@ import (
"context"
"fmt"
"os"
"strconv"
"strings"
"testing"

"github.com/cloudflare/cloudflare-go"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
)

func TestAccCloudflareTeamsListBasic(t *testing.T) {
func TestAccCloudflareTeamsList_Basic(t *testing.T) {
// Temporarily unset CLOUDFLARE_API_TOKEN if it is set as the Access
// service does not yet support the API tokens and it results in
// misleading state error messages.
Expand Down Expand Up @@ -45,7 +47,40 @@ func TestAccCloudflareTeamsListBasic(t *testing.T) {
})
}

func TestAccCloudflareTeamsListReordered(t *testing.T) {
func TestAccCloudflareTeamsList_LottaListItems(t *testing.T) {
// Temporarily unset CLOUDFLARE_API_TOKEN if it is set as the Access
// service does not yet support the API tokens and it results in
// misleading state error messages.
if os.Getenv("CLOUDFLARE_API_TOKEN") != "" {
t.Setenv("CLOUDFLARE_API_TOKEN", "")
}

rnd := generateRandomResourceName()
name := fmt.Sprintf("cloudflare_teams_list.%s", rnd)

resource.Test(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
testAccessAccPreCheck(t)
},
ProviderFactories: providerFactories,
CheckDestroy: testAccCheckCloudflareTeamsListDestroy,
Steps: []resource.TestStep{
{
Config: testAccCloudflareTeamsListConfigBigItemCount(rnd, accountID),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(name, "account_id", accountID),
resource.TestCheckResourceAttr(name, "name", rnd),
resource.TestCheckResourceAttr(name, "type", "SERIAL"),
resource.TestCheckResourceAttr(name, "description", "My description"),
resource.TestCheckResourceAttr(name, "items.#", "1000"),
),
},
},
})
}

func TestAccCloudflareTeamsList_Reordered(t *testing.T) {
// Temporarily unset CLOUDFLARE_API_TOKEN if it is set as the Access
// service does not yet support the API tokens and it results in
// misleading state error messages.
Expand Down Expand Up @@ -85,6 +120,23 @@ resource "cloudflare_teams_list" "%[1]s" {
`, rnd, accountID)
}

func testAccCloudflareTeamsListConfigBigItemCount(rnd, accountID string) string {
items := []string{}
for i := 0; i < 1000; i++ {
items = append(items, `"example-`+strconv.Itoa(i)+`"`)
}

return fmt.Sprintf(`
resource "cloudflare_teams_list" "%[1]s" {
account_id = "%[2]s"
name = "%[1]s"
description = "My description"
type = "SERIAL"
items = [%[3]s]
}
`, rnd, accountID, strings.Join(items, ","))
}

func testAccCloudflareTeamsListConfigReorderedItems(rnd, accountID string) string {
return fmt.Sprintf(`
resource "cloudflare_teams_list" "%[1]s" {
Expand Down

0 comments on commit 3709167

Please sign in to comment.