Skip to content

Commit 5d43c69

Browse files
committed
add tests for monitor update, fixed schema while testing all http monitor fields
1 parent 0076521 commit 5d43c69

File tree

3 files changed

+212
-77
lines changed

3 files changed

+212
-77
lines changed

libs/go-kibana-rest/kbapi/api._.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ func New(c *resty.Client) *API {
121121
Add: newKibanaSyntheticsMonitorAddFunc(c),
122122
Delete: newKibanaSyntheticsMonitorDeleteFunc(c),
123123
Get: newKibanaSyntheticsMonitorGetFunc(c),
124+
Update: newKibanaSyntheticsMonitorUpdateFunc(c),
124125
},
125126
PrivateLocation: &KibanaSyntheticsPrivateLocationAPI{
126127
Create: newKibanaSyntheticsPrivateLocationCreateFunc(c),

libs/go-kibana-rest/kbapi/api.kibana_synthetics.go

Lines changed: 62 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ type KibanaSyntheticsMonitorAPI struct {
6565
Add KibanaSyntheticsMonitorAdd
6666
Delete KibanaSyntheticsMonitorDelete
6767
Get KibanaSyntheticsMonitorGet
68+
Update KibanaSyntheticsMonitorUpdate
6869
}
6970

7071
type KibanaSyntheticsPrivateLocationAPI struct {
@@ -78,29 +79,27 @@ type SyntheticsStatusConfig struct {
7879
}
7980

8081
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"`
8384
}
8485

8586
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"`
9999
}
100100

101101
type SyntheticsMonitorConfig struct {
102102
Name string `json:"name"`
103-
Type MonitorType `json:"type"`
104103
Schedule MonitorSchedule `json:"schedule,omitempty"`
105104
Locations []MonitorLocation `json:"locations,omitempty"`
106105
PrivateLocations []string `json:"private_locations,omitempty"`
@@ -110,7 +109,7 @@ type SyntheticsMonitorConfig struct {
110109
APMServiceName string `json:"service.name,omitempty"`
111110
TimeoutSeconds int `json:"timeout,omitempty"`
112111
Namespace string `json:"namespace,omitempty"`
113-
Params string `json:"params,omitempty"`
112+
Params JsonObject `json:"params,omitempty"`
114113
RetestOnFailure *bool `json:"retest_on_failure,omitempty"`
115114
}
116115

@@ -143,7 +142,7 @@ type SyntheticsMonitor struct {
143142
Enabled *bool `json:"enabled,omitempty"`
144143
Alert *MonitorAlertConfig `json:"alert,omitempty"`
145144
Schedule *MonitorScheduleConfig `json:"schedule,omitempty"`
146-
Timeout string `json:"timeout,omitempty"`
145+
Timeout json.Number `json:"timeout,omitempty"`
147146
Locations []MonitorLocationConfig `json:"locations,omitempty"`
148147
Origin string `json:"origin,omitempty"`
149148
MaxAttempts int `json:"max_attempts"`
@@ -182,6 +181,8 @@ type MonitorDeleteStatus struct {
182181

183182
type KibanaSyntheticsMonitorAdd func(config SyntheticsMonitorConfig, fields HTTPMonitorFields, namespace string) (*SyntheticsMonitor, error)
184183

184+
type KibanaSyntheticsMonitorUpdate func(id MonitorID, config SyntheticsMonitorConfig, fields HTTPMonitorFields, namespace string) (*SyntheticsMonitor, error)
185+
185186
type KibanaSyntheticsMonitorGet func(id MonitorID, namespace string) (*SyntheticsMonitor, error)
186187

187188
type KibanaSyntheticsMonitorDelete func(namespace string, ids ...MonitorID) ([]MonitorDeleteStatus, error)
@@ -195,7 +196,7 @@ type KibanaSyntheticsPrivateLocationDelete func(id string, namespace string) err
195196
func newKibanaSyntheticsPrivateLocationGetFunc(c *resty.Client) KibanaSyntheticsPrivateLocationGet {
196197
return func(idOrLabel string, namespace string) (*PrivateLocation, error) {
197198

198-
path := fmt.Sprintf("%s/%s", basePath(namespace, privateLocationsSuffix), idOrLabel)
199+
path := basePathWithId(namespace, privateLocationsSuffix, idOrLabel)
199200
log.Debugf("URL to get private locations: %s", path)
200201
resp, err := c.R().Get(path)
201202
if err = handleKibanaError(err, resp); err != nil {
@@ -207,7 +208,7 @@ func newKibanaSyntheticsPrivateLocationGetFunc(c *resty.Client) KibanaSynthetics
207208

208209
func newKibanaSyntheticsPrivateLocationDeleteFunc(c *resty.Client) KibanaSyntheticsPrivateLocationDelete {
209210
return func(id string, namespace string) error {
210-
path := fmt.Sprintf("%s/%s", basePath(namespace, privateLocationsSuffix), id)
211+
path := basePathWithId(namespace, privateLocationsSuffix, id)
211212
log.Debugf("URL to delete private locations: %s", path)
212213
resp, err := c.R().Delete(path)
213214
err = handleKibanaError(err, resp)
@@ -217,7 +218,7 @@ func newKibanaSyntheticsPrivateLocationDeleteFunc(c *resty.Client) KibanaSynthet
217218

218219
func newKibanaSyntheticsMonitorGetFunc(c *resty.Client) KibanaSyntheticsMonitorGet {
219220
return func(id MonitorID, namespace string) (*SyntheticsMonitor, error) {
220-
path := fmt.Sprintf("%s/%s", basePath(namespace, monitorsSuffix), id)
221+
path := basePathWithId(namespace, monitorsSuffix, id)
221222
log.Debugf("URL to create monitor: %s", path)
222223

223224
resp, err := c.R().Get(path)
@@ -258,20 +259,26 @@ func newKibanaSyntheticsPrivateLocationCreateFunc(c *resty.Client) KibanaSynthet
258259
}
259260
}
260261

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+
261276
func newKibanaSyntheticsMonitorAddFunc(c *resty.Client) KibanaSyntheticsMonitorAdd {
262277
return func(config SyntheticsMonitorConfig, fields HTTPMonitorFields, namespace string) (*SyntheticsMonitor, error) {
263278

264279
path := basePath(namespace, monitorsSuffix)
265280
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)
275282
resp, err := c.R().SetBody(data).Post(path)
276283
if err := handleKibanaError(err, resp); err != nil {
277284
return nil, err
@@ -280,6 +287,27 @@ func newKibanaSyntheticsMonitorAddFunc(c *resty.Client) KibanaSyntheticsMonitorA
280287
}
281288
}
282289

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+
283311
func unmarshal[T interface{}](resp *resty.Response, result T) (*T, error) {
284312
respBody := resp.Body()
285313
err := json.Unmarshal(respBody, &result)
@@ -305,6 +333,10 @@ func handleKibanaError(err error, resp *resty.Response) error {
305333
return nil
306334
}
307335

336+
func basePathWithId(namespace, suffix string, id any) string {
337+
return fmt.Sprintf("%s/%s", basePath(namespace, suffix), id)
338+
}
339+
308340
func basePath(namespace, suffix string) string {
309341
return namespaceBasesPath(namespace, basePathKibanaSynthetics, suffix)
310342
}
@@ -316,5 +348,3 @@ func namespaceBasesPath(namespace, basePath, suffix string) string {
316348

317349
return fmt.Sprintf("/s/%s%s%s", namespace, basePath, suffix)
318350
}
319-
320-
//TODO: Monitor - Update https://www.elastic.co/guide/en/kibana/current/synthetics-apis.html

0 commit comments

Comments
 (0)