diff options
author | Thomas Hipp <thomashipp@gmail.com> | 2017-12-28 09:11:41 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-12-28 09:11:41 +0100 |
commit | 33733cbc2c0d6664e5c1d33b26cd5c09efe687c6 (patch) | |
tree | e28f761f91b40ebd84b882b7cb1c6d35d5e14794 | |
parent | 7b2adfc1f28d76399d63aaf50ff914268efbb87a (diff) | |
parent | fdb9486330b90d2f75d39b33a0c5abf5ccf5b4ea (diff) | |
download | terraform-provider-libvirt-33733cbc2c0d6664e5c1d33b26cd5c09efe687c6.tar terraform-provider-libvirt-33733cbc2c0d6664e5c1d33b26cd5c09efe687c6.tar.gz |
Merge pull request #264 from monstermunchkin/issues/262
domain: honor arch, machine, and emulator settings
-rw-r--r-- | libvirt/domain_def.go | 33 | ||||
-rw-r--r-- | libvirt/resource_libvirt_domain.go | 18 |
2 files changed, 34 insertions, 17 deletions
diff --git a/libvirt/domain_def.go b/libvirt/domain_def.go index 5f443a62..dc74c608 100644 --- a/libvirt/domain_def.go +++ b/libvirt/domain_def.go @@ -1,9 +1,11 @@ package libvirt import ( + "os" + + "github.com/hashicorp/terraform/helper/schema" libvirt "github.com/libvirt/libvirt-go" libvirtxml "github.com/libvirt/libvirt-go-xml" - "os" ) func newFilesystemDef() libvirtxml.DomainFilesystem { @@ -20,9 +22,7 @@ func newDomainDef() libvirtxml.Domain { domainDef := libvirtxml.Domain{ OS: &libvirtxml.DomainOS{ Type: &libvirtxml.DomainOSType{ - Type: "hvm", - Arch: "x86_64", - Machine: "pc", + Type: "hvm", }, }, Memory: &libvirtxml.DomainMemory{ @@ -75,8 +75,19 @@ func newDomainDef() libvirtxml.Domain { return domainDef } -func newDomainDefForConnection(virConn *libvirt.Connect) (libvirtxml.Domain, error) { +func newDomainDefForConnection(virConn *libvirt.Connect, rd *schema.ResourceData) (libvirtxml.Domain, error) { d := newDomainDef() + + if arch, ok := rd.GetOk("arch"); ok { + d.OS.Type.Arch = arch.(string) + } else { + arch, err := getHostArchitecture(virConn) + if err != nil { + return d, err + } + d.OS.Type.Arch = arch + } + caps, err := getHostCapabilities(virConn) if err != nil { return d, err @@ -86,7 +97,17 @@ func newDomainDefForConnection(virConn *libvirt.Connect) (libvirtxml.Domain, err return d, err } - d.Devices.Emulator = guest.Arch.Emulator + if emulator, ok := rd.GetOk("emulator"); ok { + d.Devices.Emulator = emulator.(string) + } else { + d.Devices.Emulator = guest.Arch.Emulator + } + + if machine, ok := rd.GetOk("machine"); ok { + d.OS.Type.Machine = machine.(string) + } else if len(guest.Arch.Machines) > 0 { + d.OS.Type.Machine = guest.Arch.Machines[0].Name + } canonicalmachine, err := getCanonicalMachineName(caps, d.OS.Type.Arch, d.OS.Type.Type, d.OS.Type.Machine) if err != nil { diff --git a/libvirt/resource_libvirt_domain.go b/libvirt/resource_libvirt_domain.go index 25516f7b..404fcb84 100644 --- a/libvirt/resource_libvirt_domain.go +++ b/libvirt/resource_libvirt_domain.go @@ -144,12 +144,12 @@ func resourceLibvirtDomain() *schema.Resource { "machine": { Type: schema.TypeString, Optional: true, - Default: "pc", + Computed: true, }, "arch": { Type: schema.TypeString, Optional: true, - Default: "x86_64", + Computed: true, }, "boot_device": { Type: schema.TypeList, @@ -161,8 +161,8 @@ func resourceLibvirtDomain() *schema.Resource { }, "emulator": { Type: schema.TypeString, - Default: "/usr/bin/qemu-system-x86_64", Optional: true, + Computed: true, }, "kernel": { Type: schema.TypeString, @@ -230,10 +230,11 @@ func resourceLibvirtDomainCreate(d *schema.ResourceData, meta interface{}) error return fmt.Errorf(LibVirtConIsNil) } - domainDef, err := newDomainDefForConnection(virConn) + domainDef, err := newDomainDefForConnection(virConn, d) if err != nil { return err } + if name, ok := d.GetOk("name"); ok { domainDef.Name = name.(string) } @@ -307,10 +308,6 @@ func resourceLibvirtDomainCreate(d *schema.ResourceData, meta interface{}) error } } - domainDef.OS.Type.Arch = d.Get("arch").(string) - domainDef.OS.Type.Machine = d.Get("machine").(string) - domainDef.Devices.Emulator = d.Get("emulator").(string) - if firmware, ok := d.GetOk("firmware"); ok { firmwareFile := firmware.(string) if _, err := os.Stat(firmwareFile); os.IsNotExist(err) { @@ -871,7 +868,7 @@ func resourceLibvirtDomainRead(d *schema.ResourceData, meta interface{}) error { log.Printf("[DEBUG] read: obtained XML desc for domain:\n%s", xmlDesc) - domainDef, err := newDomainDefForConnection(virConn) + domainDef, err := newDomainDefForConnection(virConn, d) if err != nil { return err } @@ -894,7 +891,6 @@ func resourceLibvirtDomainRead(d *schema.ResourceData, meta interface{}) error { d.Set("cpu", domainDef.CPU) d.Set("arch", domainDef.OS.Type.Arch) d.Set("autostart", autostart) - d.Set("arch", domainDef.OS.Type.Arch) cmdLines, err := splitKernelCmdLine(domainDef.OS.KernelArgs) if err != nil { @@ -1116,7 +1112,7 @@ func resourceLibvirtDomainDelete(d *schema.ResourceData, meta interface{}) error return fmt.Errorf("Error retrieving libvirt domain XML description: %s", err) } - domainDef, err := newDomainDefForConnection(virConn) + domainDef, err := newDomainDefForConnection(virConn, d) if err != nil { return err } |