-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathintf_led_controller.go
More file actions
130 lines (100 loc) · 2.69 KB
/
intf_led_controller.go
File metadata and controls
130 lines (100 loc) · 2.69 KB
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
123
124
125
126
127
128
129
130
package main
import (
"errors"
"github.com/aristanetworks/goeapi"
)
type InterfaceLEDStatus int
const (
InterfaceLEDStatusOff InterfaceLEDStatus = iota
InterfaceLEDStatusGreen
InterfaceLEDStatusOrange
)
type InterfaceLEDController interface {
SetStatus(intf string, s InterfaceLEDStatus) error
SetAllStatuses(s InterfaceLEDStatus) error
}
type EnableCommand struct{}
func (c *EnableCommand) GetCmd() string {
return "enable"
}
type ConfigureCommand struct{}
func (c *ConfigureCommand) GetCmd() string {
return "configure"
}
type InterfaceCommand struct {
intf string
}
func (c *InterfaceCommand) GetCmd() string {
return "interface " + c.intf
}
type ShutdownCommand struct {
shut bool
}
func (c *ShutdownCommand) GetCmd() string {
cmd := "shutdown"
if c.shut {
return cmd
}
return "no " + cmd
}
type TrafficLoopBackCommand struct {
enable bool
}
func (c *TrafficLoopBackCommand) GetCmd() string {
cmd := "traffic-loopback source system device mac"
if c.enable {
return cmd
}
return "no " + cmd
}
type AristaInterfaceLEDController struct {
managedIntfs []string
node *goeapi.Node
}
func NewAristaInterfaceLEDController(managedIntfs []string, node *goeapi.Node) *AristaInterfaceLEDController {
return &AristaInterfaceLEDController{
managedIntfs: managedIntfs,
node: node,
}
}
func (a *AristaInterfaceLEDController) SetStatus(intf string, s InterfaceLEDStatus) error {
handle, err := a.node.GetHandle("json")
if err != nil {
return err
}
_ = handle.AddCommand(&EnableCommand{})
_ = handle.AddCommand(&ConfigureCommand{})
_ = handle.AddCommand(&InterfaceCommand{intf: intf})
switch s {
case InterfaceLEDStatusOff:
_ = handle.AddCommand(&ShutdownCommand{shut: false})
_ = handle.AddCommand(&TrafficLoopBackCommand{enable: false})
case InterfaceLEDStatusOrange:
_ = handle.AddCommand(&ShutdownCommand{shut: true})
_ = handle.AddCommand(&TrafficLoopBackCommand{enable: false})
case InterfaceLEDStatusGreen:
_ = handle.AddCommand(&ShutdownCommand{shut: false})
_ = handle.AddCommand(&TrafficLoopBackCommand{enable: true})
default:
return errors.New("unknown interface LED status")
}
return handle.Call()
}
func (a *AristaInterfaceLEDController) SetAllStatuses(s InterfaceLEDStatus) error {
for _, intf := range a.managedIntfs {
if err := a.SetStatus(intf, s); err != nil {
return err
}
}
return nil
}
type NoOpInterfaceLEDController struct{}
func NewNoOpInterfaceLEDController() NoOpInterfaceLEDController {
return NoOpInterfaceLEDController{}
}
func (n NoOpInterfaceLEDController) SetStatus(_ string, _ InterfaceLEDStatus) error {
return nil
}
func (n NoOpInterfaceLEDController) SetAllStatuses(_ InterfaceLEDStatus) error {
return nil
}