Skip to content

Commit

Permalink
OSD-23985: Refactor pagerduty client and add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
samanthajayasinghe committed Jun 12, 2024
1 parent 6075bc8 commit dd24031
Show file tree
Hide file tree
Showing 7 changed files with 518 additions and 165 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ mock-gen:
mockgen -destination=./pkg/backplaneapi/mocks/clientUtilsMock.go -package=mocks github.com/openshift/backplane-cli/pkg/backplaneapi ClientUtils
mockgen -destination=./pkg/cli/session/mocks/sessionMock.go -package=mocks github.com/openshift/backplane-cli/pkg/cli/session BackplaneSessionInterface
mockgen -destination=./pkg/utils/mocks/shellCheckerMock.go -package=mocks github.com/openshift/backplane-cli/pkg/utils ShellCheckerInterface
mockgen -destination=./pkg/pagerduty/mocks/clientMock.go -package=mocks github.com/openshift/backplane-cli/pkg/pagerduty PagerDutyClient

.PHONY: build-image
build-image:
Expand Down
66 changes: 66 additions & 0 deletions pkg/pagerduty/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package pagerduty

import (
"context"
"net/http"

pdApi "github.com/PagerDuty/go-pagerduty"
)

// PagerDutyClient is an interface for the actual PD API
type PagerDutyClient interface {
ListIncidents(pdApi.ListIncidentsOptions) (*pdApi.ListIncidentsResponse, error)
ListIncidentAlerts(incidentId string) (*pdApi.ListAlertsResponse, error)
GetCurrentUser(pdApi.GetCurrentUserOptions) (*pdApi.User, error)
GetIncidentAlert(incidentID, alertID string) (*pdApi.IncidentAlertResponse, *http.Response, error)
GetService(serviceID string, opts *pdApi.GetServiceOptions) (*pdApi.Service, error)
GetServiceWithContext(ctx context.Context, serviceID string, opts *pdApi.GetServiceOptions) (*pdApi.Service, error)
ListOnCalls(opts pdApi.ListOnCallOptions) (*pdApi.ListOnCallsResponse, error)
}

type PDClient struct {
PdClient PagerDutyClient
}

// NewClient creates an instance of PDClient that is then used to connect to the actual pagerduty client.
func NewClient() *PDClient {
return &PDClient{}
}

// Connect uses the information stored in new client to create a new PagerDuty connection.
// It returns the PDClient object with pagerduty API connection initialized.
func (pd *PDClient) Connect(authToken string, options ...pdApi.ClientOptions) (client *PDClient, err error) {

// Create a new PagerDuty API client
pdApi.NewClient(authToken, options...)

return pd, nil
}

func (c *PDClient) ListIncidents(opts pdApi.ListIncidentsOptions) (*pdApi.ListIncidentsResponse, error) {
return c.PdClient.ListIncidents(opts)
}

func (c *PDClient) ListIncidentAlerts(incidentID string) (*pdApi.ListAlertsResponse, error) {
return c.PdClient.ListIncidentAlerts(incidentID)
}

func (c *PDClient) GetCurrentUser(opts pdApi.GetCurrentUserOptions) (*pdApi.User, error) {
return c.PdClient.GetCurrentUser(opts)
}

func (c *PDClient) GetIncidentAlert(incidentID, alertID string) (*pdApi.IncidentAlertResponse, *http.Response, error) {
return c.PdClient.GetIncidentAlert(incidentID, alertID)
}

func (c *PDClient) GetService(serviceID string, opts *pdApi.GetServiceOptions) (*pdApi.Service, error) {
return c.PdClient.GetService(serviceID, opts)
}

func (c *PDClient) GetServiceWithContext(ctx context.Context, serviceID string, opts *pdApi.GetServiceOptions) (*pdApi.Service, error) {
return c.PdClient.GetServiceWithContext(ctx, serviceID, opts)
}

func (c *PDClient) ListOnCalls(opts pdApi.ListOnCallOptions) (*pdApi.ListOnCallsResponse, error) {
return c.PdClient.ListOnCalls(opts)
}
95 changes: 0 additions & 95 deletions pkg/pagerduty/mock/pagerdutymock.go

This file was deleted.

143 changes: 143 additions & 0 deletions pkg/pagerduty/mocks/clientMock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit dd24031

Please sign in to comment.