diff --git a/internal/handlers/active-directory-deployment.go b/internal/handlers/active-directory-deployment.go index d5654b92..6fff5617 100644 --- a/internal/handlers/active-directory-deployment.go +++ b/internal/handlers/active-directory-deployment.go @@ -20,6 +20,7 @@ package handlers import ( + "context" "encoding/json" "errors" "fmt" @@ -237,6 +238,6 @@ func (d *activeDirectoryDeploymentToSNowDeliverer) PluginTypeID() string { return "active-directory-deployment-to-servicenow.v1" } -func (d *activeDirectoryDeploymentToSNowDeliverer) DeliverPayload(payload []byte) (*tenso.DeliveryLog, error) { - return d.Client.DeliverChangePayload(payload) +func (d *activeDirectoryDeploymentToSNowDeliverer) DeliverPayload(ctx context.Context, payload []byte) (*tenso.DeliveryLog, error) { + return d.Client.DeliverChangePayload(ctx, payload) } diff --git a/internal/handlers/awx-workflow.go b/internal/handlers/awx-workflow.go index e8c2bcde..95eca52e 100644 --- a/internal/handlers/awx-workflow.go +++ b/internal/handlers/awx-workflow.go @@ -21,6 +21,7 @@ package handlers import ( "bytes" + "context" "encoding/json" "errors" "fmt" @@ -180,7 +181,7 @@ func (a *awxWorkflowToSwiftDeliverer) PluginTypeID() string { return "infra-workflow-to-swift.v1" } -func (a *awxWorkflowToSwiftDeliverer) DeliverPayload(payload []byte) (*tenso.DeliveryLog, error) { +func (a *awxWorkflowToSwiftDeliverer) DeliverPayload(_ context.Context, payload []byte) (*tenso.DeliveryLog, error) { event, err := jsonUnmarshalStrict[awxWorkflowEvent](payload) if err != nil { return nil, err @@ -263,10 +264,10 @@ func (a *awxWorkflowToSNowDeliverer) PluginTypeID() string { return "infra-workflow-to-servicenow.v1" } -func (a *awxWorkflowToSNowDeliverer) DeliverPayload(payload []byte) (*tenso.DeliveryLog, error) { +func (a *awxWorkflowToSNowDeliverer) DeliverPayload(ctx context.Context, payload []byte) (*tenso.DeliveryLog, error) { //if the TranslationHandler wants us to ignore this payload, skip the delivery if string(payload) == "skip" { return nil, nil } - return a.Client.DeliverChangePayload(payload) + return a.Client.DeliverChangePayload(ctx, payload) } diff --git a/internal/handlers/helm-deployment.go b/internal/handlers/helm-deployment.go index dffa8fbd..b1717f66 100644 --- a/internal/handlers/helm-deployment.go +++ b/internal/handlers/helm-deployment.go @@ -21,6 +21,7 @@ package handlers import ( "bytes" + "context" "encoding/json" "errors" "fmt" @@ -152,7 +153,7 @@ func (h *helmDeploymentToElkDeliverer) PluginTypeID() string { return "helm-deployment-to-elk.v1" } -func (h *helmDeploymentToElkDeliverer) DeliverPayload(payload []byte) (*tenso.DeliveryLog, error) { +func (h *helmDeploymentToElkDeliverer) DeliverPayload(_ context.Context, payload []byte) (*tenso.DeliveryLog, error) { //Logstash wants everything on one line, so ensure we don't have unnecessary whitespace in the payload var buf bytes.Buffer err := json.Compact(&buf, payload) @@ -193,7 +194,7 @@ func (h *helmDeploymentToSwiftDeliverer) PluginTypeID() string { return "helm-deployment-to-swift.v1" } -func (h *helmDeploymentToSwiftDeliverer) DeliverPayload(payload []byte) (*tenso.DeliveryLog, error) { +func (h *helmDeploymentToSwiftDeliverer) DeliverPayload(_ context.Context, payload []byte) (*tenso.DeliveryLog, error) { event, err := jsonUnmarshalStrict[deployevent.Event](payload) if err != nil { return nil, err @@ -261,6 +262,6 @@ func (h *helmDeploymentToSNowDeliverer) PluginTypeID() string { return "helm-deployment-to-servicenow.v1" } -func (h *helmDeploymentToSNowDeliverer) DeliverPayload(payload []byte) (*tenso.DeliveryLog, error) { - return h.Client.DeliverChangePayload(payload) +func (h *helmDeploymentToSNowDeliverer) DeliverPayload(ctx context.Context, payload []byte) (*tenso.DeliveryLog, error) { + return h.Client.DeliverChangePayload(ctx, payload) } diff --git a/internal/handlers/terraform-deployment.go b/internal/handlers/terraform-deployment.go index c8cd5f58..a5a197a3 100644 --- a/internal/handlers/terraform-deployment.go +++ b/internal/handlers/terraform-deployment.go @@ -21,6 +21,7 @@ package handlers import ( "bytes" + "context" "errors" "fmt" "sort" @@ -134,7 +135,7 @@ func (h *terraformDeploymentToSwiftDeliverer) PluginTypeID() string { return "terraform-deployment-to-swift.v1" } -func (h *terraformDeploymentToSwiftDeliverer) DeliverPayload(payload []byte) (*tenso.DeliveryLog, error) { +func (h *terraformDeploymentToSwiftDeliverer) DeliverPayload(_ context.Context, payload []byte) (*tenso.DeliveryLog, error) { event, err := jsonUnmarshalStrict[deployevent.Event](payload) if err != nil { return nil, err @@ -242,6 +243,6 @@ func (d *terraformDeploymentToSNowDeliverer) PluginTypeID() string { return "terraform-deployment-to-servicenow.v1" } -func (d *terraformDeploymentToSNowDeliverer) DeliverPayload(payload []byte) (*tenso.DeliveryLog, error) { - return d.Client.DeliverChangePayload(payload) +func (d *terraformDeploymentToSNowDeliverer) DeliverPayload(ctx context.Context, payload []byte) (*tenso.DeliveryLog, error) { + return d.Client.DeliverChangePayload(ctx, payload) } diff --git a/internal/servicenow/client.go b/internal/servicenow/client.go index 09a93fbd..52bed580 100644 --- a/internal/servicenow/client.go +++ b/internal/servicenow/client.go @@ -21,6 +21,7 @@ package servicenow import ( "bytes" + "context" "crypto/tls" "encoding/json" "fmt" @@ -85,13 +86,13 @@ func NewClientFromEnv(envPrefix string) (*Client, error) { // DeliverChangePayload delivers a change payload to ServiceNow. This function // has the same interface as DeliverPayload() in the tenso.DeliveryHandler // interface. -func (c *Client) DeliverChangePayload(payload []byte) (*tenso.DeliveryLog, error) { +func (c *Client) DeliverChangePayload(ctx context.Context, payload []byte) (*tenso.DeliveryLog, error) { //if the TranslationHandler wants us to ignore this payload, skip the delivery if string(payload) == "skip" { return nil, nil } - req, err := http.NewRequest(http.MethodPost, c.EndpointURL, bytes.NewReader(payload)) + req, err := http.NewRequestWithContext(ctx, http.MethodPost, c.EndpointURL, bytes.NewReader(payload)) if err != nil { return nil, fmt.Errorf("while preparing request for POST %s: %w", c.EndpointURL, err) } diff --git a/internal/tasks/deliver.go b/internal/tasks/deliver.go index e6759155..08d83359 100644 --- a/internal/tasks/deliver.go +++ b/internal/tasks/deliver.go @@ -66,7 +66,7 @@ func (c *Context) DeliveryJob(registerer prometheus.Registerer) jobloop.Job { }).Setup(registerer) } -func (c *Context) processDelivery(_ context.Context, tx *gorp.Transaction, pd tenso.PendingDelivery, labels prometheus.Labels) (returnedError error) { +func (c *Context) processDelivery(ctx context.Context, tx *gorp.Transaction, pd tenso.PendingDelivery, labels prometheus.Labels) (returnedError error) { var event tenso.Event labels["payload_type"] = pd.PayloadType @@ -98,7 +98,7 @@ func (c *Context) processDelivery(_ context.Context, tx *gorp.Transaction, pd te } //try to translate the payload, or set up a delayed retry on failure - dlog, err := dh.DeliverPayload([]byte(*pd.Payload)) + dlog, err := dh.DeliverPayload(ctx, []byte(*pd.Payload)) if err != nil { pd.NextDeliveryAt = c.timeNow().Add(DeliveryRetryInterval) pd.FailedDeliveryCount++ diff --git a/internal/tenso/handlers.go b/internal/tenso/handlers.go index 8a160138..b2adcc2a 100644 --- a/internal/tenso/handlers.go +++ b/internal/tenso/handlers.go @@ -20,6 +20,7 @@ package tenso import ( + "context" "fmt" "strings" @@ -94,7 +95,7 @@ type DeliveryHandler interface { //talk to OpenStack. During unit tests, (nil, nil) will be provided instead. Init(pc *gophercloud.ProviderClient, eo gophercloud.EndpointOpts) error - DeliverPayload(payload []byte) (*DeliveryLog, error) + DeliverPayload(ctx context.Context, payload []byte) (*DeliveryLog, error) } // DeliveryLog can be returned by DeliverPayload() to produce additional log diff --git a/internal/test/handlers.go b/internal/test/handlers.go index 14fdd174..a91eb2dc 100644 --- a/internal/test/handlers.go +++ b/internal/test/handlers.go @@ -21,6 +21,7 @@ package test import ( "bytes" + "context" "encoding/json" "errors" "fmt" @@ -115,7 +116,7 @@ func (h *testDeliveryHandler) PluginTypeID() string { return fmt.Sprintf("test-%s.v1", h.Type) } -func (h *testDeliveryHandler) DeliverPayload(data []byte) (*tenso.DeliveryLog, error) { +func (h *testDeliveryHandler) DeliverPayload(_ context.Context, data []byte) (*tenso.DeliveryLog, error) { //We don't actually deliver anywhere, but by giving us an invalid payload, tests can "simulate" a delivery failure. _, err := parseTestPayload(data, h.Type) if err != nil {