-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for multiple HTTP methods, assertions, and request body in controller #58
base: main
Are you sure you want to change the base?
Changes from all commits
bcdac27
328acb1
ef95147
9106ed1
031c51b
ae9d304
c0a3f60
cdd3a0a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,8 +18,10 @@ package external | |
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
"strconv" | ||
"strings" | ||
"time" | ||
|
||
"github.com/checkly/checkly-go-sdk" | ||
|
@@ -32,23 +34,20 @@ type Check struct { | |
Frequency int | ||
MaxResponseTime int | ||
Endpoint string | ||
SuccessCode string | ||
GroupID int64 | ||
ID string | ||
Muted bool | ||
Labels map[string]string | ||
Assertions []checkly.Assertion | ||
Method string | ||
Body string | ||
BodyType string | ||
} | ||
|
||
func checklyCheck(apiCheck Check) (check checkly.Check, err error) { | ||
|
||
shouldFail, err := shouldFail(apiCheck.SuccessCode) | ||
if err != nil { | ||
return | ||
} | ||
|
||
tags := getTags(apiCheck.Labels) | ||
tags = append(tags, "checkly-operator") | ||
tags = append(tags, apiCheck.Namespace) | ||
tags = append(tags, "checkly-operator", apiCheck.Namespace) | ||
|
||
alertSettings := checkly.AlertSettings{ | ||
EscalationType: checkly.RunBased, | ||
|
@@ -67,49 +66,76 @@ func checklyCheck(apiCheck Check) (check checkly.Check, err error) { | |
}, | ||
} | ||
|
||
shouldFail := false | ||
assertions := apiCheck.Assertions | ||
if len(assertions) == 0 { | ||
assertions = []checkly.Assertion{ | ||
{ | ||
Source: checkly.StatusCode, | ||
Comparison: checkly.Equals, | ||
Target: "200", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would change this to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If that is done, then |
||
}, | ||
} | ||
} else { | ||
for _, assertion := range assertions { | ||
if assertion.Source == checkly.StatusCode && assertion.Comparison == checkly.Equals && assertion.Target >= "400" { | ||
shouldFail = true | ||
break | ||
} | ||
} | ||
} | ||
|
||
method := http.MethodGet | ||
if apiCheck.Method != "" { | ||
method = apiCheck.Method | ||
} | ||
|
||
body := apiCheck.Body | ||
bodyType := strings.ToUpper(apiCheck.BodyType) | ||
if bodyType == "" { | ||
bodyType = "NONE" | ||
} | ||
|
||
if bodyType == "JSON" { | ||
var jsonBody map[string]interface{} | ||
err := json.Unmarshal([]byte(body), &jsonBody) | ||
if err != nil { | ||
return check, fmt.Errorf("invalid JSON body: %w", err) | ||
} | ||
|
||
formattedBody, err := json.Marshal(jsonBody) | ||
if err != nil { | ||
return check, fmt.Errorf("failed to format JSON body: %w", err) | ||
} | ||
|
||
body = string(formattedBody) | ||
} | ||
|
||
check = checkly.Check{ | ||
Name: apiCheck.Name, | ||
Type: checkly.TypeAPI, | ||
Frequency: checkValueInt(apiCheck.Frequency, 5), | ||
DegradedResponseTime: 5000, | ||
MaxResponseTime: checkValueInt(apiCheck.MaxResponseTime, 15000), | ||
Activated: true, | ||
Muted: apiCheck.Muted, // muted for development | ||
ShouldFail: shouldFail, | ||
DoubleCheck: false, | ||
SSLCheck: false, | ||
LocalSetupScript: "", | ||
LocalTearDownScript: "", | ||
Locations: []string{}, | ||
Tags: tags, | ||
AlertSettings: alertSettings, | ||
UseGlobalAlertSettings: false, | ||
GroupID: apiCheck.GroupID, | ||
Name: apiCheck.Name, | ||
Type: checkly.TypeAPI, | ||
Frequency: checkValueInt(apiCheck.Frequency, 5), | ||
DegradedResponseTime: 5000, | ||
MaxResponseTime: checkValueInt(apiCheck.MaxResponseTime, 15000), | ||
Activated: true, | ||
Muted: apiCheck.Muted, | ||
ShouldFail: shouldFail, | ||
DoubleCheck: false, | ||
SSLCheck: false, | ||
AlertSettings: alertSettings, | ||
Locations: []string{}, | ||
Tags: tags, | ||
Request: checkly.Request{ | ||
Method: http.MethodGet, | ||
URL: apiCheck.Endpoint, | ||
Headers: []checkly.KeyValue{ | ||
// { | ||
// Key: "X-Test", | ||
// Value: "foo", | ||
// }, | ||
}, | ||
QueryParameters: []checkly.KeyValue{ | ||
// { | ||
// Key: "query", | ||
// Value: "foo", | ||
// }, | ||
}, | ||
Assertions: []checkly.Assertion{ | ||
{ | ||
Source: checkly.StatusCode, | ||
Comparison: checkly.Equals, | ||
Target: apiCheck.SuccessCode, | ||
}, | ||
}, | ||
Body: "", | ||
BodyType: "NONE", | ||
Method: method, | ||
URL: apiCheck.Endpoint, | ||
Assertions: assertions, | ||
Headers: []checkly.KeyValue{}, | ||
QueryParameters: []checkly.KeyValue{}, | ||
Body: body, | ||
BodyType: bodyType, | ||
}, | ||
UseGlobalAlertSettings: false, | ||
GroupID: apiCheck.GroupID, | ||
} | ||
|
||
return | ||
|
@@ -162,15 +188,3 @@ func Delete(ID string, client checkly.Client) (err error) { | |
|
||
return | ||
} | ||
|
||
func shouldFail(successCode string) (bool, error) { | ||
code, err := strconv.Atoi(successCode) | ||
if err != nil { | ||
return false, err | ||
} | ||
if code < 400 { | ||
return false, nil | ||
} else { | ||
return true, nil | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe this would no longer be
required
. However it should be kept for B/C and marked deprecated.