Skip to content

Commit

Permalink
Improve pagination in ListSnapshots
Browse files Browse the repository at this point in the history
  • Loading branch information
Farnaz Babaeian committed Dec 6, 2023
1 parent 428f5d2 commit 7760695
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 24 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/pr-check.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ jobs:
name: Lint
runs-on: ubuntu-20.04
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- name: golangci-lint
uses: golangci/golangci-lint-action@v3
with:
Expand All @@ -25,7 +25,7 @@ jobs:
go-version: "^1.15"

- name: Check out code
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: Cache
uses: actions/cache@v3
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:

steps:
- name: Check out code
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: Cache
uses: actions/cache@v3
Expand All @@ -32,7 +32,7 @@ jobs:
run: make container

- name: Log into registry
uses: docker/login-action@v2
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{github.actor}}
Expand Down Expand Up @@ -77,7 +77,7 @@ jobs:

steps:
- name: Checkout code
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: Create manifest
run: |
Expand Down
46 changes: 27 additions & 19 deletions pkg/cloud/snapshots.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ func (c *client) ListSnapshots(filters map[string]string) ([]Snapshot, string, e
// Initialize parameters for CloudStack API call
params := c.Snapshot.NewListSnapshotsParams()

// Default values for pageSize and currentPage
var pageSize, currentPage int
var err error

// Apply filters and pagination parameters
for key, val := range filters {
switch key {
Expand All @@ -86,28 +90,26 @@ func (c *client) ListSnapshots(filters map[string]string) ([]Snapshot, string, e
case "VolumeID":
params.SetVolumeid(val)
case "Marker":
page, err := strconv.Atoi(val)
currentPage, err = strconv.Atoi(val)
if err != nil {
klog.V(3).Infof("Invalid format for Marker: %s, defaulting to first page", val)
page = 1 // Default to the first page if the marker is invalid
currentPage = 1 // Default to the first page if the marker is invalid
}
params.SetPage(page)
params.SetPage(currentPage)
case "Limit":
limit, err := strconv.Atoi(val)
pageSize, err = strconv.Atoi(val)
if err != nil {
klog.V(3).Infof("Invalid format for Limit: %s, defaulting to a preset limit", val)
limit = 20 // Set a default limit if the input is invalid
pageSize = 20 // Set a default pageSize if the input is invalid
}
params.SetPagesize(limit)
params.SetPagesize(pageSize)
default:
klog.V(3).Infof("Not a valid filter key %s", key)
}
}

// Log the final pagination settings
currentPage, _ := params.GetPage()
currentPageSize, _ := params.GetPagesize()
klog.V(3).Infof("Fetching snapshots with Page: %d, PageSize: %d", currentPage, currentPageSize)
klog.V(3).Infof("Fetching snapshots with Page: %d, PageSize: %d", currentPage, pageSize)

// Call CloudStack API to list snapshots
resp, err := c.Snapshot.ListSnapshots(params)
Expand All @@ -123,13 +125,9 @@ func (c *client) ListSnapshots(filters map[string]string) ([]Snapshot, string, e
}

// Determine the nextPageToken
if morePagesExist(resp) {
page, pageSet := params.GetPage()
if pageSet {
nextPageToken = strconv.Itoa(page + 1) // Set nextPageToken to the next page
} else {
nextPageToken = "1" // Set to first page if page is not set
}
if morePagesExist(resp, pageSize, currentPage) {
// Set nextPageToken to the next page
nextPageToken = strconv.Itoa(currentPage + 1)
}

return snaps, nextPageToken, nil
Expand Down Expand Up @@ -259,7 +257,17 @@ func convertAPISnapshotToSnapshot(apiSnap *cloudstack.Snapshot) Snapshot {
}

// morePagesExist determines if there are more pages of results based on the API response.
func morePagesExist(resp *cloudstack.ListSnapshotsResponse) bool {
// Implement logic to determine if more pages exist
return false
func morePagesExist(resp *cloudstack.ListSnapshotsResponse, pageSize int, currentPage int) bool {
if resp == nil || pageSize <= 0 {
return false
}

totalSnapshots := resp.Count
totalPages := totalSnapshots / pageSize
// Check for any remaining snapshots not fitting in a full page
if totalSnapshots%pageSize != 0 {
totalPages++
}

return currentPage < totalPages
}

0 comments on commit 7760695

Please sign in to comment.