@@ -175,44 +175,7 @@ func (d *controllerService) CreateVolume(ctx context.Context, req *csi.CreateVol
175
175
}
176
176
177
177
if req .GetVolumeContentSource () != nil {
178
- volumeSource := req .VolumeContentSource
179
- switch volumeSource .Type .(type ) {
180
- case * csi.VolumeContentSource_Volume :
181
- diskDetails , _ := d .cloud .GetDiskByNamePrefix ("clone-" + volName )
182
- if diskDetails != nil {
183
- err := verifyVolumeDetails (opts , diskDetails )
184
- if err != nil {
185
- return nil , err
186
- }
187
- return newCreateVolumeResponse (diskDetails , req .VolumeContentSource ), nil
188
- }
189
- if srcVolume := volumeSource .GetVolume (); srcVolume != nil {
190
- srcVolumeID := srcVolume .GetVolumeId ()
191
- diskDetails , err := d .cloud .GetDiskByID (srcVolumeID )
192
- if err != nil {
193
- return nil , status .Errorf (codes .Internal , "Could not get the source volume %q: %v" , srcVolumeID , err )
194
- }
195
- if util .GiBToBytes (diskDetails .CapacityGiB ) != volSizeBytes {
196
- return nil , status .Errorf (codes .Internal , "Cannot clone volume %v, source volume size is not equal to the clone volume" , srcVolumeID )
197
- }
198
- err = verifyVolumeDetails (opts , diskDetails )
199
- if err != nil {
200
- return nil , err
201
- }
202
- diskFromSourceVolume , err := d .cloud .CloneDisk (srcVolumeID , volName )
203
- if err != nil {
204
- return nil , status .Errorf (codes .Internal , "Could not create volume %q: %v" , volName , err )
205
- }
206
-
207
- cloneDiskDetails , err := d .cloud .GetDiskByID (diskFromSourceVolume .VolumeID )
208
- if err != nil {
209
- return nil , status .Errorf (codes .Internal , "Could not create volume %q: %v" , volName , err )
210
- }
211
- return newCreateVolumeResponse (cloneDiskDetails , req .VolumeContentSource ), nil
212
- }
213
- default :
214
- return nil , status .Errorf (codes .InvalidArgument , "%v not a proper volume source" , volumeSource )
215
- }
178
+ return handleClone (d .cloud , req , volName , volSizeBytes , opts )
216
179
}
217
180
218
181
// check if disk exists
@@ -536,3 +499,42 @@ func verifyVolumeDetails(payload *cloud.DiskOptions, diskDetails *cloud.Disk) er
536
499
}
537
500
return nil
538
501
}
502
+
503
+ func handleClone (cloud cloud.Cloud , req * csi.CreateVolumeRequest , volName string , volSizeBytes int64 , opts * cloud.DiskOptions ) (* csi.CreateVolumeResponse , error ) {
504
+ volumeSource := req .VolumeContentSource
505
+ switch volumeSource .Type .(type ) {
506
+ case * csi.VolumeContentSource_Volume :
507
+ diskDetails , _ := cloud .GetDiskByNamePrefix ("clone-" + req .GetName ())
508
+ if diskDetails != nil {
509
+ err := verifyVolumeDetails (opts , diskDetails )
510
+ if err != nil {
511
+ return nil , err
512
+ }
513
+ return newCreateVolumeResponse (diskDetails , req .VolumeContentSource ), nil
514
+ }
515
+ if srcVolume := volumeSource .GetVolume (); srcVolume != nil {
516
+ srcVolumeID := srcVolume .GetVolumeId ()
517
+ diskDetails , err := cloud .GetDiskByID (srcVolumeID )
518
+ if err != nil {
519
+ return nil , status .Errorf (codes .Internal , "Could not get the source volume %q: %v" , srcVolumeID , err )
520
+ }
521
+ if util .GiBToBytes (diskDetails .CapacityGiB ) != volSizeBytes {
522
+ return nil , status .Errorf (codes .Internal , "Cannot clone volume %v, source volume size is not equal to the clone volume" , srcVolumeID )
523
+ }
524
+ err = verifyVolumeDetails (opts , diskDetails )
525
+ if err != nil {
526
+ return nil , err
527
+ }
528
+ diskFromSourceVolume , err := cloud .CloneDisk (srcVolumeID , volName )
529
+ if err != nil {
530
+ return nil , status .Errorf (codes .Internal , "Could not clone volume %q: %v" , volName , err )
531
+ }
532
+ cloneDiskDetails , err := cloud .GetDiskByID (diskFromSourceVolume .VolumeID )
533
+ if err != nil {
534
+ return nil , status .Errorf (codes .Internal , "Could not get volume %q after clone: %v" , volName , err )
535
+ }
536
+ return newCreateVolumeResponse (cloneDiskDetails , req .VolumeContentSource ), nil
537
+ }
538
+ }
539
+ return nil , status .Errorf (codes .InvalidArgument , "%v not a proper volume source" , volumeSource )
540
+ }
0 commit comments