@@ -12,7 +12,6 @@ import (
1212 "google.golang.org/grpc/codes"
1313 "google.golang.org/grpc/status"
1414 "k8s.io/apimachinery/pkg/util/wait"
15- "k8s.io/klog/v2"
1615)
1716
1817const (
@@ -25,121 +24,121 @@ const (
2524
2625// CreateSnapshot issues a request to take a Snapshot of the specified Volume with the corresponding ID and
2726// returns the resultant CloudStack Snapshot Item upon success
28- func (c * client ) CreateSnapshot (ctx context.Context , name , volID string ) (string , error ) {
29- klog .V (3 ).Infof ("The name of snapshot that is starting to be created is %s and id is %s" , name , volID )
30-
27+ func (c * client ) CreateSnapshot (ctx context.Context , name , volumeID string ) (string , error ) {
3128 // Input validation
32- if name == "" || volID == "" {
33- return "" , status .Errorf (codes .Internal , "Snapshotname %s; requested for volume %s" , name , volID )
29+ if name == "" || volumeID == "" {
30+ return "" , status .Errorf (codes .Internal , "Snapshotname %s; requested for volume %s" , name , volumeID )
3431 }
3532
3633 // Pre-API Call Checks
3734 // Retrieve volume information to ensure it exists and is in a valid state
38- volume , _ , err := c .Volume .GetVolumeByID (volID )
35+ volume , _ , err := c .Volume .GetVolumeByID (volumeID )
3936 if err != nil {
40- return "" , fmt .Errorf ("failed to retrieve volume '%s': %v" , volID , err )
37+ return "" , fmt .Errorf ("failed to retrieve volume '%s': %v" , volumeID , err )
4138 }
4239
4340 // Check if the volume is in a 'Ready' state for snapshot creation
4441 if volume .State != "Ready" {
45- return "" , fmt .Errorf ("volume '%s' is not in a 'Ready' state for snapshot creation" , volID )
42+ return "" , fmt .Errorf ("volume '%s' is not in a 'Ready' state for snapshot creation" , volumeID )
4643 }
4744
4845 // Preparing snapshot creation parameters
49- p := c .Snapshot .NewCreateSnapshotParams (volID )
46+ p := c .Snapshot .NewCreateSnapshotParams (volumeID )
5047 p .SetName (name )
51- p .SetVolumeid (volID )
48+ p .SetVolumeid (volumeID )
5249 p .SetAsyncbackup (asyncBackup )
53- klog .V (3 ).Infof ("The p is %s " , p )
5450 ctxzap .Extract (ctx ).Sugar ().Infow ("CloudStack API call" , "command" , "CreateSnapshot" , "params" , map [string ]string {
5551 "name" : name ,
56- "volumeid" : volID ,
52+ "volumeid" : volumeID ,
5753 "asyncbackup" : strconv .FormatBool (asyncBackup ),
5854 })
59-
60- klog .V (3 ).Infof ("snapshot %s of %s has default %s value for asyncBackup" , name , volID , asyncBackup )
61- snap , err := c .Snapshot .CreateSnapshot (p )
55+ snapshot , err := c .Snapshot .CreateSnapshot (p )
6256 if err != nil {
63- return "" , fmt .Errorf ("failed to create snapshot '%s' for volume '%s': %v" , name , volID , err )
57+ return "" , fmt .Errorf ("failed to create snapshot '%s' for volume '%s': %v" , name , volumeID , err )
6458 }
6559
66- return snap .Id , nil
60+ return snapshot .Id , nil
6761}
6862
6963// ListSnapshots retrieves a list of active snapshots from CloudStack for the corresponding Domain. We also
7064// provide the ability to provide limit to enable the consumer to provide accurate pagination.
7165// In addition the filters argument provides a mechanism for passing in valid filter strings to the list
7266// operation. Valid filter keys are: Name, VolumeID, Limit, Marker (DomainID has no effect)
7367
74- func (c * client ) ListSnapshots (filters map [string ]string ) ([]Snapshot , string , error ) {
75- var snaps []Snapshot
68+ func (c * client ) ListSnapshots (ctx context. Context , filters map [string ]string ) ([]Snapshot , string , error ) {
69+ var snapshotList []Snapshot
7670 var nextPageToken string
7771
7872 // Initialize parameters for CloudStack API call
79- params := c .Snapshot .NewListSnapshotsParams ()
73+ p := c .Snapshot .NewListSnapshotsParams ()
74+
75+ // Declare pointer variables for pageSize and currentPage
76+ pageSize := 20 // Default pageSize
77+ currentPage := 1 // Default to the first page
78+ var err error
8079
8180 // Apply filters and pagination parameters
82- for key , val := range filters {
81+ for key , value := range filters {
8382 switch key {
8483 case "Name" :
85- params .SetName (val )
84+ p .SetName (value )
8685 case "VolumeID" :
87- params .SetVolumeid (val )
86+ p .SetVolumeid (value )
8887 case "Marker" :
89- page , err := strconv .Atoi (val )
88+ // Try to parse the page number from the filters
89+ currentPage , err = strconv .Atoi (value )
9090 if err != nil {
91- klog . V ( 3 ). Infof ( "Invalid format for Marker: %s, defaulting to first page" , val )
92- page = 1 // Default to the first page if the marker is invalid
91+ fmt . Printf ( "Invalid format for Marker: %s, using default page\n " , value )
92+ currentPage = 1 // Reassign to default if parsing fails
9393 }
94- params .SetPage (page )
94+ p .SetPage (currentPage )
9595 case "Limit" :
96- limit , err := strconv .Atoi (val )
96+ // Try to parse the page size from the filters
97+ pageSize , err = strconv .Atoi (value )
9798 if err != nil {
98- klog . V ( 3 ). Infof ( "Invalid format for Limit: %s, defaulting to a preset limit" , val )
99- limit = 20 // Set a default limit if the input is invalid
99+ fmt . Printf ( "Invalid or unsupported format for Limit: %s, using default limit\n " , value )
100+ pageSize = 20 // Reassign to default if parsing fails or unsupported value
100101 }
101- params .SetPagesize (limit )
102+ p .SetPagesize (pageSize )
102103 default :
103- klog . V ( 3 ). Infof ( "Not a valid filter key %s" , key )
104+ fmt . Printf ( "Not a valid filter key %s\n " , key )
104105 }
105106 }
106107
107108 // Log the final pagination settings
108- currentPage , _ := params .GetPage ()
109- currentPageSize , _ := params .GetPagesize ()
110- klog .V (3 ).Infof ("Fetching snapshots with Page: %d, PageSize: %d" , currentPage , currentPageSize )
109+ fmt .Printf ("Fetching snapshots with Page: %d, PageSize: %d\n " , currentPage , pageSize )
111110
112111 // Call CloudStack API to list snapshots
113- resp , err := c .Snapshot .ListSnapshots (params )
112+ resp , err := c .Snapshot .ListSnapshots (p )
114113 if err != nil {
115- klog .Errorf ("Error listing snapshots from CloudStack: %v" , err )
116114 return nil , "" , fmt .Errorf ("error listing snapshots from CloudStack: %w" , err )
117115 }
118116
119117 // Convert the response to your []Snapshot type
120- for _ , apiSnap := range resp .Snapshots {
121- snap := convertAPISnapshotToSnapshot (apiSnap )
122- snaps = append (snaps , snap )
118+ for _ , apiSnapshot := range resp .Snapshots {
119+ snapshot := convertAPISnapshotToSnapshot (apiSnapshot )
120+ snapshotList = append (snapshotList , snapshot )
123121 }
124122
125123 // Determine the nextPageToken
126- if morePagesExist (resp ) {
127- page , pageSet := params . GetPage ()
128- if pageSet {
129- nextPageToken = strconv . Itoa ( page + 1 ) // Set nextPageToken to the next page
130- } else {
131- nextPageToken = "1" // Set to first page if page is not set
124+ if morePagesExist (resp , pageSize , currentPage ) {
125+ // Set nextPageToken to the next page
126+ nextPageToken = strconv . Itoa ( currentPage + 1 )
127+ if nextPageToken != "" {
128+ fmt . Println ( "The nextPageToken is set, more than one page is available." )
129+
132130 }
131+
133132 }
134133
135- return snaps , nextPageToken , nil
134+ return snapshotList , nextPageToken , nil
136135}
137136
138137// DeleteSnapshot issues a request to delete the Snapshot with the specified ID from the backend
139- func (c * client ) DeleteSnapshot (ctx context.Context , snapID string ) error {
140- p := c .Snapshot .NewDeleteSnapshotParams (snapID )
138+ func (c * client ) DeleteSnapshot (ctx context.Context , snapshotID string ) error {
139+ p := c .Snapshot .NewDeleteSnapshotParams (snapshotID )
141140 ctxzap .Extract (ctx ).Sugar ().Infow ("CloudStack API call" , "command" , "DeleteSnapshot" , "params" , map [string ]string {
142- "id" : snapID ,
141+ "id" : snapshotID ,
143142 })
144143 _ , err := c .Snapshot .DeleteSnapshot (p )
145144 if err != nil && strings .Contains (err .Error (), "4350" ) {
@@ -237,29 +236,39 @@ func (c *client) WaitSnapshotReady(ctx context.Context, snapshotID string) error
237236func (c * client ) snapshotIsReady (ctx context.Context , snapshotID string ) (bool , error ) {
238237 snap , err := c .GetSnapshotByID (ctx , snapshotID )
239238 if err != nil {
240- klog .Errorf ("Snapshot os not ready: %v" , err )
241- return false , err
239+ return false , fmt .Errorf ("Snapshot is not ready: %w" , err )
242240 }
243241
244242 return snap .State == snapshotReadyStatus , nil
245243}
246244
247245// convertAPISnapshotToSnapshot converts an API snapshot object to your application's Snapshot type.
248- func convertAPISnapshotToSnapshot (apiSnap * cloudstack.Snapshot ) Snapshot {
246+ func convertAPISnapshotToSnapshot (apiSnapshot * cloudstack.Snapshot ) Snapshot {
249247 // Conversion logic here
250248 return Snapshot {
251- ID : apiSnap .Id ,
252- Created : apiSnap .Created ,
253- Name : apiSnap .Name ,
254- VolumeID : apiSnap .Volumeid ,
255- ZoneID : apiSnap .Zoneid ,
256- State : apiSnap .State ,
257- VirtualSize : int (apiSnap .Virtualsize ),
249+ ID : apiSnapshot .Id ,
250+ Created : apiSnapshot .Created ,
251+ Name : apiSnapshot .Name ,
252+ VolumeID : apiSnapshot .Volumeid ,
253+ ZoneID : apiSnapshot .Zoneid ,
254+ State : apiSnapshot .State ,
255+ VirtualSize : int (apiSnapshot .Virtualsize ),
258256 }
259257}
260258
261259// morePagesExist determines if there are more pages of results based on the API response.
262- func morePagesExist (resp * cloudstack.ListSnapshotsResponse ) bool {
263- // Implement logic to determine if more pages exist
264- return false
260+ func morePagesExist (resp * cloudstack.ListSnapshotsResponse , pageSize int , currentPage int ) bool {
261+ if resp == nil || pageSize <= 0 {
262+ return false
263+ }
264+
265+ totalSnapshots := resp .Count
266+ totalPages := totalSnapshots / pageSize
267+ // Check for any remaining snapshots not fitting in a full page
268+ if totalSnapshots % pageSize != 0 {
269+ totalPages ++
270+ }
271+ fmt .Printf ("Total Page is %d" , totalPages )
272+
273+ return currentPage < totalPages
265274}
0 commit comments