From 3db390a4fbe0601af4f33f4c0f6a1c243f2119d5 Mon Sep 17 00:00:00 2001 From: Thomas Hipp Date: Sun, 30 Jul 2017 14:52:19 +0200 Subject: fix failing console port conversion When specifying a console port, it won't panic because of an invalid interface conversion. Signed-off-by: Thomas Hipp --- libvirt/resource_libvirt_domain.go | 36 ++++++++++++++----------- libvirt/resource_libvirt_domain_test.go | 48 +++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+), 15 deletions(-) (limited to 'libvirt') diff --git a/libvirt/resource_libvirt_domain.go b/libvirt/resource_libvirt_domain.go index a3ee7a2e..66b6a122 100644 --- a/libvirt/resource_libvirt_domain.go +++ b/libvirt/resource_libvirt_domain.go @@ -8,6 +8,7 @@ import ( "log" "net" "os" + "strconv" "strings" "time" @@ -270,27 +271,32 @@ func resourceLibvirtDomainCreate(d *schema.ResourceData, meta interface{}) error Value: d.Get("vcpu").(int), } - if consoleCount, ok := d.GetOk("console.#"); ok { - var consoles []libvirtxml.DomainConsole - for i := 0; i < consoleCount.(int); i++ { - console := libvirtxml.DomainConsole{} - consolePrefix := fmt.Sprintf("console.%d", i) - console.Type = d.Get(consolePrefix + ".type").(string) + var consoles []libvirtxml.DomainConsole + for i := 0; i < d.Get("console.#").(int); i++ { + console := libvirtxml.DomainConsole{} + consolePrefix := fmt.Sprintf("console.%d", i) + console.Type = d.Get(consolePrefix + ".type").(string) + consoleTargetPortInt, err := strconv.Atoi(d.Get(consolePrefix + ".target_port").(string)) + if err == nil { + consoleTargetPort := uint(consoleTargetPortInt) console.Target = &libvirtxml.DomainConsoleTarget{ - Port: d.Get(consolePrefix + ".target_port").(*uint), + Port: &consoleTargetPort, } - if source_path, ok := d.GetOk(consolePrefix + ".source_path"); ok { - console.Source = &libvirtxml.DomainChardevSource{ - Path: source_path.(string), - } + } + if source_path, ok := d.GetOk(consolePrefix + ".source_path"); ok { + console.Source = &libvirtxml.DomainChardevSource{ + Path: source_path.(string), } - if target_type, ok := d.GetOk(consolePrefix + ".target_type"); ok { - console.Target.Type = target_type.(string) + } + if target_type, ok := d.GetOk(consolePrefix + ".target_type"); ok { + if console.Target == nil { + console.Target = &libvirtxml.DomainConsoleTarget{} } - consoles = append(consoles, console) + console.Target.Type = target_type.(string) } - domainDef.Devices.Consoles = consoles + consoles = append(consoles, console) } + domainDef.Devices.Consoles = consoles disksCount := d.Get("disk.#").(int) var disks []libvirtxml.DomainDisk diff --git a/libvirt/resource_libvirt_domain_test.go b/libvirt/resource_libvirt_domain_test.go index a3c4b895..1410b639 100644 --- a/libvirt/resource_libvirt_domain_test.go +++ b/libvirt/resource_libvirt_domain_test.go @@ -420,6 +420,54 @@ func TestAccLibvirtDomain_Filesystems(t *testing.T) { }) } +func TestAccLibvirtDomain_Consoles(t *testing.T) { + var domain libvirt.Domain + + var config = fmt.Sprintf(` + resource "libvirt_domain" "acceptance-test-domain" { + name = "terraform-test" + console { + type = "pty" + target_port = "0" + source_path = "/dev/pts/1" + } + console { + type = "pty" + target_port = "0" + target_type = "virtio" + source_path = "/dev/pts/2" + } + }`) + + 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", "console.0.type", "pty"), + resource.TestCheckResourceAttr( + "libvirt_domain.acceptance-test-domain", "console.0.target_port", "0"), + resource.TestCheckResourceAttr( + "libvirt_domain.acceptance-test-domain", "console.0.source_path", "/dev/pts/1"), + resource.TestCheckResourceAttr( + "libvirt_domain.acceptance-test-domain", "console.1.type", "pty"), + resource.TestCheckResourceAttr( + "libvirt_domain.acceptance-test-domain", "console.1.target_port", "0"), + resource.TestCheckResourceAttr( + "libvirt_domain.acceptance-test-domain", "console.1.target_type", "virtio"), + resource.TestCheckResourceAttr( + "libvirt_domain.acceptance-test-domain", "console.1.source_path", "/dev/pts/2"), + ), + }, + }, + }) +} + func testAccCheckLibvirtDomainDestroy(s *terraform.State) error { virtConn := testAccProvider.Meta().(*Client).libvirt -- cgit v1.2.3