forked from luthermonson/go-proxmox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcluster_ha.go
122 lines (97 loc) · 3 KB
/
cluster_ha.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
package proxmox
import (
"context"
"errors"
"fmt"
"strconv"
"strings"
)
const (
PrioritySeparator = ":"
NodeSeparator = ","
)
type haGroupConfiguration struct {
Nodes string `json:"nodes"`
Group string `json:"group"`
Comment *string `json:"comment,omitempty"`
NoFailback *IntOrBool `json:"nofailback,omitempty"`
Restricted *IntOrBool `json:"restricted,omitempty"`
Type HAType `json:"type,omitempty"`
}
func (cl *Cluster) HAGroupCreate(ctx context.Context, groupConfiguration *HAGroupConfiguration) error {
if groupConfiguration == nil {
return errors.New("empty ha group configuration")
}
haGroupCfg := haGroupConfiguration{
Group: groupConfiguration.Group,
Comment: groupConfiguration.Comment,
NoFailback: groupConfiguration.NoFailback,
Restricted: groupConfiguration.Restricted,
Type: HATypeGroup,
}
var nodes []string
for _, haNode := range groupConfiguration.HaNodes {
if haNode.Priority != nil {
nodes = append(nodes,
fmt.Sprintf("%s%s%d", haNode.Node, PrioritySeparator, *haNode.Priority))
} else {
nodes = append(nodes,
haNode.Node)
}
}
haGroupCfg.Nodes = strings.Join(nodes, NodeSeparator)
if err := cl.client.Post(ctx, "/cluster/ha/groups", &haGroupCfg, nil); err != nil {
return err
}
return nil
}
func (cl *Cluster) HAGroupDelete(ctx context.Context, groupName string) error {
return cl.client.Delete(ctx, fmt.Sprintf("/cluster/ha/groups/%s", groupName), nil)
}
func (cl *Cluster) HAGroups(ctx context.Context) ([]HAGroupConfiguration, error) {
var haConfigurations []haGroupConfiguration
if err := cl.client.Get(ctx, "/cluster/ha/groups", &haConfigurations); err != nil {
return nil, err
}
haGroupConfigurations := make([]HAGroupConfiguration, 0, len(haConfigurations))
for _, groupCfg := range haConfigurations {
haNodes, err := prepareHANodeList(groupCfg.Nodes)
if err != nil {
return nil, fmt.Errorf("prepare ha node list : %w", err)
}
haGroupConfigurations = append(haGroupConfigurations, HAGroupConfiguration{
Group: groupCfg.Group,
HaNodes: haNodes,
Comment: groupCfg.Comment,
NoFailback: groupCfg.NoFailback,
Restricted: groupCfg.Restricted,
})
}
return haGroupConfigurations, nil
}
func prepareHANodeList(haNodeStr string) ([]HANodes, error) {
nodeStrs := strings.Split(haNodeStr, NodeSeparator)
haNodes := make([]HANodes, 0, len(nodeStrs))
for _, nodeStr := range nodeStrs {
haNode, err := splitToHANode(nodeStr)
if err != nil {
return nil, fmt.Errorf("split node string : %w", err)
}
haNodes = append(haNodes, haNode)
}
return haNodes, nil
}
func splitToHANode(haNodeStr string) (HANodes, error) {
haNodeParts := strings.Split(haNodeStr, PrioritySeparator)
haNode := HANodes{
Node: haNodeParts[0],
}
if len(haNodeParts) > 1 {
priority, err := strconv.ParseUint(haNodeParts[1], 10, 32)
if err != nil {
return HANodes{}, fmt.Errorf("cannot parse priority : %w", err)
}
haNode.Priority = AsPtr[uint](uint(priority))
}
return haNode, nil
}