Skip to content

Commit

Permalink
Merge pull request #450 from samanthajayasinghe/pd-test-tmp
Browse files Browse the repository at this point in the history
OSD-23985: Refactor pagerduty client and add unit tests
  • Loading branch information
openshift-merge-bot[bot] authored Jun 13, 2024
2 parents d49bfa2 + 3dc82d7 commit cee71b5
Show file tree
Hide file tree
Showing 8 changed files with 561 additions and 166 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
4 changes: 2 additions & 2 deletions cmd/ocm-backplane/login/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,12 +131,12 @@ func runLogin(cmd *cobra.Command, argv []string) (err error) {
}
if strings.Contains(args.pd, "/incidents/") {
incidentID := args.pd[strings.LastIndex(args.pd, "/")+1:]
clusterKey, err = pdClient.GetClusterID(incidentID)
clusterKey, err = pdClient.GetClusterIDFromIncident(incidentID)
if err != nil {
return err
}
} else {
clusterKey, err = pdClient.GetClusterID(args.pd)
clusterKey, err = pdClient.GetClusterIDFromIncident(args.pd)
if err != nil {
return err
}
Expand Down
51 changes: 51 additions & 0 deletions pkg/pagerduty/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package pagerduty

import (
"context"
"fmt"

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

// PagerDutyClient is an interface for the actual PD API
type PagerDutyClient interface {
Connect(authToken string, options ...pdApi.ClientOptions) error
ListIncidents(pdApi.ListIncidentsOptions) (*pdApi.ListIncidentsResponse, error)
ListIncidentAlerts(incidentID string) (*pdApi.ListAlertsResponse, error)
GetServiceWithContext(ctx context.Context, serviceID string, opts *pdApi.GetServiceOptions) (*pdApi.Service, error)
}

type DefaultPagerDutyClientImpl struct {
client *pdApi.Client
}

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

// 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 (c *DefaultPagerDutyClientImpl) Connect(authToken string, options ...pdApi.ClientOptions) error {

if authToken == "" {
return fmt.Errorf("empty pagerduty token")
}

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

return nil
}

func (c *DefaultPagerDutyClientImpl) ListIncidents(opts pdApi.ListIncidentsOptions) (*pdApi.ListIncidentsResponse, error) {
return c.client.ListIncidentsWithContext(context.TODO(), opts)
}

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

func (c *DefaultPagerDutyClientImpl) GetServiceWithContext(ctx context.Context, serviceID string, opts *pdApi.GetServiceOptions) (*pdApi.Service, error) {
return c.client.GetServiceWithContext(ctx, serviceID, opts)
}
95 changes: 0 additions & 95 deletions pkg/pagerduty/mock/pagerdutymock.go

This file was deleted.

100 changes: 100 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 cee71b5

Please sign in to comment.