-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathchaos.go
More file actions
56 lines (46 loc) · 1.46 KB
/
chaos.go
File metadata and controls
56 lines (46 loc) · 1.46 KB
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
package mailpitclient
import (
"bytes"
"context"
"encoding/json"
"net/http"
)
// GetChaosConfig retrieves the current chaos triggers configuration.
// This API route will return an error if Chaos is not enabled at runtime.
func (c *client) GetChaosConfig(ctx context.Context) (*ChaosResponse, error) {
endpoint := "/chaos"
resp, err := c.makeRequest(ctx, http.MethodGet, endpoint, nil)
if err != nil {
return nil, err
}
var chaos ChaosResponse
if err = c.parseResponse(resp, &chaos); err != nil {
return nil, err
}
return &chaos, nil
}
// SetChaosConfig sets the chaos triggers configuration and returns the updated values.
// This API route will return an error if Chaos is not enabled at runtime.
// If any triggers are omitted from the request, then those are reset to their
// default values with a 0% probability (ie: disabled).
// Setting a blank config will reset all triggers to their default values.
func (c *client) SetChaosConfig(ctx context.Context, config *ChaosTriggers) (*ChaosResponse, error) {
endpoint := "/chaos"
var body bytes.Buffer
if err := json.NewEncoder(&body).Encode(config); err != nil {
return nil, &Error{
Type: ErrorTypeRequest,
Message: "failed to encode chaos config",
Cause: err,
}
}
resp, err := c.makeRequest(ctx, http.MethodPut, endpoint, &body)
if err != nil {
return nil, err
}
var chaos ChaosResponse
if err = c.parseResponse(resp, &chaos); err != nil {
return nil, err
}
return &chaos, nil
}