Skip to content
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

Create Incident, List Priorities, and headers in POST method support #122

Merged
merged 10 commits into from
Jun 29, 2018
5 changes: 3 additions & 2 deletions addon.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ package pagerduty

import (
"fmt"
"github.com/google/go-querystring/query"
"net/http"

"github.com/google/go-querystring/query"
)

// Addon is a third-party add-on to PagerDuty's UI.
Expand Down Expand Up @@ -46,7 +47,7 @@ func (c *Client) ListAddons(o ListAddonOptions) (*ListAddonResponse, error) {
func (c *Client) InstallAddon(a Addon) (*Addon, error) {
data := make(map[string]Addon)
data["addon"] = a
resp, err := c.post("/addons", data)
resp, err := c.post("/addons", data, nil)
defer resp.Body.Close()
if err != nil {
return nil, err
Expand Down
9 changes: 7 additions & 2 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ type APIReference struct {
Type string `json:"type,omitempty"`
}

type APIDetails struct {
Type string `json:"type,omitempty"`
Details string `json:"details,omitempty"`
}

type errorObject struct {
Code int `json:"code,omitempty"`
Message string `json:"message,omitempty"`
Expand Down Expand Up @@ -112,12 +117,12 @@ func (c *Client) put(path string, payload interface{}, headers *map[string]strin
return c.do("PUT", path, nil, headers)
}

func (c *Client) post(path string, payload interface{}) (*http.Response, error) {
func (c *Client) post(path string, payload interface{}, headers *map[string]string) (*http.Response, error) {
data, err := json.Marshal(payload)
if err != nil {
return nil, err
}
return c.do("POST", path, bytes.NewBuffer(data), nil)
return c.do("POST", path, bytes.NewBuffer(data), headers)
}

func (c *Client) get(path string) (*http.Response, error) {
Expand Down
4 changes: 2 additions & 2 deletions escalation_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func (c *Client) ListEscalationPolicies(o ListEscalationPoliciesOptions) (*ListE
func (c *Client) CreateEscalationPolicy(e EscalationPolicy) (*EscalationPolicy, error) {
data := make(map[string]EscalationPolicy)
data["escalation_policy"] = e
resp, err := c.post(escPath, data)
resp, err := c.post(escPath, data, nil)
return getEscalationPolicyFromResponse(c, resp, err)
}

Expand Down Expand Up @@ -112,7 +112,7 @@ func (c *Client) UpdateEscalationPolicy(id string, e *EscalationPolicy) (*Escala
func (c *Client) CreateEscalationRule(escID string, e EscalationRule) (*EscalationRule, error) {
data := make(map[string]EscalationRule)
data["escalation_rule"] = e
resp, err := c.post(escPath+"/"+escID+"/escalation_rules", data)
resp, err := c.post(escPath+"/"+escID+"/escalation_rules", data, nil)
return getEscalationRuleFromResponse(c, resp, err)
}

Expand Down
44 changes: 42 additions & 2 deletions incident.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package pagerduty

import (
"encoding/json"
"fmt"

"github.com/google/go-querystring/query"
Expand Down Expand Up @@ -80,6 +81,45 @@ func (c *Client) ListIncidents(o ListIncidentsOptions) (*ListIncidentsResponse,
return &result, c.decodeJSON(resp, &result)
}

// CreateIncident is the structure POST'd to the incidents endpoint. It wraps a CreateIncidentValue
type CreateIncident struct {
Incident CreateIncidentOptions `json:"incident"`
}

// createIncidentResponse is returned from the API when creating a response.
type createIncidentResponse struct {
Incident Incident `json:incident`
}

// CreateIncidentOptions is the structure used when POSTing to the CreateIncident API endpoint.
type CreateIncidentOptions struct {
Type string `json:"type"`
Title string `json:"title"`
Service APIReference `json:"service"`
Priority APIReference `json:"priority"`
IncidentKey string `json:"incident_key"`
Body APIDetails `json:"body"`
EscalationPolicy APIReference `json:"escalation_policy"`
}

// CreateIncident creates an incident synchronously without a corresponding event from a monitoring service.
func (c *Client) CreateIncident(from string, i *CreateIncident) (*Incident, error) {
headers := make(map[string]string)
headers["From"] = from
resp, e := c.post("/incidents", i, &headers)
if e != nil {
return nil, e
}

var ii createIncidentResponse
e = json.NewDecoder(resp.Body).Decode(&ii)
if e != nil {
return nil, e
}

return &ii.Incident, nil
}

// ManageIncidents acknowledges, resolves, escalates, or reassigns one or more incidents.
func (c *Client) ManageIncidents(from string, incidents []Incident) error {
r := make(map[string][]Incident)
Expand Down Expand Up @@ -136,15 +176,15 @@ func (c *Client) ListIncidentNotes(id string) ([]IncidentNote, error) {
func (c *Client) CreateIncidentNote(id string, note IncidentNote) error {
data := make(map[string]IncidentNote)
data["note"] = note
_, err := c.post("/incidents/"+id+"/notes", data)
_, err := c.post("/incidents/"+id+"/notes", data, nil)
return err
}

// SnoozeIncident sets an incident to not alert for a specified period of time.
func (c *Client) SnoozeIncident(id string, duration uint) error {
data := make(map[string]uint)
data["duration"] = duration
_, err := c.post("/incidents/"+id+"/snooze", data)
_, err := c.post("/incidents/"+id+"/snooze", data, nil)
return err
}

Expand Down
5 changes: 3 additions & 2 deletions maintenance_window.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ package pagerduty

import (
"fmt"
"github.com/google/go-querystring/query"
"net/http"

"github.com/google/go-querystring/query"
)

// MaintenanceWindow is used to temporarily disable one or more services for a set period of time.
Expand Down Expand Up @@ -52,7 +53,7 @@ func (c *Client) ListMaintenanceWindows(o ListMaintenanceWindowsOptions) (*ListM
func (c *Client) CreateMaintenanceWindows(m MaintenanceWindow) (*MaintenanceWindow, error) {
data := make(map[string]MaintenanceWindow)
data["maintenance_window"] = m
resp, err := c.post("/maintenance_windows", data)
resp, err := c.post("/maintenance_windows", data, nil)
return getMaintenanceWindowFromResponse(c, resp, err)
}

Expand Down
33 changes: 33 additions & 0 deletions priorites.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package pagerduty

import (
"encoding/json"
)

// PriorityProperty is a single priorty object returned from the Priorities endpoint
type PriorityProperty struct {
APIObject
Name string `json:"name"`
Description string `json:"description"`
}

type Priorities struct {
APIListObject
Priorities []PriorityProperty `json:"priorities"`
}

// ListPriorities lists existing priorities
func (c *Client) ListPriorities() (*Priorities, error) {
resp, e := c.get("/priorities")
if e != nil {
return nil, e
}

var p Priorities
e = json.NewDecoder(resp.Body).Decode(&p)
if e != nil {
return nil, e
}

return &p, nil
}
6 changes: 3 additions & 3 deletions schedule.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func (c *Client) ListSchedules(o ListSchedulesOptions) (*ListSchedulesResponse,
func (c *Client) CreateSchedule(s Schedule) (*Schedule, error) {
data := make(map[string]Schedule)
data["schedule"] = s
resp, err := c.post("/schedules", data)
resp, err := c.post("/schedules", data, nil)
if err != nil {
return nil, err
}
Expand All @@ -107,7 +107,7 @@ func (c *Client) PreviewSchedule(s Schedule, o PreviewScheduleOptions) error {
}
var data map[string]Schedule
data["schedule"] = s
_, e := c.post("/schedules/preview?"+v.Encode(), data)
_, e := c.post("/schedules/preview?"+v.Encode(), data, nil)
return e
}

Expand Down Expand Up @@ -196,7 +196,7 @@ func (c *Client) ListOverrides(id string, o ListOverridesOptions) ([]Override, e
func (c *Client) CreateOverride(id string, o Override) (*Override, error) {
data := make(map[string]Override)
data["override"] = o
resp, err := c.post("/schedules/"+id+"/overrides", data)
resp, err := c.post("/schedules/"+id+"/overrides", data, nil)
if err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions service.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func (c *Client) GetService(id string, o *GetServiceOptions) (*Service, error) {
func (c *Client) CreateService(s Service) (*Service, error) {
data := make(map[string]Service)
data["service"] = s
resp, err := c.post("/services", data)
resp, err := c.post("/services", data, nil)
return getServiceFromResponse(c, resp, err)
}

Expand All @@ -140,7 +140,7 @@ func (c *Client) DeleteService(id string) error {
func (c *Client) CreateIntegration(id string, i Integration) (*Integration, error) {
data := make(map[string]Integration)
data["integration"] = i
resp, err := c.post("/services/"+id+"/integrations", data)
resp, err := c.post("/services/"+id+"/integrations", data, nil)
return getIntegrationFromResponse(c, resp, err)
}

Expand Down
5 changes: 3 additions & 2 deletions team.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ package pagerduty

import (
"fmt"
"github.com/google/go-querystring/query"
"net/http"

"github.com/google/go-querystring/query"
)

// Team is a collection of users and escalation policies that represent a group of people within an organization.
Expand Down Expand Up @@ -42,7 +43,7 @@ func (c *Client) ListTeams(o ListTeamOptions) (*ListTeamResponse, error) {

// CreateTeam creates a new team.
func (c *Client) CreateTeam(t *Team) (*Team, error) {
resp, err := c.post("/teams", t)
resp, err := c.post("/teams", t, nil)
return getTeamFromResponse(c, resp, err)
}

Expand Down
5 changes: 3 additions & 2 deletions user.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ package pagerduty

import (
"fmt"
"github.com/google/go-querystring/query"
"net/http"

"github.com/google/go-querystring/query"
)

// ContactMethod is a way of contacting the user.
Expand Down Expand Up @@ -79,7 +80,7 @@ func (c *Client) ListUsers(o ListUsersOptions) (*ListUsersResponse, error) {
func (c *Client) CreateUser(u User) (*User, error) {
data := make(map[string]User)
data["user"] = u
resp, err := c.post("/users", data)
resp, err := c.post("/users", data, nil)
return getUserFromResponse(c, resp, err)
}

Expand Down