Skip to content

Commit

Permalink
Merge pull request #115 from imjaroiswebdev/a2actionsAssociationWithS…
Browse files Browse the repository at this point in the history
…ervice

add support for `GET/POST/DELETE` action association to a service
  • Loading branch information
imjaroiswebdev authored Jan 6, 2023
2 parents cb061e1 + b225b61 commit 1866a66
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 0 deletions.
40 changes: 40 additions & 0 deletions pagerduty/automation_actions_action.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ type AutomationActionsActionTeamAssociationPayload struct {
Team *TeamReference `json:"team,omitempty"`
}

type AutomationActionsActionServiceAssociationPayload struct {
Service *ServiceReference `json:"service,omitempty"`
}

var automationActionsActionBaseUrl = "/automation_actions/actions"

// Create creates a new action
Expand Down Expand Up @@ -122,3 +126,39 @@ func (s *AutomationActionsActionService) GetAssociationToTeam(actionID, teamID s

return v, resp, nil
}

// Associate an Automation Action with a service
func (s *AutomationActionsActionService) AssociateToService(actionID, serviceID string) (*AutomationActionsActionServiceAssociationPayload, *Response, error) {
u := fmt.Sprintf("%s/%s/services", automationActionsActionBaseUrl, actionID)
v := new(AutomationActionsActionServiceAssociationPayload)
p := &AutomationActionsActionServiceAssociationPayload{
Service: &ServiceReference{ID: serviceID, Type: "service_reference"},
}

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

return v, resp, nil
}

// Dissociate an Automation Action with a service
func (s *AutomationActionsActionService) DissociateFromService(actionID, serviceID string) (*Response, error) {
u := fmt.Sprintf("%s/%s/services/%s", automationActionsActionBaseUrl, actionID, serviceID)

return s.client.newRequestDoOptions("DELETE", u, nil, nil, nil)
}

// Gets the details of an Automation Action / service relation
func (s *AutomationActionsActionService) GetAssociationToService(actionID, serviceID string) (*AutomationActionsActionServiceAssociationPayload, *Response, error) {
u := fmt.Sprintf("%s/%s/services/%s", automationActionsActionBaseUrl, actionID, serviceID)
v := new(AutomationActionsActionServiceAssociationPayload)

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

return v, resp, nil
}
72 changes: 72 additions & 0 deletions pagerduty/automation_actions_action_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -391,3 +391,75 @@ func TestAutomationActionsActionTeamAssociationGet(t *testing.T) {
t.Errorf("returned \n\n%#v want \n\n%#v", resp, want)
}
}

func TestAutomationActionsActionServiceAssociationCreate(t *testing.T) {
setup()
defer teardown()
actionID := "01DA2MLYN0J5EFC1LKWXUKDDKT"
serviceID := "1"

mux.HandleFunc(fmt.Sprintf("/automation_actions/actions/%s/services", actionID), func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "POST")
w.Write([]byte(`{"service":{"id":"1","type":"service_reference"}}`))
})

resp, _, err := client.AutomationActionsAction.AssociateToService(actionID, serviceID)
if err != nil {
t.Fatal(err)
}

want := &AutomationActionsActionServiceAssociationPayload{
&ServiceReference{
ID: serviceID,
Type: "service_reference",
},
}

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

func TestAutomationActionsActionServiceAssociationDelete(t *testing.T) {
setup()
defer teardown()
actionID := "01DA2MLYN0J5EFC1LKWXUKDDKT"
serviceID := "1"

mux.HandleFunc(fmt.Sprintf("/automation_actions/actions/%s/services/%s", actionID, serviceID), func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "DELETE")
w.WriteHeader(http.StatusNoContent)
})

if _, err := client.AutomationActionsAction.DissociateFromService(actionID, serviceID); err != nil {
t.Fatal(err)
}
}

func TestAutomationActionsActionServiceAssociationGet(t *testing.T) {
setup()
defer teardown()
actionID := "01DA2MLYN0J5EFC1LKWXUKDDKT"
serviceID := "1"

mux.HandleFunc(fmt.Sprintf("/automation_actions/actions/%s/services/%s", actionID, serviceID), func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
w.Write([]byte(`{"service":{"id":"1","type":"service_reference"}}`))
})

resp, _, err := client.AutomationActionsAction.GetAssociationToService(actionID, serviceID)
if err != nil {
t.Fatal(err)
}

want := &AutomationActionsActionServiceAssociationPayload{
&ServiceReference{
ID: serviceID,
Type: "service_reference",
},
}

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

0 comments on commit 1866a66

Please sign in to comment.