11package puppetdb
22
33import (
4+ "errors"
45 "fmt"
56 "io"
6- "math"
77 "strings"
88)
99
@@ -24,32 +24,16 @@ func (c *Client) Nodes(query string, pagination *Pagination, orderBy *OrderBy) (
2424// information for tracking progress. If pagination is nil, then a default
2525// configuration with a limit of 100 is used instead.
2626func (c * Client ) PaginatedNodes (query string , pagination * Pagination , orderBy * OrderBy ) (* NodesCursor , error ) {
27- if pagination == nil {
28- pagination = & Pagination {Limit : 100 }
29- }
30-
31- tempPagination := Pagination {
32- Limit : 1 ,
33- IncludeTotal : true ,
34- }
35-
36- // make a call to pdb for 1 node to fetch the total number of nodes for
37- // page calculations in the cursor.
38- if _ , err := c .Nodes (query , & tempPagination , orderBy ); err != nil {
39- return nil , fmt .Errorf ("failed to get node total from pdb: %w" , err )
27+ pc , err := newPageCursor (c , nodes , query , pagination , orderBy )
28+ if err != nil {
29+ return nil , fmt .Errorf ("failed to initialize page cursor: %w" , err )
4030 }
4131
42- pagination .Total = tempPagination .Total
43- pagination .IncludeTotal = true
44-
45- nc := & NodesCursor {
46- client : c ,
47- pagination : pagination ,
48- query : query ,
49- orderBy : orderBy ,
32+ cursor := NodesCursor {
33+ pageCursor : pc ,
5034 }
5135
52- return nc , nil
36+ return & cursor , nil
5337}
5438
5539// Node will return a single node by certname
@@ -95,53 +79,17 @@ type Node struct {
9579// NodesCursor is a pagination cursor that provides convenience methods for
9680// stepping through pages of nodes.
9781type NodesCursor struct {
98- client * Client
99- pagination * Pagination
100- query string
101- orderBy * OrderBy
102- currentPage []Node
82+ * pageCursor
10383}
10484
10585// Next returns a page of nodes and iterates the pagination cursor by the
10686// offset. If there are no more results left, the error will be io.EOF.
10787func (nc * NodesCursor ) Next () ([]Node , error ) {
108- // this block increases the offset and checks of it's greater than or equal
109- // to the total only if we have already returned a first page.
110- if nc .currentPage != nil {
111- nc .pagination .Offset = nc .pagination .Offset + nc .pagination .Limit
112-
113- if nc .pagination .Offset >= nc .pagination .Total {
114- return []Node {}, io .EOF
115- }
116- }
117-
118- var err error
119-
120- nc .currentPage , err = nc .client .Nodes (nc .query , nc .pagination , nc .orderBy )
121- if err != nil {
122- return nil , fmt .Errorf ("client call for Nodes returned an error: %w" , err )
123- }
124-
125- if nc .CurrentPage () == nc .TotalPages () {
126- err = io .EOF
127- }
128-
129- return nc .currentPage , err
130- }
131-
132- // TotalPages returns the total number of pages that can returns nodes.
133- func (nc * NodesCursor ) TotalPages () int {
134- pagesf := float64 (nc .pagination .Total ) / float64 (nc .pagination .Limit )
135- pages := int (math .Ceil (pagesf ))
136-
137- return pages
138- }
139-
140- // CurrentPage returns the current page number the cursor is at.
141- func (nc * NodesCursor ) CurrentPage () int {
142- if nc .pagination .Offset == 0 {
143- return 1
88+ payload := []Node {}
89+ err := nc .next (& payload )
90+ if err != nil && ! errors .Is (err , io .EOF ) {
91+ return nil , err
14492 }
14593
146- return nc . pagination . Offset / nc . pagination . Limit + 1
94+ return payload , err
14795}
0 commit comments