Skip to content

Commit

Permalink
Merge pull request PagerDuty#11 from heimweh/f-schedule-overrides
Browse files Browse the repository at this point in the history
Add support for schedule overrides
  • Loading branch information
heimweh authored Jul 2, 2017
2 parents afd4447 + 1b19e47 commit 4e69895
Show file tree
Hide file tree
Showing 2 changed files with 128 additions and 2 deletions.
63 changes: 61 additions & 2 deletions pagerduty/schedule.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@ import (
// related methods of the PagerDuty API.
type ScheduleService service

// Override represents an override
type Override struct {
Override *Override `json:"override,omitempty"`
ID string `json:"id,omitempty"`
Start string `json:"start,omitempty"`
End string `json:"end,omitempty"`
User *UserReference `json:"user,omitempty"`
}

// Schedule represents a schedule.
type Schedule struct {
Description string `json:"description,omitempty"`
Expand Down Expand Up @@ -68,17 +77,35 @@ type ListSchedulesOptions struct {
Limit int `url:"limit,omitempty"`
More bool `url:"more,omitempty"`
Offset int `url:"offset,omitempty"`
Total int `url:"total,omitempty"`
Query string `url:"query,omitempty"`
Total int `url:"total,omitempty"`
}

// ListSchedulesResponse represents a list response of schedules.
type ListSchedulesResponse struct {
Limit int `url:"limit,omitempty"`
More bool `url:"more,omitempty"`
Offset int `url:"offset,omitempty"`
Total int `url:"total,omitempty"`
Schedules []*Schedule `json:"schedules,omitempty"`
Total int `url:"total,omitempty"`
}

// ListOverridesOptions represents options when listing overrides.
type ListOverridesOptions struct {
Editable bool `url:"editable,omitempty"`
ID string `url:"id,omitempty"`
Overflow bool `url:"overflow,omitempty"`
Since string `url:"since,omitempty"`
Until string `url:"until,omitempty"`
}

// ListOverridesResponse represents a list response of schedules.
type ListOverridesResponse struct {
Limit int `url:"limit,omitempty"`
More bool `url:"more,omitempty"`
Offset int `url:"offset,omitempty"`
Overrides []*Override `json:"overrides,omitempty"`
Total int `url:"total,omitempty"`
}

// GetScheduleOptions represents options when retrieving a schedule.
Expand Down Expand Up @@ -163,6 +190,38 @@ func (s *ScheduleService) Update(id string, schedule *Schedule) (*Schedule, *Res
return v.Schedule, resp, nil
}

// ListOverrides lists existing overrides.
func (s *ScheduleService) ListOverrides(scheduleID string, o *ListOverridesOptions) (*ListOverridesResponse, *Response, error) {
u := fmt.Sprintf("/schedules/%s/overrides", scheduleID)
v := new(ListOverridesResponse)

resp, err := s.client.newRequestDo("GET", u, o, nil, &v)
if err != nil {
return nil, nil, err
}

return v, resp, nil
}

// CreateOverride creates an override for a specific user covering the specified time range.
func (s *ScheduleService) CreateOverride(id string, override *Override) (*Override, *Response, error) {
u := fmt.Sprintf("/schedules/%s/overrides", id)
v := new(Override)

resp, err := s.client.newRequestDo("POST", u, nil, &Override{Override: override}, &v)
if err != nil {
return nil, nil, err
}

return v.Override, resp, nil
}

// DeleteOverride deletes an override.
func (s *ScheduleService) DeleteOverride(id string, overrideID string) (*Response, error) {
u := fmt.Sprintf("/schedules/%s/overrides/%s", id, overrideID)
return s.client.newRequestDo("DELETE", u, nil, nil, nil)
}

func normalizeTime(l *ScheduleLayer) error {
s, err := timeToUTC(l.Start)
if err != nil {
Expand Down
67 changes: 67 additions & 0 deletions pagerduty/schedule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,3 +136,70 @@ func TestSchedulesUpdate(t *testing.T) {
t.Errorf("returned \n\n%#v want \n\n%#v", resp, want)
}
}

func TestSchedulesListOverrides(t *testing.T) {
setup()
defer teardown()

mux.HandleFunc("/schedules/1/overrides", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
w.Write([]byte(`{"overrides": [{"id": "1"}]}`))
})

resp, _, err := client.Schedules.ListOverrides("1", &ListOverridesOptions{})
if err != nil {
t.Fatal(err)
}

want := &ListOverridesResponse{
Overrides: []*Override{
{
ID: "1",
},
},
}

if !reflect.DeepEqual(resp, want) {
t.Errorf("returned \n\n%#v want \n\n%#v", resp, want)
}
}

func TestSchedulesCreateOverride(t *testing.T) {
setup()
defer teardown()

mux.HandleFunc("/schedules/1/overrides", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "POST")
w.Write([]byte(`{"override": {"id": "1", "user": { "id": "1" }}}`))
})

resp, _, err := client.Schedules.CreateOverride("1", &Override{User: &UserReference{ID: "1"}})
if err != nil {
t.Fatal(err)
}

want := &Override{
ID: "1",
User: &UserReference{
ID: "1",
},
}

if !reflect.DeepEqual(resp, want) {
t.Errorf("returned \n\n%#v want \n\n%#v", resp, want)
}
}

func TestSchedulesDeleteOverride(t *testing.T) {
setup()
defer teardown()

mux.HandleFunc("/schedules/1/overrides/1", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "DELETE")
w.WriteHeader(http.StatusNoContent)
})

if _, err := client.Schedules.DeleteOverride("1", "1"); err != nil {
t.Fatal(err)
}
}

0 comments on commit 4e69895

Please sign in to comment.