Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

resource/cloudflare_list_item: retry list ID fetches #3303

Merged
merged 1 commit into from
May 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/3303.txt
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"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 @@
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)

Check failure on line 315 in internal/framework/service/list_item/resource.go

View workflow job for this annotation

GitHub Actions / tfproviderlint (ubuntu-latest)

R018: prefer resource.Retry() or (resource.StateChangeConf).WaitForState() over time.Sleep()
}

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
}
Loading