Skip to content

Commit 4b8d936

Browse files
authored
Merge pull request #19 from benzaita/OX-1168
OX-1168 Work around for non-versioned breaking API change in ChaosSearch
2 parents b5adb49 + 3deae25 commit 4b8d936

File tree

2 files changed

+61
-1
lines changed

2 files changed

+61
-1
lines changed

client/operation_read_object_group.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,54 @@ func (l appLogger) Log(args ...interface{}) {
1919
log.Printf("AWS: %+v", args...)
2020
}
2121

22+
// BUG: ChaosSearch breaking API change now uses two representations of "regex", one as an object, the other as a string.
23+
// The function `recoverFilterJSON` takes a JSON string as input, checks the structure of the "regex" key,
24+
// and returns a modified JSON string according to the specified rules.
25+
// For example:
26+
// `{"AND":[{"field":"key","regex":{"pattern":".*","strict":true}}]}`
27+
// is converted back into
28+
// `{"AND":[{"field":"key","regex":".*"}]}`
29+
func recoverFilterJSON(input string) (string, error) {
30+
// Unmarshal the input JSON string into a map structure.
31+
var data map[string]interface{}
32+
if err := json.Unmarshal([]byte(input), &data); err != nil {
33+
return "", err // Return an error if JSON is invalid.
34+
}
35+
36+
// Iterate over the "AND" array in the JSON structure.
37+
for _, item := range data["AND"].([]interface{}) {
38+
entry := item.(map[string]interface{}) // Cast to a map.
39+
regex := entry["regex"] // Access the "regex" key.
40+
41+
// Check the type of the "regex" key.
42+
switch regexValue := regex.(type) {
43+
case map[string]interface{}:
44+
// If "regex" is a map, check if the "strict" key is true.
45+
if strict, ok := regexValue["strict"].(bool); ok && strict {
46+
// If "strict" is true, set "regex" to the value of the "pattern" key.
47+
entry["regex"] = regexValue["pattern"]
48+
} else {
49+
// If "strict" is not true, return an error.
50+
return "", fmt.Errorf("strict key is not true")
51+
}
52+
case string:
53+
// If "regex" is a string, no action is needed.
54+
default:
55+
// Return an error if "regex" is neither a string nor a map.
56+
return "", fmt.Errorf("unexpected type for regex key")
57+
}
58+
}
59+
60+
// Marshal the modified map structure back into a JSON string.
61+
outputBytes, err := json.Marshal(data)
62+
if err != nil {
63+
return "", err // Return an error if marshaling fails.
64+
}
65+
66+
// Return the modified JSON string.
67+
return string(outputBytes), nil
68+
}
69+
2270
func (client *Client) ReadObjectGroup(ctx context.Context, req *ReadObjectGroupRequest) (*ReadObjectGroupResponse, error) {
2371
var resp ReadObjectGroupResponse
2472

@@ -141,6 +189,18 @@ func mapBucketTaggingToResponse(tagging *s3.GetBucketTaggingOutput, v *ReadObjec
141189
return err
142190
}
143191

192+
193+
// NOTE: this is to work around a non-versioned breaking API change in ChaosSearch.
194+
// The API might return a slightly modified JSON string.
195+
// Here, we recover the "original" format in case that happens.
196+
inputFilterJSON := v.FilterJSON
197+
outputJSON, err := recoverFilterJSON(inputFilterJSON)
198+
if err != nil {
199+
return err
200+
} else {
201+
v.FilterJSON = outputJSON
202+
}
203+
144204
var retentionObject struct {
145205
Overall int `json:"overall"`
146206
}

terraformprovider/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ HOSTNAME=registry.terraform.io
33
NAMESPACE=benzaita
44
NAME=chaossearch
55
BINARY=terraform-provider-${NAME}
6-
VERSION=0.12.2
6+
VERSION=0.12.3
77
OS_ARCH=$(shell go env GOOS)_$(shell go env GOARCH)
88

99
default: install

0 commit comments

Comments
 (0)