forked from luthermonson/go-proxmox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
containers.go
80 lines (66 loc) · 2.65 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
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) (status string, err error) {
return status, c.client.Post(ctx, fmt.Sprintf("/nodes/%s/lxc/%d/status/start", c.Node, c.VMID), nil, &status)
}
func (c *Container) Stop(ctx context.Context) (status *ContainerStatus, err error) {
return status, c.client.Post(ctx, fmt.Sprintf("/nodes/%s/lxc/%d/status/stop", c.Node, c.VMID), nil, &status)
}
func (c *Container) Suspend(ctx context.Context) (status *ContainerStatus, err error) {
return status, c.client.Post(ctx, fmt.Sprintf("/nodes/%s/lxc/%d/status/suspend", c.Node, c.VMID), nil, &status)
}
func (c *Container) Reboot(ctx context.Context) (status *ContainerStatus, err error) {
return status, c.client.Post(ctx, fmt.Sprintf("/nodes/%s/lxc/%d/status/reboot", c.Node, c.VMID), nil, &status)
}
func (c *Container) Resume(ctx context.Context) (status *ContainerStatus, err error) {
return status, c.client.Post(ctx, fmt.Sprintf("/nodes/%s/lxc/%d/status/resume", c.Node, c.VMID), nil, &status)
}
func (c *Container) TermProxy(ctx context.Context) (vnc *VNC, err error) {
return vnc, c.client.Post(ctx, fmt.Sprintf("/nodes/%s/lxk/%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)
}