forked from luthermonson/go-proxmox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cluster.go
100 lines (77 loc) · 2.47 KB
/
cluster.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
package proxmox
import (
"context"
"fmt"
"net/url"
"strconv"
"strings"
)
func (c *Client) Cluster(ctx context.Context) (*Cluster, error) {
cluster := &Cluster{
client: c,
}
// requires (/, Sys.Audit), do not error out if no access to still get the cluster
if err := cluster.Status(ctx); !IsNotAuthorized(err) {
return cluster, err
}
return cluster, nil
}
func (cl *Cluster) Status(ctx context.Context) error {
return cl.client.Get(ctx, "/cluster/status", cl)
}
func (cl *Cluster) NextID(ctx context.Context) (int, error) {
var ret string
if err := cl.client.Get(ctx, "/cluster/nextid", &ret); err != nil {
return 0, err
}
return strconv.Atoi(ret)
}
// Resources retrieves a summary list of all resources in the cluster.
// It calls /cluster/resources api v2 endpoint with an optional "type" parameter
// to filter searched values.
// It returns a list of ClusterResources.
func (cl *Cluster) Resources(ctx context.Context, filters ...string) (rs ClusterResources, err error) {
u := url.URL{Path: "/cluster/resources"}
// filters are variadic because they're optional, munging everything passed into one big string to make
// a good request and the api will error out if there's an issue
if f := strings.Replace(strings.Join(filters, ""), " ", "", -1); f != "" {
params := url.Values{}
params.Add("type", f)
u.RawQuery = params.Encode()
}
return rs, cl.client.Get(ctx, u.String(), &rs)
}
func (cl *Cluster) Backups(ctx context.Context, params *ClusterBackupsOptions) (task *Task, err error) {
var upid UPID
if params == nil {
params = &ClusterBackupsOptions{}
}
if err = cl.client.Post(ctx, "/cluster/backup", params, &upid); err != nil {
return nil, err
}
return NewTask(upid, cl.client), nil
}
func (cl *Cluster) GetBackups(ctx context.Context) (*[]ClusterBackupSchedule, error) {
var backups *[]ClusterBackupSchedule
if err := cl.client.Get(ctx, "/cluster/backup", &backups); err != nil {
return nil, err
}
return backups, nil
}
func (cl *Cluster) DeleteBackupSchedule(ctx context.Context, id string) (task *Task, err error) {
var upid UPID
if err = cl.client.Delete(ctx, fmt.Sprintf("/cluster/backup/%s", id), &upid); err != nil {
return nil, err
}
return NewTask(upid, cl.client), nil
}
func (cl *Cluster) Tasks(ctx context.Context) (Tasks, error) {
var tasks Tasks
if err := cl.client.Get(ctx, "/cluster/tasks", &tasks); err != nil {
return nil, err
}
for index := range tasks {
tasks[index].client = cl.client
}
return tasks, nil
}