@@ -30,46 +30,33 @@ import (
3030
3131func (c * client ) GetSnapshotByID (ctx context.Context , snapshotID string ) (* Snapshot , error ) {
3232 logger := klog .FromContext (ctx )
33- p := c .Snapshot .NewListSnapshotsParams ()
34- if snapshotID != "" {
35- p .SetId (snapshotID )
36- }
37- if c .projectID != "" {
38- p .SetProjectid (c .projectID )
39- }
40- logger .V (2 ).Info ("CloudStack API call" , "command" , "ListSnapshots" , "params" , map [string ]string {
41- "id" : snapshotID ,
42- "projectid" : c .projectID ,
33+ logger .V (2 ).Info ("CloudStack API call" , "command" , "GetSnapshotByID" , "params" , map [string ]string {
34+ "id" : snapshotID ,
4335 })
44- l , err := c .Snapshot .ListSnapshots (p )
36+
37+ snapshot , count , err := c .Snapshot .GetSnapshotByID (snapshotID )
4538 if err != nil {
39+ if count == 0 {
40+ return nil , ErrNotFound
41+ }
42+
4643 return nil , err
4744 }
48- if l .Count == 0 {
49- return nil , ErrNotFound
50- }
51- if l .Count > 1 {
52- return nil , ErrTooManyResults
53- }
54- snapshot := l .Snapshots [0 ]
55- s := Snapshot {
45+
46+ return & Snapshot {
5647 ID : snapshot .Id ,
5748 Name : snapshot .Name ,
5849 DomainID : snapshot .Domainid ,
5950 ProjectID : snapshot .Projectid ,
6051 ZoneID : snapshot .Zoneid ,
6152 VolumeID : snapshot .Volumeid ,
62- }
63-
64- return & s , nil
53+ }, nil
6554}
6655
6756func (c * client ) CreateSnapshot (ctx context.Context , volumeID , name string ) (* Snapshot , error ) {
6857 logger := klog .FromContext (ctx )
6958 p := c .Snapshot .NewCreateSnapshotParams (volumeID )
70- if name != "" {
71- p .SetName (name )
72- }
59+ p .SetName (name )
7360 logger .V (2 ).Info ("CloudStack API call" , "command" , "CreateSnapshot" , "params" , map [string ]string {
7461 "volumeid" : volumeID ,
7562 "name" : name ,
@@ -80,7 +67,7 @@ func (c *client) CreateSnapshot(ctx context.Context, volumeID, name string) (*Sn
8067 return nil , status .Errorf (codes .Internal , "Error %v" , err )
8168 }
8269
83- snap := Snapshot {
70+ return & Snapshot {
8471 ID : snapshot .Id ,
8572 Name : snapshot .Name ,
8673 Size : snapshot .Virtualsize ,
@@ -89,9 +76,7 @@ func (c *client) CreateSnapshot(ctx context.Context, volumeID, name string) (*Sn
8976 ZoneID : snapshot .Zoneid ,
9077 VolumeID : snapshot .Volumeid ,
9178 CreatedAt : snapshot .Created ,
92- }
93-
94- return & snap , nil
79+ }, nil
9580}
9681
9782func (c * client ) DeleteSnapshot (_ context.Context , snapshotID string ) error {
@@ -107,58 +92,49 @@ func (c *client) DeleteSnapshot(_ context.Context, snapshotID string) error {
10792
10893func (c * client ) GetSnapshotByName (ctx context.Context , name string ) (* Snapshot , error ) {
10994 logger := klog .FromContext (ctx )
110- if name == "" {
111- return nil , ErrNotFound
112- }
113- p := c .Snapshot .NewListSnapshotsParams ()
114- p .SetName (name )
115- if c .projectID != "" {
116- p .SetProjectid (c .projectID )
117- }
118- logger .V (2 ).Info ("CloudStack API call" , "command" , "ListSnapshots" , "params" , map [string ]string {
119- "name" : name ,
120- "projectid" : c .projectID ,
95+ logger .V (2 ).Info ("CloudStack API call" , "command" , "GetSnapshotByName" , "params" , map [string ]string {
96+ "name" : name ,
12197 })
122- l , err := c .Snapshot .ListSnapshots ( p )
98+ snapshot , count , err := c .Snapshot .GetSnapshotByName ( name )
12399 if err != nil {
100+ if count == 0 {
101+ return nil , ErrNotFound
102+ }
103+
124104 return nil , err
125105 }
126- if l .Count == 0 {
127- return nil , ErrNotFound
128- }
129- if l .Count > 1 {
130- return nil , ErrTooManyResults
131- }
132- snapshot := l .Snapshots [0 ]
133- s := Snapshot {
106+
107+ return & Snapshot {
134108 ID : snapshot .Id ,
135109 Name : snapshot .Name ,
136110 DomainID : snapshot .Domainid ,
137111 ProjectID : snapshot .Projectid ,
138112 ZoneID : snapshot .Zoneid ,
139113 VolumeID : snapshot .Volumeid ,
140114 CreatedAt : snapshot .Created ,
141- }
142-
143- return & s , nil
115+ }, nil
144116}
145117
146118func (c * client ) ListSnapshots (ctx context.Context , volumeID , snapshotID string ) ([]* Snapshot , error ) {
147119 logger := klog .FromContext (ctx )
148120 p := c .Snapshot .NewListSnapshotsParams ()
121+ // snapshotID is optional: csi.ListSnapshotsRequest
149122 if snapshotID != "" {
150123 p .SetId (snapshotID )
151124 }
125+ // volumeID is optional: csi.ListSnapshotsRequest
152126 if volumeID != "" {
153127 p .SetVolumeid (volumeID )
154128 }
129+
130+ // There is no list function that uses the client default project id option
155131 if c .projectID != "" {
156132 p .SetProjectid (c .projectID )
157133 }
134+
158135 logger .V (2 ).Info ("CloudStack API call" , "command" , "ListSnapshots" , "params" , map [string ]string {
159- "id" : snapshotID ,
160- "volumeid" : volumeID ,
161- "projectid" : c .projectID ,
136+ "id" : snapshotID ,
137+ "volumeid" : volumeID ,
162138 })
163139 l , err := c .Snapshot .ListSnapshots (p )
164140 if err != nil {
0 commit comments