From 9be42d2996fd6acd34b1bda8a4245d3aa7544135 Mon Sep 17 00:00:00 2001 From: Thomas Hipp Date: Fri, 1 Dec 2017 13:29:00 +0100 Subject: support local cdroms Local cdroms can now be added directly to the domain. See the following example: ```hcl resource "libvirt_domain" "test-domain" { name = "test" disk { file = "path/to/file.iso" } } ``` Furthermore, a small Tiny Core Linux (TCL) ISO image has been added to the testdata. This can be used for testing purposes. Signed-off-by: Thomas Hipp --- libvirt/disk_def.go | 15 --------------- libvirt/disk_def_test.go | 10 ---------- libvirt/resource_libvirt_domain.go | 33 ++++++++++++++++++++++++++++---- libvirt/resource_libvirt_domain_test.go | 15 +++++++++++---- libvirt/testdata/tcl.iso | Bin 0 -> 16834560 bytes 5 files changed, 40 insertions(+), 33 deletions(-) create mode 100644 libvirt/testdata/tcl.iso (limited to 'libvirt') diff --git a/libvirt/disk_def.go b/libvirt/disk_def.go index f4e4e86a..6e4cceea 100644 --- a/libvirt/disk_def.go +++ b/libvirt/disk_def.go @@ -24,21 +24,6 @@ func newDefDisk(i int) libvirtxml.DomainDisk { } } -func newCDROM() libvirtxml.DomainDisk { - return libvirtxml.DomainDisk{ - Type: "file", - Device: "cdrom", - Target: &libvirtxml.DomainDiskTarget{ - Dev: "hda", - Bus: "ide", - }, - Driver: &libvirtxml.DomainDiskDriver{ - Name: "qemu", - Type: "raw", - }, - } -} - func randomWWN(strlen int) string { const chars = "abcdef0123456789" result := make([]byte, strlen) diff --git a/libvirt/disk_def_test.go b/libvirt/disk_def_test.go index a6184cc6..d13d26d8 100644 --- a/libvirt/disk_def_test.go +++ b/libvirt/disk_def_test.go @@ -21,13 +21,3 @@ func TestDefaultDiskMarshall(t *testing.T) { t.Fatalf("could not marshall this:\n%s", spew.Sdump(b)) } } - -func TestDefaultCDROMMarshall(t *testing.T) { - b := newCDROM() - buf := new(bytes.Buffer) - enc := xml.NewEncoder(buf) - enc.Indent(" ", " ") - if err := enc.Encode(b); err != nil { - t.Fatalf("could not marshall this:\n%s", spew.Sdump(b)) - } -} diff --git a/libvirt/resource_libvirt_domain.go b/libvirt/resource_libvirt_domain.go index 95a7b7fe..95ac6c52 100644 --- a/libvirt/resource_libvirt_domain.go +++ b/libvirt/resource_libvirt_domain.go @@ -450,6 +450,24 @@ func resourceLibvirtDomainCreate(d *schema.ResourceData, meta interface{}) error if !strings.HasSuffix(url.Path, ".qcow2") { disk.Driver.Type = "raw" } + } else if _, ok := diskMap["file"].(string); ok { + // support for local disks, e.g. CDs + disk.Type = "file" + disk.Source = &libvirtxml.DomainDiskSource{ + File: diskMap["file"].(string), + } + + if strings.HasSuffix(diskMap["file"].(string), ".iso") { + disk.Device = "cdrom" + disk.Target = &libvirtxml.DomainDiskTarget{ + Dev: "hda", + Bus: "ide", + } + disk.Driver = &libvirtxml.DomainDiskDriver{ + Name: "qemu", + Type: "raw", + } + } } disks = append(disks, disk) @@ -899,7 +917,10 @@ func resourceLibvirtDomainRead(d *schema.ResourceData, meta interface{}) error { // Emulator is the same as the default don't set it in domainDef // or it will show as changed d.Set("emulator", domainDef.Devices.Emulator) - var disks []map[string]interface{} + var ( + disks []map[string]interface{} + disk map[string]interface{} + ) for _, diskDef := range domainDef.Devices.Disks { // network drives do not have a volume associated if diskDef.Type == "network" { @@ -914,10 +935,14 @@ func resourceLibvirtDomainRead(d *schema.ResourceData, meta interface{}) error { if err != nil { return err } - disk := map[string]interface{}{ + disk = map[string]interface{}{ "url": url.String(), } disks = append(disks, disk) + } else if diskDef.Device == "cdrom" { + disk = map[string]interface{}{ + "file": diskDef.Source.File, + } } else { var virVol *libvirt.StorageVol if len(diskDef.Source.File) > 0 { @@ -942,11 +967,11 @@ func resourceLibvirtDomainRead(d *schema.ResourceData, meta interface{}) error { return fmt.Errorf("Error retrieving volume for disk: %s", err) } - disk := map[string]interface{}{ + disk = map[string]interface{}{ "volume_id": virVolKey, } - disks = append(disks, disk) } + disks = append(disks, disk) } d.Set("disks", disks) var filesystems []map[string]interface{} diff --git a/libvirt/resource_libvirt_domain_test.go b/libvirt/resource_libvirt_domain_test.go index 47c99ef5..43ce2478 100644 --- a/libvirt/resource_libvirt_domain_test.go +++ b/libvirt/resource_libvirt_domain_test.go @@ -290,9 +290,15 @@ func TestAccLibvirtDomainKernelInitrdCmdline(t *testing.T) { func TestAccLibvirtDomain_NetworkInterface(t *testing.T) { var domain libvirt.Domain + currentDir, err := os.Getwd() + if err != nil { + t.Fatal("Unexpected error:", err) + } + var config = fmt.Sprintf(` - resource "libvirt_volume" "acceptance-test-volume" { - name = "terraform-test" + resource "libvirt_network" "acceptance-test-network" { + name = "terraform-test" + addresses = ["10.17.3.0/24"] } resource "libvirt_domain" "acceptance-test-domain" { @@ -303,11 +309,12 @@ func TestAccLibvirtDomain_NetworkInterface(t *testing.T) { network_interface = { network_name = "default" mac = "52:54:00:A9:F5:17" + wait_for_lease = 1 } disk { - volume_id = "${libvirt_volume.acceptance-test-volume.id}" + file = "%s/testdata/tcl.iso" } - }`) + }`, currentDir) resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, diff --git a/libvirt/testdata/tcl.iso b/libvirt/testdata/tcl.iso new file mode 100644 index 00000000..303088d0 Binary files /dev/null and b/libvirt/testdata/tcl.iso differ -- cgit v1.2.3