-
Notifications
You must be signed in to change notification settings - Fork 3
/
datastore.go
180 lines (159 loc) · 5.12 KB
/
datastore.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
package main
import (
"fmt"
"time"
)
// Frontend is the HAProxy frontend data structure, serializable to JSON.
type Frontend struct {
ID string `json:"-"`
ProxyType string `json:"-"`
Name string `json:"name"`
Bind string `json:"bind"`
DefaultBackend string `json:"defaultBackend"` // TODO: validation of defaults?
Mode string `json:"mode"` // http
KeepAlive string `json:"keepalive"` // default|close|server-close
Option string `json:"option"` // httplog
Rules []string `json:"rules"`
Meta map[string]string `json:"meta"`
}
// String returns the string representation of a frontend.
func (f *Frontend) String() string {
return f.Name
}
// ToHAProxyFrontend will convert this instance to an haproxy-client.Frontend object.
func (f *Frontend) ToHAProxyFrontend() *Frontend {
return &Frontend{
Name: f.Name,
Bind: f.Bind,
DefaultBackend: f.DefaultBackend,
Mode: f.Mode,
KeepAlive: f.KeepAlive,
Option: f.Option,
Rules: f.Rules,
}
}
// Frontends represents an array of Frontend instances.
type Frontends []*Frontend
// ToInterfaces converts a Frontends instance to an array of empty interfaces.
func (f Frontends) ToInterfaces() []interface{} {
if len(f) == 0 {
return nil
}
ifs := make([]interface{}, len(f))
for i, v := range f {
ifs[i] = v
}
return ifs
}
// ToHAProxyFrontends will convert this instance to an haproxy-client.Frontends object.
func (f Frontends) ToHAProxyFrontends() Frontends {
x := []*Frontend{}
for _, frontend := range f {
x = append(x, frontend.ToHAProxyFrontend())
}
values := Frontends(x)
return values
}
// Backend is the HAProxy backend data structure, serializable to JSON.
type Backend struct {
ID string `json:"-"`
ProxyType string `json:"-"`
Name string `json:"name"`
Version string `json:"version"` // TODO: proper formatting?
Balance string `json:"balance"`
Host string `json:"host"`
Mode string `json:"mode"`
Members BackendMembers `json:"members"`
Meta map[string]string `json:"meta"`
}
// String returns the string representation of a backend.
func (b *Backend) String() string {
return b.Name
}
// ToHAProxyBackend will convert this instance to an haproxy-client.Backend object.
func (b *Backend) ToHAProxyBackend() *Backend {
return &Backend{
Name: b.Name,
Balance: b.Balance,
Host: b.Host,
Mode: b.Mode,
Members: b.Members.ToHAProxyBackendMembers(),
}
}
// Backends represents an array of Backend instances.
type Backends []*Backend
// ToInterfaces converts a Backends instance to an array of empty interfaces.
func (b Backends) ToInterfaces() []interface{} {
if len(b) == 0 {
return nil
}
ifs := make([]interface{}, len(b))
for i, v := range b {
ifs[i] = v
}
return ifs
}
// ToHAProxyBackends will convert this instance to an haproxy-client.Backends object.
func (b Backends) ToHAProxyBackends() Backends {
x := []*Backend{}
for _, backend := range b {
x = append(x, backend.ToHAProxyBackend())
}
values := Backends(x)
return values
}
// BackendMember is a struct representing the individual member nodes of a backend.
type BackendMember struct {
Name string `json:"name"`
Version string `json:"version"`
Host string `json:"host"`
Port int `json:"port"`
LastKnown time.Time `json:"lastKnown"`
Meta map[string]string `json:"meta"`
}
// String returns the string representation of a backend member.
func (m *BackendMember) String() string {
return fmt.Sprintf("%s-%s", m.Name, m.Version)
}
// ToHAProxyBackendMember will convert this instance to an haproxy-client.BackendMember object.
func (m *BackendMember) ToHAProxyBackendMember() *BackendMember {
return &BackendMember{
Name: m.Name,
Host: m.Host,
Port: m.Port,
LastKnown: m.LastKnown,
}
}
// BackendMembers represents an array of BackendMember instances.
type BackendMembers []BackendMember
// ToInterfaces converts a BackendMembers instance to an array of empty interfaces.
func (m BackendMembers) ToInterfaces() []interface{} {
if len(m) == 0 {
return nil
}
ifs := make([]interface{}, len(m))
for i, v := range m {
ifs[i] = v
}
return ifs
}
// ToHAProxyBackendMembers will convert this instance to an haproxy-client.BackendMembers object.
func (m BackendMembers) ToHAProxyBackendMembers() BackendMembers {
x := []BackendMember{}
for _, member := range m {
x = append(x, *member.ToHAProxyBackendMember())
}
values := BackendMembers(x)
return values
}
// Datastore interface defines methods to manipulate backend data.
type Datastore interface {
GetAllFrontends() (Frontends, *Error)
GetFrontend(key string) (*Frontend, *Error)
SaveFrontend(f *Frontend) *Error
DeleteFrontend(key string) *Error
GetAllBackends() (Backends, *Error)
GetBackend(key string) (*Backend, *Error)
SaveBackend(b *Backend) *Error
DeleteBackend(key string) *Error
}