Skip to content

Commit

Permalink
Added IncidentNotes,Tags,GlobalRouter Endpoints
Browse files Browse the repository at this point in the history
1.change io/ioutil  package to io
2.Added Events endpoint
3.Bug fixes in incident, schedules, member, roles  services
  • Loading branch information
dheeruk12 committed Feb 20, 2023
1 parent 460e3da commit 52a85f7
Show file tree
Hide file tree
Showing 9 changed files with 414 additions and 22 deletions.
11 changes: 7 additions & 4 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"io"
"net/http"
"net/url"
)
Expand Down Expand Up @@ -43,7 +43,9 @@ type Client struct {
MaintenanceWindow *MaintenanceWindowService
NotificationRules *NotificationRulesService
ContactMethod *ContactMethodService
AccountRole *AccountRoleService
AccountRole *AccountRoleService
GlobalRouter *GlobalRouterService
Events *EventsService
}

type Response struct {
Expand Down Expand Up @@ -87,7 +89,8 @@ func NewClient(config *Config) (*Client, error) {
c.NotificationRules = &NotificationRulesService{c}
c.ContactMethod = &ContactMethodService{c}
c.AccountRole = &AccountRoleService{c}

c.GlobalRouter = &GlobalRouterService{c}
c.Events = &EventsService{c}
return c, nil

}
Expand Down Expand Up @@ -125,7 +128,7 @@ func (c *Client) doRequest(req *http.Request) (*Response, error) {
if err != nil {
return nil, err
}
body, err := ioutil.ReadAll(res.Body)
body, err := io.ReadAll(res.Body)
if err != nil {
return nil, err
}
Expand Down
54 changes: 54 additions & 0 deletions client/events.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package client

import (
"encoding/json"
"fmt"
)

type EventsService service

type AlertPayload struct {
Message string `json:"message"`
Summary string `json:"summary"`
AlertType string `json:"alert_type"`
Suppressed bool `json:"suppressed,omitempty"`
EntityID string `json:"entity_id"`
Payload interface{} `json:"payload"` // payload map[string]interface{}{"severity": "SEV1"}
}

type IntegrationObject struct {
Name string `json:"name"`
Summary string `json:"summary"`
Service string `json:"service"`
Team string `json:"team"`
IntegrationKey string `json:"integration_key"`
IsEnabled bool `json:"is_enabled"`
}

type AlertResponse struct {
UniqueID string `json:"unique_id"`
Message string `json:"message"`
Summary string `json:"summary"`
AlertType string `json:"alert_type"`
Suppressed bool `json:"suppressed"`
EntityID string `json:"entity_id"`
Payload string `json:"payload"`
IntegrationObject IntegrationObject `json:"integration_object"`
Integration string `json:"integration"`
CreationDate string `json:"creation_date"`
}

func (c *EventsService) SendAlert(integrationKey string, payload *AlertPayload) (*AlertResponse, error) {
path := fmt.Sprintf("/api/events/%s/", integrationKey)

body, err := c.client.newRequestDo("POST", path, payload)
if err != nil {
return nil, err
}
var i AlertResponse
err = json.Unmarshal(body.BodyBytes, &i)
if err != nil {
return nil, err
}
return &i, nil
}
83 changes: 83 additions & 0 deletions client/global_router.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package client

import (
"encoding/json"
"fmt"
)

type GlobalRouterService service

type GlobalRouterPayload struct {
UniqueID string `json:"unique_id,omitempty"`
Name string `json:"name"`
Description string `json:"description"`
IsEnabled bool `json:"is_enabled,omitempty"`
IntegrationKey string `json:"integration_key,omitempty"`
}

func (c *GlobalRouterService) CreateGlobalRouter(router *GlobalRouterPayload) (*GlobalRouterPayload, error) {
path := "/api/v2/account/events/router/"

body, err := c.client.newRequestDo("POST", path, router)
if err != nil {
return nil, err
}
var r GlobalRouterPayload
err = json.Unmarshal(body.BodyBytes, &r)
if err != nil {
return nil, err
}
return &r, nil
}

func (c *GlobalRouterService) UpdateGlobalRouter(id string, router *GlobalRouterPayload) (*GlobalRouterPayload, error) {
path := fmt.Sprintf("/api/v2/account/events/router/%s/", id)

body, err := c.client.newRequestDo("PATCH", path, router)
if err != nil {
return nil, err
}
var r GlobalRouterPayload
err = json.Unmarshal(body.BodyBytes, &r)
if err != nil {
return nil, err
}
return &r, nil
}

func (c *GlobalRouterService) GetGlobalRouters() ([]GlobalRouterPayload, error) {
path := "/api/v2/account/events/router/"
body, err := c.client.newRequestDo("GET", path, nil)
if err != nil {
return nil, err
}
var i []GlobalRouterPayload
err = json.Unmarshal(body.BodyBytes, &i)
if err != nil {
return nil, err
}
return i, nil
}

func (c *GlobalRouterService) GetGlobalRouter(id string) (*GlobalRouterPayload, error) {
path := fmt.Sprintf("/api/v2/account/events/router/%s/", id)
body, err := c.client.newRequestDo("GET", path, nil)
if err != nil {
return nil, err
}
var r GlobalRouterPayload
err = json.Unmarshal(body.BodyBytes, &r)
if err != nil {
return nil, err
}
return &r, nil
}

func (c *GlobalRouterService) DeleteGlobalRouter(id string) error {
path := fmt.Sprintf("/api/v2/account/events/router/%s/", id)
_, err := c.client.newRequestDo("DELETE", path, nil)
if err != nil {
return err
}
return nil
}
88 changes: 88 additions & 0 deletions client/global_routing_rules.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package client

import (
"encoding/json"
"fmt"
)

type GlobalRoutingRuleAction struct {
UniqueId string `json:"unique_id,omitempty"`
ActionType int `json:"action_type"`
Integration string `json:"integration,omitempty"`
}

// action := client.GlobalRoutingRuleAction{ ActionType: 1}

type GlobalRoutingRule struct {
Unique_Id string `json:"unique_id,omitempty"`
Name string `json:"name"`
Position int `json:"position,omitempty"`
RuleJson string `json:"rule_json"`
Actions []GlobalRoutingRuleAction `json:"actions"` // []client.GlobalRoutingRuleAction{action}
}

func (c *GlobalRouterService) CreateGlobalRoutingRule(routerID string, rule *GlobalRoutingRule) (*GlobalRoutingRule, error) {
path := fmt.Sprintf("/api/v2/account/events/router/%s/rulesets/", routerID)
body, err := c.client.newRequestDo("POST", path, rule)
if err != nil {
return nil, err
}
var s GlobalRoutingRule
err = json.Unmarshal(body.BodyBytes, &s)
if err != nil {
return nil, err
}
return &s, nil
}

func (c *GlobalRouterService) GetGlobalRoutingRules(routerID string) ([]GlobalRoutingRule, error) {

path := fmt.Sprintf("/api/v2/account/events/router/%s/rulesets/", routerID)
body, err := c.client.newRequestDo("GET", path, nil)
if err != nil {
return nil, err
}
var s []GlobalRoutingRule
err = json.Unmarshal(body.BodyBytes, &s)
if err != nil {
return nil, err
}
return s, nil
}

func (c *GlobalRouterService) GetGlobalRoutingRule(routerID, rulesetID string) (*GlobalRoutingRule, error) {
path := fmt.Sprintf("/api/v2/account/events/router/%s/rulesets/%s/", routerID, rulesetID)
body, err := c.client.newRequestDo("GET", path, nil)
if err != nil {
return nil, err
}
var s GlobalRoutingRule
err = json.Unmarshal(body.BodyBytes, &s)
if err != nil {
return nil, err
}
return &s, nil
}

func (c *GlobalRouterService) UpdateGlobalRoutingRule(routerID, rulesetID string, rule *GlobalRoutingRule) (*GlobalRoutingRule, error) {
path := fmt.Sprintf("/api/v2/account/events/router/%s/rulesets/%s/", routerID, rulesetID)
body, err := c.client.newRequestDo("PUT", path, rule)
if err != nil {
return nil, err
}
var s GlobalRoutingRule
err = json.Unmarshal(body.BodyBytes, &s)
if err != nil {
return nil, err
}
return &s, nil
}

func (c *GlobalRouterService) DeleteGlobalRoutingRule(routerID, rulesetID string) error {
path := fmt.Sprintf("/api/v2/account/events/router/%s/rulesets/%s/", routerID, rulesetID)
_, err := c.client.newRequestDo("DELETE", path, nil)
if err != nil {
return err
}
return nil
}
Loading

0 comments on commit 52a85f7

Please sign in to comment.