Skip to content

Commit c0e46b5

Browse files
authored
Merge pull request dmacvicar#593 from enool/wip/tcp_console
domain: telnet console support
2 parents f02e17b + d03a5f7 commit c0e46b5

File tree

5 files changed

+74
-16
lines changed

5 files changed

+74
-16
lines changed

examples/ubuntu/ubuntu-example.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ resource "libvirt_domain" "domain-ubuntu" {
5858
}
5959

6060
console {
61-
type = "pty"
61+
type = "tcp"
6262
target_type = "virtio"
6363
target_port = "1"
6464
}

libvirt/domain.go

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -390,19 +390,37 @@ func setConsoles(d *schema.ResourceData, domainDef *libvirtxml.Domain) {
390390
Port: &consoleTargetPort,
391391
}
392392
}
393-
if sourcePath, ok := d.GetOk(prefix + ".source_path"); ok {
394-
console.Source = &libvirtxml.DomainChardevSource{
395-
Dev: &libvirtxml.DomainChardevSourceDev{
396-
Path: sourcePath.(string),
397-
},
398-
}
399-
}
400393
if targetType, ok := d.GetOk(prefix + ".target_type"); ok {
401394
if console.Target == nil {
402395
console.Target = &libvirtxml.DomainConsoleTarget{}
403396
}
404397
console.Target.Type = targetType.(string)
405398
}
399+
switch d.Get(prefix + ".type").(string) {
400+
case "tcp":
401+
sourceHost := d.Get(prefix + ".source_host")
402+
sourceService := d.Get(prefix + ".source_service")
403+
console.Source = &libvirtxml.DomainChardevSource{
404+
TCP: &libvirtxml.DomainChardevSourceTCP{
405+
Mode: "bind",
406+
Host: sourceHost.(string),
407+
Service: sourceService.(string),
408+
},
409+
}
410+
console.Protocol = &libvirtxml.DomainChardevProtocol{
411+
Type: "telnet",
412+
}
413+
case "pty":
414+
fallthrough
415+
default:
416+
if sourcePath, ok := d.GetOk(prefix + ".source_path"); ok {
417+
console.Source = &libvirtxml.DomainChardevSource{
418+
Dev: &libvirtxml.DomainChardevSourceDev{
419+
Path: sourcePath.(string),
420+
},
421+
}
422+
}
423+
}
406424
domainDef.Devices.Consoles = append(domainDef.Devices.Consoles, console)
407425
}
408426
}

libvirt/resource_libvirt_domain.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,18 @@ func resourceLibvirtDomain() *schema.Resource {
277277
Optional: true,
278278
ForceNew: true,
279279
},
280+
"source_host": {
281+
Type: schema.TypeString,
282+
Optional: true,
283+
ForceNew: true,
284+
Default: "127.0.0.1",
285+
},
286+
"source_service": {
287+
Type: schema.TypeString,
288+
Optional: true,
289+
ForceNew: true,
290+
Default: "0",
291+
},
280292
"target_port": {
281293
Type: schema.TypeString,
282294
Required: true,

libvirt/resource_libvirt_domain_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -833,6 +833,13 @@ func TestAccLibvirtDomain_Consoles(t *testing.T) {
833833
target_type = "virtio"
834834
source_path = "/dev/pts/2"
835835
}
836+
console {
837+
type = "tcp"
838+
target_port = "0"
839+
target_type = "virtio"
840+
source_host = "127.0.1.1"
841+
source_service = "cisco-sccp"
842+
}
836843
}`, randomDomainName, randomDomainName)
837844

838845
resource.Test(t, resource.TestCase{
@@ -858,6 +865,16 @@ func TestAccLibvirtDomain_Consoles(t *testing.T) {
858865
"libvirt_domain."+randomDomainName, "console.1.target_type", "virtio"),
859866
resource.TestCheckResourceAttr(
860867
"libvirt_domain."+randomDomainName, "console.1.source_path", "/dev/pts/2"),
868+
resource.TestCheckResourceAttr(
869+
"libvirt_domain."+randomDomainName, "console.2.type", "tcp"),
870+
resource.TestCheckResourceAttr(
871+
"libvirt_domain."+randomDomainName, "console.2.target_port", "0"),
872+
resource.TestCheckResourceAttr(
873+
"libvirt_domain."+randomDomainName, "console.2.target_type", "virtio"),
874+
resource.TestCheckResourceAttr(
875+
"libvirt_domain."+randomDomainName, "console.2.source_host", "127.0.1.1"),
876+
resource.TestCheckResourceAttr(
877+
"libvirt_domain."+randomDomainName, "console.2.source_service", "cisco-sccp"),
861878
),
862879
},
863880
},

website/docs/r/domain.html.markdown

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -463,14 +463,25 @@ resource "libvirt_domain" "my_machine" {
463463
}
464464
```
465465

466-
~> **Note well:**
467-
<ul>
468-
<li>You can repeat the `console` block to create more than one console, in the
469-
same way that you can repeat `disk` blocks (see [above](#handling-disks)).</li>
470-
<li>The `target_type` is optional for the first console and defaults to `serial`.</li>
471-
<li>All subsequent `console` blocks must specify a `target_type` of `virtio`.</li>
472-
<li>The `source_path` is optional for all consoles.</li>
473-
</ul>
466+
Attributes:
467+
468+
* `type` - Console device type. Valid values are "pty" and "tcp".
469+
* `target_port` - Target port
470+
* `target_type` - (Optional) for the first console and defaults to `serial`.
471+
Subsequent `console` blocks must have a different type - usually `virtio`.
472+
473+
Additional attributes when type is "pty":
474+
475+
* `source_path` - (Optional) Source path
476+
477+
Additional attributes when type is "tcp":
478+
479+
* `source_host` - (Optional) IP address to listen on. Defaults to 127.0.0.1.
480+
* `source_service` - (Optional) Port number or a service name. Defaults to a
481+
random port.
482+
483+
Note that you can repeat the `console` block to create more than one console.
484+
This works the same way as with the `disk` blocks (see [above](#handling-disks)).
474485

475486
See [libvirt Domain XML Console element](https://libvirt.org/formatdomain.html#elementsConsole)
476487
for more information.

0 commit comments

Comments
 (0)