forked from yukirii/go-tmsh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
node.go
85 lines (73 loc) · 1.98 KB
/
node.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
package tmsh
import (
"fmt"
"strings"
)
// Node contains information about each node
type Node struct {
Name string `ltm:"name"`
Addr string `ltm:"addr"`
MonitorRule string `ltm:"monitor-rule"`
MonitorStatus string `ltm:"monitor-status"`
EnabledState string `ltm:"status.enabled-state"`
}
// GetAllNodes returns a list of all nodes.
func (bigip *BigIP) GetAllNodes() ([]Node, error) {
ret, err := bigip.ExecuteCommand("show ltm node field-fmt")
if err != nil {
return nil, err
}
var nodes []Node
for _, s := range splitLtmOutput(ret) {
var n Node
if err := Unmarshal(s, &n); err != nil {
return nil, err
}
nodes = append(nodes, n)
}
return nodes, nil
}
// GetNode gets a node by name. Return nil if the node does not exist.
func (bigip *BigIP) GetNode(name string) (*Node, error) {
ret, _ := bigip.ExecuteCommand("show ltm node " + name + " field-fmt")
if strings.Contains(ret, "was not found.") {
return nil, fmt.Errorf(ret)
}
var node Node
if err := Unmarshal(ret, &node); err != nil {
return nil, err
}
return &node, nil
}
// CreateNode creates a new node.
func (bigip *BigIP) CreateNode(name, ipaddr string) error {
ret, _ := bigip.ExecuteCommand("create ltm node " + name + " address " + ipaddr)
if ret != "" {
return fmt.Errorf(ret)
}
return nil
}
// DeleteNode removes a node.
func (bigip *BigIP) DeleteNode(name string) error {
ret, _ := bigip.ExecuteCommand("delete ltm node " + name)
if ret != "" {
return fmt.Errorf(ret)
}
return nil
}
// EnableNode changes the status of a node to enable.
func (bigip *BigIP) EnableNode(name string) error {
ret, _ := bigip.ExecuteCommand("modify ltm node " + name + " session user-enabled")
if ret != "" {
return fmt.Errorf(ret)
}
return nil
}
// DisableNode changes the status of a node to disable.
func (bigip *BigIP) DisableNode(name string) error {
ret, _ := bigip.ExecuteCommand("modify ltm node " + name + " session user-disabled")
if ret != "" {
return fmt.Errorf(ret)
}
return nil
}