Skip to content

Commit 9b2156a

Browse files
authored
Merge pull request #258 from atc0005/i245-fix-paging-collection-logic
Fix paging collection logic
2 parents 820bd88 + 6c606da commit 9b2156a

File tree

3 files changed

+30
-5
lines changed

3 files changed

+30
-5
lines changed

internal/rsat/organizations.go

+9-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ package rsat
99

1010
import (
1111
"context"
12+
"encoding/json"
1213
"fmt"
1314
"sort"
1415
"strconv"
@@ -41,7 +42,12 @@ type OrganizationsResponse struct {
4142
Total int `json:"total"`
4243

4344
// Page is the page number for the current query response results.
44-
Page int `json:"page"`
45+
//
46+
// NOTE: In practice, this value has been found to be returned as an
47+
// integer in the first response and as a string value for each additional
48+
// page of results. The json.Number type accepts either format when
49+
// decoding the response.
50+
Page json.Number `json:"page"`
4551

4652
// PerPage is the pagination limit applied to API query results. If not
4753
// specified by the client this is the default value set by the API.
@@ -131,6 +137,8 @@ func GetOrganizations(ctx context.Context, client *APIClient) ([]Organization, e
131137
logger.Error().Err(closeErr).Msg("error closing response body")
132138
}
133139

140+
allOrgs = append(allOrgs, orgsQueryResp.Organizations...)
141+
134142
numNewOrgs := len(orgsQueryResp.Organizations)
135143
numCollectedOrgs := len(allOrgs)
136144
numOrgsRemaining := orgsQueryResp.Subtotal - numCollectedOrgs

internal/rsat/rsat.go

+12-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"io"
1515
"net/http"
1616
"net/url"
17+
"os"
1718

1819
"github.com/rs/zerolog"
1920
)
@@ -157,7 +158,17 @@ func decode(dst interface{}, reader io.Reader, logger zerolog.Logger, sourceName
157158
sourceName,
158159
limit,
159160
)
160-
dec := json.NewDecoder(io.LimitReader(reader, limit))
161+
162+
limitReader := io.LimitReader(reader, limit)
163+
164+
// If debug or greater logging is enabled write the JSON payload in the
165+
// response as-is to stderr for review.
166+
if zerolog.GlobalLevel() == zerolog.DebugLevel ||
167+
zerolog.GlobalLevel() == zerolog.TraceLevel {
168+
limitReader = io.TeeReader(io.LimitReader(reader, limit), os.Stderr)
169+
}
170+
171+
dec := json.NewDecoder(limitReader)
161172

162173
// This project does not use all fields from Red Hat Satellite API
163174
// responses so we do not attempt to assert that we've accounted for all

internal/rsat/syncplans.go

+9-3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ package rsat
99

1010
import (
1111
"context"
12+
"encoding/json"
1213
"fmt"
1314
"math"
1415
"strconv"
@@ -49,7 +50,12 @@ type SyncPlansResponse struct {
4950
Total int `json:"total"`
5051

5152
// Page is the page number for the current query response results.
52-
Page int `json:"page"`
53+
//
54+
// NOTE: In practice, this value has been found to be returned as an
55+
// integer in the first response and as a string value for each additional
56+
// page of results. The json.Number type accepts either format when
57+
// decoding the response.
58+
Page json.Number `json:"page"`
5359

5460
// PerPage is the pagination limit applied to API query results. If not
5561
// specified by the client this is the default value set by the API.
@@ -467,12 +473,12 @@ func getOrgSyncPlans(ctx context.Context, client *APIClient, org Organization) (
467473
syncPlansQueryResp.SyncPlans[i].OrganizationTitle = org.Title
468474
}
469475

476+
allSyncPlans = append(allSyncPlans, syncPlansQueryResp.SyncPlans...)
477+
470478
numNewSyncPlans := len(syncPlansQueryResp.SyncPlans)
471479
numCollectedSyncPlans := len(allSyncPlans)
472480
numSyncPlansRemaining := syncPlansQueryResp.Subtotal - numCollectedSyncPlans
473481

474-
allSyncPlans = append(allSyncPlans, syncPlansQueryResp.SyncPlans...)
475-
476482
subLogger.Debug().
477483
Str("api_endpoint", apiURL).
478484
Int("sync_plans_collected", numCollectedSyncPlans).

0 commit comments

Comments
 (0)