diff --git a/README.md b/README.md index 95803c4..0e2c153 100644 --- a/README.md +++ b/README.md @@ -15,6 +15,7 @@ * Memcached * InfluxDB * Nats + * AWS ## Usage diff --git a/checks/aws/sqs/check.go b/checks/aws/sqs/check.go new file mode 100644 index 0000000..b221bbf --- /dev/null +++ b/checks/aws/sqs/check.go @@ -0,0 +1,37 @@ +package sqs + +import ( + "context" + "fmt" + + "github.com/aws/aws-sdk-go-v2/service/sqs" + "github.com/aws/aws-sdk-go-v2/service/sqs/types" +) + +// Config is the AWS SQS checker configuration settings. +type Config struct { + // Client is the initialized instance of the AWS SQS client used for making API requests to the AWS SQS service. + Client SqsActions + // QueueUrl is a pointer to the string that contains the URL of the SQS queue to which the client connects. + QueueUrl *string +} + +// SqsActions defines the set of operations used from the SQS client. +type SqsActions interface { + GetQueueAttributes(ctx context.Context, params *sqs.GetQueueAttributesInput, optFns ...func(*sqs.Options)) (*sqs.GetQueueAttributesOutput, error) +} + +// New creates a new AWS SQS health check that verifies if a connection to the queue exists +func New(config Config) func(ctx context.Context) error { + return func(ctx context.Context) error { + arnAttributeName := types.QueueAttributeNameQueueArn + if _, err := config.Client.GetQueueAttributes(ctx, &sqs.GetQueueAttributesInput{ + QueueUrl: config.QueueUrl, + AttributeNames: []types.QueueAttributeName{arnAttributeName}, + }); err != nil { + return fmt.Errorf("unable to get queue ARN: %w", err) + } + + return nil + } +} diff --git a/checks/aws/sqs/check_test.go b/checks/aws/sqs/check_test.go new file mode 100644 index 0000000..981d9a5 --- /dev/null +++ b/checks/aws/sqs/check_test.go @@ -0,0 +1,71 @@ +package sqs + +import ( + "context" + "errors" + "testing" + + "github.com/aws/aws-sdk-go-v2/service/sqs" + "github.com/aws/aws-sdk-go-v2/service/sqs/types" +) + +// SimpleMockSQSClient is a manual mock of the SQSAPI interface. +type SimpleMockSQSClient struct { + // Add fields to store mock outputs and any other state + GetQueueAttributesOutput *sqs.GetQueueAttributesOutput + GetQueueAttributesErr error +} + +// GetQueueAttributes is the mock method that mimics the corresponding SQSAPI method. +func (m *SimpleMockSQSClient) GetQueueAttributes(ctx context.Context, params *sqs.GetQueueAttributesInput, optFns ...func(*sqs.Options)) (*sqs.GetQueueAttributesOutput, error) { + // Return the mocked response and error stored in the mock struct + return m.GetQueueAttributesOutput, m.GetQueueAttributesErr +} + +func TestNew(t *testing.T) { + queueURL := "http://example.com/queue" + mockSQSClient := &SimpleMockSQSClient{ + GetQueueAttributesOutput: &sqs.GetQueueAttributesOutput{ + Attributes: map[string]string{ + string(types.QueueAttributeNameQueueArn): "arn:aws:sqs:us-east-1:123456789012:queue1", + }, + }, + GetQueueAttributesErr: nil, + } + + config := Config{ + Client: mockSQSClient, + QueueUrl: &queueURL, + } + + checker := New(config) + + err := checker(context.Background()) + if err != nil { + t.Errorf("Expected no error, but got %v", err) + } +} + +func TestNewError(t *testing.T) { + queueURL := "http://example.com/queue" + mockSQSClient := &SimpleMockSQSClient{ + GetQueueAttributesOutput: &sqs.GetQueueAttributesOutput{ + Attributes: map[string]string{ + string(types.QueueAttributeNameQueueArn): "arn:aws:sqs:us-east-1:123456789012:queue1", + }, + }, + GetQueueAttributesErr: errors.New("failed to get queue attributes"), + } + + config := Config{ + Client: mockSQSClient, + QueueUrl: &queueURL, + } + + checker := New(config) + + err := checker(context.Background()) + if err == nil { + t.Errorf("Expected error, but got none") + } +} diff --git a/go.mod b/go.mod index b2ccc76..483a97c 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module github.com/hellofresh/health-go/v5 go 1.20 require ( + github.com/aws/aws-sdk-go-v2/service/sqs v1.31.4 github.com/bradfitz/gomemcache v0.0.0-20230905024940-24af94b03874 github.com/go-sql-driver/mysql v1.7.1 github.com/gocql/gocql v1.6.0 @@ -23,6 +24,10 @@ require ( require ( github.com/apapsch/go-jsonmerge/v2 v2.0.0 // indirect + github.com/aws/aws-sdk-go-v2 v1.26.1 // indirect + github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.5 // indirect + github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.5 // indirect + github.com/aws/smithy-go v1.20.2 // indirect github.com/cespare/xxhash/v2 v2.2.0 // indirect github.com/davecgh/go-spew v1.1.1 // indirect github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect diff --git a/go.sum b/go.sum index 0cb43b0..ec56228 100644 --- a/go.sum +++ b/go.sum @@ -3,6 +3,16 @@ github.com/Masterminds/semver/v3 v3.1.1/go.mod h1:VPu/7SZ7ePZ3QOrcuXROw5FAcLl4a0 github.com/RaveNoX/go-jsoncommentstrip v1.0.0/go.mod h1:78ihd09MekBnJnxpICcwzCMzGrKSKYe4AqU6PDYYpjk= github.com/apapsch/go-jsonmerge/v2 v2.0.0 h1:axGnT1gRIfimI7gJifB699GoE/oq+F2MU7Dml6nw9rQ= github.com/apapsch/go-jsonmerge/v2 v2.0.0/go.mod h1:lvDnEdqiQrp0O42VQGgmlKpxL1AP2+08jFMw88y4klk= +github.com/aws/aws-sdk-go-v2 v1.26.1 h1:5554eUqIYVWpU0YmeeYZ0wU64H2VLBs8TlhRB2L+EkA= +github.com/aws/aws-sdk-go-v2 v1.26.1/go.mod h1:ffIFB97e2yNsv4aTSGkqtHnppsIJzw7G7BReUZ3jCXM= +github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.5 h1:aw39xVGeRWlWx9EzGVnhOR4yOjQDHPQ6o6NmBlscyQg= +github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.5/go.mod h1:FSaRudD0dXiMPK2UjknVwwTYyZMRsHv3TtkabsZih5I= +github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.5 h1:PG1F3OD1szkuQPzDw3CIQsRIrtTlUC3lP84taWzHlq0= +github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.5/go.mod h1:jU1li6RFryMz+so64PpKtudI+QzbKoIEivqdf6LNpOc= +github.com/aws/aws-sdk-go-v2/service/sqs v1.31.4 h1:mE2ysZMEeQ3ulHWs4mmc4fZEhOfeY1o6QXAfDqjbSgw= +github.com/aws/aws-sdk-go-v2/service/sqs v1.31.4/go.mod h1:lCN2yKnj+Sp9F6UzpoPPTir+tSaC9Jwf6LcmTqnXFZw= +github.com/aws/smithy-go v1.20.2 h1:tbp628ireGtzcHDDmLT/6ADHidqnwgF57XOXZe6tp4Q= +github.com/aws/smithy-go v1.20.2/go.mod h1:krry+ya/rV9RDcV/Q16kpu6ypI4K2czasz0NC3qS14E= github.com/bitly/go-hostpool v0.0.0-20171023180738-a3a6125de932 h1:mXoPYz/Ul5HYEDvkta6I8/rnYM5gSdSV2tJ6XbZuEtY= github.com/bitly/go-hostpool v0.0.0-20171023180738-a3a6125de932/go.mod h1:NOuUCSz6Q9T7+igc/hlvDOUdtWKryOrtFyIVABv/p7k= github.com/bmatcuk/doublestar v1.1.1/go.mod h1:UD6OnuiIn0yFxxA2le/rnRU1G4RaI4UvFv1sNto9p6w=