Skip to content

Commit

Permalink
Add support for command line triggers (#59)
Browse files Browse the repository at this point in the history
* chore: add CURD functions for trigger configuration support

* test: unit test for trigger functions

* test: integration tests for trigger check and groups

* style: deleted commented code

* chore: changed deleteTrigger functions to match new API format with no token

* test: fixed tests to match new deleteTrigger endpoint

* test: fix integration tests for triggers

Co-authored-by: Pilar Checkly <[email protected]>
Co-authored-by: Pilar Checkly <[email protected]>
Co-authored-by: Pilar Checkly <[email protected]>
  • Loading branch information
4 people authored Dec 14, 2021
1 parent c01573c commit 0c1318d
Show file tree
Hide file tree
Showing 6 changed files with 408 additions and 0 deletions.
125 changes: 125 additions & 0 deletions checkly.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"net/http/httputil"
"net/url"
"os"
"strconv"
"strings"
)

Expand Down Expand Up @@ -814,6 +815,130 @@ func (c *client) UpdateMaintenanceWindow(
return &result, nil
}

// CreateTriggerCheck creates a new trigger with the specified details.
func (c *client) CreateTriggerCheck(
ctx context.Context,
checkID string,
) (*TriggerCheck, error) {
status, res, err := c.apiCall(ctx, http.MethodPost, fmt.Sprintf("triggers/checks/%s", checkID), nil)
if err != nil {
return nil, err
}
var result TriggerCheck
err = json.NewDecoder(strings.NewReader(res)).Decode(&result)

if err != nil {
return nil, err
}
if status != http.StatusOK && status != http.StatusCreated {
return nil, fmt.Errorf("unexpected response status: %d, res: %q", status, res)
}
return &result, nil
}

// GetTriggerCheck takes the ID of an existing trigger, and returns the
// corresponding trigger.
func (c *client) GetTriggerCheck(
ctx context.Context,
checkID string,
) (*TriggerCheck, error) {
status, res, err := c.apiCall(ctx, http.MethodGet, fmt.Sprintf("triggers/checks/%s", checkID), nil)
if err != nil {
return nil, err
}
if status != http.StatusOK {
return nil, fmt.Errorf("unexpected response status %d: %q", status, res)
}
result := TriggerCheck{}
err = json.NewDecoder(strings.NewReader(res)).Decode(&result)
if err != nil {
return nil, fmt.Errorf("decoding error for data %q: %v", res, err)
}
return &result, nil
}

// DeleteTriggerCheck deletes the window with the specified ID.
func (c *client) DeleteTriggerCheck(
ctx context.Context,
checkID string,
) error {
status, res, err := c.apiCall(
ctx,
http.MethodDelete,
fmt.Sprintf("triggers/checks/%s", checkID),
nil,
)
if err != nil {
return err
}
if status != http.StatusNoContent {
return fmt.Errorf("unexpected response status %d: %q", status, res)
}
return nil
}

// CreateTriggerGroup creates a new trigger with the specified details.
func (c *client) CreateTriggerGroup(
ctx context.Context,
groupID int64,
) (*TriggerGroup, error) {
status, res, err := c.apiCall(ctx, http.MethodPost, fmt.Sprintf("triggers/check-groups/%d", groupID), nil)
if err != nil {
return nil, err
}
var result TriggerGroup
err = json.NewDecoder(strings.NewReader(res)).Decode(&result)

if err != nil {
return nil, err
}
if status != http.StatusOK && status != http.StatusCreated {
return nil, fmt.Errorf("unexpected response status: %d, res: %q", status, res)
}
return &result, nil
}

// GetTriggerGroup takes the ID of an existing trigger, and returns the
// corresponding trigger.
func (c *client) GetTriggerGroup(
ctx context.Context,
groupID int64,
) (*TriggerGroup, error) {
status, res, err := c.apiCall(ctx, http.MethodGet, fmt.Sprintf("triggers/check-groups/%d", groupID), nil)
if err != nil {
return nil, err
}
if status != http.StatusOK {
return nil, fmt.Errorf("unexpected response status %d: %q", status, res)
}
result := TriggerGroup{}
err = json.NewDecoder(strings.NewReader(res)).Decode(&result)
if err != nil {
return nil, fmt.Errorf("decoding error for data %q: %v", res, err)
}
return &result, nil
}

// DeleteTriggerGroup deletes the window with the specified ID.
func (c *client) DeleteTriggerGroup(
ctx context.Context,
groupID int64,
) error {
status, res, err := c.apiCall(
ctx,
http.MethodDelete,
fmt.Sprintf("triggers/check-groups/%s", strconv.FormatInt(groupID, 10)),
nil,
)
if err != nil {
return err
}
if status != http.StatusNoContent {
return fmt.Errorf("unexpected response status %d: %q", status, res)
}
return nil
}

func payloadFromAlertChannel(ac AlertChannel) map[string]interface{} {
payload := map[string]interface{}{
"id": ac.ID,
Expand Down
79 changes: 79 additions & 0 deletions checkly_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,3 +209,82 @@ func TestGetMaintenanceWindowIntegration(t *testing.T) {
t.Error(cmp.Diff(testMaintenanceWindow, *gotMaintenanceWindow, ignoreMaintenanceWindowFields))
}
}

//TriggerCheck

func TestCreateTriggerCheckIntegration(t *testing.T) {
client := setupClient(t)

gotCheck, err := client.Create(context.Background(), wantCheck)
if err != nil {
t.Error(err)
}

gotTriggerCheck, err := client.CreateTriggerCheck(context.Background(), gotCheck.ID)
if err != nil {
t.Error(err)
}
defer client.Delete(context.Background(), gotCheck.ID)

if !cmp.Equal(gotCheck.ID, gotTriggerCheck.CheckId, ignoreTriggerCheck) {
t.Error(cmp.Diff(gotCheck.ID, gotTriggerCheck.CheckId, ignoreTriggerCheck))
}
}

func TestGetTriggerCheckIntegration(t *testing.T) {
client := setupClient(t)
gotCheck, err := client.Create(context.Background(), wantCheck)
if err != nil {
t.Error(err)
}
tc, err := client.CreateTriggerCheck(context.Background(), gotCheck.ID)
if err != nil {
t.Error(err)
}
gotTriggerCheck, err := client.GetTriggerCheck(context.Background(), tc.CheckId)
if err != nil {
t.Error(err)
}
defer client.Delete(context.Background(), gotCheck.ID)
if !cmp.Equal(gotCheck.ID, gotTriggerCheck.CheckId, ignoreTriggerCheck) {
t.Error(cmp.Diff(gotCheck.ID, gotTriggerCheck.CheckId, ignoreTriggerCheck))
}
}

//TriggerGroup

func TestCreateTriggerGroupIntegration(t *testing.T) {
client := setupClient(t)
gotGroup, err := client.CreateGroup(context.Background(), wantGroup)
if err != nil {
t.Error(err)
}
gotTriggerGroup, err := client.CreateTriggerGroup(context.Background(), gotGroup.ID)
if err != nil {
t.Error(err)
}
defer client.DeleteGroup(context.Background(), gotGroup.ID)
if !cmp.Equal(gotGroup.ID, gotTriggerGroup.GroupId, ignoreTriggerGroup) {
t.Error(cmp.Diff(gotGroup.ID, gotTriggerGroup.GroupId, ignoreTriggerGroup))
}
}

func TestGetTriggerGroupIntegration(t *testing.T) {
client := setupClient(t)
gotGroup, err := client.CreateGroup(context.Background(), wantGroup)
if err != nil {
t.Error(err)
}
tc, err := client.CreateTriggerGroup(context.Background(), gotGroup.ID)
if err != nil {
t.Error(err)
}
defer client.DeleteTriggerGroup(context.Background(), tc.GroupId)
gotTriggerGroup, err := client.GetTriggerGroup(context.Background(), tc.GroupId)
if err != nil {
t.Error(err)
}
if !cmp.Equal(gotGroup.ID, gotTriggerGroup.GroupId, ignoreTriggerGroup) {
t.Error(cmp.Diff(gotGroup.ID, gotTriggerGroup.GroupId, ignoreTriggerGroup))
}
}
134 changes: 134 additions & 0 deletions checkly_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"net/http"
"net/http/httptest"
"os"
"path"
"strconv"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -1198,3 +1200,135 @@ func TestGetMaintenanceWindow(t *testing.T) {
t.Error(cmp.Diff(testMaintenanceWindow, *mw, ignoreMaintenanceWindowFields))
}
}

var testTriggerCheck = checkly.TriggerCheck{
ID: 1,
CheckId: "721d28d6-149f-4f32-95e1-e497b23156f4",
Token: "MDMnt4oPjBBZ",
CreatedAt: "2013-08-24",
UpdatedAt: "2013-08-24",
CalledAt: "2013-08-24",
}

var ignoreTriggerCheck = cmpopts.IgnoreFields(checkly.TriggerCheck{}, "ID", "CreatedAt", "UpdatedAt", "CalledAt", "Token")

func TestCreateTriggerCheck(t *testing.T) {
t.Parallel()
ts := cannedResponseServer(t,
http.MethodPost,
fmt.Sprintf("/v1/triggers/checks/%s", testTriggerCheck.CheckId),
validateEmptyBody,
http.StatusCreated,
"CreateTriggerCheck.json",
)
defer ts.Close()
client := checkly.NewClient(ts.URL, "dummy-key", ts.Client(), nil)
gotTriggerCheck, err := client.CreateTriggerCheck(context.Background(), testTriggerCheck.CheckId)
if err != nil {
t.Error(err)
}
if !cmp.Equal(testTriggerCheck, *gotTriggerCheck, ignoreTriggerCheck) {
t.Error(cmp.Diff(testTriggerCheck, *gotTriggerCheck, ignoreTriggerCheck))
}
}
func TestDeleteTriggerCheck(t *testing.T) {
t.Parallel()
ts := cannedResponseServer(t,
http.MethodDelete,
path.Join("/v1/triggers/checks/", testTriggerCheck.CheckId),
validateEmptyBody,
http.StatusNoContent,
"Empty.json",
)
defer ts.Close()
client := checkly.NewClient(ts.URL, "dummy-key", ts.Client(), nil)
err := client.DeleteTriggerCheck(context.Background(), testTriggerCheck.CheckId)
if err != nil {
t.Error(err)
}
}
func TestGetTriggerCheck(t *testing.T) {
t.Parallel()
ts := cannedResponseServer(t,
http.MethodGet,
fmt.Sprintf("/v1/triggers/checks/%s", testTriggerCheck.CheckId),
validateEmptyBody,
http.StatusOK,
"CreateTriggerCheck.json",
)
defer ts.Close()
client := checkly.NewClient(ts.URL, "dummy-key", ts.Client(), nil)
gotTriggerCheck, err := client.GetTriggerCheck(context.Background(), testTriggerCheck.CheckId)
if err != nil {
t.Error(err)
}
if !cmp.Equal(testTriggerCheck, *gotTriggerCheck, ignoreTriggerCheck) {
t.Error(cmp.Diff(testTriggerCheck, *gotTriggerCheck, ignoreTriggerCheck))
}
}

var testTriggerGroup = checkly.TriggerGroup{
ID: 1,
GroupId: 215,
Token: "MDMnt4oPjBBZ",
CreatedAt: "2013-08-24",
UpdatedAt: "2013-08-24",
CalledAt: "2013-08-24",
}

var ignoreTriggerGroup = cmpopts.IgnoreFields(checkly.TriggerGroup{}, "ID", "CreatedAt", "UpdatedAt", "CalledAt", "Token")

func TestCreateTriggerGroup(t *testing.T) {
t.Parallel()
ts := cannedResponseServer(t,
http.MethodPost,
fmt.Sprintf("/v1/triggers/check-groups/%d", testTriggerGroup.GroupId),
validateEmptyBody,
http.StatusCreated,
"CreateTriggerGroup.json",
)
defer ts.Close()
client := checkly.NewClient(ts.URL, "dummy-key", ts.Client(), nil)
gotTriggerGroup, err := client.CreateTriggerGroup(context.Background(), testTriggerGroup.GroupId)
if err != nil {
t.Error(err)
}
if !cmp.Equal(testTriggerGroup, *gotTriggerGroup, ignoreTriggerGroup) {
t.Error(cmp.Diff(testTriggerGroup, *gotTriggerGroup, ignoreTriggerGroup))
}
}
func TestDeleteTriggerGroup(t *testing.T) {
t.Parallel()
ts := cannedResponseServer(t,
http.MethodDelete,
path.Join("/v1/triggers/check-groups/", strconv.FormatInt(testTriggerGroup.GroupId, 10)),
validateEmptyBody,
http.StatusNoContent,
"Empty.json",
)
defer ts.Close()
client := checkly.NewClient(ts.URL, "dummy-key", ts.Client(), nil)
err := client.DeleteTriggerGroup(context.Background(), testTriggerGroup.GroupId)
if err != nil {
t.Error(err)
}
}
func TestGetTriggerGroup(t *testing.T) {
t.Parallel()
ts := cannedResponseServer(t,
http.MethodGet,
fmt.Sprintf("/v1/triggers/check-groups/%d", testTriggerGroup.GroupId),
validateEmptyBody,
http.StatusOK,
"CreateTriggerGroup.json",
)
defer ts.Close()
client := checkly.NewClient(ts.URL, "dummy-key", ts.Client(), nil)
gotTriggerGroup, err := client.GetTriggerGroup(context.Background(), testTriggerGroup.GroupId)
if err != nil {
t.Error(err)
}
if !cmp.Equal(testTriggerGroup, *gotTriggerGroup, ignoreTriggerGroup) {
t.Error(cmp.Diff(testTriggerGroup, *gotTriggerGroup, ignoreTriggerGroup))
}
}
7 changes: 7 additions & 0 deletions testdata/CreateTriggerCheck.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"checkId": "721d28d6-149f-4f32-95e1-e497b23156f4",
"token": "MDMnt4oPjBBZ",
"created_at": "2013-08-24",
"updated_at": "2013-08-24",
"called_at": "2013-08-24"
}
7 changes: 7 additions & 0 deletions testdata/CreateTriggerGroup.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"groupId": 215,
"token": "MDMnt4oPjBBZ",
"created_at": "2013-08-24",
"updated_at": "2013-08-24",
"called_at": "2013-08-24"
}
Loading

0 comments on commit 0c1318d

Please sign in to comment.