Skip to content

Commit

Permalink
[sqs] Initial boilerplate
Browse files Browse the repository at this point in the history
  • Loading branch information
dzbarsky committed Aug 23, 2023
1 parent dfa1e3e commit 8c1aa7f
Show file tree
Hide file tree
Showing 6 changed files with 127 additions and 1 deletion.
13 changes: 13 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"aws-in-a-box/services/kinesis"
"aws-in-a-box/services/kms"
"aws-in-a-box/services/s3"
"aws-in-a-box/services/sqs"
)

func versionString() string {
Expand Down Expand Up @@ -63,6 +64,8 @@ func main() {
enableS3 := flag.Bool("experimental_enableS3", true, "Enable S3 service")
s3InitialBuckets := flag.String("s3InitialBuckets", "", "Buckets to create at startup. Example: bucket1,bucket2,bucket3")

enableSQS := flag.Bool("enableSQS", true, "Enable SQS service")

flag.Parse()

var level slog.Level
Expand Down Expand Up @@ -132,6 +135,16 @@ func main() {
logger.Info("Enabled DynamoDB (EXPERIMENTAL!!!)")
}

if *enableSQS {
logger := logger.With("service", "dynamodb")
s := sqs.New(sqs.Options{
Logger: logger,
ArnGenerator: arnGenerator,
})
s.RegisterHTTPHandlers(logger, methodRegistry)
logger.Info("Enabled SQS")
}

handlerChain := []server.HandlerFunc{server.HandlerFuncFromRegistry(logger, methodRegistry)}

if *enableS3 {
Expand Down
2 changes: 1 addition & 1 deletion services/kms/itest/kms_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package kms
package itest

import (
"context"
Expand Down
17 changes: 17 additions & 0 deletions services/sqs/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package sqs

import "aws-in-a-box/awserrors"

func XXXTodoException(message string) *awserrors.Error {
return &awserrors.Error{
Code: 400,
Body: awserrors.ErrorBody{
Type: "XXXTodoException",
Message: message,
},
}
}

func QueueNameExists(message string) *awserrors.Error {
return awserrors.Generate400Exception("QueueNameExists", message)
}
13 changes: 13 additions & 0 deletions services/sqs/http.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package sqs

import (
"golang.org/x/exp/slog"

"aws-in-a-box/http"
)

const service = "TODO"

func (s *SQS) RegisterHTTPHandlers(logger *slog.Logger, methodRegistry http.Registry) {
http.Register(logger, methodRegistry, service, "CreateQueue", s.CreateQueue)
}
72 changes: 72 additions & 0 deletions services/sqs/sqs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package sqs

import (
"sync"

"golang.org/x/exp/maps"
"golang.org/x/exp/slog"

"aws-in-a-box/arn"
"aws-in-a-box/awserrors"
)

type Queue struct {
// Immutable
CreationTimestamp int64
Attributes map[string]string
URL string

// Mutable
Messages []byte
Tags map[string]string
}

type SQS struct {
logger *slog.Logger
arnGenerator arn.Generator

mu sync.Mutex
queues map[string]Queue
tags map[string]string
}

type Options struct {
Logger *slog.Logger
ArnGenerator arn.Generator
}

func New(options Options) *SQS {
if options.Logger == nil {
options.Logger = slog.Default()
}

s := &SQS{
logger: options.Logger,
arnGenerator: options.ArnGenerator,
}

return s
}

// https://docs.aws.amazon.com/AWSSimpleQueueService/latest/APIReference/API_CreateQueue.html
func (s *SQS) CreateQueue(input CreateQueueInput) (*CreateQueueOutput, *awserrors.Error) {
s.mu.Lock()
defer s.mu.Unlock()

if queue, ok := s.queues[input.QueueName]; ok {
if maps.Equal(queue.Attributes, input.Attributes) {
return &CreateQueueOutput{
QueueUrl: queue.URL,
}, nil
}
return nil, QueueNameExists("")
}

s.queues[input.QueueName] = Queue{
Attributes: input.Attributes,
Tags: input.Tags,
URL: "TODO",
}

return nil, nil
}
11 changes: 11 additions & 0 deletions services/sqs/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package sqs

type CreateQueueInput struct {
Attributes map[string]string
QueueName string
Tags map[string]string `json:"tags"`
}

type CreateQueueOutput struct {
QueueUrl string
}

0 comments on commit 8c1aa7f

Please sign in to comment.