Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions pagerduty/schedules.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,3 +123,19 @@ func (s *SchedulesService) Entries(id string, opt *ScheduleEntriesOptions) (*Sch

return entries, res, err
}

// GetOnCall returns a list all the users on-call in a given schedule for a given time range.
func (s *SchedulesService) GetOnCall(scheduleID string, opt *ScheduleEntriesOptions) (*Users, *http.Response, error) {
uri, err := addOptions("schedules/"+scheduleID+"/users", opt)
if err != nil {
return nil, nil, err
}
user := new(Users)

res, err := s.client.Get(uri, user)
if err != nil {
return nil, res, err
}

return user, res, nil
}
72 changes: 63 additions & 9 deletions pagerduty/users.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package pagerduty

import (
"errors"
"fmt"
"net/http"
)
Expand All @@ -12,14 +13,53 @@ type UsersService struct {

// User type
type User struct {
ID string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Email string `json:"email,omitempty"`
Role string `json:"role,omitempty"`
TimeZone string `json:"time_zone,omitempty"`
Color string `json:"color,omitempty"`
UserURL string `json:"user_url,omitempty"`
AvatarURL string `json:"avatar_url,omitempty"`
ID string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Email string `json:"email,omitempty"`
Role string `json:"role,omitempty"`
TimeZone string `json:"time_zone,omitempty"`
Color string `json:"color,omitempty"`
UserURL string `json:"user_url,omitempty"`
AvatarURL string `json:"avatar_url,omitempty"`
Description string `json:"description,omitempty"`
InvitationSent bool `json:"invitation_sent,omitempty"`
MarketingOptOut bool `json:"marketing_opt_out,omitempty"`
ContactMethods []Contact `json:"contact_methods,omitempty"`
NotificationRules []Rules `json:"notification_rules,omitempty"`
}

// Rules type
type Rules struct {
ID string `json:"id,omitempty"`
Urgency string `json:"urgency,omitempty"`
StartDelayInMinutes int `json:"start_delay_in_minutes,omitempty"`
CreatedAt string `json:"created_at,omitempty"`
ContactMethods []Contact `json:"contact_methods,omitempty"`
}

// Sound type
type Sound struct {
Type string `json:"type,omitempty"`
File string `json:"file,omitempty"`
}

// Contact type
type Contact struct {
ID string `json:"id,omitempty"`
Label string `json:"label,omitempty"`
Address string `json:"address,omitempty"`
UserID string `json:"user_id,omitempty"`
Type string `json:"type,omitempty"`
Email string `json:"email,omitempty"`
SendShortEmail bool `json:"send_short_email,omitempty"`
CountryCode int `json:"country_code,omitempty"`
PhoneNumber string `json:"phone_number,omitempty"`
DeviceToken string `json:"device_token,omitempty"`
DeviceType string `json:"device_type,omitempty"`
Sound string `json:"sound,omitempty"`
Sounds []Sound `json:"sounds,omitempty"`
Blacklisted bool `json:"blacklisted,omitempty"`
Enabled bool `json:"enabled,omitempty"`
}

// Users is a list of users
Expand All @@ -29,7 +69,8 @@ type Users struct {

// UsersOptions provides optional parameters to list requests
type UsersOptions struct {
Query string `json:"query,omitempty"`
Query string `url:"query,omitempty"`
Include []string `url:"include[],omitempty"`
}

// List returns a list of users
Expand Down Expand Up @@ -60,3 +101,16 @@ func (s *UsersService) Get(id string) (*User, *http.Response, error) {

return user, res, err
}

// FindContactMethod Seaches a users contact methods for a type
func (s *User) FindContactMethod(methodType string) (Contact, error) {

for _, c := range s.ContactMethods {
if c.Type == methodType {
return c, nil
}
}

return Contact{}, errors.New(fmt.Sprintf("contact of type '%s' was not found", methodType))

}
29 changes: 26 additions & 3 deletions pagerduty/users_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ package pagerduty_test

import (
"fmt"
. "github.com/danryan/go-pagerduty/pagerduty"
"net/http"
"reflect"
"testing"

. "github.com/danryan/go-pagerduty/pagerduty"
)

func TestUser_marshal(t *testing.T) {
Expand All @@ -17,13 +16,37 @@ func TestUser_marshal(t *testing.T) {
Name: "Bill Williams",
Email: "bill.williams@example.com",
UserURL: "/users/ABCDEF",
ContactMethods: []Contact{
Contact{
ID: "PNB9UG1",
Label: "Mobile",
Address: "4072559655",
UserID: "PSXBF4M",
Type: "SMS",
CountryCode: 1,
PhoneNumber: "4072559655",
Enabled: true,
},
},
}

want := `{
"id": "ABCDEF",
"name": "Bill Williams",
"email": "bill.williams@example.com",
"user_url": "/users/ABCDEF"
"user_url": "/users/ABCDEF",
"contact_methods" : [
{
"id": "PNB9UG1",
"label": "Mobile",
"address": "4072559655",
"user_id": "PSXBF4M",
"type": "SMS",
"country_code": 1,
"phone_number": "4072559655",
"enabled": true
}
]
}`

testJSONMarshal(t, u, want)
Expand Down