-
Notifications
You must be signed in to change notification settings - Fork 0
/
templates.go
231 lines (196 loc) · 7.2 KB
/
templates.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
/*
Copyright 2022 Ciena Corporation
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package gns3
import (
"encoding/json"
"fmt"
"github.com/google/uuid"
)
const (
TemplatesPath = "v2/templates"
)
//nolint:tagliatelle
type Template struct {
Builtin bool `json:"builtin,omitempty" yaml:"builtin"`
Category string `json:"category,omitempty" yaml:"category"`
ComputeId string `json:"compute_id,omitempty" yaml:"compute_id"`
DefaultNameFormat string `json:"default_name_format,omitempty" yaml:"default_name_format"`
FirstPortName string `json:"first_port_name,omitempty" yaml:"first_port_name"`
Name string `json:"name,omitempty" yaml:"name"`
Symbol string `json:"symbol,omitempty" yaml:"symbol"`
TemplateId string `json:"template_id,omitempty" yaml:"template_id"`
TemplateType string `json:"template_type,omitempty" yaml:"template_type"`
Usage string `json:"usage,omitempty" yaml:"usage"`
Qemu *TemplateQemu `json:"-" yaml:"qemu"`
Docker *TemplateDocker `json:"-" yaml:"docker"`
}
//nolint:tagliatelle
type TemplateQemu struct {
Options string `json:"options,omitempty" yaml:"options"`
KernelCommandLine string `json:"kernel_command_line,omitempty" yaml:"kernel_command_line"`
AdapterType string `json:"adapter_type,omitempty" yaml:"adapter_type"`
Adapters int `json:"adapters,omitempty" yaml:"adapters"`
BootPriority string `json:"boot_priority,omitempty" yaml:"boot_priority"`
ConsoleType string `json:"console_type,omitempty" yaml:"console_type"`
HdaDiskInterface string `json:"hda_disk_interface,omitempty" yaml:"hda_disk_interface"`
Ram int64 `json:"ram,omitempty" yaml:"ram"`
Path string `json:"qemu_path,omitempty" yaml:"qemu_path"`
PortNameFormat string `json:"port_name_format,omitempty" yaml:"port_name_format"`
BiosImage string `json:"bios_image,omitempty" yaml:"bios_image"`
HdaDiskImage string `json:"hda_disk_image,omitempty" yaml:"hda_disk_image"`
CdromImage string `json:"cdrom_image,omitempty" yaml:"cdrom_image"`
}
//nolint:tagliatelle
type TemplateDocker struct {
Image string `json:"image,omitempty" yaml:"image,omitempty"`
Usage string `json:"usage,omitempty" yaml:"usage,omitempty"`
Adapters int `json:"adapters,omitempty" yaml:"adapters,omitempty"`
StartCommand string `json:"start_command,omitempty" yaml:"start_command,omitempty"`
Environment string `json:"environment,omitempty" yaml:"environment,omitempty"`
ConsoleType string `json:"console_type,omitempty" yaml:"console_type,omitempty"`
ConsoleAutoStart bool `json:"console_auto_start,omitempty" yaml:"console_auto_start,omitempty"`
ConsoleHttpPort int `json:"console_http_port,omitempty" yaml:"console_http_port,omitempty"`
ConsoleHttpPath string `json:"console_http_path,omitempty" yaml:"console_http_path,omitempty"`
ConsoleResolution string `json:"console_resolution,omitempty" yaml:"console_resolution,omitempty"`
ExtraHosts string `json:"extra_hosts,omitempty" yaml:"extra_hosts,omitempty"`
ExtraVolumes []string `json:"extra_volumes,omitempty" yaml:"extra_volumes,omitempty"`
CustomAdapters []*struct {
Schema string `json:"$schema,omitempty" yaml:"$schema,omitempty"`
Description string `json:"description,omitempty" yaml:"description,omitempty"`
Type any `json:"type,omitempty" yaml:"type,omitempty"`
Properties any `json:"properties,omitempty" yaml:"properties,omitempty"`
Required []string `json:"required,omitempty" yaml:"required,omitempty"`
AdditionalProperties bool `json:"additionalProperties,omitempty" yaml:"additionalProperties,omitempty"`
} `json:"custom_adapters,omitempty" yaml:"custom_adapters,omitempty"`
}
type Templates struct {
gns3 *Gns3
}
func (g *Gns3) Templates() *Templates {
return &Templates{gns3: g}
}
func (t *Templates) List() ([]Template, error) {
var list []Template
err := t.gns3.Get(TemplatesPath, &list)
if err != nil {
return nil, err
}
return list, nil
}
func (t *Templates) Get(id string) (*Template, error) {
var template Template
_, err := uuid.Parse(id)
if err == nil {
// Think this may be a UUID, so try to delete directly
err = t.gns3.Get(fmt.Sprintf("%s/%s", TemplatesPath, id), &template)
if err == nil {
return &template, nil
}
}
// not a UUID, so get a list of templates and search based on name
list, err := t.List()
if err != nil {
return nil, err
}
for _, val := range list {
if val.Name == id {
return &val, nil
}
}
return nil, ErrNotFound
}
func (t *Templates) Close(id string) (string, error) {
template, err := t.Get(id)
if err != nil {
return "", err
}
return template.TemplateId, t.gns3.Post(fmt.Sprintf("%s/%s/close", TemplatesPath, id), "", nil, nil)
}
func (t *Templates) Create(template *Template) (*Template, error) {
var out Template
err := t.gns3.Post(TemplatesPath, "application/json", template, &out)
if err != nil {
return nil, err
}
return &out, nil
}
func (t *Templates) Delete(id string) (string, error) {
template, err := t.Get(id)
if err != nil {
return "", err
}
return template.TemplateId, t.gns3.Delete(fmt.Sprintf("%s/%s", TemplatesPath, template.TemplateId))
}
func (t Template) MarshalJSON() ([]byte, error) {
var m map[string]interface{}
type _Template Template
res, err := json.Marshal(_Template(t))
if err != nil {
return nil, err
}
err = json.Unmarshal(res, &m)
if err != nil {
return nil, err
}
var typeData map[string]interface{}
// for now marshal the qemu options
if t.TemplateType == TemplateTypeQemu {
res, err = json.Marshal(t.Qemu)
if err != nil {
return nil, err
}
err = json.Unmarshal(res, &typeData)
if err != nil {
return nil, err
}
}
if t.TemplateType == TemplateTypeDocker {
res, err = json.Marshal(t.Docker)
if err != nil {
return nil, err
}
err = json.Unmarshal(res, &typeData)
if err != nil {
return nil, err
}
}
// add the type data for qemu to the template map
for k, v := range typeData {
m[k] = v
}
return json.Marshal(m)
}
func (t *Template) UnmarshalJSON(data []byte) error {
type _Template Template
var template _Template
err := json.Unmarshal(data, &template)
if err != nil {
return err
}
*t = Template(template)
if t.TemplateType == TemplateTypeQemu {
t.Qemu = &TemplateQemu{}
err = json.Unmarshal(data, &t.Qemu)
if err != nil {
return err
}
}
if t.TemplateType == TemplateTypeDocker {
t.Docker = &TemplateDocker{}
err = json.Unmarshal(data, &t.Docker)
if err != nil {
return err
}
}
return nil
}