forked from luthermonson/go-proxmox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcluster_ha_resources.go
81 lines (66 loc) · 2 KB
/
cluster_ha_resources.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
package proxmox
import (
"context"
"errors"
"fmt"
"strconv"
"strings"
)
func (cl *Cluster) CreateHAResource(ctx context.Context, haResource HAResource) error {
if err := cl.client.Post(ctx, "/cluster/ha/resources", &haResource, nil); err != nil {
return err
}
return nil
}
func (cl *Cluster) DeleteHAResource(ctx context.Context, sid SID) error {
if err := cl.client.Delete(ctx, fmt.Sprintf("/cluster/ha/resources/%s:%d", sid.Type, sid.ID), nil); err != nil {
return err
}
return nil
}
func (cl *Cluster) GetHAResource(ctx context.Context, sid SID) (HAResource, error) {
var haResource HAResource
if err := cl.client.Get(ctx, fmt.Sprintf("/cluster/ha/resources/%s:%d", sid.Type, sid.ID), &haResource); err != nil {
return haResource, err
}
return haResource, nil
}
func (cl *Cluster) ListHAResources(ctx context.Context, resourceType HAResourceType) ([]HAResource, error) {
var haResources []haResource
if err := cl.client.Get(ctx, fmt.Sprintf("/cluster/ha/resources?type=%s", resourceType), &haResources); err != nil {
return nil, err
}
return fillHAResources(haResources)
}
func fillHAResources(resources []haResource) ([]HAResource, error) {
haResources := make([]HAResource, 0, len(resources))
for _, resource := range resources {
sid, err := parseSid(resource.Sid)
if err != nil {
return nil, fmt.Errorf("parse sid: %w", err)
}
haResources = append(haResources, HAResource{
ID: sid.ID,
Group: AsPtr(resource.Group),
Comment: AsPtr(resource.Comment),
MaxRelocate: AsPtr(resource.MaxRelocate),
MaxRestart: AsPtr(resource.MaxRestart),
State: AsPtr(resource.State),
})
}
return haResources, nil
}
func parseSid(sid string) (SID, error) {
sidParts := strings.Split(sid, ":")
if len(sidParts) != 2 {
return SID{}, errors.New("invalid sid parts count")
}
vmid, err := strconv.Atoi(sidParts[1])
if err != nil {
return SID{}, fmt.Errorf("parse sid vmid: %w", err)
}
return SID{
Type: HAResourceType(sidParts[0]),
ID: vmid,
}, nil
}