summaryrefslogtreecommitdiff
path: root/libvirt/domain_def.go
blob: 56424722c1b2728770b2662c23817d30d6115cd7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package libvirt

import (
	"encoding/xml"
	"os"
)

type defDomain struct {
	XMLName  xml.Name  `xml:"domain"`
	Name     string    `xml:"name"`
	Type     string    `xml:"type,attr"`
	Xmlns    string    `xml:"xmlns:qemu,attr"`
	Os       defOs     `xml:"os"`
	Memory   defMemory `xml:"memory"`
	VCpu     defVCpu   `xml:"vcpu"`
	Metadata struct {
		XMLName          xml.Name `xml:"metadata"`
		TerraformLibvirt defMetadata
	}
	Features struct {
		Acpi string `xml:"acpi"`
		Apic string `xml:"apic"`
		Pae  string `xml:"pae"`
	} `xml:"features"`
	Devices struct {
		Controller        []defController       `xml:"controller,omitempty"`
		Disks             []defDisk             `xml:"disk"`
		NetworkInterfaces []defNetworkInterface `xml:"interface"`
		Console           []defConsole          `xml:"console"`
		Graphics          *defGraphics          `xml:"graphics,omitempty"`
		// QEMU guest agent channel
		QemuGAChannel struct {
			Type   string `xml:"type,attr"`
			Source struct {
				Mode string `xml:"mode,attr"`
			} `xml:"source"`
			Target struct {
				Type string `xml:"type,attr"`
				Name string `xml:"name,attr"`
			} `xml:"target"`
		} `xml:"channel"`
		Rng struct {
			Model   string `xml:"model,attr"`
			Backend struct {
				Model string `xml:"model,attr"`
			} `xml:"backend"`
		} `xml:"rng"`
	} `xml:"devices"`
	CmdLine struct {
		XMLName xml.Name `xml:"qemu:commandline"`
		Cmd     []defCmd `xml:"qemu:arg"`
	}
}

type defGraphics struct {
	Type     string `xml:"type,attr"`
	Autoport string `xml:"autoport,attr"`
	Listen   struct {
		Type string `xml:"type,attr"`
	} `xml:"listen"`
}

type defMetadata struct {
	XMLName xml.Name `xml:"http://github.com/dmacvicar/terraform-provider-libvirt/ user_data"`
	Xml     string   `xml:",cdata"`
}

type defOs struct {
	Type   defOsType  `xml:"type"`
	Loader *defLoader `xml:"loader,omitempty"`
	NvRam  *defNvRam  `xml:"nvram,omitempty"`
}

type defOsType struct {
	Arch    string `xml:"arch,attr,omitempty"`
	Machine string `xml:"machine,attr,omitempty"`
	Name    string `xml:",chardata"`
}

type defMemory struct {
	Unit   string `xml:"unit,attr"`
	Amount int    `xml:",chardata"`
}

type defVCpu struct {
	Placement string `xml:"unit,attr"`
	Amount    int    `xml:",chardata"`
}

type defCmd struct {
	Value string `xml:"value,attr"`
}

type defLoader struct {
	ReadOnly string `xml:"readonly,attr,omitempty"`
	Type     string `xml:"type,attr,omitempty"`
	File     string `xml:",chardata"`
}

// <nvram>/var/lib/libvirt/qemu/nvram/sled12sp1_VARS.fd</nvram>
type defNvRam struct {
	File string `xml:",chardata"`
}

type defController struct {
	Type  string `xml:"type,attr,omitempty"`
	Model string `xml:"model,attr,omitempty"`
}

type defConsole struct {
	Type   string `xml:"type,attr"`
	Source struct {
		Path string `xml:"path,attr,omitempty"`
	} `xml:"source"`
	Target struct {
		Type string `xml:"type,attr,omitempty"`
		Port string `xml:"port,attr"`
	} `xml:"target"`
}

// Creates a domain definition with the defaults
// the provider uses
func newDomainDef() defDomain {
	// libvirt domain definition
	domainDef := defDomain{}
	if v := os.Getenv("TERRAFORM_LIBVIRT_TEST_DOMAIN_TYPE"); v != "" {
		domainDef.Type = v
	} else {
		domainDef.Type = "kvm"
	}
	domainDef.Xmlns = ""

	domainDef.Os = defOs{}
	domainDef.Os.Type = defOsType{}
	domainDef.Os.Type.Name = "hvm"

	domainDef.Memory = defMemory{}
	domainDef.Memory.Unit = "MiB"
	domainDef.Memory.Amount = 512

	domainDef.VCpu = defVCpu{}
	domainDef.VCpu.Placement = "static"
	domainDef.VCpu.Amount = 1

	domainDef.Devices.Graphics = &defGraphics{}
	domainDef.Devices.Graphics.Type = "spice"
	domainDef.Devices.Graphics.Autoport = "yes"
	domainDef.Devices.Graphics.Listen.Type = "none"

	domainDef.Devices.QemuGAChannel.Type = "unix"
	domainDef.Devices.QemuGAChannel.Source.Mode = "bind"
	domainDef.Devices.QemuGAChannel.Target.Type = "virtio"
	domainDef.Devices.QemuGAChannel.Target.Name = "org.qemu.guest_agent.0"

	domainDef.Devices.Rng.Model = "virtio"
	domainDef.Devices.Rng.Backend.Model = "random"

	return domainDef
}