aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJ. Eduardo <j.eduardo@gmail.com>2017-06-19 23:35:07 +0200
committerFlavio Castelli <flavio@castelli.me>2017-07-02 22:16:19 +0200
commit65bc2689d50f3238a052389e30d81730ad2c601d (patch)
treeb20bfd743f0ad9d42cfe27de52efd6eb9b6d2aba
parent8b8c66e3bd53aeab3806179aefaec2b453dbd4ad (diff)
downloadterraform-provider-libvirt-65bc2689d50f3238a052389e30d81730ad2c601d.tar
terraform-provider-libvirt-65bc2689d50f3238a052389e30d81730ad2c601d.tar.gz
Added support for using raw volumes and an example using the functionality.
-rw-r--r--examples/format/libvirt.tf72
-rw-r--r--libvirt/resource_libvirt_volume.go23
-rw-r--r--libvirt/resource_libvirt_volume_test.go59
3 files changed, 153 insertions, 1 deletions
diff --git a/examples/format/libvirt.tf b/examples/format/libvirt.tf
new file mode 100644
index 00000000..1a0cc0e8
--- /dev/null
+++ b/examples/format/libvirt.tf
@@ -0,0 +1,72 @@
+provider "libvirt" {
+ uri = "qemu:///system"
+}
+
+resource "libvirt_network" "tf" {
+ name = "tf"
+ domain = "tf.local"
+ mode = "nat"
+ addresses = ["10.0.100.0/24"]
+}
+
+# raw image from file
+resource "libvirt_volume" "debian8-raw" {
+ name = "debian8-raw"
+ format = "raw"
+ source = "http://localhost:8000/debian8.img"
+}
+
+# qcow2 image from file
+resource "libvirt_volume" "debian8-qcow2" {
+ name = "debian8-qcow2"
+ source = "http://localhost:8000/debian8.qcow2"
+}
+
+# volume with raw backing storage
+resource "libvirt_volume" "vol-debian8-raw" {
+ name = "vol-debian8-raw"
+ base_volume_id = "${libvirt_volume.debian8-raw.id}"
+ base_volume_format = "raw"
+}
+
+# volume with qcow2 backing storage
+resource "libvirt_volume" "vol-debian8-qcow2" {
+ name = "vol-debian8-qcow2"
+ base_volume_id = "${libvirt_volume.debian8-qcow2.id}"
+}
+
+# domain using raw-backed volume
+resource "libvirt_domain" "domain-debian8-raw" {
+ name = "domain-debian8-raw"
+ memory = "256"
+ vcpu = 1
+ network_interface {
+ network_name = "tf"
+ }
+ disk {
+ volume_id = "${libvirt_volume.vol-debian8-raw.id}"
+ }
+ graphics {
+ type = "spice"
+ listen_type = "address"
+ autoport = true
+ }
+}
+
+# domain using qcow2-backed volume
+resource "libvirt_domain" "domain-debian8-qcow2" {
+ name = "domain-debian8-qcow2"
+ memory = "256"
+ vcpu = 1
+ network_interface {
+ network_name = "tf"
+ }
+ disk {
+ volume_id = "${libvirt_volume.vol-debian8-qcow2.id}"
+ }
+ graphics {
+ type = "spice"
+ listen_type = "address"
+ autoport = true
+ }
+}
diff --git a/libvirt/resource_libvirt_volume.go b/libvirt/resource_libvirt_volume.go
index 5e20c079..a25d157d 100644
--- a/libvirt/resource_libvirt_volume.go
+++ b/libvirt/resource_libvirt_volume.go
@@ -35,6 +35,11 @@ func volumeCommonSchema() map[string]*schema.Schema {
Computed: true,
ForceNew: true,
},
+ "format": &schema.Schema{
+ Type: schema.TypeString,
+ Optional: true,
+ ForceNew: true,
+ },
"base_volume_id": &schema.Schema{
Type: schema.TypeString,
Optional: true,
@@ -50,6 +55,11 @@ func volumeCommonSchema() map[string]*schema.Schema {
Optional: true,
ForceNew: true,
},
+ "base_volume_format": &schema.Schema{
+ Type: schema.TypeString,
+ Optional: true,
+ ForceNew: true,
+ },
}
}
@@ -106,6 +116,12 @@ func resourceLibvirtVolumeCreate(d *schema.ResourceData, meta interface{}) error
volumeDef.Name = name.(string)
}
+ volumeFormat := "qcow2"
+ if _, ok := d.GetOk("format"); ok {
+ volumeFormat = d.Get("format").(string)
+ }
+ volumeDef.Target.Format.Type = volumeFormat
+
var (
img image
volume *libvirt.StorageVol = nil
@@ -171,7 +187,12 @@ func resourceLibvirtVolumeCreate(d *schema.ResourceData, meta interface{}) error
volume = nil
volumeDef.BackingStore = new(defBackingStore)
- volumeDef.BackingStore.Format.Type = "qcow2"
+
+ baseVolumeFormat := "qcow2"
+ if _, ok := d.GetOk("base_volume_format"); ok {
+ baseVolumeFormat = d.Get("base_volume_format").(string)
+ }
+ volumeDef.BackingStore.Format.Type = baseVolumeFormat
baseVolume, err := virConn.LookupStorageVolByKey(baseVolumeId.(string))
if err != nil {
return fmt.Errorf("Can't retrieve volume %s", baseVolumeId.(string))
diff --git a/libvirt/resource_libvirt_volume_test.go b/libvirt/resource_libvirt_volume_test.go
index 99fc9608..b76fd848 100644
--- a/libvirt/resource_libvirt_volume_test.go
+++ b/libvirt/resource_libvirt_volume_test.go
@@ -147,3 +147,62 @@ func TestAccLibvirtVolume_DownloadFromSource(t *testing.T) {
},
})
}
+
+func TestAccLibvirtVolume_Format(t *testing.T) {
+ var volume libvirt.StorageVol
+
+ const testAccCheckLibvirtVolumeConfig_format = `
+ resource "libvirt_volume" "terraform-acceptance-test-3" {
+ name = "terraform-test"
+ format = "raw"
+ size = 1073741824
+ }`
+
+ resource.Test(t, resource.TestCase{
+ PreCheck: func() { testAccPreCheck(t) },
+ Providers: testAccProviders,
+ CheckDestroy: testAccCheckLibvirtVolumeDestroy,
+ Steps: []resource.TestStep{
+ resource.TestStep{
+ Config: testAccCheckLibvirtVolumeConfig_format,
+ Check: resource.ComposeTestCheckFunc(
+ testAccCheckLibvirtVolumeExists("libvirt_volume.terraform-acceptance-test-3", &volume),
+ resource.TestCheckResourceAttr(
+ "libvirt_volume.terraform-acceptance-test-3", "name", "terraform-test"),
+ resource.TestCheckResourceAttr(
+ "libvirt_volume.terraform-acceptance-test-3", "size", "1073741824"),
+ resource.TestCheckResourceAttr(
+ "libvirt_volume.terraform-acceptance-test-3", "format", "raw"),
+ ),
+ },
+ },
+ })
+}
+
+func TestAccLibvirtVolume_BaseVolumeFormat(t *testing.T) {
+ var volume libvirt.StorageVol
+
+ const testAccCheckLibvirtVolumeConfig_format = `
+ resource "libvirt_volume" "terraform-acceptance-test-4" {
+ name = "terraform-test"
+ base_volume_format = "raw"
+ }`
+
+ resource.Test(t, resource.TestCase{
+ PreCheck: func() { testAccPreCheck(t) },
+ Providers: testAccProviders,
+ CheckDestroy: testAccCheckLibvirtVolumeDestroy,
+ Steps: []resource.TestStep{
+ resource.TestStep{
+ Config: testAccCheckLibvirtVolumeConfig_format,
+ Check: resource.ComposeTestCheckFunc(
+ testAccCheckLibvirtVolumeExists("libvirt_volume.terraform-acceptance-test-4", &volume),
+ resource.TestCheckResourceAttr(
+ "libvirt_volume.terraform-acceptance-test-4", "name", "terraform-test"),
+ resource.TestCheckResourceAttr(
+ "libvirt_volume.terraform-acceptance-test-4", "base_volume_format", "raw"),
+ ),
+ },
+ },
+ })
+}