Skip to content

Commit

Permalink
ignore changes on qcow2 size if it is just qemu-img round up to secto…
Browse files Browse the repository at this point in the history
…r size

Co-authored-by: Duncan Mac-Vicar P <[email protected]>
Closes: #741
  • Loading branch information
jamonation and dmacvicar committed Sep 15, 2024
1 parent 7d844fb commit 9b39ff0
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 0 deletions.
29 changes: 29 additions & 0 deletions libvirt/helper/suppress/volumes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package suppress

import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"strconv"
)

const qcow2SectorSize = 512

// Suppress the diff if the specified volume size is qemu-img round up to sector size

Check failure on line 10 in libvirt/helper/suppress/volumes.go

View workflow job for this annotation

GitHub Actions / Lint (1.21.x)

Comment should end in a period (godot)
func Qcow2SizeDiffSuppressFunc(_, oldSizeStr, newSizeStr string, k *schema.ResourceData) bool {
if format := k.Get("format"); format != "qcow2" {
return false
}

// On first apply these are nil strings, so there's always a diff
if oldSizeStr == "" {
return false
}

oldSize, _ := strconv.ParseUint(oldSizeStr, 10, 64)
newSize, _ := strconv.ParseUint(newSizeStr, 10, 64)

if oldSize-newSize < qcow2SectorSize {

Check failure on line 24 in libvirt/helper/suppress/volumes.go

View workflow job for this annotation

GitHub Actions / Lint (1.21.x)

S1008: should use 'return oldSize-newSize < qcow2SectorSize' instead of 'if oldSize-newSize < qcow2SectorSize { return true }; return false' (gosimple)
return true
}

return false
}
3 changes: 3 additions & 0 deletions libvirt/resource_libvirt_volume.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"

"github.com/dmacvicar/terraform-provider-libvirt/libvirt/helper/suppress"
)

func resourceLibvirtVolume() *schema.Resource {
Expand Down Expand Up @@ -38,6 +40,7 @@ func resourceLibvirtVolume() *schema.Resource {
Optional: true,
Computed: true,
ForceNew: true,
DiffSuppressFunc: suppress.Qcow2SizeDiffSuppressFunc,
},
"format": {
Type: schema.TypeString,
Expand Down
37 changes: 37 additions & 0 deletions libvirt/resource_libvirt_volume_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,43 @@ func TestAccLibvirtVolume_Basic(t *testing.T) {
})
}

func TestAccLibvirtVolume_SizeRound(t *testing.T) {
var volume libvirt.StorageVol
randomVolumeResource := acctest.RandStringFromCharSet(10, acctest.CharSetAlpha)
randomVolumeName := acctest.RandStringFromCharSet(10, acctest.CharSetAlpha)
randomPoolName := acctest.RandStringFromCharSet(10, acctest.CharSetAlpha)
randomPoolPath := "/tmp/terraform-provider-libvirt-pool-" + randomPoolName
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckLibvirtVolumeDestroy,
Steps: []resource.TestStep{
{
Config: fmt.Sprintf(`
resource "libvirt_pool" "%s" {
name = "%s"
type = "dir"
path = "%s"
}
resource "libvirt_volume" "%s" {
name = "%s"
size = 1073741823
pool = "${libvirt_pool.%s.name}"
}`, randomPoolName, randomPoolName, randomPoolPath, randomVolumeResource, randomVolumeName, randomPoolName),
Check: resource.ComposeTestCheckFunc(
testAccCheckLibvirtVolumeExists("libvirt_volume."+randomVolumeResource, &volume),
resource.TestCheckResourceAttr(
"libvirt_volume."+randomVolumeResource, "name", randomVolumeName),
resource.TestCheckResourceAttr(
"libvirt_volume."+randomVolumeResource, "size", "1073741824"),
),
},
},
})
}


func TestAccLibvirtVolume_BackingStoreTestByID(t *testing.T) {
var volume libvirt.StorageVol
random := acctest.RandStringFromCharSet(10, acctest.CharSetAlpha)
Expand Down

0 comments on commit 9b39ff0

Please sign in to comment.