Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/heimweh/go-pagerduty
Browse files Browse the repository at this point in the history
  • Loading branch information
Vladimir Drapalyuk committed Apr 10, 2021
2 parents 83d3358 + e772e42 commit 318975a
Show file tree
Hide file tree
Showing 22 changed files with 2,114 additions and 58 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ dist: trusty
sudo: false
language: go
go:
- "1.12.4"
- "1.15.6"

env:
GO111MODULE=off
Expand Down
102 changes: 102 additions & 0 deletions pagerduty/business_service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package pagerduty

import "fmt"

// BusinessServiceService handles the communication with business service
// related methods of the PagerDuty API.
type BusinessServiceService service

// BusinessService represents a business service.
type BusinessService struct {
ID string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Type string `json:"type,omitempty"`
Summary string `json:"summary,omitempty"`
Self string `json:"self,omitempty"`
PointOfContact string `json:"point_of_contact,omitempty"`
HTMLUrl string `json:"html_url,omitempty"`
Description string `json:"description,omitempty"`
Team *BusinessServiceTeam `json:"team,omitempty"`
}

// BusinessServiceTeam represents a team object in a business service
type BusinessServiceTeam struct {
ID string `json:"id,omitempty"`
Type string `json:"type,omitempty"`
Self string `json:"self,omitempty"`
}

// BusinessServicePayload represents payload with a business service object
type BusinessServicePayload struct {
BusinessService *BusinessService `json:"business_service,omitempty"`
}

// ListBusinessServicesResponse represents a list response of business services.
type ListBusinessServicesResponse struct {
Total int `json:"total,omitempty"`
BusinessServices []*BusinessService `json:"business_services,omitempty"`
Offset int `json:"offset,omitempty"`
More bool `json:"more,omitempty"`
Limit int `json:"limit,omitempty"`
}

// List lists existing business services.
func (s *BusinessServiceService) List() (*ListBusinessServicesResponse, *Response, error) {
u := "/business_services"
v := new(ListBusinessServicesResponse)

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

return v, resp, nil
}

// Create creates a new business service.
func (s *BusinessServiceService) Create(ruleset *BusinessService) (*BusinessService, *Response, error) {
u := "/business_services"
v := new(BusinessServicePayload)
p := &BusinessServicePayload{BusinessService: ruleset}

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

return v.BusinessService, resp, nil
}

// Get gets a business service.
func (s *BusinessServiceService) Get(ID string) (*BusinessService, *Response, error) {
u := fmt.Sprintf("/business_services/%s", ID)
v := new(BusinessServicePayload)
p := &BusinessServicePayload{}

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

return v.BusinessService, resp, nil
}

// Delete deletes a business service.
func (s *BusinessServiceService) Delete(ID string) (*Response, error) {
u := fmt.Sprintf("/business_services/%s", ID)
return s.client.newRequestDo("DELETE", u, nil, nil, nil)
}

// Update updates a business service.
func (s *BusinessServiceService) Update(ID string, ruleset *BusinessService) (*BusinessService, *Response, error) {
u := fmt.Sprintf("/business_services/%s", ID)
v := new(BusinessServicePayload)
p := BusinessServicePayload{BusinessService: ruleset}

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

return v.BusinessService, resp, nil
}
143 changes: 143 additions & 0 deletions pagerduty/business_service_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
package pagerduty

import (
"encoding/json"
"net/http"
"reflect"
"testing"
)

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

mux.HandleFunc("/business_services", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
w.Write([]byte(`{"total": 1, "offset": 0, "more": false, "limit": 25, "business_services":[{"id": "1"}]}`))
})

resp, _, err := client.BusinessServices.List()
if err != nil {
t.Fatal(err)
}

want := &ListBusinessServicesResponse{
Total: 1,
Offset: 0,
More: false,
Limit: 25,
BusinessServices: []*BusinessService{
{
ID: "1",
},
},
}

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

func TestBusinessServiceCreate(t *testing.T) {
setup()
defer teardown()
input := &BusinessService{Name: "foo"}

mux.HandleFunc("/business_services", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "POST")
v := new(BusinessService)
v.Name = "foo"
json.NewDecoder(r.Body).Decode(v)
if !reflect.DeepEqual(v, input) {
t.Errorf("Request body = %+v, want %+v", v, input)
}
w.Write([]byte(`{"business_service":{"name": "foo", "id":"1"}}`))
})

resp, _, err := client.BusinessServices.Create(input)
if err != nil {
t.Fatal(err)
}

want := &BusinessService{
Name: "foo",
ID: "1",
}

if !reflect.DeepEqual(resp, want) {
t.Errorf("returned \n\n%#v want \n\n%#v", resp, want)
}
}
func TestBusinessServiceGet(t *testing.T) {
setup()
defer teardown()

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

ID := "1"
resp, _, err := client.BusinessServices.Get(ID)

if err != nil {
t.Fatal(err)
}

want := &BusinessService{
Name: "foo",
ID: "1",
}

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

func TestBusinessServiceUpdate(t *testing.T) {
setup()
defer teardown()
input := &BusinessService{
Name: "foo",
}

mux.HandleFunc("/business_services/1", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "PUT")
v := new(BusinessService)
v.Name = "foo"

json.NewDecoder(r.Body).Decode(v)
if !reflect.DeepEqual(v, input) {
t.Errorf("Request body = %+v, want %+v", v, input)
}
w.Write([]byte(`{"business_service":{"name": "foo", "id":"1"}}`))
})

resp, _, err := client.BusinessServices.Update("1", input)
if err != nil {
t.Fatal(err)
}

want := &BusinessService{
Name: "foo",
ID: "1",
}

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

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

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

if _, err := client.BusinessServices.Delete("1"); err != nil {
t.Fatal(err)
}
}
2 changes: 1 addition & 1 deletion pagerduty/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,5 @@ type Error struct {
}

func (e *Error) Error() string {
return fmt.Sprintf("%s API call to %s failed %v. Code: %d, Errors: %v, Message: %s", e.ErrorResponse.Request.Method, e.ErrorResponse.Request.URL.String(), e.ErrorResponse.Response.Status, e.Code, e.Errors, e.Message)
return fmt.Sprintf("%s API call to %s failed %v. Code: %d, Errors: %v, Message: %s", e.ErrorResponse.Response.Request.Method, e.ErrorResponse.Response.Request.URL.String(), e.ErrorResponse.Response.Status, e.Code, e.Errors, e.Message)
}
4 changes: 2 additions & 2 deletions pagerduty/error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ func TestErrorResponses(t *testing.T) {
t.Run(tc.name, func(t *testing.T) {
v := new(errorResponse)

r := &Response{Response: &http.Response{Body: ioutil.NopCloser(bytes.NewBuffer([]byte(tc.body)))}}
r := &Response{Response: &http.Response{Body: ioutil.NopCloser(bytes.NewBuffer([]byte(tc.body)))}, BodyBytes: []byte(tc.body)}

if err := decodeJSON(r, v); err != nil {
if err := client.DecodeJSON(r, v); err != nil {
t.Fatal(err)
}

Expand Down
6 changes: 3 additions & 3 deletions pagerduty/event_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func (s *EventRuleService) List() (*ListEventRulesResponse, *Response, error) {
return v, resp, nil
}

// Create creates a new escalation policy.
// Create creates a new event rule.
func (s *EventRuleService) Create(eventRule *EventRule) (*EventRule, *Response, error) {
u := "/event_rules"
v := new(EventRule)
Expand All @@ -49,13 +49,13 @@ func (s *EventRuleService) Create(eventRule *EventRule) (*EventRule, *Response,
return v, resp, nil
}

// Delete deletes an existing escalation policy.
// Delete deletes an existing event rule.
func (s *EventRuleService) Delete(id string) (*Response, error) {
u := fmt.Sprintf("/event_rules/%s", id)
return s.client.newRequestDo("DELETE", u, nil, nil, nil)
}

// Update updates an existing escalation policy.
// Update updates an existing event rule.
func (s *EventRuleService) Update(id string, eventRule *EventRule) (*EventRule, *Response, error) {
u := fmt.Sprintf("/event_rules/%s", id)
v := new(EventRule)
Expand Down
Loading

0 comments on commit 318975a

Please sign in to comment.