Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check custom volume size is a multiple of 512 bytes #796

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions libvirt/helper/suppress/volumes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
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.
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)

return oldSize-newSize < qcow2SectorSize
}
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
Loading