Copilot commented on code in PR #102: URL: https://github.com/apache/cloudstack-kubernetes-provider/pull/102#discussion_r3828141477
########## pagination.go: ########## @@ -0,0 +1,70 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package cloudstack + +// pageableParams is the paging surface that every cloudstack-go List*Params +// type exposes. +type pageableParams interface { + SetPage(int) + SetPagesize(int) +} + +// listAll makes further requests to fetch the remaining items if the count is higher +// than the number of items returned. +func listAll[T any](p pageableParams, list func() (count int, items []T, err error)) ([]T, error) { + count, items, err := list() + if err != nil { + return nil, err + } + + // Nothing was truncated, or there is nothing to page through. + if len(items) >= count || len(items) == 0 { + return items, nil + } + + // The server just demonstrated how many records it will return at a time, + // which is the one page size it is guaranteed to accept. + pageSize := len(items) + collected := items + + for page := 2; len(collected) < count; page++ { + p.SetPage(page) + p.SetPagesize(pageSize) + + _, items, err := list() + if err != nil { + return nil, err + } Review Comment: In listAll, the loop discards the updated `count` returned by subsequent page requests (`_, items, err := list()`). If CloudStack's `Count` increases between pages (e.g., resources added while paging), listAll can stop once it reaches the initial count even though more pages exist. Capture the per-page count and keep `count` in sync (at least as the max seen) so paging doesn't truncate prematurely. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
