Skip to content

Commit

Permalink
Merge pull request #16 from brienze1/develop
Browse files Browse the repository at this point in the history
  • Loading branch information
github-actions[bot] committed Nov 27, 2022
2 parents f9a887b + 420fe80 commit 16fd4a1
Show file tree
Hide file tree
Showing 7 changed files with 122 additions and 153 deletions.
22 changes: 14 additions & 8 deletions build/local/docker-compose-local.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ version: "3"

services:
localstack:
container_name: validator_local_localstack
image: localstack/localstack
ports:
- '4566-4597:4566-4597'
Expand All @@ -13,34 +14,39 @@ services:
- ./scripts/localstack:/docker-entrypoint-initaws.d
- /var/run/docker.sock:/var/run/docker.sock
redis:
container_name: validator_local_redis
image: redis
command: redis-server --requirepass password123
hostname: redis
ports:
- "6379:6379"
crypto-robot-validator:
container_name: validator-lambda
build:
context: ../../../crypto-robot-validator
dockerfile: Dockerfile
volumes:
- ./lambda-files:/lambda-files
entrypoint: sh -c "cp crypto-robot-validator.zip /lambda-files"
container_name: validator_local-lambda
build:
context: ../../../crypto-robot-validator
dockerfile: Dockerfile
volumes:
- ./lambda-files:/lambda-files
entrypoint: >
sh -c "zip -r crypto-robot-validator.zip config validator &&
cp crypto-robot-validator.zip /lambda-files"
biscoint-mock:
container_name: biscoint-mock
container_name: validator_local_biscoint-mock
image: lfbrienze/biscoint-mock:latest
ports:
- "8080:8080"
entrypoint: >
sh -c "java -jar application/target/biscoint-mock.jar"
awscli:
container_name: validator_local_awscli
depends_on:
- crypto-robot-validator
image: amazon/aws-cli:latest
volumes:
- ./lambda-files:/lambda-files
- ./scripts/localstack:/init-scripts/localstack
- ./scripts/lambda:/init-scripts/lambda
- ./cloudformation:/cloudformation
entrypoint: >
sh -c "sh /init-scripts/localstack/01-profile-creation.sh &&
sh /init-scripts/lambda/01-wait-for-localstack.sh &&
Expand Down
18 changes: 9 additions & 9 deletions build/local/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ services:
ports:
- "6379:6379"
crypto-robot-validator:
container_name: validator-lambda
build:
context: ../../../crypto-robot-validator
dockerfile: Dockerfile
volumes:
- ./lambda-files:/lambda-files
entrypoint: >
sh -c "zip -r crypto-robot-validator.zip config validator &&
cp crypto-robot-validator.zip /lambda-files"
container_name: validator-lambda
build:
context: ../../../crypto-robot-validator
dockerfile: Dockerfile
volumes:
- ./lambda-files:/lambda-files
entrypoint: >
sh -c "zip -r crypto-robot-validator.zip config validator &&
cp crypto-robot-validator.zip /lambda-files"
biscoint-mock:
container_name: validator_biscoint-mock
image: lfbrienze/biscoint-mock:latest
Expand Down
167 changes: 91 additions & 76 deletions build/local/scripts/lambda/02-resources-creation-local.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,6 @@

echo "-----------------Script-02----------------- [validator]"

echo "########### Creating DLQ SQS ###########"
aws sqs create-queue --queue-name cryptoValidatorQueueDLQ --endpoint-url http://localstack:4566

echo "########### Creating SQS ###########"
aws sqs create-queue \
--queue-name cryptoValidatorQueue \
--attributes '{
"RedrivePolicy": "{\"deadLetterTargetArn\":\"arn:aws:sqs:sa-east-1:000000000000:cryptoValidatorQueueDLQ\",\"maxReceiveCount\":\"3\"}",
"MessageRetentionPeriod": "259200",
"VisibilityTimeout": "90"
}' \
--endpoint-url http://localstack:4566

echo "########### Listing SQS ###########"
aws sqs list-queues --endpoint-url http://localstack:4566

echo "########### Subscribing SQS to SNS ###########"
aws sns subscribe \
--topic-arn arn:aws:sns:sa-east-1:000000000000:cryptoOperationTriggerTopic \
--protocol sqs \
--notification-endpoint "http://localhost:4566/000000000000/cryptoValidatorQueue" \
--endpoint-url http://localstack:4566

echo "########### Listing SNS Subscriptions ###########"
aws sns list-subscriptions --endpoint-url http://localstack:4566

echo "########### Creating SNS ###########"
aws sns create-topic --name cryptoOperationExecutorTopic --endpoint-url http://localstack:4566

echo "########### Listing SNS ###########"
aws sns list-topics --endpoint-url http://localstack:4566

echo "########### Create secrets manager for cache values ###########"
aws secretsmanager create-secret --name crypto_robot.secrets.cache --secret-string '{"redis_address":"localhost:6379","redis_password":"password123","redis_user":"default"}' --endpoint-url http://localstack:4566

Expand All @@ -44,47 +12,94 @@ aws secretsmanager create-secret --name crypto_robot.secrets.encryption --secret
echo "########### Copy the lambda function to the S3 bucket ###########"
aws s3 cp /lambda-files/crypto-robot-validator.zip s3://lambda-functions --endpoint-url http://localstack:4566

echo "########### Create the lambda validatorLambda ###########"
aws lambda create-function \
--endpoint-url http://localstack:4566 \
--function-name validatorLambda \
--role arn:aws:iam::000000000000:role/admin-role \
--code S3Bucket=lambda-functions,S3Key=crypto-robot-validator.zip \
--handler ./validator \
--runtime go1.x \
--description "SQS Lambda handler for crypto-robot-validator." \
--timeout 60 \
--memory-size 128 \
--environment "Variables={VALIDATOR_ENV=localstack}"

echo "########### Map the cryptoValidatorQueue to the validatorLambda lambda function ###########"
aws lambda create-event-source-mapping \
--function-name validatorLambda \
--batch-size 1 \
--event-source-arn "arn:aws:sqs:sa-east-1:000000000000:cryptoValidatorQueue" \
--endpoint-url http://localstack:4566

# Only need this for local testing
echo "########### Creating DynamoDB 'crypto_robot.clients' table ###########"
aws dynamodb create-table \
--table-name crypto_robot.clients \
--attribute-definitions AttributeName=client_id,AttributeType=S \
--key-schema AttributeName=client_id,KeyType=HASH \
--provisioned-throughput ReadCapacityUnits=5,WriteCapacityUnits=5 \
--endpoint-url=http://localstack:4566

echo "########### Creating DynamoDB 'crypto_robot.operations' table ###########"
aws dynamodb create-table \
--table-name crypto_robot.operations \
--attribute-definitions AttributeName=operation_id,AttributeType=S \
--key-schema AttributeName=operation_id,KeyType=HASH \
--provisioned-throughput ReadCapacityUnits=5,WriteCapacityUnits=5 \
--endpoint-url=http://localstack:4566

echo "########### Creating DynamoDB 'crypto_robot.credentials' table ###########"
aws dynamodb create-table \
--table-name crypto_robot.credentials \
--attribute-definitions AttributeName=client_id,AttributeType=S \
--key-schema AttributeName=client_id,KeyType=HASH \
--provisioned-throughput ReadCapacityUnits=5,WriteCapacityUnits=5 \
--endpoint-url=http://localstack:4566
echo "########### Cloudformation Start ###########"
aws cloudformation deploy \
--stack-name crypto-robot-validator \
--template-file "/cloudformation/crypto-robot-validator.yaml" \
--endpoint-url http://localstack:4566

#echo "########### Creating DLQ SQS ###########"
#aws sqs create-queue --queue-name cryptoValidatorQueueDLQ --endpoint-url http://localstack:4566
#
#echo "########### Creating SQS ###########"
#aws sqs create-queue \
#--queue-name cryptoValidatorQueue \
#--attributes '{
# "RedrivePolicy": "{\"deadLetterTargetArn\":\"arn:aws:sqs:sa-east-1:000000000000:cryptoValidatorQueueDLQ\",\"maxReceiveCount\":\"3\"}",
# "MessageRetentionPeriod": "259200",
# "VisibilityTimeout": "90"
#}' \
#--endpoint-url http://localstack:4566
#
#echo "########### Listing SQS ###########"
#aws sqs list-queues --endpoint-url http://localstack:4566
#
#echo "########### Subscribing SQS to SNS ###########"
#aws sns subscribe \
#--topic-arn arn:aws:sns:sa-east-1:000000000000:cryptoOperationTriggerTopic \
#--protocol sqs \
#--notification-endpoint "http://localhost:4566/000000000000/cryptoValidatorQueue" \
#--endpoint-url http://localstack:4566
#
#echo "########### Listing SNS Subscriptions ###########"
#aws sns list-subscriptions --endpoint-url http://localstack:4566
#
#echo "########### Creating SNS ###########"
#aws sns create-topic --name cryptoOperationExecutorTopic --endpoint-url http://localstack:4566
#
#echo "########### Listing SNS ###########"
#aws sns list-topics --endpoint-url http://localstack:4566
#
#echo "########### Create secrets manager for cache values ###########"
#aws secretsmanager create-secret --name crypto_robot.secrets.cache --secret-string '{"redis_address":"localhost:6379","redis_password":"password123","redis_user":"default"}' --endpoint-url http://localstack:4566
#
#echo "########### Create secrets manager for encryption values ###########"
#aws secretsmanager create-secret --name crypto_robot.secrets.encryption --secret-string '{"encryption_key":"9y$B?E(H+MbQeThWmZq4t7w!z%C*F)J@"}' --endpoint-url http://localstack:4566
#
#echo "########### Copy the lambda function to the S3 bucket ###########"
#aws s3 cp /lambda-files/crypto-robot-validator.zip s3://lambda-functions --endpoint-url http://localstack:4566
#
#echo "########### Create the lambda validatorLambda ###########"
#aws lambda create-function \
# --endpoint-url http://localstack:4566 \
# --function-name validatorLambda \
# --role arn:aws:iam::000000000000:role/admin-role \
# --code S3Bucket=lambda-functions,S3Key=crypto-robot-validator.zip \
# --handler ./validator \
# --runtime go1.x \
# --description "SQS Lambda handler for crypto-robot-validator." \
# --timeout 60 \
# --memory-size 128 \
# --environment "Variables={VALIDATOR_ENV=localstack}"
#
#echo "########### Map the cryptoValidatorQueue to the validatorLambda lambda function ###########"
#aws lambda create-event-source-mapping \
# --function-name validatorLambda \
# --batch-size 1 \
# --event-source-arn "arn:aws:sqs:sa-east-1:000000000000:cryptoValidatorQueue" \
# --endpoint-url http://localstack:4566
#
## Only need this for local testing
#echo "########### Creating DynamoDB 'crypto_robot.clients' table ###########"
#aws dynamodb create-table \
#--table-name crypto_robot.clients \
#--attribute-definitions AttributeName=client_id,AttributeType=S \
#--key-schema AttributeName=client_id,KeyType=HASH \
#--provisioned-throughput ReadCapacityUnits=5,WriteCapacityUnits=5 \
#--endpoint-url=http://localstack:4566
#
#echo "########### Creating DynamoDB 'crypto_robot.operations' table ###########"
#aws dynamodb create-table \
#--table-name crypto_robot.operations \
#--attribute-definitions AttributeName=operation_id,AttributeType=S \
#--key-schema AttributeName=operation_id,KeyType=HASH \
#--provisioned-throughput ReadCapacityUnits=5,WriteCapacityUnits=5 \
#--endpoint-url=http://localstack:4566
#
#echo "########### Creating DynamoDB 'crypto_robot.credentials' table ###########"
#aws dynamodb create-table \
#--table-name crypto_robot.credentials \
#--attribute-definitions AttributeName=client_id,AttributeType=S \
#--key-schema AttributeName=client_id,KeyType=HASH \
#--provisioned-throughput ReadCapacityUnits=5,WriteCapacityUnits=5 \
#--endpoint-url=http://localstack:4566
11 changes: 1 addition & 10 deletions cmd/local/main_local.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package main

import (
"context"
"encoding/json"
"github.com/aws/aws-lambda-go/events"
"github.com/aws/aws-lambda-go/lambdacontext"
"github.com/brienze1/crypto-robot-validator/internal/validator"
Expand Down Expand Up @@ -44,19 +43,11 @@ func createSQSEvent() events.SQSEvent {
"start_time": "2022-09-17T12:05:07.45066-03:00"
}`

snsEventMessage, _ := json.Marshal(createSNSEvent(operationMessage))

return events.SQSEvent{
Records: []events.SQSMessage{
{
Body: string(snsEventMessage),
Body: operationMessage,
},
},
}
}

func createSNSEvent(message string) events.SNSEntity {
return events.SNSEntity{
Message: message,
}
}
15 changes: 5 additions & 10 deletions internal/validator/delivery/handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,26 +29,21 @@ func (h *handler) Handle(context context.Context, event events.SQSEvent) error {
h.logger.SetCorrelationID(ctx.AwsRequestID)
h.logger.Info("Event received", event, ctx)

snsMessage := &events.SNSEntity{}
if err := json.Unmarshal([]byte(event.Records[0].Body), snsMessage); err != nil {
return h.abort(err, "Error while trying to parse the SNS message")
}

operationRequestDto := &dto.OperationRequest{}
if err := json.Unmarshal([]byte(snsMessage.Message), operationRequestDto); err != nil {
return h.abort(err, "Error while trying to parse the operation request object")
if err := json.Unmarshal([]byte(event.Records[0].Body), operationRequestDto); err != nil {
return h.abort(err, "Error while trying to parse the SNS message", event)
}

if err := h.validationUseCase.Validate(operationRequestDto.ToModel()); err != nil {
return h.abort(err, "Error while trying to run ValidationUseCase")
return h.abort(err, "Error while trying to run ValidationUseCase", operationRequestDto)
}

h.logger.Info("Event succeeded", event, ctx)
return nil
}

func (h *handler) abort(err error, message string) custom_error.BaseErrorAdapter {
func (h *handler) abort(err error, message string, metadata ...interface{}) custom_error.BaseErrorAdapter {
handlerError := exceptions.HandlerError(err, message)
h.logger.Error(handlerError, "Event failed: "+message)
h.logger.Error(handlerError, "Event failed: "+message, metadata)
return handlerError
}
10 changes: 1 addition & 9 deletions test/integrated/step_definitions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,23 +212,15 @@ func processShouldExitWith(status int) error {
}

func createSQSEvent(message string) events.SQSEvent {
snsEventMessage, _ := json.Marshal(createSNSEvent(message))

return events.SQSEvent{
Records: []events.SQSMessage{
{
Body: string(snsEventMessage),
Body: message,
},
},
}
}

func createSNSEvent(message string) events.SNSEntity {
return events.SNSEntity{
Message: message,
}
}

type ctx struct {
context.Context
awsRequestId string
Expand Down
Loading

0 comments on commit 16fd4a1

Please sign in to comment.