Skip to content
Permalink

Comparing changes

This is a direct comparison between two commits made in this repository or its related repositories. View the default comparison for this range or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: leaseweb/cloudstack-csi-driver
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 5f953af144194c4ab03e7278e580dda47b2bc63b
Choose a base ref
..
head repository: leaseweb/cloudstack-csi-driver
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 776069585ca3a77737e25067701366e2e483b5cb
Choose a head ref
Showing with 27 additions and 19 deletions.
  1. +27 −19 pkg/cloud/snapshots.go
46 changes: 27 additions & 19 deletions pkg/cloud/snapshots.go
Original file line number Diff line number Diff line change
@@ -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 {
@@ -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)
@@ -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
@@ -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
}