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

filter/firewall-rules fix for double API calls #1016

Merged
merged 3 commits into from
Aug 10, 2022

Conversation

tamas-jozsa
Copy link
Contributor

@tamas-jozsa tamas-jozsa commented Aug 4, 2022

Description

Recent pagination updates to these APIs are causing endpoints to be called twice. This PR aims to fix them.

Has your change been tested?

Unit tests are passing, and I tried this version in cf-terraforming, where it's working as expected

Types of changes

What sort of change does your code introduce/modify?

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist:

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have added tests to cover my changes.
  • All new and existing tests passed.
  • This change is using publicly documented (api.cloudflare.com or developers.cloudflare.com) and stable APIs.

Closes #1024

@github-actions
Copy link
Contributor

github-actions bot commented Aug 4, 2022

changelog detected ✅

filter.go Outdated Show resolved Hide resolved
@@ -91,20 +93,8 @@ func (api *API) Filter(ctx context.Context, rc *ResourceContainer, filterID stri
//
// API reference: https://developers.cloudflare.com/firewall/api/cf-filters/get/#get-all-filters
func (api *API) Filters(ctx context.Context, rc *ResourceContainer, params FilterListParams) ([]Filter, *ResultInfo, error) {
uri := buildURI(fmt.Sprintf("/zones/%s/filters", rc.Identifier), params)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this won't work for the multiple pages condition as it requires the ResultInfo to have the pagination populated to move the pager.

what is the issue you're trying to solve here? right now, I'm not particularly fussed with the additional HTTP call providing it's not duplicating results in the array. I have an idea to remove it however, it requires making all endpoints use pagination by default which will need some digging internally to ensure we don't break things.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've opened a bug describing my findings: #1024

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And I also updated my PR and verified that the changes work as expected.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the List* style methods need to support automatic pagination and if someone passes in their own pagination options. i've pulled this locally and confirmed it's still not fixed for the scenario where we allow manual pagination. below is an example of it not working:

package main

import (
	"context"
	"log"

	"github.com/cloudflare/cloudflare-go"
	"github.com/davecgh/go-spew/spew"
)

func main() {

	api, err := cloudflare.New(os.Getenv("CLOUDFLARE_API_KEY"), os.Getenv("CLOUDFLARE_API_EMAIL"))
	if err != nil {
		log.Fatal(err)
	}
	ctx := context.Background()

	filters, res, _ := api.Filters(ctx, cloudflare.ZoneIdentifier("0da42c8d2132a9ddaf714f9e7c920711"), cloudflare.FilterListParams{ResultInfo: cloudflare.ResultInfo{Page: 2}})

	spew.Dump(len(filters), res) 
    // => filters should be > 0 however it is empty and `res` shows an empty ResultInfo
    // 
	// (*cloudflare.ResultInfo)(0xc0000e82d8)({
	// 	Page: (int) 0,
	// 	PerPage: (int) 0,
	// 	TotalPages: (int) 0,
	// 	Count: (int) 0,
	// 	Total: (int) 0,
	// 	Cursor: (string) "",
	// 	Cursors: (cloudflare.ResultInfoCursors) {
	// 		Before: (string) "",
	// 		After: (string) ""
	// 	}
	// })

Copy link
Contributor Author

@tamas-jozsa tamas-jozsa Aug 9, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gotcha, I missed that requirement. I've then introduced an autoPaginate var that I'll set to false in case either Page or PerPage is provided. It looks better know I think.

However, I noticed, that not all PerPage values are supported. For example, if you provide 3, you get

filters.api.unknown_error

Wonder if we could handle this better.

Also, I further updated method signatures as discussed.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gotcha, I missed that requirement. I've then introduced an autoPaginate var that I'll set to false in case either Page or PerPage is provided. It looks better know I think.

definite improvement however, i'm really not happy with how we are handling pagination as a whole because this is quite verbose to add in all endpoints that need to support it. i'd really prefer if we had a way in the ListParams to explicitly say we only intended to fetch a single result. something we can work on with the experimental client down the track i guess.

Wonder if we could handle this better.

something to raise with the service team; i suspect there are minimums in the per page counts as < 5 (or even 10) don't really make sense for pagination.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with you. We can do better and abstract away pagination logic in a more efficient and reusable manner.

FWIW, you can retrieve a single results with the current setup from any page.

For example, if you want one result from page 2:

fwRules, resultInfo, err := api.Filters(context.Background(), 
  cloudflare.ZoneIdentifier("b1fbb152bbde3bd28919a7f4bdca841f"), 
  cloudflare.FilterListParams{ResultInfo: cloudflare.ResultInfo{PerPage: 1, Page: 2}})

Or 1 result from page 1

fwRules, resultInfo, err := api.Filters(context.Background(), 
  cloudflare.ZoneIdentifier("b1fbb152bbde3bd28919a7f4bdca841f"), 
  cloudflare.FilterListParams{ResultInfo: cloudflare.ResultInfo{PerPage: 1}})

Or everything from page 2

fwRules, resultInfo, err := api.Filters(context.Background(), 
  cloudflare.ZoneIdentifier("b1fbb152bbde3bd28919a7f4bdca841f"), 
  cloudflare.FilterListParams{ResultInfo: cloudflare.ResultInfo{Page: 2}})

filter.go Outdated Show resolved Hide resolved
@tamas-jozsa tamas-jozsa linked an issue Aug 8, 2022 that may be closed by this pull request
2 tasks
@jacobbednarz
Copy link
Member

if you add a CHANGELOG entry (per the automation), we'll get this merged. thanks!

@codecov-commenter
Copy link

Codecov Report

Merging #1016 (ff622a4) into master (6c5ea4a) will increase coverage by 0.12%.
The diff coverage is 74.24%.

@@            Coverage Diff             @@
##           master    #1016      +/-   ##
==========================================
+ Coverage   49.06%   49.18%   +0.12%     
==========================================
  Files         108      112       +4     
  Lines       10428    10549     +121     
==========================================
+ Hits         5116     5189      +73     
- Misses       4200     4232      +32     
- Partials     1112     1128      +16     
Impacted Files Coverage Δ
access_application.go 76.52% <ø> (ø)
access_bookmark.go 72.44% <ø> (ø)
filter.go 42.96% <72.72%> (+0.51%) ⬆️
firewall_rules.go 53.98% <75.75%> (+1.39%) ⬆️
workers_account_settings.go 35.71% <0.00%> (ø)
workers_subdomain.go 57.14% <0.00%> (ø)
workers_tail.go 52.00% <0.00%> (ø)
r2_bucket.go 100.00% <0.00%> (ø)

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

.changelog/1016.txt Outdated Show resolved Hide resolved
.changelog/1016.txt Outdated Show resolved Hide resolved
@jacobbednarz jacobbednarz merged commit d9c0391 into master Aug 10, 2022
@jacobbednarz jacobbednarz deleted the tamas/fix-double-api-calls branch August 10, 2022 09:32
@github-actions github-actions bot added this to the v0.47.0 milestone Aug 10, 2022
github-actions bot pushed a commit that referenced this pull request Aug 10, 2022
@github-actions
Copy link
Contributor

This functionality has been released in v0.47.0.

For further feature requests or bug reports with this functionality, please create a new GitHub issue following the template. Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Fix pagination for filter/firewall-rules API
3 participants