@@ -2,6 +2,7 @@ package driver
22
33import (
44 "context"
5+ "encoding/json"
56 "errors"
67 "fmt"
78 "math/rand"
@@ -118,34 +119,50 @@ func (cs *controllerServer) CreateVolume(ctx context.Context, req *csi.CreateVol
118119 }
119120 }
120121
122+ // We have to create the volume.
123+
124+ // Determine volume size using requested capacity range.
125+ sizeInGB , err := determineSize (req )
126+ if err != nil {
127+ return nil , status .Error (codes .InvalidArgument , err .Error ())
128+ }
129+
121130 if snapshotID != "" {
122131 logger .Info ("Creating volume from snapshot" , "snapshotID" , snapshotID )
123132 // Call the cloud connector's CreateVolumeFromSnapshot if implemented
124- volID , err := cs .connector .CreateVolumeFromSnapshot (ctx , diskOfferingID , vol .ZoneID , name , vol .DomainID , vol .ProjectID , snapshotID , vol .Size )
133+ printVolumeAsJSON (req )
134+ snapshot , err := cs .connector .GetSnapshotByID (ctx , snapshotID )
135+ if errors .Is (err , cloud .ErrNotFound ) {
136+ return nil , status .Errorf (codes .NotFound , "Snapshot %v not found" , snapshotID )
137+ } else if err != nil {
138+ // Error with CloudStack
139+ return nil , status .Errorf (codes .Internal , "Error %v" , err )
140+ }
141+ logger .Info ("Disk offering ID" , "diskOfferingID: " , diskOfferingID )
142+ logger .Info ("Zone ID" , "ZoneID" , snapshot .ZoneID )
143+ logger .Info ("Name" , "name" , name )
144+ logger .Info ("Domain ID" , "DomainID" , snapshot .DomainID )
145+ logger .Info ("Project ID" , "ProjectID" , snapshot .ProjectID )
146+ logger .Info ("Snapshot ID" , "snapshotID" , snapshotID )
147+ logger .Info ("Volume size" , "Size" , sizeInGB )
148+ volFromSnapshot , err := cs .connector .CreateVolumeFromSnapshot (ctx , snapshot .ZoneID , name , snapshot .DomainID , snapshot .ProjectID , snapshotID , sizeInGB )
125149 if err != nil {
126150 return nil , status .Errorf (codes .Internal , "Cannot create volume from snapshot %s: %v" , snapshotID , err .Error ())
127151 }
128152 resp := & csi.CreateVolumeResponse {
129153 Volume : & csi.Volume {
130- VolumeId : volID ,
131- CapacityBytes : vol .Size ,
154+ VolumeId : volFromSnapshot . ID ,
155+ CapacityBytes : volFromSnapshot .Size ,
132156 VolumeContext : req .GetParameters (),
157+ ContentSource : req .GetVolumeContentSource (),
133158 AccessibleTopology : []* csi.Topology {
134- Topology {ZoneID : vol .ZoneID }.ToCSI (),
159+ Topology {ZoneID : volFromSnapshot .ZoneID }.ToCSI (),
135160 },
136161 },
137162 }
138163 return resp , nil
139164 }
140165
141- // We have to create the volume.
142-
143- // Determine volume size using requested capacity range.
144- sizeInGB , err := determineSize (req )
145- if err != nil {
146- return nil , status .Error (codes .InvalidArgument , err .Error ())
147- }
148-
149166 // Determine zone using topology constraints.
150167 var zoneID string
151168 topologyRequirement := req .GetAccessibilityRequirements ()
@@ -189,7 +206,7 @@ func (cs *controllerServer) CreateVolume(ctx context.Context, req *csi.CreateVol
189206 VolumeId : volID ,
190207 CapacityBytes : util .GigaBytesToBytes (sizeInGB ),
191208 VolumeContext : req .GetParameters (),
192- // ContentSource: req.GetVolumeContentSource(), TODO: snapshot support.
209+ ContentSource : req .GetVolumeContentSource (),
193210 AccessibleTopology : []* csi.Topology {
194211 Topology {ZoneID : zoneID }.ToCSI (),
195212 },
@@ -199,6 +216,11 @@ func (cs *controllerServer) CreateVolume(ctx context.Context, req *csi.CreateVol
199216 return resp , nil
200217}
201218
219+ func printVolumeAsJSON (vol * csi.CreateVolumeRequest ) {
220+ b , _ := json .MarshalIndent (vol , "" , " " )
221+ fmt .Println (string (b ))
222+ }
223+
202224func checkVolumeSuitable (vol * cloud.Volume ,
203225 diskOfferingID string , capRange * csi.CapacityRange , topologyRequirement * csi.TopologyRequirement ,
204226) (bool , string ) {
0 commit comments