@@ -50,6 +50,7 @@ type GCECompute interface {
50
50
GetDiskSourceURI (volKey * meta.Key ) string
51
51
GetDiskTypeURI (volKey * meta.Key , diskType string ) string
52
52
WaitForAttach (ctx context.Context , volKey * meta.Key , instanceZone , instanceName string ) error
53
+ ResizeDisk (ctx context.Context , volKey * meta.Key , requestBytes int64 ) (int64 , error )
53
54
// Regional Disk Methods
54
55
GetReplicaZoneURI (zone string ) string
55
56
// Instance Methods
@@ -593,6 +594,65 @@ func (cloud *CloudProvider) CreateSnapshot(ctx context.Context, volKey *meta.Key
593
594
}
594
595
}
595
596
597
+ func (cloud * CloudProvider ) ResizeDisk (ctx context.Context , volKey * meta.Key , requestBytes int64 ) (int64 , error ) {
598
+ cloudDisk , err := cloud .GetDisk (ctx , volKey )
599
+ if err != nil {
600
+ return - 1 , fmt .Errorf ("failed to get disk: %v" , err )
601
+ }
602
+
603
+ sizeGb := cloudDisk .GetSizeGb ()
604
+ requestGb := common .BytesToGb (requestBytes )
605
+
606
+ // If disk is already of size equal or greater than requested size, we simply return
607
+ if sizeGb >= requestGb {
608
+ return requestBytes , nil
609
+ }
610
+
611
+ switch volKey .Type () {
612
+ case meta .Zonal :
613
+ return cloud .resizeZonalDisk (ctx , volKey , requestGb )
614
+ case meta .Regional :
615
+ return cloud .resizeRegionalDisk (ctx , volKey , requestGb )
616
+ default :
617
+ return - 1 , fmt .Errorf ("could not resize disk, key was neither zonal nor regional, instead got: %v" , volKey .String ())
618
+ }
619
+ }
620
+
621
+ func (cloud * CloudProvider ) resizeZonalDisk (ctx context.Context , volKey * meta.Key , requestGb int64 ) (int64 , error ) {
622
+ resizeReq := & compute.DisksResizeRequest {
623
+ SizeGb : requestGb ,
624
+ }
625
+ op , err := cloud .service .Disks .Resize (cloud .project , volKey .Zone , volKey .Name , resizeReq ).Context (ctx ).Do ()
626
+ if err != nil {
627
+ return - 1 , fmt .Errorf ("failed to resize zonal volume %v: %v" , volKey .String (), err )
628
+ }
629
+
630
+ err = cloud .waitForZonalOp (ctx , op , volKey .Zone )
631
+ if err != nil {
632
+ return - 1 , fmt .Errorf ("failed waiting for op for zonal resize for %s: %v" , volKey .String (), err )
633
+ }
634
+
635
+ return requestGb , nil
636
+ }
637
+
638
+ func (cloud * CloudProvider ) resizeRegionalDisk (ctx context.Context , volKey * meta.Key , requestGb int64 ) (int64 , error ) {
639
+ resizeReq := & computebeta.RegionDisksResizeRequest {
640
+ SizeGb : requestGb ,
641
+ }
642
+
643
+ op , err := cloud .betaService .RegionDisks .Resize (cloud .project , volKey .Region , volKey .Name , resizeReq ).Context (ctx ).Do ()
644
+ if err != nil {
645
+ return - 1 , fmt .Errorf ("failed to resize regional volume %v: %v" , volKey .String (), err )
646
+ }
647
+
648
+ err = cloud .waitForRegionalOp (ctx , op , volKey .Region )
649
+ if err != nil {
650
+ return - 1 , fmt .Errorf ("failed waiting for op for regional resize for %s: %v" , volKey .String (), err )
651
+ }
652
+
653
+ return requestGb , nil
654
+ }
655
+
596
656
func (cloud * CloudProvider ) createZonalDiskSnapshot (ctx context.Context , volKey * meta.Key , snapshotName string ) (* compute.Snapshot , error ) {
597
657
snapshotToCreate := & compute.Snapshot {
598
658
Name : snapshotName ,
0 commit comments