Skip to content

Commit

Permalink
Merge pull request #3303 from cloudflare/apply-retry-to-list-items
Browse files Browse the repository at this point in the history
resource/cloudflare_list_item: retry list ID fetches
  • Loading branch information
jacobbednarz committed May 13, 2024
2 parents dfe76e3 + 62c7492 commit ef58111
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 8 deletions.
3 changes: 3 additions & 0 deletions .changelog/3303.txt
@@ -0,0 +1,3 @@
```release-note:bug
resource/cloudflare_list_item: retry list ID fetch operations for the identifiers
```
34 changes: 26 additions & 8 deletions internal/framework/service/list_item/resource.go
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"strconv"
"strings"
"time"

cfv1 "github.com/cloudflare/cloudflare-go"
"github.com/cloudflare/terraform-provider-cloudflare/internal/framework/flatteners"
Expand Down Expand Up @@ -290,16 +291,33 @@ func createListItem(ctx context.Context, client *muxclient.Client, data *ListIte
if err != nil {
return cfv1.ListItem{}, fmt.Errorf("failed to create list item: %w", err)
}
// this is extremely inefficient however, it's the only option as the list
// service uses a polling model and does not expose the ID.
searchTerm := getSearchTerm(data)
items, err := client.V1.ListListItems(ctx, accountIdentifier, cfv1.ListListItemsParams{
ID: listID,
Search: searchTerm,
})

// terraform-plugin-framework doesn't have a built in retryable HTTP client (yet)
// so we use a simple loop with a break when we get the data we expect here.
var items []cfv1.ListItem
attempts := 0

for attempts < 5 {
attempts += 1

// this is extremely inefficient however, it's the only option as the list
// service uses a polling model and does not expose the ID.
searchTerm := getSearchTerm(data)
items, err = client.V1.ListListItems(ctx, accountIdentifier, cfv1.ListListItemsParams{
ID: listID,
Search: searchTerm,
})

if len(items) == 1 {
break
}

time.Sleep(time.Duration(attempts) * time.Second)
}

if len(items) != 1 {
return cfv1.ListItem{}, fmt.Errorf("failed to match exactly one list item: %w", err)
return cfv1.ListItem{}, errors.New("failed to match exactly one list item")
}

return items[0], nil
}

0 comments on commit ef58111

Please sign in to comment.