-
Notifications
You must be signed in to change notification settings - Fork 128
/
solver.go
208 lines (203 loc) · 6.27 KB
/
solver.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
package solver
import (
"github.com/hbagdi/deck/crud"
"github.com/hbagdi/deck/diff"
cruds "github.com/hbagdi/deck/solver/kong"
drycrud "github.com/hbagdi/deck/solver/kong/dry"
"github.com/hbagdi/go-kong/kong"
"github.com/pkg/errors"
)
// Solve generates a diff and walks the graph.
func Solve(doneCh chan struct{}, syncer *diff.Syncer,
client *kong.Client, dry bool) []error {
var r *crud.Registry
var err error
if dry {
r, err = buildDryRegistry(client)
} else {
r, err = buildRegistry(client)
}
if err != nil {
return append([]error{}, errors.Wrapf(err, "cannot build registry"))
}
return syncer.Run(doneCh, 10, func(e diff.Event) (crud.Arg, error) {
return r.Do(e.Kind, e.Op, e)
})
}
func buildDryRegistry(client *kong.Client) (*crud.Registry, error) {
var r crud.Registry
err := r.Register("service", &drycrud.ServiceCRUD{})
if err != nil {
return nil, errors.Wrapf(err, "registering 'service' crud")
}
err = r.Register("route", &drycrud.RouteCRUD{})
if err != nil {
return nil, errors.Wrapf(err, "registering 'route' crud")
}
err = r.Register("upstream", &drycrud.UpstreamCRUD{})
if err != nil {
return nil, errors.Wrapf(err, "registering 'upstream' crud")
}
err = r.Register("target", &drycrud.TargetCRUD{})
if err != nil {
return nil, errors.Wrapf(err, "registering 'target' crud")
}
err = r.Register("certificate", &drycrud.CertificateCRUD{})
if err != nil {
return nil, errors.Wrapf(err, "registering 'certificate' crud")
}
err = r.Register("ca_certificate", &drycrud.CACertificateCRUD{})
if err != nil {
return nil, errors.Wrapf(err, "registering 'ca_certificate' crud")
}
err = r.Register("plugin", &drycrud.PluginCRUD{})
if err != nil {
return nil, errors.Wrapf(err, "registering 'plugin' crud")
}
err = r.Register("consumer", &drycrud.ConsumerCRUD{})
if err != nil {
return nil, errors.Wrapf(err, "registering 'consumer' crud")
}
err = r.Register("key-auth", &drycrud.KeyAuthCRUD{})
if err != nil {
return nil, errors.Wrapf(err, "registering 'key-auth' crud")
}
err = r.Register("hmac-auth", &drycrud.HMACAuthCRUD{})
if err != nil {
return nil, errors.Wrapf(err, "registering 'hmac-auth' crud")
}
err = r.Register("jwt-auth", &drycrud.JWTAuthCRUD{})
if err != nil {
return nil, errors.Wrapf(err, "registering 'jwt-auth' crud")
}
err = r.Register("basic-auth", &drycrud.BasicAuthCRUD{})
if err != nil {
return nil, errors.Wrapf(err, "registering 'basic-auth' crud")
}
err = r.Register("acl-group", &drycrud.ACLGroupCRUD{})
if err != nil {
return nil, errors.Wrapf(err, "registering 'acl-group' crud")
}
err = r.Register("oauth2-cred", &drycrud.Oauth2CredCRUD{})
if err != nil {
return nil, errors.Wrapf(err, "registering 'oauth2-cred' crud")
}
return &r, nil
}
func buildRegistry(client *kong.Client) (*crud.Registry, error) {
var r crud.Registry
var err error
service, err := cruds.NewServiceCRUD(client)
if err != nil {
return nil, errors.Wrap(err, "creating a service CRUD")
}
err = r.Register("service", service)
if err != nil {
return nil, errors.Wrapf(err, "registering 'service' crud")
}
route, err := cruds.NewRouteCRUD(client)
if err != nil {
return nil, errors.Wrap(err, "creating a route CRUD")
}
err = r.Register("route", route)
if err != nil {
return nil, errors.Wrapf(err, "registering 'route' crud")
}
upstream, err := cruds.NewUpstreamCRUD(client)
if err != nil {
return nil, errors.Wrap(err, "creating a upstream CRUD")
}
err = r.Register("upstream", upstream)
if err != nil {
return nil, errors.Wrapf(err, "registering 'upstream' crud")
}
target, err := cruds.NewTargetCRUD(client)
if err != nil {
return nil, errors.Wrap(err, "creating a target CRUD")
}
err = r.Register("target", target)
if err != nil {
return nil, errors.Wrapf(err, "registering 'target' crud")
}
certificate, err := cruds.NewCertificateCRUD(client)
if err != nil {
return nil, errors.Wrap(err, "creating a certificate CRUD")
}
err = r.Register("certificate", certificate)
if err != nil {
return nil, errors.Wrapf(err, "registering 'certificate' crud")
}
caCertificate, err := cruds.NewCACertificateCRUD(client)
if err != nil {
return nil, errors.Wrap(err, "creating a caCertificate CRUD")
}
err = r.Register("ca_certificate", caCertificate)
if err != nil {
return nil, errors.Wrapf(err, "registering 'ca_certificate' crud")
}
plugin, err := cruds.NewPluginCRUD(client)
if err != nil {
return nil, errors.Wrap(err, "creating a plugin CRUD")
}
err = r.Register("plugin", plugin)
if err != nil {
return nil, errors.Wrapf(err, "registering 'plugin' crud")
}
consumer, err := cruds.NewConsumerCRUD(client)
if err != nil {
return nil, errors.Wrap(err, "creating a 'consumer' CRUD")
}
err = r.Register("consumer", consumer)
if err != nil {
return nil, errors.Wrapf(err, "registering 'consumer' crud")
}
keyAuth, err := cruds.NewKeyAuthCRUD(client)
if err != nil {
return nil, errors.Wrap(err, "creating a 'key-auth' CRUD")
}
err = r.Register("key-auth", keyAuth)
if err != nil {
return nil, errors.Wrapf(err, "registering 'key-auth' crud")
}
hmacAuth, err := cruds.NewHMACAuthCRUD(client)
if err != nil {
return nil, errors.Wrapf(err, "creating 'hmac-auth' crud")
}
err = r.Register("hmac-auth", hmacAuth)
if err != nil {
return nil, errors.Wrapf(err, "registering 'hmac-auth' crud")
}
jwtAuth, err := cruds.NewJWTAuthCRUD(client)
if err != nil {
return nil, errors.Wrapf(err, "creating 'jwt-auth' crud")
}
err = r.Register("jwt-auth", jwtAuth)
if err != nil {
return nil, errors.Wrapf(err, "registering 'jwt-auth' crud")
}
basicAuth, err := cruds.NewBasicAuthCRUD(client)
if err != nil {
return nil, errors.Wrapf(err, "creating 'basic-auth' crud")
}
err = r.Register("basic-auth", basicAuth)
if err != nil {
return nil, errors.Wrapf(err, "registering 'basic-auth' crud")
}
aclGroups, err := cruds.NewACLGroupCRUD(client)
if err != nil {
return nil, errors.Wrapf(err, "creating 'acl' crud")
}
err = r.Register("acl-group", aclGroups)
if err != nil {
return nil, errors.Wrapf(err, "registering 'acl-group' crud")
}
oauth2Cred, err := cruds.NewOauth2CredCRUD(client)
if err != nil {
return nil, errors.Wrapf(err, "creating 'oauth2-cred' crud")
}
err = r.Register("oauth2-cred", oauth2Cred)
if err != nil {
return nil, errors.Wrapf(err, "registering 'oauth2-cred' crud")
}
return &r, nil
}