-
Notifications
You must be signed in to change notification settings - Fork 6
/
port_set.go
221 lines (184 loc) · 6.81 KB
/
port_set.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
package main
import (
"errors"
"fmt"
"net/url"
"strings"
)
type PortSetting string
const (
Index Setting = "Index"
Name Setting = "Name"
Speed Setting = "Speed"
IngressRateLimit Setting = "IngressRateLimit"
EgressRateLimit Setting = "EgressRateLimit"
FlowControl Setting = "FlowControl"
)
type Port struct {
Index int8
Name string
Speed string
IngressRateLimit string
EgressRateLimit string
FlowControl string
}
type PortSetCommand struct {
Address string `required:"" help:"the Netgear switch's IP address or host name to connect to" short:"a"`
Ports []int `required:"" help:"port number (starting with 1), use multiple times for setting multiple ports at once" short:"p" name:"port"`
Name *string `optional:"" help:"sets the name of a port, 1-16 character limit" short:"n"`
Speed string `optional:"" help:"set the speed and duplex of the port ['100M full', '100M half', '10M full', '10M half', 'Auto', 'Disable']" short:"s"`
IngressRateLimit string `optional:"" help:"set an incoming rate limit for the port ['1 Mbit/s', '128 Mbit/s', '16 Mbit/s', '2 Mbit/s', '256 Mbit/s', '32 Mbit/s', '4 Mbit/s', '512 Kbit/s', '512 Mbit/s', '64 Mbit/s', '8 Mbit/s', 'No Limit']" short:"i"`
EgressRateLimit string `optional:"" help:"set an outgoing rate limit for the port ['1 Mbit/s', '128 Mbit/s', '16 Mbit/s', '2 Mbit/s', '256 Mbit/s', '32 Mbit/s', '4 Mbit/s', '512 Kbit/s', '512 Mbit/s', '64 Mbit/s', '8 Mbit/s', 'No Limit']" short:"o"`
FlowControl string `optional:"" help:"enable/disable flow control on port ['Off', 'On']"`
}
func (portSet *PortSetCommand) Run(args *GlobalOptions) error {
settings, hash, err := requestPortSettings(args, portSet.Address)
if err != nil {
return err
}
err = ensureModelIs30x(args, portSet.Address)
if err != nil {
return err
}
for _, switchPort := range portSet.Ports {
if switchPort > len(settings) || switchPort < 1 {
return errors.New(fmt.Sprintf("given port id %d, doesn't fit in range 1..%d", switchPort, len(settings)))
}
portSetting := settings[switchPort-1]
// If the port name was not set by the user, set it to the existing name (otherwise an empty port name is always considered to be the
// "new" value which blanks the port name on the setting next update)
if portSet.Name == nil {
portSet.Name = &portSetting.Name
}
name, err := comparePortSettings(Name, portSetting.Name, *portSet.Name)
if err != nil {
return err
}
speed, err := comparePortSettings(Speed, portSetting.Speed, portSet.Speed)
if err != nil {
return err
}
inRateLimit, err := comparePortSettings(IngressRateLimit, portSetting.IngressRateLimit, portSet.IngressRateLimit)
if err != nil {
return err
}
outRateLimit, err := comparePortSettings(EgressRateLimit, portSetting.EgressRateLimit, portSet.EgressRateLimit)
if err != nil {
return err
}
flowControl, err := comparePortSettings(FlowControl, portSetting.FlowControl, portSet.FlowControl)
if err != nil {
return err
}
portUpdateValues := url.Values{
"hash": {hash},
fmt.Sprintf("%s%d", "port", portSetting.Index): {"checked"},
"SPEED": {speed},
"FLOW_CONTROL": {flowControl},
"DESCRIPTION": {name},
"IngressRate": {inRateLimit},
"EgressRate": {outRateLimit},
"priority": {"0"},
}
result, err := requestPortSettingsUpdate(args, portSet.Address, portUpdateValues.Encode())
if err != nil {
return err
}
if result != "SUCCESS" {
return errors.New(result)
}
}
settings, _, err = requestPortSettings(args, portSet.Address)
if err != nil {
return err
}
changedPorts := collectChangedPortConfiguration(portSet.Ports, settings)
prettyPrintPortSettings(args.OutputFormat, changedPorts)
return err
}
func collectChangedPortConfiguration(ports []int, settings []Port) (changedPorts []Port) {
for _, configuredPort := range ports {
for _, portSetting := range settings {
if int(portSetting.Index) == configuredPort {
changedPorts = append(changedPorts, portSetting)
}
}
}
return changedPorts
}
func comparePortSettings(name Setting, defaultValue string, newValue string) (string, error) {
if len(newValue) == 0 && name != Name {
return defaultValue, nil
}
switch name {
case Name:
if defaultValue != newValue {
if len(newValue) <= 16 {
return newValue, nil
} else {
return defaultValue, errors.New("port name could not be set. Port name must be 16 characters or less")
}
}
return defaultValue, nil
case Speed:
speed := bidiMapLookup(newValue, portSpeedMap)
if speed == "unknown" {
return speed, errors.New("port speed could not be set. Accepted values are: " + valuesAsString(portSpeedMap))
}
return speed, nil
case IngressRateLimit:
inRateLimit := bidiMapLookup(newValue, portRateLimitMap)
if inRateLimit == "unknown" {
return inRateLimit, errors.New("ingress rate limit could not be set. Accepted values are: " + valuesAsString(portRateLimitMap))
}
return inRateLimit, nil
case EgressRateLimit:
outRateLimit := bidiMapLookup(newValue, portRateLimitMap)
if outRateLimit == "unknown" {
return outRateLimit, errors.New("egress rate limit could not be set. Accepted values are: " + valuesAsString(portRateLimitMap))
}
return outRateLimit, nil
case FlowControl:
flowControl := bidiMapLookup(newValue, portFlowControlMap)
if flowControl == "unknown" {
return flowControl, errors.New("flow control could not be set. Accepted values are: " + valuesAsString(portFlowControlMap))
}
return flowControl, nil
default:
return defaultValue, errors.New("could not find port setting")
}
}
func requestPortSettings(args *GlobalOptions, host string) (portSettings []Port, hash string, err error) {
model, _, err := readTokenAndModel2GlobalOptions(args, host)
if err != nil {
return portSettings, hash, err
}
var requestUrl string
if isModel30x(model) {
requestUrl = fmt.Sprintf("http://%s/dashboard.cgi", host)
} else if isModel316(model) {
requestUrl = fmt.Sprintf("http://%s/iss/specific/dashboard.html", host)
} else {
panic("model not supported")
}
dashboardData, err := requestPage(args, host, requestUrl)
if err != nil {
return portSettings, hash, err
}
if checkIsLoginRequired(dashboardData) {
return portSettings, hash, errors.New("no content. please, (re-)login first")
}
hash, err = findHashInHtml(model, strings.NewReader(dashboardData))
if err != nil {
return portSettings, hash, err
}
portSettings, err = findPortSettingsInHtml(model, strings.NewReader(dashboardData))
if err != nil {
return portSettings, hash, err
}
return portSettings, hash, err
}
func requestPortSettingsUpdate(args *GlobalOptions, host string, data string) (string, error) {
requestUrl := fmt.Sprintf("http://%s/port_status.cgi", host)
return postPage(args, host, requestUrl, data)
}