summaryrefslogtreecommitdiff
path: root/libvirt
diff options
context:
space:
mode:
authorJ. Eduardo <j.eduardo@gmail.com>2017-07-31 22:16:24 +0200
committerFlavio Castelli <flavio@castelli.me>2017-08-01 09:11:49 +0200
commit968bf16201a9f13030f84765b027d7e45fdbef57 (patch)
tree2f581ce06f099681af4550c25593365428b06b21 /libvirt
parent78341e5971cac8f23e7c36321180a71ee6816131 (diff)
downloadterraform-provider-libvirt-968bf16201a9f13030f84765b027d7e45fdbef57.tar
terraform-provider-libvirt-968bf16201a9f13030f84765b027d7e45fdbef57.tar.gz
Added support for specifying NVRAM templates when using UEFI images.
Diffstat (limited to 'libvirt')
-rw-r--r--libvirt/resource_libvirt_domain.go29
-rw-r--r--libvirt/resource_libvirt_domain_test.go96
2 files changed, 115 insertions, 10 deletions
diff --git a/libvirt/resource_libvirt_domain.go b/libvirt/resource_libvirt_domain.go
index 66b6a122..844b6cf7 100644
--- a/libvirt/resource_libvirt_domain.go
+++ b/libvirt/resource_libvirt_domain.go
@@ -75,7 +75,7 @@ func resourceLibvirtDomain() *schema.Resource {
ForceNew: true,
},
"nvram": &schema.Schema{
- Type: schema.TypeString,
+ Type: schema.TypeMap,
Optional: true,
ForceNew: true,
},
@@ -244,21 +244,30 @@ func resourceLibvirtDomainCreate(d *schema.ResourceData, meta interface{}) error
if _, err := os.Stat(firmwareFile); os.IsNotExist(err) {
return fmt.Errorf("Could not find firmware file '%s'.", firmwareFile)
}
- domainDef.OS = &libvirtxml.DomainOS{
- Loader: &libvirtxml.DomainLoader{
- Path: firmwareFile,
- Readonly: "yes",
- Type: "pflash",
- },
+ domainDef.OS.Loader = &libvirtxml.DomainLoader{
+ Path: firmwareFile,
+ Readonly: "yes",
+ Type: "pflash",
+ Secure: "no",
}
if nvram, ok := d.GetOk("nvram"); ok {
- nvramFile := nvram.(string)
+ nvram_map := nvram.(map[string]interface{})
+
+ nvramFile := nvram_map["file"].(string)
if _, err := os.Stat(nvramFile); os.IsNotExist(err) {
return fmt.Errorf("Could not find nvram file '%s'.", nvramFile)
}
+ nvramTemplateFile := ""
+ if nvram_template, ok := nvram_map["template"]; ok {
+ nvramTemplateFile = nvram_template.(string)
+ if _, err := os.Stat(nvramTemplateFile); os.IsNotExist(err) {
+ return fmt.Errorf("Could not find nvram template file '%s'.", nvramTemplateFile)
+ }
+ }
domainDef.OS.NVRam = &libvirtxml.DomainNVRam{
- NVRam: nvramFile,
+ NVRam: nvramFile,
+ Template: nvramTemplateFile,
}
}
}
@@ -768,7 +777,7 @@ func resourceLibvirtDomainRead(d *schema.ResourceData, meta interface{}) error {
}
d.Set("name", domainDef.Name)
- d.Set("vpu", domainDef.VCPU)
+ d.Set("vcpu", domainDef.VCPU)
d.Set("memory", domainDef.Memory)
d.Set("firmware", domainDef.OS.Loader)
d.Set("nvram", domainDef.OS.NVRam)
diff --git a/libvirt/resource_libvirt_domain_test.go b/libvirt/resource_libvirt_domain_test.go
index 1410b639..e4d2f542 100644
--- a/libvirt/resource_libvirt_domain_test.go
+++ b/libvirt/resource_libvirt_domain_test.go
@@ -3,6 +3,7 @@ package libvirt
import (
"encoding/xml"
"fmt"
+ "io/ioutil"
"log"
"testing"
@@ -587,3 +588,98 @@ func testAccCheckLibvirtScsiDisk(n string, domain *libvirt.Domain) resource.Test
return nil
}
}
+
+func createNvramFile() (string, error) {
+ // size of an accepted, valid, nvram backing store
+ nvram_dummy_buffer := make([]byte, 131072)
+ file, err := ioutil.TempFile("/tmp", "nvram")
+ if err != nil {
+ return "", err
+ }
+ file.Chmod(0777)
+ _, err = file.Write(nvram_dummy_buffer)
+ if err != nil {
+ return "", err
+ }
+ if file.Close() != nil {
+ return "", err
+ }
+ return file.Name(), nil
+}
+
+func TestAccLibvirtDomain_FirmwareNoTemplate(t *testing.T) {
+ nvram_path, err := createNvramFile()
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ var domain libvirt.Domain
+ var config = fmt.Sprintf(`
+ resource "libvirt_domain" "acceptance-test-domain" {
+ name = "terraform-test-firmware-no-template"
+ firmware = "/usr/share/ovmf/OVMF.fd"
+ nvram {
+ file = "%s"
+ }
+ }`, nvram_path)
+
+ resource.Test(t, resource.TestCase{
+ PreCheck: func() { testAccPreCheck(t) },
+ Providers: testAccProviders,
+ CheckDestroy: testAccCheckLibvirtDomainDestroy,
+ Steps: []resource.TestStep{
+ resource.TestStep{
+ Config: config,
+ Check: resource.ComposeTestCheckFunc(
+ testAccCheckLibvirtDomainExists("libvirt_domain.acceptance-test-domain", &domain),
+ resource.TestCheckResourceAttr(
+ "libvirt_domain.acceptance-test-domain", "name", "terraform-test-firmware-no-template"),
+ resource.TestCheckResourceAttr(
+ "libvirt_domain.acceptance-test-domain", "nvram.file", nvram_path),
+ resource.TestCheckResourceAttr(
+ "libvirt_domain.acceptance-test-domain", "firmware", "/usr/share/ovmf/OVMF.fd"),
+ ),
+ },
+ },
+ })
+}
+
+func TestAccLibvirtDomain_FirmwareTemplate(t *testing.T) {
+ nvram_path, err := createNvramFile()
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ var domain libvirt.Domain
+ var config = fmt.Sprintf(`
+ resource "libvirt_domain" "acceptance-test-domain" {
+ name = "terraform-test-firmware-with-template"
+ firmware = "/usr/share/ovmf/OVMF.fd"
+ nvram {
+ file = "%s"
+ template = "/usr/share/qemu/OVMF.fd"
+ }
+ }`, nvram_path)
+
+ resource.Test(t, resource.TestCase{
+ PreCheck: func() { testAccPreCheck(t) },
+ Providers: testAccProviders,
+ CheckDestroy: testAccCheckLibvirtDomainDestroy,
+ Steps: []resource.TestStep{
+ resource.TestStep{
+ Config: config,
+ Check: resource.ComposeTestCheckFunc(
+ testAccCheckLibvirtDomainExists("libvirt_domain.acceptance-test-domain", &domain),
+ resource.TestCheckResourceAttr(
+ "libvirt_domain.acceptance-test-domain", "name", "terraform-test-firmware-with-template"),
+ resource.TestCheckResourceAttr(
+ "libvirt_domain.acceptance-test-domain", "nvram.file", nvram_path),
+ resource.TestCheckResourceAttr(
+ "libvirt_domain.acceptance-test-domain", "nvram.template", "/usr/share/qemu/OVMF.fd"),
+ resource.TestCheckResourceAttr(
+ "libvirt_domain.acceptance-test-domain", "firmware", "/usr/share/ovmf/OVMF.fd"),
+ ),
+ },
+ },
+ })
+}