forked from TykTechnologies/tyk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice_discovery.go
276 lines (234 loc) · 6.78 KB
/
service_discovery.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
package main
import (
"io/ioutil"
"net/http"
"strconv"
"strings"
"github.com/Jeffail/gabs"
"github.com/TykTechnologies/tyk/apidef"
)
const arrayName = "tyk_array"
type ServiceDiscovery struct {
spec *apidef.ServiceDiscoveryConfiguration
isNested bool
isTargetList bool
endpointReturnsList bool
portSeperate bool
dataPath string
parentPath string
portPath string
targetPath string
}
func (s *ServiceDiscovery) Init(spec *apidef.ServiceDiscoveryConfiguration) {
s.spec = spec
s.isNested = spec.UseNestedQuery
s.isTargetList = spec.UseTargetList
s.endpointReturnsList = spec.EndpointReturnsList
s.targetPath = spec.TargetPath
if spec.PortDataPath != "" {
s.portSeperate = true
s.portPath = spec.PortDataPath
}
if spec.ParentDataPath != "" {
s.parentPath = spec.ParentDataPath
}
s.dataPath = spec.DataPath
}
func (s *ServiceDiscovery) getServiceData(name string) (string, error) {
log.Debug("Getting ", name)
resp, err := http.Get(name)
if err != nil {
return "", err
}
defer resp.Body.Close()
contents, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
}
return string(contents), nil
}
func (s *ServiceDiscovery) decodeToNameSpace(namespace string, jsonParsed *gabs.Container) interface{} {
log.Debug("Namespace: ", namespace)
value := jsonParsed.Path(namespace).Data()
return value
}
func (s *ServiceDiscovery) decodeToNameSpaceAsArray(namespace string, jsonParsed *gabs.Container) []*gabs.Container {
log.Debug("Array Namespace: ", namespace)
log.Debug("Container: ", jsonParsed)
value, _ := jsonParsed.Path(namespace).Children()
log.Debug("Array value:", value)
return value
}
func (s *ServiceDiscovery) addPortFromObject(host string, obj *gabs.Container) string {
if !s.portSeperate {
return host
}
// Grab the port object
port := s.decodeToNameSpace(s.portPath, obj)
switch x := port.(type) {
case []interface{}:
port = x[0]
}
var portToUse string
switch x := port.(type) {
case string:
portToUse = x
case float64:
portToUse = strconv.Itoa(int(x))
}
return host + ":" + portToUse
}
func (s *ServiceDiscovery) NestedObject(item *gabs.Container) string {
parentData := s.decodeToNameSpace(s.parentPath, item)
// Get the data path from the decoded object
subContainer := gabs.Container{}
switch x := parentData.(type) {
case string:
s.ParseObject(x, &subContainer)
default:
log.Debug("Get Nested Object: parentData is not a string")
return ""
}
return s.Object(&subContainer)
}
func (s *ServiceDiscovery) Object(item *gabs.Container) string {
hostnameData := s.decodeToNameSpace(s.dataPath, item)
if str, ok := hostnameData.(string); ok {
// Get the port
str = s.addPortFromObject(str, item)
return str
}
log.Warning("Get Object: hostname is not a string")
return ""
}
func (s *ServiceDiscovery) Hostname(item *gabs.Container) string {
var hostname string
// Get a nested object
if s.isNested {
hostname = s.NestedObject(item)
} else {
hostname = s.Object(item)
}
return hostname
}
func (s *ServiceDiscovery) isList(val string) bool {
return strings.HasPrefix(val, "[")
}
func (s *ServiceDiscovery) SubObjectFromList(objList *gabs.Container) []string {
hostList := []string{}
var hostname string
var set []*gabs.Container
if s.endpointReturnsList {
// pre-process the object since we've nested it
set = s.decodeToNameSpaceAsArray(arrayName, objList)
log.Debug("set: ", set)
} else if s.isNested { // It's an object, but the value may be nested
// Get the actual raw string object
parentData := s.decodeToNameSpace(s.parentPath, objList)
// Get the data path from the decoded object
subContainer := gabs.Container{}
// Now check if this string is a list
nestedString, ok := parentData.(string)
if !ok {
log.Debug("parentData is not a string")
return hostList
}
if s.isList(nestedString) {
log.Debug("Yup, it's a list")
jsonData := s.rawListToObj(nestedString)
s.ParseObject(jsonData, &subContainer)
set = s.decodeToNameSpaceAsArray(arrayName, &subContainer)
// Hijack this here because we need to use a non-nested get
for _, item := range set {
log.Debug("Child in list: ", item)
hostname = s.Object(item) + s.targetPath
// Add to list
hostList = append(hostList, hostname)
}
return hostList
}
log.Debug("Not a list")
s.ParseObject(nestedString, &subContainer)
set = s.decodeToNameSpaceAsArray(s.dataPath, objList)
log.Debug("set (object list): ", objList)
} else if s.parentPath != "" {
set = s.decodeToNameSpaceAsArray(s.parentPath, objList)
}
for _, item := range set {
log.Debug("Child in list: ", item)
hostname = s.Hostname(item) + s.targetPath
// Add to list
hostList = append(hostList, hostname)
}
return hostList
}
func (s *ServiceDiscovery) SubObject(obj *gabs.Container) string {
return s.Hostname(obj) + s.targetPath
}
func (s *ServiceDiscovery) rawListToObj(rawData string) string {
// Modify to turn a list object into a regular object
return `{"` + arrayName + `":` + rawData + `}`
}
func (s *ServiceDiscovery) ParseObject(contents string, jsonParsed *gabs.Container) error {
log.Debug("Parsing raw data: ", contents)
jp, err := gabs.ParseJSON([]byte(contents))
if err != nil {
log.Error(err)
}
*jsonParsed = *jp
log.Debug("Got:", jsonParsed)
return err
}
func (s *ServiceDiscovery) ProcessRawData(rawData string) (*apidef.HostList, error) {
var jsonParsed gabs.Container
hostlist := apidef.NewHostList()
if s.endpointReturnsList {
// Convert to an object
jsonData := s.rawListToObj(rawData)
if err := s.ParseObject(jsonData, &jsonParsed); err != nil {
log.Error("Parse object failed: ", err)
return nil, err
}
log.Debug("Parsed object list: ", jsonParsed)
// Treat JSON as a list and then apply the data path
if s.isTargetList {
// Get all values
asList := s.SubObjectFromList(&jsonParsed)
log.Debug("Host list:", asList)
hostlist.Set(asList)
return hostlist, nil
}
// Get the top value
list := s.SubObjectFromList(&jsonParsed)
var host string
for _, v := range list {
host = v
break
}
hostlist.Set([]string{host})
return hostlist, nil
}
// It's an object
s.ParseObject(rawData, &jsonParsed)
if s.isTargetList {
// It's a list object
log.Debug("It's a target list - getting sub object from list")
log.Debug("Passing in: ", jsonParsed)
asList := s.SubObjectFromList(&jsonParsed)
hostlist.Set(asList)
log.Debug("Got from object: ", hostlist)
return hostlist, nil
}
// It's a single object
host := s.SubObject(&jsonParsed)
hostlist.Set([]string{host})
return hostlist, nil
}
func (s *ServiceDiscovery) Target(serviceURL string) (*apidef.HostList, error) {
// Get the data
rawData, err := s.getServiceData(serviceURL)
if err != nil {
return nil, err
}
return s.ProcessRawData(rawData)
}