-
Notifications
You must be signed in to change notification settings - Fork 17
/
types.go
114 lines (90 loc) · 2.47 KB
/
types.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
package types
import (
"fmt"
"strings"
"time"
)
var (
NAT = "nat"
ROUTE = "route"
TUNNEL = "tunnel"
)
var (
Protocols = []string{"tcp", "udp"}
Schedulers = []string{"rr", "wrr", "lc"}
)
var (
ErrServiceNotFound = ErrNotFound("service not found")
ErrDestinationNotFound = ErrNotFound("destination not found")
ErrCheckNotFound = ErrNotFound("check not found")
ErrServiceConflict = ErrConflict("service already exists")
ErrDestinationConflict = ErrConflict("destination already exists")
)
type ErrConflict string
func (e ErrConflict) Error() string {
return string(e)
}
type ErrNotFound string
func (e ErrNotFound) Error() string {
return string(e)
}
type ErrValidation struct {
Type string
Errors map[string]string
}
func (e ErrValidation) Error() string {
return fmt.Sprintf("Validation failed. %#v", e)
}
type Service struct {
Name string `validate:"required"`
Address string
Port uint16 `validate:"gte=1,lte=47808,required"`
Protocol string `validate:"protocols,required"`
Scheduler string `validate:"schedulers,required"`
Mode string `validate:"required"`
Persistent uint32
}
func (svc Service) GetId() string {
return svc.Name
}
func (svc Service) IsNat() bool {
return svc.Mode == NAT
}
func (svc Service) IpvsId() string {
return fmt.Sprintf("%s-%d-%s", svc.Address, svc.Port, svc.Protocol)
}
func (s Service) Equal(svc Service) bool {
return s.Name == svc.Name || (s.Address == svc.Address && s.Port == svc.Port && s.Protocol == svc.Protocol)
}
type Destination struct {
Name string `validate:"required"`
Address string `validate:"required"`
Port uint16 `validate:"gte=1,lte=47808,required"`
Weight int32
Mode string `validate:"required"`
ServiceId string `validate:"required"`
}
func (dst Destination) GetId() string {
return dst.Name
}
func (dst Destination) IpvsId() string {
return fmt.Sprintf("%s-%d", strings.Replace(dst.Address, ".", "_", 4), dst.Port)
}
func (d Destination) Equal(dst Destination) bool {
return d.Name == dst.Name || (d.Address == dst.Address && d.Port == dst.Port)
}
type DestinationList []Destination
func (l DestinationList) Len() int { return len(l) }
func (l DestinationList) Swap(i, j int) { l[i], l[j] = l[j], l[i] }
func (l DestinationList) Less(i, j int) bool {
return l[i].Name < l[j].Name
}
type CheckSpec struct {
ServiceID string
Type string
HttpPath string
Script string
Shell string
Interval time.Duration
Timeout time.Duration
}