@@ -65,6 +65,7 @@ type KibanaSyntheticsMonitorAPI struct {
65
65
Add KibanaSyntheticsMonitorAdd
66
66
Delete KibanaSyntheticsMonitorDelete
67
67
Get KibanaSyntheticsMonitorGet
68
+ Update KibanaSyntheticsMonitorUpdate
68
69
}
69
70
70
71
type KibanaSyntheticsPrivateLocationAPI struct {
@@ -78,29 +79,27 @@ type SyntheticsStatusConfig struct {
78
79
}
79
80
80
81
type MonitorAlertConfig struct {
81
- Status SyntheticsStatusConfig `json:"status,omitempty"`
82
- Tls SyntheticsStatusConfig `json:"tls,omitempty"`
82
+ Status * SyntheticsStatusConfig `json:"status,omitempty"`
83
+ Tls * SyntheticsStatusConfig `json:"tls,omitempty"`
83
84
}
84
85
85
86
type HTTPMonitorFields struct {
86
- Url string `json:"url"`
87
- SslSetting JsonObject `json:"ssl,omitempty"` //https://www.elastic.co/guide/en/beats/heartbeat/current/configuration-ssl.html
88
- MaxRedirects int `json:"max_redirects,omitempty"`
89
- Mode HttpMonitorMode `json:"mode,omitempty"`
90
- Ipv4 * bool `json:"ipv4,omitempty"`
91
- Ipv6 * bool `json:"ipv6,omitempty"`
92
- Username string `json:"username,omitempty"`
93
- Password string `json:"password,omitempty"`
94
- ProxyHeader JsonObject `json:"proxy_headers,omitempty"`
95
- ProxyUrl string `json:"proxy_url,omitempty"`
96
- Response JsonObject `json:"response,omitempty"`
97
- ResponseIncludeBody * bool `json:"response.include_body,omitempty"` //TODO: test with Response
98
- Check JsonObject `json:"check,omitempty"`
87
+ Url string `json:"url"`
88
+ SslSetting JsonObject `json:"ssl,omitempty"` //https://www.elastic.co/guide/en/beats/heartbeat/current/configuration-ssl.html
89
+ MaxRedirects string `json:"max_redirects,omitempty"`
90
+ Mode HttpMonitorMode `json:"mode,omitempty"`
91
+ Ipv4 * bool `json:"ipv4,omitempty"`
92
+ Ipv6 * bool `json:"ipv6,omitempty"`
93
+ Username string `json:"username,omitempty"`
94
+ Password string `json:"password,omitempty"`
95
+ ProxyHeader JsonObject `json:"proxy_headers,omitempty"`
96
+ ProxyUrl string `json:"proxy_url,omitempty"`
97
+ Response JsonObject `json:"response,omitempty"`
98
+ Check JsonObject `json:"check,omitempty"`
99
99
}
100
100
101
101
type SyntheticsMonitorConfig struct {
102
102
Name string `json:"name"`
103
- Type MonitorType `json:"type"`
104
103
Schedule MonitorSchedule `json:"schedule,omitempty"`
105
104
Locations []MonitorLocation `json:"locations,omitempty"`
106
105
PrivateLocations []string `json:"private_locations,omitempty"`
@@ -110,7 +109,7 @@ type SyntheticsMonitorConfig struct {
110
109
APMServiceName string `json:"service.name,omitempty"`
111
110
TimeoutSeconds int `json:"timeout,omitempty"`
112
111
Namespace string `json:"namespace,omitempty"`
113
- Params string `json:"params,omitempty"`
112
+ Params JsonObject `json:"params,omitempty"`
114
113
RetestOnFailure * bool `json:"retest_on_failure,omitempty"`
115
114
}
116
115
@@ -143,7 +142,7 @@ type SyntheticsMonitor struct {
143
142
Enabled * bool `json:"enabled,omitempty"`
144
143
Alert * MonitorAlertConfig `json:"alert,omitempty"`
145
144
Schedule * MonitorScheduleConfig `json:"schedule,omitempty"`
146
- Timeout string `json:"timeout,omitempty"`
145
+ Timeout json. Number `json:"timeout,omitempty"`
147
146
Locations []MonitorLocationConfig `json:"locations,omitempty"`
148
147
Origin string `json:"origin,omitempty"`
149
148
MaxAttempts int `json:"max_attempts"`
@@ -182,6 +181,8 @@ type MonitorDeleteStatus struct {
182
181
183
182
type KibanaSyntheticsMonitorAdd func (config SyntheticsMonitorConfig , fields HTTPMonitorFields , namespace string ) (* SyntheticsMonitor , error )
184
183
184
+ type KibanaSyntheticsMonitorUpdate func (id MonitorID , config SyntheticsMonitorConfig , fields HTTPMonitorFields , namespace string ) (* SyntheticsMonitor , error )
185
+
185
186
type KibanaSyntheticsMonitorGet func (id MonitorID , namespace string ) (* SyntheticsMonitor , error )
186
187
187
188
type KibanaSyntheticsMonitorDelete func (namespace string , ids ... MonitorID ) ([]MonitorDeleteStatus , error )
@@ -195,7 +196,7 @@ type KibanaSyntheticsPrivateLocationDelete func(id string, namespace string) err
195
196
func newKibanaSyntheticsPrivateLocationGetFunc (c * resty.Client ) KibanaSyntheticsPrivateLocationGet {
196
197
return func (idOrLabel string , namespace string ) (* PrivateLocation , error ) {
197
198
198
- path := fmt . Sprintf ( "%s/%s" , basePath ( namespace , privateLocationsSuffix ) , idOrLabel )
199
+ path := basePathWithId ( namespace , privateLocationsSuffix , idOrLabel )
199
200
log .Debugf ("URL to get private locations: %s" , path )
200
201
resp , err := c .R ().Get (path )
201
202
if err = handleKibanaError (err , resp ); err != nil {
@@ -207,7 +208,7 @@ func newKibanaSyntheticsPrivateLocationGetFunc(c *resty.Client) KibanaSynthetics
207
208
208
209
func newKibanaSyntheticsPrivateLocationDeleteFunc (c * resty.Client ) KibanaSyntheticsPrivateLocationDelete {
209
210
return func (id string , namespace string ) error {
210
- path := fmt . Sprintf ( "%s/%s" , basePath ( namespace , privateLocationsSuffix ) , id )
211
+ path := basePathWithId ( namespace , privateLocationsSuffix , id )
211
212
log .Debugf ("URL to delete private locations: %s" , path )
212
213
resp , err := c .R ().Delete (path )
213
214
err = handleKibanaError (err , resp )
@@ -217,7 +218,7 @@ func newKibanaSyntheticsPrivateLocationDeleteFunc(c *resty.Client) KibanaSynthet
217
218
218
219
func newKibanaSyntheticsMonitorGetFunc (c * resty.Client ) KibanaSyntheticsMonitorGet {
219
220
return func (id MonitorID , namespace string ) (* SyntheticsMonitor , error ) {
220
- path := fmt . Sprintf ( "%s/%s" , basePath ( namespace , monitorsSuffix ) , id )
221
+ path := basePathWithId ( namespace , monitorsSuffix , id )
221
222
log .Debugf ("URL to create monitor: %s" , path )
222
223
223
224
resp , err := c .R ().Get (path )
@@ -258,20 +259,26 @@ func newKibanaSyntheticsPrivateLocationCreateFunc(c *resty.Client) KibanaSynthet
258
259
}
259
260
}
260
261
262
+ func newKibanaSyntheticsMonitorUpdateFunc (c * resty.Client ) KibanaSyntheticsMonitorUpdate {
263
+ return func (id MonitorID , config SyntheticsMonitorConfig , fields HTTPMonitorFields , namespace string ) (* SyntheticsMonitor , error ) {
264
+
265
+ path := basePathWithId (namespace , monitorsSuffix , id )
266
+ log .Debugf ("URL to create monitor: %s" , path )
267
+ data := buildMonitorJson (config , fields )
268
+ resp , err := c .R ().SetBody (data ).Put (path )
269
+ if err := handleKibanaError (err , resp ); err != nil {
270
+ return nil , err
271
+ }
272
+ return unmarshal (resp , SyntheticsMonitor {})
273
+ }
274
+ }
275
+
261
276
func newKibanaSyntheticsMonitorAddFunc (c * resty.Client ) KibanaSyntheticsMonitorAdd {
262
277
return func (config SyntheticsMonitorConfig , fields HTTPMonitorFields , namespace string ) (* SyntheticsMonitor , error ) {
263
278
264
279
path := basePath (namespace , monitorsSuffix )
265
280
log .Debugf ("URL to create monitor: %s" , path )
266
-
267
- data := struct {
268
- SyntheticsMonitorConfig
269
- HTTPMonitorFields
270
- }{
271
- config ,
272
- fields ,
273
- }
274
-
281
+ data := buildMonitorJson (config , fields )
275
282
resp , err := c .R ().SetBody (data ).Post (path )
276
283
if err := handleKibanaError (err , resp ); err != nil {
277
284
return nil , err
@@ -280,6 +287,27 @@ func newKibanaSyntheticsMonitorAddFunc(c *resty.Client) KibanaSyntheticsMonitorA
280
287
}
281
288
}
282
289
290
+ // current idea here is to switch fields HTTPMonitorFields to interface{} and to
291
+ // type switch in the function for future monitor types
292
+ func buildMonitorJson (config SyntheticsMonitorConfig , fields HTTPMonitorFields ) interface {} {
293
+
294
+ type MonitorTypeConfig struct {
295
+ Type MonitorType `json:"type"`
296
+ }
297
+
298
+ mType := MonitorTypeConfig {Type : Http }
299
+
300
+ return struct {
301
+ SyntheticsMonitorConfig
302
+ MonitorTypeConfig
303
+ HTTPMonitorFields
304
+ }{
305
+ config ,
306
+ mType ,
307
+ fields ,
308
+ }
309
+ }
310
+
283
311
func unmarshal [T interface {}](resp * resty.Response , result T ) (* T , error ) {
284
312
respBody := resp .Body ()
285
313
err := json .Unmarshal (respBody , & result )
@@ -305,6 +333,10 @@ func handleKibanaError(err error, resp *resty.Response) error {
305
333
return nil
306
334
}
307
335
336
+ func basePathWithId (namespace , suffix string , id any ) string {
337
+ return fmt .Sprintf ("%s/%s" , basePath (namespace , suffix ), id )
338
+ }
339
+
308
340
func basePath (namespace , suffix string ) string {
309
341
return namespaceBasesPath (namespace , basePathKibanaSynthetics , suffix )
310
342
}
@@ -316,5 +348,3 @@ func namespaceBasesPath(namespace, basePath, suffix string) string {
316
348
317
349
return fmt .Sprintf ("/s/%s%s%s" , namespace , basePath , suffix )
318
350
}
319
-
320
- //TODO: Monitor - Update https://www.elastic.co/guide/en/kibana/current/synthetics-apis.html
0 commit comments