-
Notifications
You must be signed in to change notification settings - Fork 0
/
validator.go
72 lines (60 loc) · 1.82 KB
/
validator.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
package snmpproxy
import (
"fmt"
"strings"
"time"
)
type RequestValidator struct {
maxTimeout time.Duration
maxRetries uint8
}
func (v *RequestValidator) Validate(apiRequest *ApiRequest) error { //nolint:cyclop // No need to split this
if apiRequest.Timeout > v.maxTimeout {
return fmt.Errorf(
"maximum allowed timeout is %d seconds, got %d seconds",
v.maxTimeout/time.Second,
apiRequest.Timeout/time.Second,
)
}
if apiRequest.Retries > v.maxRetries {
return fmt.Errorf("maximum allowed number of retries is %d, got %d", v.maxRetries, apiRequest.Retries)
}
if len(apiRequest.Requests) == 0 {
return fmt.Errorf("at least one Request must be provided")
}
for i, request := range apiRequest.Requests {
switch request.RequestType {
case Get, GetNext, Walk:
default:
return fmt.Errorf("request[%d]: unexpected RequestType: %s", i, request.RequestType)
}
if len(request.Oids) == 0 {
return fmt.Errorf("request[%d]: at least one OID must be provided", i)
}
if request.RequestType == Walk && len(request.Oids) > 1 {
return fmt.Errorf(
"request[%d]: only single OID is supported with RequestType = Walk, got %d: %s",
i,
len(request.Oids),
strings.Join(request.Oids, ", "),
)
}
for _, oid := range request.Oids {
if oid[0] != '.' {
return fmt.Errorf("request[%d]: all OIDs must begin with a dot, got: %s", i, oid)
}
}
if request.RequestType == Walk && request.MaxRepetitions == 0 {
return fmt.Errorf(
"request[%d]: field max_repetitions is required for RequestType = Walk, "+
"and it mustn't be zero, oid: %s",
i,
request.Oids[0],
)
}
}
return nil
}
func NewRequestValidator(maxTimeoutSeconds uint, maxRetries uint8) *RequestValidator {
return &RequestValidator{maxTimeout: time.Duration(maxTimeoutSeconds) * time.Second, maxRetries: maxRetries}
}