diff --git a/goal.tf b/goal.tf new file mode 100644 index 0000000..a0f1105 --- /dev/null +++ b/goal.tf @@ -0,0 +1,11 @@ +resource "aws_api_gateway_resource" "goals" { + rest_api_id = aws_api_gateway_rest_api.api.id + parent_id = aws_api_gateway_resource.xeffect.id + path_part = "goal" +} + +resource "aws_api_gateway_resource" "goal" { + rest_api_id = aws_api_gateway_rest_api.api.id + parent_id = aws_api_gateway_resource.goals.id + path_part = "{goalId}" +} diff --git a/goal_create.tf b/goal_create.tf index 3774537..e7636a2 100644 --- a/goal_create.tf +++ b/goal_create.tf @@ -15,16 +15,9 @@ resource "aws_lambda_function" "goal_create" { role = aws_iam_role.iam_for_lambda.arn } -resource "aws_api_gateway_resource" "goal" { - path_part = "goal" - parent_id = aws_api_gateway_resource.xeffect.id - - rest_api_id = aws_api_gateway_rest_api.api.id -} - resource "aws_api_gateway_method" "goal_create" { rest_api_id = aws_api_gateway_rest_api.api.id - resource_id = aws_api_gateway_resource.goal.id + resource_id = aws_api_gateway_resource.goals.id http_method = "POST" authorization = "NONE" @@ -32,7 +25,7 @@ resource "aws_api_gateway_method" "goal_create" { resource "aws_api_gateway_integration" "goal_create" { rest_api_id = aws_api_gateway_rest_api.api.id - resource_id = aws_api_gateway_resource.goal.id + resource_id = aws_api_gateway_resource.goals.id http_method = aws_api_gateway_method.goal_create.http_method integration_http_method = "POST" diff --git a/goal_get.tf b/goal_get.tf new file mode 100644 index 0000000..bd4705b --- /dev/null +++ b/goal_get.tf @@ -0,0 +1,38 @@ +data "archive_file" "goal_get" { + type = "zip" + source_file = "goal_get/goal_get" + output_path = "goal_get/${var.lambda_zip_file}" +} + +resource "aws_lambda_function" "goal_get" { + function_name = "goal_get" + filename = data.archive_file.goal_get.output_path + handler = "goal_get" + source_code_hash = data.archive_file.goal_get.output_base64sha256 + runtime = "go1.x" + memory_size = 128 + timeout = 10 + role = aws_iam_role.iam_for_lambda.arn +} + +resource "aws_api_gateway_method" "goal_get" { + rest_api_id = aws_api_gateway_rest_api.api.id + resource_id = aws_api_gateway_resource.goal.id + + http_method = "GET" + authorization = "NONE" + + request_parameters = { + "method.request.path.goalId" = true + } +} + +resource "aws_api_gateway_integration" "goal_get" { + rest_api_id = aws_api_gateway_rest_api.api.id + resource_id = aws_api_gateway_resource.goal.id + + http_method = aws_api_gateway_method.goal_get.http_method + integration_http_method = "POST" + type = "AWS_PROXY" + uri = aws_lambda_function.goal_get.invoke_arn +} diff --git a/goal_get/go.mod b/goal_get/go.mod new file mode 100644 index 0000000..5dad4d6 --- /dev/null +++ b/goal_get/go.mod @@ -0,0 +1,24 @@ +module github.com/maxstanley/xeffect_backend/goal_get + +go 1.17 + +require ( + github.com/aws/aws-lambda-go v1.27.1 // indirect + github.com/aws/aws-sdk-go-v2 v1.11.2 // indirect + github.com/aws/aws-sdk-go-v2/config v1.11.1 // indirect + github.com/aws/aws-sdk-go-v2/credentials v1.6.5 // indirect + github.com/aws/aws-sdk-go-v2/feature/dynamodb/attributevalue v1.4.5 // indirect + github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.8.2 // indirect + github.com/aws/aws-sdk-go-v2/internal/configsources v1.1.2 // indirect + github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.0.2 // indirect + github.com/aws/aws-sdk-go-v2/internal/ini v1.3.2 // indirect + github.com/aws/aws-sdk-go-v2/service/dynamodb v1.11.0 // indirect + github.com/aws/aws-sdk-go-v2/service/dynamodbstreams v1.9.0 // indirect + github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.5.0 // indirect + github.com/aws/aws-sdk-go-v2/service/internal/endpoint-discovery v1.3.3 // indirect + github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.5.2 // indirect + github.com/aws/aws-sdk-go-v2/service/sso v1.7.0 // indirect + github.com/aws/aws-sdk-go-v2/service/sts v1.12.0 // indirect + github.com/aws/smithy-go v1.9.0 // indirect + github.com/jmespath/go-jmespath v0.4.0 // indirect +) diff --git a/goal_get/go.sum b/goal_get/go.sum new file mode 100644 index 0000000..14a2e6b --- /dev/null +++ b/goal_get/go.sum @@ -0,0 +1,56 @@ +github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= +github.com/aws/aws-lambda-go v1.27.1 h1:MAH6hbrsktcSr/gGQKLvHeJPeoOoaspJqh+O4g05bpA= +github.com/aws/aws-lambda-go v1.27.1/go.mod h1:jJmlefzPfGnckuHdXX7/80O3BvUUi12XOkbv4w9SGLU= +github.com/aws/aws-sdk-go-v2 v1.11.2 h1:SDiCYqxdIYi6HgQfAWRhgdZrdnOuGyLDJVRSWLeHWvs= +github.com/aws/aws-sdk-go-v2 v1.11.2/go.mod h1:SQfA+m2ltnu1cA0soUkj4dRSsmITiVQUJvBIZjzfPyQ= +github.com/aws/aws-sdk-go-v2/config v1.11.1 h1:KXSjb7ZMLRtjxClFptukTYibiOqJS9NwBO+9WD3UMto= +github.com/aws/aws-sdk-go-v2/config v1.11.1/go.mod h1:VvfkzUhVtntSg1JfGFMSKS0CyiTZd3NqBxK5af4zsME= +github.com/aws/aws-sdk-go-v2/credentials v1.6.5 h1:ZrsO2js2v4T95rsCIWoAb/ck5+U1kwkizGdZHY+ni3s= +github.com/aws/aws-sdk-go-v2/credentials v1.6.5/go.mod h1:HWSOnsnqVMbLcWUmom6AN1cqhcLzLJ62AObW28CbYbU= +github.com/aws/aws-sdk-go-v2/feature/dynamodb/attributevalue v1.4.5 h1:OndYhAu3k1jCFAdsgTwCZjw4ZRFFmgJloLn56gq8qgU= +github.com/aws/aws-sdk-go-v2/feature/dynamodb/attributevalue v1.4.5/go.mod h1:uHm3HmFxnQ+D4uwKElkjxw2aHVg6+zpTDvkOP1TzZTo= +github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.8.2 h1:KiN5TPOLrEjbGCvdTQR4t0U4T87vVwALZ5Bg3jpMqPY= +github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.8.2/go.mod h1:dF2F6tXEOgmW5X1ZFO/EPtWrcm7XkW07KNcJUGNtt4s= +github.com/aws/aws-sdk-go-v2/internal/configsources v1.1.2 h1:XJLnluKuUxQG255zPNe+04izXl7GSyUVafIsgfv9aw4= +github.com/aws/aws-sdk-go-v2/internal/configsources v1.1.2/go.mod h1:SgKKNBIoDC/E1ZCDhhMW3yalWjwuLjMcpLzsM/QQnWo= +github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.0.2 h1:EauRoYZVNPlidZSZJDscjJBQ22JhVF2+tdteatax2Ak= +github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.0.2/go.mod h1:xT4XX6w5Sa3dhg50JrYyy3e4WPYo/+WjY/BXtqXVunU= +github.com/aws/aws-sdk-go-v2/internal/ini v1.3.2 h1:IQup8Q6lorXeiA/rK72PeToWoWK8h7VAPgHNWdSrtgE= +github.com/aws/aws-sdk-go-v2/internal/ini v1.3.2/go.mod h1:VITe/MdW6EMXPb0o0txu/fsonXbMHUU2OC2Qp7ivU4o= +github.com/aws/aws-sdk-go-v2/service/dynamodb v1.11.0 h1:te+nIFwPf5Bi/cZvd9g/+EF0gkJT3c0J/5+NMx0NBZg= +github.com/aws/aws-sdk-go-v2/service/dynamodb v1.11.0/go.mod h1:ELltfl9ri0n4sZ/VjPZBgemNMd9mYIpCAuZhc7NP7l4= +github.com/aws/aws-sdk-go-v2/service/dynamodbstreams v1.9.0 h1:b5Rb7tW92sRCGLMTUmhY6VPFZpDfE1vrrGtZm8+/1T0= +github.com/aws/aws-sdk-go-v2/service/dynamodbstreams v1.9.0/go.mod h1:RiesWyLiePOOwyT5ySDupQosvbG+OTMv9pws/EhDu4U= +github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.5.0 h1:lPLbw4Gn59uoKqvOfSnkJr54XWk5Ak1NK20ZEiSWb3U= +github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.5.0/go.mod h1:80NaCIH9YU3rzTTs/J/ECATjXuRqzo/wB6ukO6MZ0XY= +github.com/aws/aws-sdk-go-v2/service/internal/endpoint-discovery v1.3.3 h1:ru9+IpkVIuDvIkm9Q0DEjtWHnh6ITDoZo8fH2dIjlqQ= +github.com/aws/aws-sdk-go-v2/service/internal/endpoint-discovery v1.3.3/go.mod h1:zOyLMYyg60yyZpOCniAUuibWVqTU4TuLmMa/Wh4P+HA= +github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.5.2 h1:CKdUNKmuilw/KNmO2Q53Av8u+ZyXMC2M9aX8Z+c/gzg= +github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.5.2/go.mod h1:FgR1tCsn8C6+Hf+N5qkfrE4IXvUL1RgW87sunJ+5J4I= +github.com/aws/aws-sdk-go-v2/service/sso v1.7.0 h1:E4fxAg/UE8a6yiLZYv8/EP0uXKPPRImiMau4ift6S/g= +github.com/aws/aws-sdk-go-v2/service/sso v1.7.0/go.mod h1:KnIpszaIdwI33tmc/W/GGXyn22c1USYxA/2KyvoeDY0= +github.com/aws/aws-sdk-go-v2/service/sts v1.12.0 h1:7g0252k2TF3eA1DtfkTQB/tqI41YvbUPaolwTR0/ITc= +github.com/aws/aws-sdk-go-v2/service/sts v1.12.0/go.mod h1:UV2N5HaPfdbDpkgkz4sRzWCvQswZjdO1FfqCWl0t7RA= +github.com/aws/smithy-go v1.9.0 h1:c7FUdEqrQA1/UVKKCNDFQPNKGp4FQg3YW4Ck5SLTG58= +github.com/aws/smithy-go v1.9.0/go.mod h1:SObp3lf9smib00L/v3U2eAKG8FyQ7iLrJnQiAmR5n+E= +github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU= +github.com/cpuguy83/go-md2man/v2 v2.0.0/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9YPoQUg= +github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo= +github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfCI6z80xFu9LTZmf1ZRjMHUOPmWr69U= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= +github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/urfave/cli/v2 v2.2.0/go.mod h1:SE9GqnLQmjVa0iPEY0f1w3ygNIYcIJ0OKPMoW2caLfQ= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/goal_get/main.go b/goal_get/main.go new file mode 100644 index 0000000..c7148fe --- /dev/null +++ b/goal_get/main.go @@ -0,0 +1,81 @@ +package main + +import ( + "context" + "encoding/json" + + "github.com/aws/aws-lambda-go/events" + "github.com/aws/aws-lambda-go/lambda" + "github.com/aws/aws-sdk-go-v2/config" + "github.com/aws/aws-sdk-go-v2/feature/dynamodb/attributevalue" + "github.com/aws/aws-sdk-go-v2/service/dynamodb" + "github.com/aws/aws-sdk-go-v2/service/dynamodb/types" +) + +type Request events.APIGatewayProxyRequest +type Response events.APIGatewayProxyResponse + +var GOAL_TABLE = "xeffect_goals" + +type Goal struct { + Title string `json:"title" validate:"required"` + Motivation string `json:"motivation" validate:"required"` +} + +func returnError(err error) (Response, error) { + return Response{ + StatusCode: 400, + IsBase64Encoded: false, + Headers: map[string]string{ + "Content-Type": "text/plain", + }, + Body: err.Error(), + }, nil +} + +func handleGoalGetEvent(ctx context.Context, event Request) (Response, error) { + goalId := event.PathParameters["goalId"] + + cfg, err := config.LoadDefaultConfig(ctx, config.WithRegion("eu-west-2")) + if err != nil { + return returnError(err) + } + + input := &dynamodb.GetItemInput{ + TableName: &GOAL_TABLE, + Key: map[string]types.AttributeValue{ + "uuid": &types.AttributeValueMemberS{ + Value: goalId, + }, + }, + } + + client := dynamodb.NewFromConfig(cfg) + result, err := client.GetItem(ctx, input) + if err != nil { + return returnError(err) + } + + var goal Goal + if err := attributevalue.UnmarshalMap(result.Item, &goal); err != nil { + return returnError(err) + } + + body, err := json.Marshal(goal) + if err != nil { + return returnError(err) + } + + return Response{ + StatusCode: 200, + IsBase64Encoded: false, + Headers: map[string]string{ + "Content-Type": "application/json", + }, + Body: string(body), + }, nil +} + +func main() { + lambda.Start(handleGoalGetEvent) +} diff --git a/goal_get_all.tf b/goal_get_all.tf new file mode 100644 index 0000000..3d05590 --- /dev/null +++ b/goal_get_all.tf @@ -0,0 +1,34 @@ +data "archive_file" "goal_get_all" { + type = "zip" + source_file = "goal_get_all/goal_get_all" + output_path = "goal_get_all/${var.lambda_zip_file}" +} + +resource "aws_lambda_function" "goal_get_all" { + function_name = "goal_get_all" + filename = data.archive_file.goal_get_all.output_path + handler = "goal_get_all" + source_code_hash = data.archive_file.goal_get_all.output_base64sha256 + runtime = "go1.x" + memory_size = 128 + timeout = 10 + role = aws_iam_role.iam_for_lambda.arn +} + +resource "aws_api_gateway_method" "goal_get_all" { + rest_api_id = aws_api_gateway_rest_api.api.id + resource_id = aws_api_gateway_resource.goals.id + + http_method = "GET" + authorization = "NONE" +} + +resource "aws_api_gateway_integration" "goal_get_all" { + rest_api_id = aws_api_gateway_rest_api.api.id + resource_id = aws_api_gateway_resource.goals.id + + http_method = aws_api_gateway_method.goal_get_all.http_method + integration_http_method = "POST" + type = "AWS_PROXY" + uri = aws_lambda_function.goal_get_all.invoke_arn +} diff --git a/goal_get_all/go.mod b/goal_get_all/go.mod new file mode 100644 index 0000000..606ef4a --- /dev/null +++ b/goal_get_all/go.mod @@ -0,0 +1,24 @@ +module github.com/maxstanley/xeffect_backend/goal_get_all + +go 1.17 + +require ( + github.com/aws/aws-lambda-go v1.27.1 // indirect + github.com/aws/aws-sdk-go-v2 v1.11.2 // indirect + github.com/aws/aws-sdk-go-v2/config v1.11.1 // indirect + github.com/aws/aws-sdk-go-v2/credentials v1.6.5 // indirect + github.com/aws/aws-sdk-go-v2/feature/dynamodb/attributevalue v1.4.5 // indirect + github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.8.2 // indirect + github.com/aws/aws-sdk-go-v2/internal/configsources v1.1.2 // indirect + github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.0.2 // indirect + github.com/aws/aws-sdk-go-v2/internal/ini v1.3.2 // indirect + github.com/aws/aws-sdk-go-v2/service/dynamodb v1.11.0 // indirect + github.com/aws/aws-sdk-go-v2/service/dynamodbstreams v1.9.0 // indirect + github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.5.0 // indirect + github.com/aws/aws-sdk-go-v2/service/internal/endpoint-discovery v1.3.3 // indirect + github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.5.2 // indirect + github.com/aws/aws-sdk-go-v2/service/sso v1.7.0 // indirect + github.com/aws/aws-sdk-go-v2/service/sts v1.12.0 // indirect + github.com/aws/smithy-go v1.9.0 // indirect + github.com/jmespath/go-jmespath v0.4.0 // indirect +) diff --git a/goal_get_all/go.sum b/goal_get_all/go.sum new file mode 100644 index 0000000..14a2e6b --- /dev/null +++ b/goal_get_all/go.sum @@ -0,0 +1,56 @@ +github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= +github.com/aws/aws-lambda-go v1.27.1 h1:MAH6hbrsktcSr/gGQKLvHeJPeoOoaspJqh+O4g05bpA= +github.com/aws/aws-lambda-go v1.27.1/go.mod h1:jJmlefzPfGnckuHdXX7/80O3BvUUi12XOkbv4w9SGLU= +github.com/aws/aws-sdk-go-v2 v1.11.2 h1:SDiCYqxdIYi6HgQfAWRhgdZrdnOuGyLDJVRSWLeHWvs= +github.com/aws/aws-sdk-go-v2 v1.11.2/go.mod h1:SQfA+m2ltnu1cA0soUkj4dRSsmITiVQUJvBIZjzfPyQ= +github.com/aws/aws-sdk-go-v2/config v1.11.1 h1:KXSjb7ZMLRtjxClFptukTYibiOqJS9NwBO+9WD3UMto= +github.com/aws/aws-sdk-go-v2/config v1.11.1/go.mod h1:VvfkzUhVtntSg1JfGFMSKS0CyiTZd3NqBxK5af4zsME= +github.com/aws/aws-sdk-go-v2/credentials v1.6.5 h1:ZrsO2js2v4T95rsCIWoAb/ck5+U1kwkizGdZHY+ni3s= +github.com/aws/aws-sdk-go-v2/credentials v1.6.5/go.mod h1:HWSOnsnqVMbLcWUmom6AN1cqhcLzLJ62AObW28CbYbU= +github.com/aws/aws-sdk-go-v2/feature/dynamodb/attributevalue v1.4.5 h1:OndYhAu3k1jCFAdsgTwCZjw4ZRFFmgJloLn56gq8qgU= +github.com/aws/aws-sdk-go-v2/feature/dynamodb/attributevalue v1.4.5/go.mod h1:uHm3HmFxnQ+D4uwKElkjxw2aHVg6+zpTDvkOP1TzZTo= +github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.8.2 h1:KiN5TPOLrEjbGCvdTQR4t0U4T87vVwALZ5Bg3jpMqPY= +github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.8.2/go.mod h1:dF2F6tXEOgmW5X1ZFO/EPtWrcm7XkW07KNcJUGNtt4s= +github.com/aws/aws-sdk-go-v2/internal/configsources v1.1.2 h1:XJLnluKuUxQG255zPNe+04izXl7GSyUVafIsgfv9aw4= +github.com/aws/aws-sdk-go-v2/internal/configsources v1.1.2/go.mod h1:SgKKNBIoDC/E1ZCDhhMW3yalWjwuLjMcpLzsM/QQnWo= +github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.0.2 h1:EauRoYZVNPlidZSZJDscjJBQ22JhVF2+tdteatax2Ak= +github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.0.2/go.mod h1:xT4XX6w5Sa3dhg50JrYyy3e4WPYo/+WjY/BXtqXVunU= +github.com/aws/aws-sdk-go-v2/internal/ini v1.3.2 h1:IQup8Q6lorXeiA/rK72PeToWoWK8h7VAPgHNWdSrtgE= +github.com/aws/aws-sdk-go-v2/internal/ini v1.3.2/go.mod h1:VITe/MdW6EMXPb0o0txu/fsonXbMHUU2OC2Qp7ivU4o= +github.com/aws/aws-sdk-go-v2/service/dynamodb v1.11.0 h1:te+nIFwPf5Bi/cZvd9g/+EF0gkJT3c0J/5+NMx0NBZg= +github.com/aws/aws-sdk-go-v2/service/dynamodb v1.11.0/go.mod h1:ELltfl9ri0n4sZ/VjPZBgemNMd9mYIpCAuZhc7NP7l4= +github.com/aws/aws-sdk-go-v2/service/dynamodbstreams v1.9.0 h1:b5Rb7tW92sRCGLMTUmhY6VPFZpDfE1vrrGtZm8+/1T0= +github.com/aws/aws-sdk-go-v2/service/dynamodbstreams v1.9.0/go.mod h1:RiesWyLiePOOwyT5ySDupQosvbG+OTMv9pws/EhDu4U= +github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.5.0 h1:lPLbw4Gn59uoKqvOfSnkJr54XWk5Ak1NK20ZEiSWb3U= +github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.5.0/go.mod h1:80NaCIH9YU3rzTTs/J/ECATjXuRqzo/wB6ukO6MZ0XY= +github.com/aws/aws-sdk-go-v2/service/internal/endpoint-discovery v1.3.3 h1:ru9+IpkVIuDvIkm9Q0DEjtWHnh6ITDoZo8fH2dIjlqQ= +github.com/aws/aws-sdk-go-v2/service/internal/endpoint-discovery v1.3.3/go.mod h1:zOyLMYyg60yyZpOCniAUuibWVqTU4TuLmMa/Wh4P+HA= +github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.5.2 h1:CKdUNKmuilw/KNmO2Q53Av8u+ZyXMC2M9aX8Z+c/gzg= +github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.5.2/go.mod h1:FgR1tCsn8C6+Hf+N5qkfrE4IXvUL1RgW87sunJ+5J4I= +github.com/aws/aws-sdk-go-v2/service/sso v1.7.0 h1:E4fxAg/UE8a6yiLZYv8/EP0uXKPPRImiMau4ift6S/g= +github.com/aws/aws-sdk-go-v2/service/sso v1.7.0/go.mod h1:KnIpszaIdwI33tmc/W/GGXyn22c1USYxA/2KyvoeDY0= +github.com/aws/aws-sdk-go-v2/service/sts v1.12.0 h1:7g0252k2TF3eA1DtfkTQB/tqI41YvbUPaolwTR0/ITc= +github.com/aws/aws-sdk-go-v2/service/sts v1.12.0/go.mod h1:UV2N5HaPfdbDpkgkz4sRzWCvQswZjdO1FfqCWl0t7RA= +github.com/aws/smithy-go v1.9.0 h1:c7FUdEqrQA1/UVKKCNDFQPNKGp4FQg3YW4Ck5SLTG58= +github.com/aws/smithy-go v1.9.0/go.mod h1:SObp3lf9smib00L/v3U2eAKG8FyQ7iLrJnQiAmR5n+E= +github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU= +github.com/cpuguy83/go-md2man/v2 v2.0.0/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9YPoQUg= +github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo= +github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfCI6z80xFu9LTZmf1ZRjMHUOPmWr69U= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= +github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/urfave/cli/v2 v2.2.0/go.mod h1:SE9GqnLQmjVa0iPEY0f1w3ygNIYcIJ0OKPMoW2caLfQ= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/goal_get_all/main.go b/goal_get_all/main.go new file mode 100644 index 0000000..76074b0 --- /dev/null +++ b/goal_get_all/main.go @@ -0,0 +1,74 @@ +package main + +import ( + "context" + "encoding/json" + + "github.com/aws/aws-lambda-go/events" + "github.com/aws/aws-lambda-go/lambda" + "github.com/aws/aws-sdk-go-v2/config" + "github.com/aws/aws-sdk-go-v2/feature/dynamodb/attributevalue" + "github.com/aws/aws-sdk-go-v2/service/dynamodb" +) + +type Request events.APIGatewayProxyRequest +type Response events.APIGatewayProxyResponse + +var GOAL_TABLE = "xeffect_goals" + +type Goal struct { + UUID string `json:"uuid"` + Title string `json:"title" validate:"required"` + Motivation string `json:"motivation" validate:"required"` +} + +func returnError(err error) (Response, error) { + return Response{ + StatusCode: 400, + IsBase64Encoded: false, + Headers: map[string]string{ + "Content-Type": "text/plain", + }, + Body: err.Error(), + }, nil +} + +func handleGoalGetAllEvent(ctx context.Context, event Request) (Response, error) { + cfg, err := config.LoadDefaultConfig(ctx, config.WithRegion("eu-west-2")) + if err != nil { + return returnError(err) + } + + input := &dynamodb.ScanInput{ + TableName: &GOAL_TABLE, + } + + client := dynamodb.NewFromConfig(cfg) + result, err := client.Scan(ctx, input) + if err != nil { + return returnError(err) + } + + goals := []Goal{} + if err := attributevalue.UnmarshalListOfMaps(result.Items, &goals); err != nil { + return returnError(err) + } + + body, err := json.Marshal(goals) + if err != nil { + return returnError(err) + } + + return Response{ + StatusCode: 200, + IsBase64Encoded: false, + Headers: map[string]string{ + "Content-Type": "application/json", + }, + Body: string(body), + }, nil +} + +func main() { + lambda.Start(handleGoalGetAllEvent) +} diff --git a/lambda.tf b/lambda.tf index bde817c..4f5f196 100644 --- a/lambda.tf +++ b/lambda.tf @@ -79,9 +79,15 @@ resource "aws_api_gateway_deployment" "v1" { triggers = { redeployment = sha1(jsonencode([ aws_api_gateway_integration.version, - aws_api_gateway_integration.goal_create + aws_api_gateway_integration.goal_create, + aws_api_gateway_integration.goal_get_all, + aws_api_gateway_integration.goal_get, ])) } + + lifecycle { + create_before_destroy = true + } } # API Stage. @@ -109,3 +115,21 @@ resource "aws_lambda_permission" "goal_create" { source_arn = "${aws_api_gateway_rest_api.api.execution_arn}/*/*/*" } + +resource "aws_lambda_permission" "goal_get_all" { + statement_id = "AllowExectionFromAPIGateway" + action = "lambda:InvokeFunction" + function_name = aws_lambda_function.goal_get_all.function_name + principal = "apigateway.amazonaws.com" + + source_arn = "${aws_api_gateway_rest_api.api.execution_arn}/*/*/*" +} + +resource "aws_lambda_permission" "goal_get" { + statement_id = "AllowExectionFromAPIGateway" + action = "lambda:InvokeFunction" + function_name = aws_lambda_function.goal_get.function_name + principal = "apigateway.amazonaws.com" + + source_arn = "${aws_api_gateway_rest_api.api.execution_arn}/*/*/*" +} diff --git a/version/main.go b/version/main.go index 0fb8fbd..ba774ba 100644 --- a/version/main.go +++ b/version/main.go @@ -17,7 +17,7 @@ func handleVersionEvent(ctx context.Context, event Request) (Response, error) { Headers: map[string]string{ "Content-Type": "text/plain", }, - Body: "0.1.0", + Body: "0.2.0", }, nil }