Skip to content

Commit

Permalink
Add none option to graphics
Browse files Browse the repository at this point in the history
  • Loading branch information
flx5 committed Dec 30, 2023
1 parent 8ce864f commit 401c36c
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 1 deletion.
8 changes: 7 additions & 1 deletion libvirt/domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,12 @@ func setGraphics(d *schema.ResourceData, domainDef *libvirtxml.Domain, arch stri
return fmt.Errorf("missing graphics type for domain")
}

// If graphics is explicitly disabled exit early.
if graphicsType == "none" {
domainDef.Devices.Graphics = nil
return nil
}

autoport := d.Get(prefix + ".autoport").(bool)
listener := libvirtxml.DomainGraphicListener{}

Expand Down Expand Up @@ -347,7 +353,7 @@ func setGraphics(d *schema.ResourceData, domainDef *libvirtxml.Domain, arch stri
domainDef.Devices.Graphics[0].VNC.WebSocket = websocket.(int)
}
default:
return fmt.Errorf("this provider only supports vnc/spice as graphics type. Provided: '%s'", graphicsType)
return fmt.Errorf("this provider only supports vnc/spice/none as graphics type. Provided: '%s'", graphicsType)
}
}
return nil
Expand Down
42 changes: 42 additions & 0 deletions libvirt/resource_libvirt_domain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -760,6 +760,25 @@ func TestAccLibvirtDomain_Graphics(t *testing.T) {
}
}`, randomPoolName, randomPoolName, randomPoolPath, randomVolumeName, randomVolumeName, randomPoolName, randomDomainName, randomDomainName)

var configNone = fmt.Sprintf(`
resource "libvirt_pool" "%s" {
name = "%s"
type = "dir"
path = "%s"
}
resource "libvirt_volume" "%s" {
name = "%s"
pool = "${libvirt_pool.%s.name}"
}
resource "libvirt_domain" "%s" {
name = "%s"
graphics {
type = "none"
}
}`, randomPoolName, randomPoolName, randomPoolPath, randomVolumeName, randomVolumeName, randomPoolName, randomDomainName, randomDomainName)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Expand All @@ -769,6 +788,7 @@ func TestAccLibvirtDomain_Graphics(t *testing.T) {
Config: config,
Check: resource.ComposeTestCheckFunc(
testAccCheckLibvirtDomainExists("libvirt_domain."+randomDomainName, &domain),
resource.TestCheckResourceAttr("libvirt_domain."+randomDomainName, "graphics.#", "1"),
resource.TestCheckResourceAttr(
"libvirt_domain."+randomDomainName, "graphics.0.type", "spice"),
resource.TestCheckResourceAttr(
Expand All @@ -789,6 +809,7 @@ func TestAccLibvirtDomain_Graphics(t *testing.T) {
Config: configListenAddress,
Check: resource.ComposeTestCheckFunc(
testAccCheckLibvirtDomainExists("libvirt_domain."+randomDomainName, &domain),
resource.TestCheckResourceAttr("libvirt_domain."+randomDomainName, "graphics.#", "1"),
resource.TestCheckResourceAttr(
"libvirt_domain."+randomDomainName, "graphics.0.type", "spice"),
resource.TestCheckResourceAttr(
Expand All @@ -801,6 +822,27 @@ func TestAccLibvirtDomain_Graphics(t *testing.T) {
},
},
})

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckLibvirtDomainDestroy,
Steps: []resource.TestStep{
{
Config: configNone,
Check: resource.ComposeTestCheckFunc(
testAccCheckLibvirtDomainExists("libvirt_domain."+randomDomainName, &domain),
resource.TestCheckResourceAttr("libvirt_domain."+randomDomainName, "graphics.#", "1"),
resource.TestCheckResourceAttr(
"libvirt_domain."+randomDomainName, "graphics.0.type", "none"),
resource.TestCheckResourceAttr(
"libvirt_domain."+randomDomainName, "graphics.0.autoport", "true"),
resource.TestCheckResourceAttr(
"libvirt_domain."+randomDomainName, "graphics.0.listen_type", "none"),
),
},
},
})
}

func TestAccLibvirtDomain_IgnitionObject(t *testing.T) {
Expand Down
3 changes: 3 additions & 0 deletions website/docs/r/domain.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,9 @@ resource "libvirt_domain" "my_machine" {
~> **Note well:** the `graphics` block is ignored for the architectures
`s390x` and `ppc64`.

To remove the graphics entirely set `type` to `none`.
Consider adding a serial console for output if you enable this option.
This can be useful for minimal server images printing only to the serial console.

### Console devices

Expand Down

0 comments on commit 401c36c

Please sign in to comment.