forked from luthermonson/go-proxmox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
containers.go
279 lines (229 loc) · 10.8 KB
/
containers.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
package proxmox
import (
"context"
"fmt"
"net/url"
)
func (c *Container) Clone(ctx context.Context, params *ContainerCloneOptions) (newid int, task *Task, err error) {
var upid UPID
if params == nil {
params = &ContainerCloneOptions{}
}
if params.NewID <= 0 {
cluster, err := c.client.Cluster(ctx)
if err != nil {
return newid, nil, err
}
newid, err := cluster.NextID(ctx)
if err != nil {
return newid, nil, err
}
params.NewID = newid
}
if err := c.client.Post(ctx, fmt.Sprintf("/nodes/%s/lxc/%d/clone", c.Node, c.VMID), params, &upid); err != nil {
return 0, nil, err
}
return newid, NewTask(upid, c.client), nil
}
func (c *Container) Delete(ctx context.Context) (task *Task, err error) {
var upid UPID
if err := c.client.Delete(ctx, fmt.Sprintf("/nodes/%s/lxc/%d", c.Node, c.VMID), &upid); err != nil {
return nil, err
}
return NewTask(upid, c.client), nil
}
func (c *Container) Config(ctx context.Context, options ...ContainerOption) (*Task, error) {
var upid UPID
data := make(map[string]interface{})
for _, option := range options {
data[option.Name] = option.Value
}
err := c.client.Put(ctx, fmt.Sprintf("/nodes/%s/lxc/%d/config", c.Node, c.VMID), data, &upid)
return NewTask(upid, c.client), err
}
func (c *Container) Start(ctx context.Context) (task *Task, err error) {
var upid UPID
if err := c.client.Post(ctx, fmt.Sprintf("/nodes/%s/lxc/%d/status/start", c.Node, c.VMID), nil, &upid); err != nil {
return nil, err
}
return NewTask(upid, c.client), nil
}
func (c *Container) Stop(ctx context.Context) (task *Task, err error) {
var upid UPID
if err := c.client.Post(ctx, fmt.Sprintf("/nodes/%s/lxc/%d/status/stop", c.Node, c.VMID), nil, &upid); err != nil {
return nil, err
}
return NewTask(upid, c.client), nil
}
func (c *Container) Suspend(ctx context.Context) (task *Task, err error) {
var upid UPID
if err := c.client.Post(ctx, fmt.Sprintf("/nodes/%s/lxc/%d/status/suspend", c.Node, c.VMID), nil, &upid); err != nil {
return nil, err
}
return NewTask(upid, c.client), nil
}
func (c *Container) Reboot(ctx context.Context) (task *Task, err error) {
var upid UPID
if err := c.client.Post(ctx, fmt.Sprintf("/nodes/%s/lxc/%d/status/reboot", c.Node, c.VMID), nil, &upid); err != nil {
return nil, err
}
return NewTask(upid, c.client), nil
}
func (c *Container) Resume(ctx context.Context) (task *Task, err error) {
var upid UPID
if err := c.client.Post(ctx, fmt.Sprintf("/nodes/%s/lxc/%d/status/resume", c.Node, c.VMID), nil, &upid); err != nil {
return nil, err
}
return NewTask(upid, c.client), nil
}
func (c *Container) Shutdown(ctx context.Context, force bool, timeout int) (task *Task, err error) {
var upid UPID
if err := c.client.Post(ctx, fmt.Sprintf("/nodes/%s/lxc/%d/status/shutdown", c.Node, c.VMID), map[string]interface{}{"forceStop": force, "timeout": timeout}, &upid); err != nil {
return nil, err
}
return NewTask(upid, c.client), nil
}
func (c *Container) TermProxy(ctx context.Context) (vnc *VNC, err error) {
return vnc, c.client.Post(ctx, fmt.Sprintf("/nodes/%s/lxc/%d/termproxy", c.Node, c.VMID), nil, &vnc)
}
func (c *Container) VNCWebSocket(vnc *VNC) (chan string, chan string, chan error, func() error, error) {
p := fmt.Sprintf("/nodes/%s/lxc/%d/vncwebsocket?port=%d&vncticket=%s",
c.Node, c.VMID, vnc.Port, url.QueryEscape(vnc.Ticket))
return c.client.VNCWebSocket(p, vnc)
}
func (c *Container) Feature(ctx context.Context) (hasFeature bool, err error) {
var feature struct {
HasFeature bool `json:"hasFeature"`
}
err = c.client.Get(ctx, fmt.Sprintf("/nodes/%s/lxc/%d/feature", c.Node, c.VMID), &feature)
return feature.HasFeature, err
}
func (c *Container) Interfaces(ctx context.Context) (interfaces ContainerInterfaces, err error) {
err = c.client.Get(ctx, fmt.Sprintf("/nodes/%s/lxc/%d/interfaces", c.Node, c.VMID), &interfaces)
return interfaces, err
}
func (c *Container) Migrate(ctx context.Context, params *ContainerMigrateOptions) (task *Task, err error) {
var upid UPID
if err := c.client.Post(ctx, fmt.Sprintf("/nodes/%s/lxc/%d/migrate", c.Node, c.VMID), params, &upid); err != nil {
return nil, err
}
return NewTask(upid, c.client), nil
}
func (c *Container) Resize(ctx context.Context, disk, size string) (task *Task, err error) {
var upid UPID
if err := c.client.Post(ctx, fmt.Sprintf("/nodes/%s/lxc/%d/resize", c.Node, c.VMID), map[string]interface{}{"disk": disk, "size": size}, &upid); err != nil {
return nil, err
}
return NewTask(upid, c.client), nil
}
func (c *Container) MoveVolume(ctx context.Context, params *VirtualMachineMoveDiskOptions) (task *Task, err error) {
var upid UPID
if err := c.client.Post(ctx, fmt.Sprintf("/nodes/%s/lxc/%d/move_volume", c.Node, c.VMID), params, &upid); err != nil {
return nil, err
}
return NewTask(upid, c.client), nil
}
func (c *Container) RRDData(ctx context.Context, timeframe Timeframe, consolidationFunction ConsolidationFunction) (rrddata []*RRDData, err error) {
u := url.URL{Path: fmt.Sprintf("/lxc/%s/qemu/%d/rrddata", c.Node, c.VMID)}
// consolidation functions are variadic because they're optional, putting everything into one string and sending that
params := url.Values{}
if len(consolidationFunction) > 0 {
f := ""
for _, cf := range consolidationFunction {
f = f + string(cf)
}
params.Add("cf", f)
}
params.Add("timeframe", string(timeframe))
u.RawQuery = params.Encode()
err = c.client.Get(ctx, u.String(), &rrddata)
return
}
func (c *Container) Template(ctx context.Context) error {
return c.client.Post(ctx, fmt.Sprintf("/nodes/%s/lxc/%d/template", c.Node, c.VMID), nil, nil)
}
func (c *Container) VNCProxy(ctx context.Context, vncOptions VNCProxyOptions) (vnc *VNC, err error) {
return vnc, c.client.Post(ctx, fmt.Sprintf("/nodes/%s/lxc/%d/vncproxy", c.Node, c.VMID), vncOptions, &vnc)
}
func (c *Container) Snapshots(ctx context.Context) (snapshots []*ContainerSnapshot, err error) {
err = c.client.Get(ctx, fmt.Sprintf("/nodes/%s/lxc/%d/snapshot", c.Node, c.VMID), &snapshots)
return
}
func (c *Container) NewSnapshot(ctx context.Context, snapName string) (task *Task, err error) {
var upid UPID
if err := c.client.Post(ctx, fmt.Sprintf("/nodes/%s/lxc/%d/snapshot", c.Node, c.VMID), map[string]interface{}{"snapname": snapName}, &upid); err != nil {
return nil, err
}
return NewTask(upid, c.client), nil
}
func (c *Container) GetSnapshot(ctx context.Context, snapshot string) (snap []*ContainerSnapshot, err error) {
return snap, c.client.Get(ctx, fmt.Sprintf("/nodes/%s/lxc/%d/snapshot/%s", c.Node, c.VMID, snapshot), &snap)
}
func (c *Container) DeleteSnapshot(ctx context.Context, snapshot string) (task *Task, err error) {
var upid UPID
if err := c.client.Delete(ctx, fmt.Sprintf("/nodes/%s/lxc/%d/snapshot/%s", c.Node, c.VMID, snapshot), &upid); err != nil {
return nil, err
}
return NewTask(upid, c.client), nil
}
func (c *Container) RollbackSnapshot(ctx context.Context, snapshot string, start bool) (task *Task, err error) {
var upid UPID
if err := c.client.Post(ctx, fmt.Sprintf("/nodes/%s/lxc/%d/snapshot/%s/rollback", c.Node, c.VMID, snapshot), map[string]interface{}{"start": start}, &upid); err != nil {
return nil, err
}
return NewTask(upid, c.client), nil
}
func (c *Container) GetSnapshotConfig(ctx context.Context, snapshot string) (config map[string]interface{}, err error) {
return config, c.client.Get(ctx, fmt.Sprintf("/nodes/%s/lxc/%d/snapshot/%s/config", c.Node, c.VMID, snapshot), &config)
}
func (c *Container) UpdateSnapshot(ctx context.Context, snapshot string) error {
return c.client.Put(ctx, fmt.Sprintf("/nodes/%s/lxc/%d/snapshot/%s/config", c.Node, c.VMID, snapshot), nil, nil)
}
func (c *Container) Firewall(ctx context.Context) (firewall *Firewall, err error) {
return firewall, c.client.Get(ctx, fmt.Sprintf("/nodes/%s/lxc/%d/firewall", c.Node, c.VMID), &firewall)
}
func (c *Container) GetFirewallAliases(ctx context.Context) (aliases []*FirewallAlias, err error) {
return aliases, c.client.Get(ctx, fmt.Sprintf("/nodes/%s/lxc/%d/firewall/aliases", c.Node, c.VMID), &aliases)
}
func (c *Container) NewFirewallAlias(ctx context.Context, alias *FirewallAlias) error {
return c.client.Post(ctx, fmt.Sprintf("/nodes/%s/lxc/%d/firewall/aliases", c.Node, c.VMID), alias, nil)
}
func (c *Container) GetFirewallAlias(ctx context.Context, name string) (alias *FirewallAlias, err error) {
return alias, c.client.Get(ctx, fmt.Sprintf("/nodes/%s/lxc/%d/firewall/aliases/%s", c.Node, c.VMID, name), &alias)
}
func (c *Container) UpdateFirewallAlias(ctx context.Context, name string, alias *FirewallAlias) error {
return c.client.Put(ctx, fmt.Sprintf("/nodes/%s/lxc/%d/firewall/aliases/%s", c.Node, c.VMID, name), alias, nil)
}
func (c *Container) DeleteFirewallAlias(ctx context.Context, name string) error {
return c.client.Delete(ctx, fmt.Sprintf("/nodes/%s/lxc/%d/firewall/aliases/%s", c.Node, c.VMID, name), nil)
}
func (c *Container) GetFirewallIPSet(ctx context.Context) (ipsets []*FirewallIPSet, err error) {
return ipsets, c.client.Get(ctx, fmt.Sprintf("/nodes/%s/lxc/%d/firewall/ipset", c.Node, c.VMID), &ipsets)
}
func (c *Container) NewFirewallIPSet(ctx context.Context, ipset *FirewallIPSet) error {
return c.client.Post(ctx, fmt.Sprintf("/nodes/%s/lxc/%d/firewall/ipset", c.Node, c.VMID), ipset, nil)
}
func (c *Container) DeleteFirewallIPSet(ctx context.Context, name string, force bool) error {
return c.client.Delete(ctx, fmt.Sprintf("/nodes/%s/lxc/%d/firewall/ipset/%s", c.Node, c.VMID, name), map[string]interface{}{"force": force})
}
func (c *Container) FirewallRules(ctx context.Context) (rules []*FirewallRule, err error) {
return rules, c.client.Get(ctx, fmt.Sprintf("/nodes/%s/lxc/%d/firewall/rules", c.Node, c.VMID), &rules)
}
func (c *Container) NewFirewallRule(ctx context.Context, rule *FirewallRule) error {
return c.client.Post(ctx, fmt.Sprintf("/nodes/%s/lxc/%d/firewall/rules", c.Node, c.VMID), rule, nil)
}
func (c *Container) GetFirewallRule(ctx context.Context, rulePos int) (rule *FirewallRule, err error) {
return rule, c.client.Get(ctx, fmt.Sprintf("/nodes/%s/lxc/%d/firewall/rules/%d", c.Node, c.VMID, rulePos), &rule)
}
func (c *Container) UpdateFirewallRule(ctx context.Context, rulePos int, rule *FirewallRule) error {
return c.client.Put(ctx, fmt.Sprintf("/nodes/%s/lxc/%d/firewall/rules/%d", c.Node, c.VMID, rulePos), rule, nil)
}
func (c *Container) DeleteFirewallRule(ctx context.Context, rulePos int) error {
return c.client.Delete(ctx, fmt.Sprintf("/nodes/%s/lxc/%d/firewall/rules/%d", c.Node, c.VMID, rulePos), nil)
}
func (c *Container) GetFirewallOptions(ctx context.Context) (options *FirewallVirtualMachineOption, err error) {
return options, c.client.Get(ctx, fmt.Sprintf("/nodes/%s/lxc/%d/firewall/options", c.Node, c.VMID), &options)
}
func (c *Container) UpdateFirewallOptions(ctx context.Context, options *FirewallVirtualMachineOption) error {
return c.client.Put(ctx, fmt.Sprintf("/nodes/%s/lxc/%d/firewall/options", c.Node, c.VMID), options, nil)
}