@@ -15,7 +15,9 @@ func resourceLibvirtVolume() *schema.Resource {
1515 return & schema.Resource {
1616 CreateContext : resourceLibvirtVolumeCreate ,
1717 ReadContext : resourceLibvirtVolumeRead ,
18+ UpdateContext : resourceLibvirtVolumeUpdate ,
1819 DeleteContext : resourceLibvirtVolumeDelete ,
20+ CustomizeDiff : resourceLibvirtVolumeCustomDiff ,
1921 Schema : map [string ]* schema.Schema {
2022 "name" : {
2123 Type : schema .TypeString ,
@@ -37,7 +39,6 @@ func resourceLibvirtVolume() *schema.Resource {
3739 Type : schema .TypeInt ,
3840 Optional : true ,
3941 Computed : true ,
40- ForceNew : true ,
4142 },
4243 "format" : {
4344 Type : schema .TypeString ,
@@ -359,9 +360,48 @@ func resourceLibvirtVolumeRead(ctx context.Context, d *schema.ResourceData, meta
359360 return nil
360361}
361362
362- // resourceLibvirtVolumeDelete removed a volume resource.
363+ // resourceLibvirtVolumeUpdate dinamically updates the size of a volume.
364+ // When the new size is less than the previous one the volume will be destroyed and recreated
365+ func resourceLibvirtVolumeUpdate (ctx context.Context , d * schema.ResourceData , meta interface {}) diag.Diagnostics {
366+ client := meta .(* Client )
367+
368+ if d .HasChange ("size" ) {
369+ old , new := d .GetChange ("size" )
370+ oldSize := old .(int )
371+ newSize := new .(int )
372+
373+ if newSize > oldSize {
374+ log .Printf ("[INFO] Resizing volume from %d to %d" , oldSize , newSize )
375+
376+ err := volumeResize (ctx , client , d .Id (), uint64 (oldSize ), uint64 (newSize ))
377+ if err != nil {
378+ return diag .FromErr (err )
379+ }
380+ d .Set ("size" , newSize )
381+ }
382+ }
383+
384+ return resourceLibvirtVolumeRead (ctx , d , meta )
385+ }
386+
387+ // resourceLibvirtVolumeDelete removes a volume resource.
363388func resourceLibvirtVolumeDelete (ctx context.Context , d * schema.ResourceData , meta interface {}) diag.Diagnostics {
364389 client := meta .(* Client )
365390
366391 return diag .FromErr (volumeDelete (ctx , client , d .Id ()))
367392}
393+
394+ // resourceLibvirtVolumeCustomDiff will notify the user that the volume needs to be recreated when the new size is less than the old one.
395+ // The volume will then be destroyed and recreated
396+ func resourceLibvirtVolumeCustomDiff (ctx context.Context , d * schema.ResourceDiff , meta interface {}) error {
397+ if d .HasChange ("size" ) {
398+ oldSize , newSize := d .GetChange ("size" )
399+
400+ if newSize .(int ) < oldSize .(int ) {
401+ log .Printf ("[DEBUG] new size < old size: the volume will be destroyed and recreated" )
402+ d .ForceNew ("size" )
403+ }
404+ }
405+
406+ return nil
407+ }
0 commit comments