-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnode.go
309 lines (289 loc) · 14.4 KB
/
node.go
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
package gogns3
import "encoding/json"
// Label of a node or link
type Label struct {
Rotation int `json:"rotation,omitempty"`
Style string `json:"style,omitempty"`
Text string `json:"text"`
X int `json:"x"`
Y int `json:"y"`
}
// NodeProperties are specific to an emulator. For some emulators, some fields
// are required, some are forbidden and it depends on the node type. I can't see
// a neater solution than duplicating the type in order to specify which field
// must be exported and which field must not. This way, we only have a
// marshalling problem with some custom MarshalJSON() functions, but nothing
// special to do when unmarshalling. Note than the omitempty keyword is useless
// with bool type as missing value does not mean false for the GNS3 server API.
type NodeProperties struct {
nodeType string
AdapterType string `json:"adapter_type"`
Adapters int `json:"adapters"`
BiosImage string `json:"bios_image"`
BiosImageMd5sum string `json:"bios_image_md5sum"`
BootPriority string `json:"boot_priority"`
CdromImage string `json:"cdrom_image"`
CdromImageMd5sum string `json:"cdrom_image_md5sum"`
CPUThrottling int `json:"cpu_throttling"`
CPUs int `json:"cpus"`
HdaDiskImage string `json:"hda_disk_image"`
HdaDiskImageMd5sum string `json:"hda_disk_image_md5sum"`
HdaDiskInterface string `json:"hda_disk_interface"`
HdbDiskImage string `json:"hdb_disk_image"`
HdbDiskImageMd5sum string `json:"hdb_disk_image_md5sum"`
HdbDiskInterface string `json:"hdb_disk_interface"`
HdcDiskImage string `json:"hdc_disk_image"`
HdcDiskImageMd5sum string `json:"hdc_disk_image_md5sum"`
HdcDiskInterface string `json:"hdc_disk_interface"`
HddDiskImage string `json:"hdd_disk_image"`
HddDiskImageMd5sum string `json:"hdd_disk_image_md5sum"`
HddDiskInterface string `json:"hdd_disk_interface"`
Initrd string `json:"initrd"`
InitrdMd5sum string `json:"initrd_md5sum"`
KernelCommandLine string `json:"kernel_command_line"`
KernelImage string `json:"kernel_image"`
KernelImageMd5sum string `json:"kernel_image_md5sum"`
LegacyNetworking bool `json:"legacy_networking"`
MacAddress string `json:"mac_address"`
Options string `json:"options"`
PortsMapping []NodeEthernetPortsMapping `json:"ports_mapping"`
Platform string `json:"platform"`
ProcessPriority string `json:"process_priority"`
QemuPath string `json:"qemu_path"`
RAM int `json:"ram"`
Usage string `json:"usage"`
}
type nodeEthernetSwitchProperties struct {
nodeType string
AdapterType string `json:"-"`
Adapters int `json:"-"`
BiosImage string `json:"-"`
BiosImageMd5sum string `json:"-"`
BootPriority string `json:"-"`
CdromImage string `json:"-"`
CdromImageMd5sum string `json:"-"`
CPUThrottling int `json:"-"`
CPUs int `json:"-"`
HdaDiskImage string `json:"-"`
HdaDiskImageMd5sum string `json:"-"`
HdaDiskInterface string `json:"-"`
HdbDiskImage string `json:"-"`
HdbDiskImageMd5sum string `json:"-"`
HdbDiskInterface string `json:"-"`
HdcDiskImage string `json:"-"`
HdcDiskImageMd5sum string `json:"-"`
HdcDiskInterface string `json:"-"`
HddDiskImage string `json:"-"`
HddDiskImageMd5sum string `json:"-"`
HddDiskInterface string `json:"-"`
Initrd string `json:"-"`
InitrdMd5sum string `json:"-"`
KernelCommandLine string `json:"-"`
KernelImage string `json:"-"`
KernelImageMd5sum string `json:"-"`
LegacyNetworking bool `json:"-"`
MacAddress string `json:"-"`
Options string `json:"-"`
PortsMapping []NodeEthernetPortsMapping `json:"ports_mapping,omitempty"`
Platform string `json:"-"`
ProcessPriority string `json:"-"`
QemuPath string `json:"-"`
RAM int `json:"-"`
Usage string `json:"-"`
}
type nodeQemuProperties struct {
nodeType string
AdapterType string `json:"adapter_type,omitempty"`
Adapters int `json:"adapters,omitempty"`
BiosImage string `json:"bios_image,omitempty"`
BiosImageMd5sum string `json:"bios_image_md5sum,omitempty"`
BootPriority string `json:"boot_priority,omitempty"`
CdromImage string `json:"cdrom_image,omitempty"`
CdromImageMd5sum string `json:"cdrom_image_md5sum,omitempty"`
CPUThrottling int `json:"cpu_throttling,omitempty"`
CPUs int `json:"cpus,omitempty"`
HdaDiskImage string `json:"hda_disk_image,omitempty"`
HdaDiskImageMd5sum string `json:"hda_disk_image_md5sum,omitempty"`
HdaDiskInterface string `json:"hda_disk_interface,omitempty"`
HdbDiskImage string `json:"hdb_disk_image,omitempty"`
HdbDiskImageMd5sum string `json:"hdb_disk_image_md5sum,omitempty"`
HdbDiskInterface string `json:"hdb_disk_interface,omitempty"`
HdcDiskImage string `json:"hdc_disk_image,omitempty"`
HdcDiskImageMd5sum string `json:"hdc_disk_image_md5sum,omitempty"`
HdcDiskInterface string `json:"hdc_disk_interface,omitempty"`
HddDiskImage string `json:"hdd_disk_image,omitempty"`
HddDiskImageMd5sum string `json:"hdd_disk_image_md5sum,omitempty"`
HddDiskInterface string `json:"hdd_disk_interface,omitempty"`
Initrd string `json:"initrd,omitempty"`
InitrdMd5sum string `json:"initrd_md5sum,omitempty"`
KernelCommandLine string `json:"kernel_command_line,omitempty"`
KernelImage string `json:"kernel_image,omitempty"`
KernelImageMd5sum string `json:"kernel_image_md5sum,omitempty"`
LegacyNetworking bool `json:"legacy_networking,omitempty"`
MacAddress string `json:"mac_address,omitempty"`
Options string `json:"options,omitempty"`
PortsMapping []NodeEthernetPortsMapping `json:"-"`
Platform string `json:"platform,omitempty"`
ProcessPriority string `json:"process_priority,omitempty"`
QemuPath string `json:"qemu_path,omitempty"`
RAM int `json:"ram,omitempty"`
Usage string `json:"usage,omitempty"`
}
type nodeVpcsProperties struct {
nodeType string
AdapterType string `json:"-"`
Adapters int `json:"-"`
BiosImage string `json:"-"`
BiosImageMd5sum string `json:"-"`
BootPriority string `json:"-"`
CdromImage string `json:"-"`
CdromImageMd5sum string `json:"-"`
CPUThrottling int `json:"-"`
CPUs int `json:"-"`
HdaDiskImage string `json:"-"`
HdaDiskImageMd5sum string `json:"-"`
HdaDiskInterface string `json:"-"`
HdbDiskImage string `json:"-"`
HdbDiskImageMd5sum string `json:"-"`
HdbDiskInterface string `json:"-"`
HdcDiskImage string `json:"-"`
HdcDiskImageMd5sum string `json:"-"`
HdcDiskInterface string `json:"-"`
HddDiskImage string `json:"-"`
HddDiskImageMd5sum string `json:"-"`
HddDiskInterface string `json:"-"`
Initrd string `json:"-"`
InitrdMd5sum string `json:"-"`
KernelCommandLine string `json:"-"`
KernelImage string `json:"-"`
KernelImageMd5sum string `json:"-"`
LegacyNetworking bool `json:"-"`
MacAddress string `json:"-"`
Options string `json:"-"`
PortsMapping []NodeEthernetPortsMapping `json:"-"`
Platform string `json:"-"`
ProcessPriority string `json:"-"`
QemuPath string `json:"-"`
RAM int `json:"-"`
Usage string `json:"-"`
}
// MarshalJSON allows to customize the JSON output The design of the GNS3 server
// does not permit unncessary fields, we thus have to filter them. For each node
// type the properties structure is defined multiple times and the right cast
// into the relevant specific type is done at marshalling time.
func (p NodeProperties) MarshalJSON() ([]byte, error) {
switch p.nodeType {
case "ethernet_switch":
return json.Marshal(nodeEthernetSwitchProperties(p))
case "qemu":
return json.Marshal(nodeQemuProperties(p))
case "vpcs":
return json.Marshal(nodeVpcsProperties(p))
}
return json.Marshal(nil)
}
// NodeEthernetPortsMapping are specific to an Ethernet switch
type NodeEthernetPortsMapping struct {
Name string `json:"name"`
PortNumber int `json:"port_number"`
Type string `json:"type"`
Vlan int `json:"vlan"`
EtherType string `json:"ethertype,omitempty"`
}
// Node is the basic structure used for a GNS3 node
type Node struct {
CommandLine string `json:"command_line,omitempty"`
ComputeID string `json:"compute_id"`
Console int `json:"console,omitempty"`
ConsoleType string `json:"console_type,omitempty"`
FirstPortName string `json:"first_port_name,omitempty"`
Label *Label `json:"label,omitempty"`
Name string `json:"name"`
NodeType string `json:"node_type"`
PortNameFormat string `json:"port_name_format,omitempty"`
PortSegmentSize int `json:"port_segment_size,omitempty"`
Project *Project `json:"-"`
Properties NodeProperties `json:"properties,omitempty"`
Status string `json:"status,omitempty"`
Symbol string `json:"symbol,omitempty"`
UUID string `json:"node_id,omitempty"`
X int `json:"x,omitempty"`
Y int `json:"y,omitempty"`
Z int `json:"z,omitempty"`
}
type nodeAlias Node
// MarshalJSON allows to copy the nodeType of the Node structure to the child
// NodeProperties structure that will need this information to select the right
// object type for casting. The nodeAlias prevents infinite loop recursions.
func (n Node) MarshalJSON() ([]byte, error) {
n.Properties.nodeType = n.NodeType
return json.Marshal(nodeAlias(n))
}
func (n *Node) url() string {
return n.Project.url() + "/nodes/" + n.UUID
}
// Read reads an existing node in the project
func (n *Node) Read() error {
// save the project information as it will be reset
project := n.Project
nodes, _ := n.Project.GetNodes()
for _, node := range nodes {
if node.Name == n.Name {
*n = node
// restore the project information
n.Project = project
return nil
}
}
return &ServerError{Status: 404, Message: "Node does not exist in the project"}
}
// Exists checks a node exists in the project
func (n *Node) Exists() (bool, error) {
// Use a new struct because Read() will overwrite it
node := Node{
Name: n.Name,
Project: n.Project,
}
err := node.Read()
return err == nil, err
}
// Create creates a node in the project
func (n *Node) Create() error {
b, _ := json.Marshal(n)
status, content, err := n.Project.Server.HTTPRequest("POST", n.Project.url()+"/nodes", b)
if !(status >= 200 && status < 300) {
serverError := ServerError{}
json.Unmarshal(content, &serverError)
return &serverError
}
json.Unmarshal(content, n)
return err
}
// Delete deletes a node in the project
// Read() may be called before a Delete() can be executed
func (n *Node) Delete() error {
if n.UUID == "" {
n.Read()
}
status, content, err := n.Project.Server.HTTPRequest("DELETE", n.url(), nil)
if !(status >= 200 && status < 300) {
serverError := ServerError{}
json.Unmarshal(content, &serverError)
return &serverError
}
return err
}
// Update updates a node in the project
func (n *Node) Update() error {
var UUID = n.UUID
n.UUID = ""
b, _ := json.Marshal(n)
status, content, err := n.Project.Server.HTTPRequest("PUT", n.url()+UUID, b)
if !(status >= 200 && status < 300) {
serverError := ServerError{}
json.Unmarshal(content, &serverError)
return &serverError
}
json.Unmarshal(content, n)
return err
}