Skip to content

Commit bf71a85

Browse files
jamonationdmacvicar
andcommitted
ignore changes on qcow2 size if it is just qemu-img round up to sector size
Co-authored-by: Duncan Mac-Vicar P <[email protected]> Closes: #741
1 parent 7d844fb commit bf71a85

File tree

3 files changed

+69
-0
lines changed

3 files changed

+69
-0
lines changed

libvirt/helper/suppress/volumes.go

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package suppress
2+
3+
import (
4+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
5+
"strconv"
6+
)
7+
8+
const qcow2SectorSize = 512
9+
10+
// Suppress the diff if the specified volume size is qemu-img round up to sector size
11+
func Qcow2SizeDiffSuppressFunc(_, oldSizeStr, newSizeStr string, k *schema.ResourceData) bool {
12+
if format := k.Get("format"); format != "qcow2" {
13+
return false
14+
}
15+
16+
// On first apply these are nil strings, so there's always a diff
17+
if oldSizeStr == "" {
18+
return false
19+
}
20+
21+
oldSize, _ := strconv.ParseUint(oldSizeStr, 10, 64)
22+
newSize, _ := strconv.ParseUint(newSizeStr, 10, 64)
23+
24+
if oldSize-newSize < 512 {
25+
return true
26+
}
27+
28+
return false
29+
}

libvirt/resource_libvirt_volume.go

+3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import (
99
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
1010
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
1111
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
12+
13+
"github.com/dmacvicar/terraform-provider-libvirt/libvirt/helper/suppress"
1214
)
1315

1416
func resourceLibvirtVolume() *schema.Resource {
@@ -38,6 +40,7 @@ func resourceLibvirtVolume() *schema.Resource {
3840
Optional: true,
3941
Computed: true,
4042
ForceNew: true,
43+
DiffSuppressFunc: suppress.Qcow2SizeDiffSuppressFunc,
4144
},
4245
"format": {
4346
Type: schema.TypeString,

libvirt/resource_libvirt_volume_test.go

+37
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,43 @@ func TestAccLibvirtVolume_Basic(t *testing.T) {
125125
})
126126
}
127127

128+
func TestAccLibvirtVolume_SizeRound(t *testing.T) {
129+
var volume libvirt.StorageVol
130+
randomVolumeResource := acctest.RandStringFromCharSet(10, acctest.CharSetAlpha)
131+
randomVolumeName := acctest.RandStringFromCharSet(10, acctest.CharSetAlpha)
132+
randomPoolName := acctest.RandStringFromCharSet(10, acctest.CharSetAlpha)
133+
randomPoolPath := "/tmp/terraform-provider-libvirt-pool-" + randomPoolName
134+
resource.Test(t, resource.TestCase{
135+
PreCheck: func() { testAccPreCheck(t) },
136+
Providers: testAccProviders,
137+
CheckDestroy: testAccCheckLibvirtVolumeDestroy,
138+
Steps: []resource.TestStep{
139+
{
140+
Config: fmt.Sprintf(`
141+
resource "libvirt_pool" "%s" {
142+
name = "%s"
143+
type = "dir"
144+
path = "%s"
145+
}
146+
147+
resource "libvirt_volume" "%s" {
148+
name = "%s"
149+
size = 1073741823
150+
pool = "${libvirt_pool.%s.name}"
151+
}`, randomPoolName, randomPoolName, randomPoolPath, randomVolumeResource, randomVolumeName, randomPoolName),
152+
Check: resource.ComposeTestCheckFunc(
153+
testAccCheckLibvirtVolumeExists("libvirt_volume."+randomVolumeResource, &volume),
154+
resource.TestCheckResourceAttr(
155+
"libvirt_volume."+randomVolumeResource, "name", randomVolumeName),
156+
resource.TestCheckResourceAttr(
157+
"libvirt_volume."+randomVolumeResource, "size", "1073741824"),
158+
),
159+
},
160+
},
161+
})
162+
}
163+
164+
128165
func TestAccLibvirtVolume_BackingStoreTestByID(t *testing.T) {
129166
var volume libvirt.StorageVol
130167
random := acctest.RandStringFromCharSet(10, acctest.CharSetAlpha)

0 commit comments

Comments
 (0)