Skip to content

Commit

Permalink
Properly propagate context to API client
Browse files Browse the repository at this point in the history
  • Loading branch information
LukeCarrier committed Jul 18, 2023
1 parent 1ce77ba commit 3ce0d51
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 65 deletions.
2 changes: 1 addition & 1 deletion bblnazuredevops/internal/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ type AggregatedClient struct {
SecurityClient security.Client
IdentityClient identity.Client
WorkItemTrackingClient workitemtracking.Client
InvokeCheckClient client.ChecksClient
InvokeCheckClient client.InvokeClient
ManualApprovalCheckClient client.ManualApprovalClient
ExclusiveLockCheckClient client.ExclusiveLockClient
GitAppClient githubappclient.GithubAppClient
Expand Down
89 changes: 44 additions & 45 deletions bblnazuredevops/internal/service/checks/common/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package client

import (
"bytes"
"context"
"encoding/json"
"fmt"
exclusivelockmodel "github.com/babylonhealth/terraform-provider-bblnazuredevops/bblnazuredevops/internal/service/checks/exclusivelock/model"
Expand Down Expand Up @@ -34,10 +35,10 @@ type GetChecksPayload struct {
} `json:"dataProviderContext"`
}

func (c *Client) GetInvokeRestAPICheckByID(projectID string, resourceID string, checkID int64) (invokerestapimodel.CheckConfigurationData, bool, error) {
func (c *Client) GetInvokeRestAPICheckByID(ctx context.Context, projectID string, resourceID string, checkID int64) (invokerestapimodel.CheckConfigurationData, bool, error) {
found := false

checkList, err := c.getInvokeRestAPIChecks(projectID, resourceID)
checkList, err := c.getInvokeRestAPIChecks(ctx, projectID, resourceID)

if err != nil {
return invokerestapimodel.CheckConfigurationData{}, found, err
Expand All @@ -54,10 +55,10 @@ func (c *Client) GetInvokeRestAPICheckByID(projectID string, resourceID string,
checkID, resourceID, projectID)
}

func (c *Client) GetManualApprovalCheckByID(projectID string, resourceID string, checkID int64) (manualapprovalmodel.ManualApprovalCheckConfig, bool, error) {
func (c *Client) GetManualApprovalCheckByID(ctx context.Context, projectID string, resourceID string, checkID int64) (manualapprovalmodel.ManualApprovalCheckConfig, bool, error) {
found := false

checkList, err := c.getManualApprovalChecks(projectID, resourceID)
checkList, err := c.getManualApprovalChecks(ctx, projectID, resourceID)
if err != nil {
return manualapprovalmodel.ManualApprovalCheckConfig{}, found, err
}
Expand All @@ -73,10 +74,10 @@ func (c *Client) GetManualApprovalCheckByID(projectID string, resourceID string,
checkID, resourceID, projectID)
}

func (c *Client) GetExclusiveLockCheckByID(projectID string, resourceID string, checkID int64) (exclusivelockmodel.ExclusiveLockCheckConfig, bool, error) {
func (c *Client) GetExclusiveLockCheckByID(ctx context.Context, projectID string, resourceID string, checkID int64) (exclusivelockmodel.ExclusiveLockCheckConfig, bool, error) {
found := false

checkList, err := c.getExclusiveLockChecks(projectID, resourceID)
checkList, err := c.getExclusiveLockChecks(ctx, projectID, resourceID)
if err != nil {
return exclusivelockmodel.ExclusiveLockCheckConfig{}, found, err
}
Expand All @@ -92,7 +93,7 @@ func (c *Client) GetExclusiveLockCheckByID(projectID string, resourceID string,
checkID, resourceID, projectID)
}

func (c *Client) getAllChecks(projectID string, resourceID string) ([]byte, error) {
func (c *Client) getAllChecks(ctx context.Context, projectID string, resourceID string) ([]byte, error) {
payload := GetChecksPayload{}
payload.ContributionIds = []string{"ms.vss-pipelinechecks.checks-data-provider"}
payload.DataProviderContext.Properties.ResourceID = resourceID
Expand All @@ -104,16 +105,16 @@ func (c *Client) getAllChecks(projectID string, resourceID string) ([]byte, erro
}

url := "/_apis/Contribution/HierarchyQuery"
respBytes, err := c.SendRequest("POST", url, string(jsonPayload))
respBytes, err := c.SendRequest(ctx, "POST", url, string(jsonPayload))
if err != nil {
return []byte{}, err
}

return respBytes, nil
}

func (c *Client) getInvokeRestAPIChecks(projectID string, resourceID string) ([]invokerestapimodel.CheckConfigurationData, error) {
allChecksBytes, err := c.getAllChecks(projectID, resourceID)
func (c *Client) getInvokeRestAPIChecks(ctx context.Context, projectID string, resourceID string) ([]invokerestapimodel.CheckConfigurationData, error) {
allChecksBytes, err := c.getAllChecks(ctx, projectID, resourceID)
if err != nil {
return []invokerestapimodel.CheckConfigurationData{}, err
}
Expand All @@ -127,8 +128,8 @@ func (c *Client) getInvokeRestAPIChecks(projectID string, resourceID string) ([]
return result.DataProviders.MsVssPipelinechecksChecksDataProvider.CheckConfigurationDataList, nil
}

func (c *Client) getManualApprovalChecks(projectID string, resourceID string) ([]manualapprovalmodel.ManualApprovalCheckConfig, error) {
allChecksBytes, err := c.getAllChecks(projectID, resourceID)
func (c *Client) getManualApprovalChecks(ctx context.Context, projectID string, resourceID string) ([]manualapprovalmodel.ManualApprovalCheckConfig, error) {
allChecksBytes, err := c.getAllChecks(ctx, projectID, resourceID)
if err != nil {
return []manualapprovalmodel.ManualApprovalCheckConfig{}, err
}
Expand All @@ -148,8 +149,8 @@ func (c *Client) getManualApprovalChecks(projectID string, resourceID string) ([
return configs, nil
}

func (c *Client) getExclusiveLockChecks(projectID string, resourceID string) ([]exclusivelockmodel.ExclusiveLockCheckConfig, error) {
allChecksBytes, err := c.getAllChecks(projectID, resourceID)
func (c *Client) getExclusiveLockChecks(ctx context.Context, projectID string, resourceID string) ([]exclusivelockmodel.ExclusiveLockCheckConfig, error) {
allChecksBytes, err := c.getAllChecks(ctx, projectID, resourceID)
if err != nil {
return []exclusivelockmodel.ExclusiveLockCheckConfig{}, err
}
Expand All @@ -169,7 +170,7 @@ func (c *Client) getExclusiveLockChecks(projectID string, resourceID string) ([]
return configs, nil
}

func (c *Client) AddInvokeRestAPICheck(projectID string, resourceID string, check invokerestapimodel.InvokeRESTAPIValues) (invokerestapimodel.CheckConfiguration, error) {
func (c *Client) AddInvokeRestAPICheck(ctx context.Context, projectID string, resourceID string, check invokerestapimodel.InvokeRESTAPIValues) (invokerestapimodel.CheckConfiguration, error) {
restAPIPayload := populateInvokeRestAPIPayload(resourceID, check)

jsonPayload, err := json.Marshal(restAPIPayload)
Expand All @@ -178,7 +179,7 @@ func (c *Client) AddInvokeRestAPICheck(projectID string, resourceID string, chec
}

url := fmt.Sprintf("/%s/_apis/pipelines/checks/configurations", projectID)
respBytes, err := c.SendRequest("POST", url, string(jsonPayload))
respBytes, err := c.SendRequest(ctx, "POST", url, string(jsonPayload))
if err != nil {
return invokerestapimodel.CheckConfiguration{}, err
}
Expand All @@ -193,7 +194,7 @@ func (c *Client) AddInvokeRestAPICheck(projectID string, resourceID string, chec
return checkConf, nil
}

func (c *Client) AddManualApprovalCheck(projectID string, resourceID string,
func (c *Client) AddManualApprovalCheck(ctx context.Context, projectID string, resourceID string,
check manualapprovalmodel.ManualApprovalValues) (manualapprovalmodel.ManualApprovalCheckConfig, error) {
manualApproval := populateManualApprovalPayload(resourceID, check)

Expand All @@ -203,7 +204,7 @@ func (c *Client) AddManualApprovalCheck(projectID string, resourceID string,
}

url := fmt.Sprintf("/%s/_apis/pipelines/checks/configurations", projectID)
respBytes, err := c.SendRequest("POST", url, string(jsonPayload))
respBytes, err := c.SendRequest(ctx, "POST", url, string(jsonPayload))
if err != nil {
return manualapprovalmodel.ManualApprovalCheckConfig{}, err
}
Expand All @@ -218,7 +219,7 @@ func (c *Client) AddManualApprovalCheck(projectID string, resourceID string,
return checkConf, nil
}

func (c *Client) AddExclusiveLockCheck(projectID string, resourceID string,
func (c *Client) AddExclusiveLockCheck(ctx context.Context, projectID string, resourceID string,
check exclusivelockmodel.ExclusiveLockValues) (exclusivelockmodel.ExclusiveLockCheckConfig, error) {
exclusiveLock := populateExclusiveLockPayload(resourceID, check)

Expand All @@ -228,7 +229,7 @@ func (c *Client) AddExclusiveLockCheck(projectID string, resourceID string,
}

url := fmt.Sprintf("/%s/_apis/pipelines/checks/configurations", projectID)
respBytes, err := c.SendRequest("POST", url, string(jsonPayload))
respBytes, err := c.SendRequest(ctx, "POST", url, string(jsonPayload))
if err != nil {
return exclusivelockmodel.ExclusiveLockCheckConfig{}, err
}
Expand All @@ -243,7 +244,7 @@ func (c *Client) AddExclusiveLockCheck(projectID string, resourceID string,
return checkConf, nil
}

func (c *Client) UpdateManualApprovalCheck(projectID string, resourceID string, checkID string,
func (c *Client) UpdateManualApprovalCheck(ctx context.Context, projectID string, resourceID string, checkID string,
check manualapprovalmodel.ManualApprovalValues) (manualapprovalmodel.ManualApprovalCheckConfig, error) {
manualApproval := populateManualApprovalPayload(resourceID, check)
manualApproval.ID = checkID
Expand All @@ -254,7 +255,7 @@ func (c *Client) UpdateManualApprovalCheck(projectID string, resourceID string,
}

url := fmt.Sprintf("/%s/_apis/pipelines/checks/configurations/%s", projectID, checkID)
respBytes, err := c.SendRequest("PATCH", url, string(jsonPayload))
respBytes, err := c.SendRequest(ctx, "PATCH", url, string(jsonPayload))
if err != nil {
return manualapprovalmodel.ManualApprovalCheckConfig{}, err
}
Expand All @@ -269,7 +270,7 @@ func (c *Client) UpdateManualApprovalCheck(projectID string, resourceID string,
return checkConf, nil
}

func (c *Client) UpdateExclusiveLockCheck(projectID string, resourceID string, checkID string,
func (c *Client) UpdateExclusiveLockCheck(ctx context.Context, projectID string, resourceID string, checkID string,
check exclusivelockmodel.ExclusiveLockValues) (exclusivelockmodel.ExclusiveLockCheckConfig, error) {
exclusiveLock := populateExclusiveLockPayload(resourceID, check)
exclusiveLock.ID = checkID
Expand All @@ -280,7 +281,7 @@ func (c *Client) UpdateExclusiveLockCheck(projectID string, resourceID string, c
}

url := fmt.Sprintf("/%s/_apis/pipelines/checks/configurations/%s", projectID, checkID)
respBytes, err := c.SendRequest("PATCH", url, string(jsonPayload))
respBytes, err := c.SendRequest(ctx, "PATCH", url, string(jsonPayload))
if err != nil {
return exclusivelockmodel.ExclusiveLockCheckConfig{}, err
}
Expand All @@ -295,7 +296,7 @@ func (c *Client) UpdateExclusiveLockCheck(projectID string, resourceID string, c
return checkConf, nil
}

func (c *Client) UpdateCheck(projectID string, resourceID string, checkID string, check invokerestapimodel.InvokeRESTAPIValues) (invokerestapimodel.CheckConfiguration, error) {
func (c *Client) UpdateCheck(ctx context.Context, projectID string, resourceID string, checkID string, check invokerestapimodel.InvokeRESTAPIValues) (invokerestapimodel.CheckConfiguration, error) {
restAPIPayload := populateInvokeRestAPIPayload(resourceID, check)
restAPIPayload.ID = checkID

Expand All @@ -305,7 +306,7 @@ func (c *Client) UpdateCheck(projectID string, resourceID string, checkID string
}

url := fmt.Sprintf("/%s/_apis/pipelines/checks/configurations/%s", projectID, checkID)
respBytes, err := c.SendRequest("PATCH", url, string(jsonPayload))
respBytes, err := c.SendRequest(ctx, "PATCH", url, string(jsonPayload))
if err != nil {
return invokerestapimodel.CheckConfiguration{}, err
}
Expand All @@ -320,9 +321,9 @@ func (c *Client) UpdateCheck(projectID string, resourceID string, checkID string
return checkConf, nil
}

func (c *Client) DeleteCheck(projectID string, checkID string) error {
func (c *Client) DeleteCheck(ctx context.Context, projectID string, checkID string) error {
url := fmt.Sprintf("/%s/_apis/pipelines/checks/configurations/%s", projectID, checkID)
_, err := c.SendRequest("DELETE", url, "")
_, err := c.SendRequest(ctx, "DELETE", url, "")

return err
}
Expand Down Expand Up @@ -397,10 +398,8 @@ func populateExclusiveLockPayload(resourceID string,
return exclusiveLock
}

func (c *Client) SendRequest(httpMethod string, url string, jsonPayload string) ([]byte, error) {
req, err := http.NewRequest(httpMethod,
c.baseUrl+url,
bytes.NewBufferString(jsonPayload))
func (c *Client) SendRequest(ctx context.Context, httpMethod string, url string, jsonPayload string) ([]byte, error) {
req, err := http.NewRequestWithContext(ctx, httpMethod, c.baseUrl+url, bytes.NewBufferString(jsonPayload))
if err != nil {
return []byte{}, err
}
Expand Down Expand Up @@ -448,22 +447,22 @@ func NewClient(baseUrl string, auth string, timeout *time.Duration) *Client {
}

type ManualApprovalClient interface {
GetManualApprovalCheckByID(projectID string, resourceID string, checkID int64) (manualapprovalmodel.ManualApprovalCheckConfig, bool, error)
AddManualApprovalCheck(projectID string, resourceID string, check manualapprovalmodel.ManualApprovalValues) (manualapprovalmodel.ManualApprovalCheckConfig, error)
UpdateManualApprovalCheck(projectID string, resourceID string, checkID string, check manualapprovalmodel.ManualApprovalValues) (manualapprovalmodel.ManualApprovalCheckConfig, error)
DeleteCheck(projectID string, checkID string) error
GetManualApprovalCheckByID(ctx context.Context, projectID string, resourceID string, checkID int64) (manualapprovalmodel.ManualApprovalCheckConfig, bool, error)
AddManualApprovalCheck(ctx context.Context, projectID string, resourceID string, check manualapprovalmodel.ManualApprovalValues) (manualapprovalmodel.ManualApprovalCheckConfig, error)
UpdateManualApprovalCheck(ctx context.Context, projectID string, resourceID string, checkID string, check manualapprovalmodel.ManualApprovalValues) (manualapprovalmodel.ManualApprovalCheckConfig, error)
DeleteCheck(ctx context.Context, projectID string, checkID string) error
}

type ExclusiveLockClient interface {
GetExclusiveLockCheckByID(projectID string, resourceID string, checkID int64) (exclusivelockmodel.ExclusiveLockCheckConfig, bool, error)
AddExclusiveLockCheck(projectID string, resourceID string, check exclusivelockmodel.ExclusiveLockValues) (exclusivelockmodel.ExclusiveLockCheckConfig, error)
UpdateExclusiveLockCheck(projectID string, resourceID string, checkID string, check exclusivelockmodel.ExclusiveLockValues) (exclusivelockmodel.ExclusiveLockCheckConfig, error)
DeleteCheck(projectID string, checkID string) error
GetExclusiveLockCheckByID(ctx context.Context, projectID string, resourceID string, checkID int64) (exclusivelockmodel.ExclusiveLockCheckConfig, bool, error)
AddExclusiveLockCheck(ctx context.Context, projectID string, resourceID string, check exclusivelockmodel.ExclusiveLockValues) (exclusivelockmodel.ExclusiveLockCheckConfig, error)
UpdateExclusiveLockCheck(ctx context.Context, projectID string, resourceID string, checkID string, check exclusivelockmodel.ExclusiveLockValues) (exclusivelockmodel.ExclusiveLockCheckConfig, error)
DeleteCheck(ctx context.Context, projectID string, checkID string) error
}

type ChecksClient interface {
GetInvokeRestAPICheckByID(projectID string, resourceID string, checkID int64) (invokerestapimodel.CheckConfigurationData, bool, error)
AddInvokeRestAPICheck(projectID string, resourceID string, check invokerestapimodel.InvokeRESTAPIValues) (invokerestapimodel.CheckConfiguration, error)
UpdateCheck(projectID string, resourceID string, checkID string, check invokerestapimodel.InvokeRESTAPIValues) (invokerestapimodel.CheckConfiguration, error)
DeleteCheck(projectID string, checkID string) error
type InvokeClient interface {
GetInvokeRestAPICheckByID(ctx context.Context, projectID string, resourceID string, checkID int64) (invokerestapimodel.CheckConfigurationData, bool, error)
AddInvokeRestAPICheck(ctx context.Context, projectID string, resourceID string, check invokerestapimodel.InvokeRESTAPIValues) (invokerestapimodel.CheckConfiguration, error)
UpdateCheck(ctx context.Context, projectID string, resourceID string, checkID string, check invokerestapimodel.InvokeRESTAPIValues) (invokerestapimodel.CheckConfiguration, error)
DeleteCheck(ctx context.Context, projectID string, checkID string) error
}
Loading

0 comments on commit 3ce0d51

Please sign in to comment.