From 5231aab33be5ce04e74c9cb9eb61afca84320d68 Mon Sep 17 00:00:00 2001 From: Axel Koehler Date: Wed, 19 Apr 2023 11:30:03 +0200 Subject: [PATCH] Implement get-responder-alerts API endpoint --- incident/incident.go | 9 +++++++ incident/incident_test.go | 34 ++++++++++++++++++++++++ incident/request.go | 55 +++++++++++++++++++++++++++++++++++++++ incident/result.go | 5 ++++ 4 files changed, 103 insertions(+) diff --git a/incident/incident.go b/incident/incident.go index 214db30..903d60b 100644 --- a/incident/incident.go +++ b/incident/incident.go @@ -178,3 +178,12 @@ func (c *Client) ListNotes(context context.Context, request *ListNotesRequest) ( } return result, nil } + +func (c *Client) GetResponderAlerts(context context.Context, request *GetResponderAlertsRequest) (*GetResponderAlertsResult, error) { + result := &GetResponderAlertsResult{} + err := c.client.Exec(context, request, result) + if err != nil { + return nil, err + } + return result, nil +} diff --git a/incident/incident_test.go b/incident/incident_test.go index 3fb575e..8fe944d 100644 --- a/incident/incident_test.go +++ b/incident/incident_test.go @@ -504,3 +504,37 @@ func TestResponders_Validate(t *testing.T) { err = validateResponders(Responders) assert.Nil(t, err) } + +func TestGetResponderAlertsRequest_Endpoint(t *testing.T) { + request := &GetResponderAlertsRequest{ + Id: "adea9e79-5527-4e49-b345-e55ae180ae59", + Identifier: Id, + } + endpoint := request.ResourcePath() + params := request.RequestParams() + assert.Equal(t, "/v1/incidents/adea9e79-5527-4e49-b345-e55ae180ae59/responder-alert-ids", endpoint) + assert.Equal(t, "id", params["identifierType"]) +} + +func TestGetResponderAlertsRequest_GetParams(t *testing.T) { + request := &GetResponderAlertsRequest{ + Limit: 10, + Offset: 30, + Order: "desc", + Direction: "next", + } + params := request.RequestParams() + assert.Equal(t, "10", params["limit"]) + assert.Equal(t, "30", params["offset"]) + assert.Equal(t, "next", params["direction"]) + assert.Equal(t, "desc", params["order"]) +} + +func TestGetResponderAlertsRequest_Validate(t *testing.T) { + request := &GetResponderAlertsRequest{} + err := request.Validate() + assert.Equal(t, err.Error(), errors.New("Incident ID cannot be blank.").Error()) + request.Id = "adea9e79-5527-4e49-b345-e55ae180ae59" + err = request.Validate() + assert.Nil(t, err) +} diff --git a/incident/request.go b/incident/request.go index 8f878de..f8893cc 100644 --- a/incident/request.go +++ b/incident/request.go @@ -772,6 +772,61 @@ func (r *ListNotesRequest) RequestParams() map[string]string { return params } +type GetResponderAlertsRequest struct { + client.BaseRequest + Identifier IdentifierType + Id string + Limit int + Offset int + Order Order + Direction string +} + +func (r *GetResponderAlertsRequest) Validate() error { + if r.Id == "" { + return errors.New("Incident ID cannot be blank.") + } + if r.Identifier != "" && r.Identifier != Id && r.Identifier != Tiny { + return errors.New("Identifier type should be one of these: 'Id', 'Tiny' or empty.") + } + return nil +} + +func (r *GetResponderAlertsRequest) ResourcePath() string { + return "/v1/incidents/" + r.Id + "/responder-alert-ids" +} + +func (r *GetResponderAlertsRequest) Method() string { + return http.MethodGet +} + +func (r *GetResponderAlertsRequest) RequestParams() map[string]string { + + params := make(map[string]string) + + if r.Identifier == Tiny { + params["identifierType"] = "tiny" + } else { + params["identifierType"] = "id" + } + + if r.Limit != 0 { + params["limit"] = strconv.Itoa(r.Limit) + } + if r.Offset != 0 { + params["offset"] = strconv.Itoa(r.Offset) + } + if r.Direction != "" { + params["direction"] = r.Direction + + } + if r.Order != "" { + params["order"] = string(r.Order) + } + + return params +} + type IdentifierType string type ResponderType string type Priority string diff --git a/incident/result.go b/incident/result.go index b8d35e7..23309e2 100644 --- a/incident/result.go +++ b/incident/result.go @@ -80,3 +80,8 @@ type Paging struct { First string `json:"first"` Last string `json:"last"` } + +type GetResponderAlertsResult struct { + client.ResultMetadata + AlertIds []string `json:"data"` +}