Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change dyanmo and get user #2

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion bundle_lambda_layer
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
!#/bin/bash
#!/bin/bash

cd ./src/lambda_layer/ruby

Expand Down
25 changes: 11 additions & 14 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -177,26 +177,24 @@ module "bpi_emitter" {
# project_init_queue = aws_sqs_queue.project_init_queue.id
# }

resource "aws_dynamodb_table" "projects_table" {
name = "FarmProjects"
billing_mode = "PAY_PER_REQUEST"
hash_key = "ProjectId"
resource "aws_dynamodb_table" "farm_table" {
name = "FarmTable"
billing_mode = "PROVISIONED"
write_capacity = 10
read_capacity = 10
hash_key = "hk"
range_key = "rk"

attribute {
name = "ProjectId"
name = "hk"
type = "S"
}
}

resource "aws_dynamodb_table" "application_settings" {
name = "FarmApplicationSettings"
billing_mode = "PAY_PER_REQUEST"
hash_key = "SettingName"

attribute {
name = "SettingName"
name = "rk"
type = "S"
}

}

module "api" {
Expand All @@ -209,8 +207,7 @@ module "api" {
client_endpoint = "https://${aws_s3_bucket.client_bucket.website_endpoint}"

dynamo_tables = {
projects = aws_dynamodb_table.projects_table.name,
application_settings = aws_dynamodb_table.application_settings.name
projects = aws_dynamodb_table.farm_table.name
}
}

31 changes: 13 additions & 18 deletions src/api/bake_tasks/create.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,26 @@ def lambda_handler(event:, context:)
db = Aws::DynamoDB::Client.new(region: ENV['REGION'])
bake_q = Aws::SQS::Queue.new(ENV["PROJECT_INIT_QUEUE"])
request_body = event["body"] ? JSON.parse(event["body"]) : nil
user_id = event.dig("requestContext", "authorizer", "claims", "sub")
project_id = event["pathParameters"]["project_id"]
bake_task_id = SecureRandom.uuid

puts event.inspect

new_task = {
"StartedAt" => Time.now.strftime('%Y-%m-%dT%H:%M:%S.%L%z'),
"BakeTaskId" => SecureRandom.uuid
bake_task = {
"hk" => bake_task_id,
"rk" => "BAKE_TASK",
"started_at" => Time.now.strftime('%Y-%m-%dT%H:%M:%S.%L%z'),
}

project_bake_task = {
"hk" => "project##{project_id}",
"rk" => "bake_task##{render_task_id}",
}

begin
res = db.update_item({
table_name: ENV['PROJECTS_TABLE'],
key: {
"ProjectId" => project_id
},
return_values: "ALL_NEW",
update_expression: "set #BakeTasks = list_append(if_not_exists(#BakeTasks, :EmptyList), :BakeTask)",
expression_attribute_names: {
"#BakeTasks" => "BakeTasks"
},
expression_attribute_values: {
":EmptyList" => [],
":BakeTask" => [new_task]
}
})
task = db.put_item(table_name: ENV['PROJECTS_TABLE'], item: bake_task)
project_task = db.put_item(table_name: ENV['PROJECTS_TABLE'], item: project_bake_task)

bake_q.send_message(
message_body: 'Body',
Expand Down
26 changes: 16 additions & 10 deletions src/api/projects/create.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,27 @@
require 'securerandom'
require 'shared'

include Shared

def lambda_handler(event:, context:)
# Get all projects for this user from dynamo
db = Aws::DynamoDB::Client.new(region: ENV['REGION'])
s3_client = Aws::S3::Client.new()
bucket = Aws::S3::Bucket.new(ENV["BUCKET"], client: s3_client)
user_id = event.dig("requestContext", "authorizer", "claims", "sub")
database = Aws::DynamoDB::Client.new(region: ENV['REGION'])

request_body = event["body"] ? JSON.parse(event["body"]) : nil
item = {
"ProjectName" => request_body["name"],
"ProjectId" => SecureRandom.uuid
new_project_id = SecureRandom.uuid
project_item = {
"hk" => new_project_id,
"rk" => "PROJECT",
"data" => request_body["name"]
}

user_project_item = {
"hk" => "user##{user_id}",
"rk" => "project##{new_project_id}"
}
begin
db.put_item(table_name: ENV['PROJECTS_TABLE'], item: item)
{ statusCode: 200, body: JSON.generate(item) }
database.put_item(table_name: ENV['PROJECTS_TABLE'], item: project_item)
database.put_item(table_name: ENV['PROJECTS_TABLE'], item: user_project_item)
{ statusCode: 200, body: JSON.generate(project_item) }
rescue Aws::DynamoDB::Errors::ServiceError => error
puts 'Unable to create project:'
puts error.message
Expand Down
16 changes: 14 additions & 2 deletions src/api/projects/destroy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,23 @@

def lambda_handler(event:, context:)
# Get all projects for this user from dynamo
user_id = event.dig("requestContext", "authorizer", "claims", "sub")
project_id = event["pathParameters"]["project_id"]
db = Aws::DynamoDB::Client.new(region: ENV['REGION'])

project = {
"hk" => project_id,
"rk" => "PROJECT"
}

user_project = {
"hk" => "user##{project_id}",
"rk" => "project##{project_id}"
}
begin
db.delete_item(table_name: ENV['PROJECTS_TABLE'], key: {"ProjectId" => project_id})
{ statusCode: 200, body: JSON.generate(item) }
db.delete_item(table_name: ENV['PROJECTS_TABLE'], key: project)
db.delete_item(table_name: ENV['PROJECTS_TABLE'], key: user_project)
{ statusCode: 200, body: project_id }
rescue Aws::DynamoDB::Errors::ServiceError => error
puts 'Unable to delete project:'
puts error.message
Expand Down
12 changes: 10 additions & 2 deletions src/api/projects/index.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,19 @@

def lambda_handler(event:, context:)
# Get all projects for this user from dynamo
user_id = event.dig("requestContext", "authorizer", "claims", "sub")
db = Aws::DynamoDB::Client.new(region: ENV['REGION'])

begin
resp = db.scan(table_name: ENV['PROJECTS_TABLE'])
{ statusCode: 200, body: JSON.generate(resp[:items]) }
projects = db.query(
table_name: ENV['PROJECTS_TABLE'],
key_condition_expression: "hk = :user and begins_with(rk, :project)",
expression_attribute_values: {
":user" => "user##{user_id}",
":project" => 'project'
}
).items
{ statusCode: 200, body: JSON.generate(projects) }
rescue Aws::DynamoDB::Errors::ServiceError => error
puts 'Unable to create project:'
puts error.message
Expand Down
18 changes: 12 additions & 6 deletions src/api/projects/show.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,19 @@
require 'securerandom'

def lambda_handler(event:, context:)
# Get all projects for this user from dynamo
project_id = event["pathParameters"]["project_id"]
db = Aws::DynamoDB::Client.new(region: ENV['REGION'])
s3_client = Aws::S3::Client.new()
bucket = Aws::S3::Bucket.new(ENV["BUCKET"], client: s3_client)
# Get all projects for this user from dynamo
user_id = event.dig("requestContext", "authorizer", "claims", "sub")
project_id = event["pathParameters"]["project_id"]
db = Aws::DynamoDB::Client.new(region: ENV['REGION'])
s3_client = Aws::S3::Client.new()
bucket = Aws::S3::Bucket.new(ENV["BUCKET"], client: s3_client)

project = db.get_item(table_name: ENV['PROJECTS_TABLE'], key: {"ProjectId" => project_id}).item
user_project = db.get_item(table_name: ENV['PROJECTS_TABLE'], key: {"hk" => "user##{user_id}", "rk" => "project##{project_id}" }).item

if user_project
project = db.get_item(table_name: ENV['PROJECTS_TABLE'], key: {"hk" => project_id, "rk" => "PROJECT"}).item
{ statusCode: 200, body: JSON.generate(project) }
else
{ statusCode: 400, body: "" }
end
end
46 changes: 21 additions & 25 deletions src/api/render_tasks/create.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,43 +6,39 @@ def lambda_handler(event:, context:)
# Get all projects for this user from dynamo
db = Aws::DynamoDB::Client.new(region: ENV['REGION'])
sqs = Aws::SQS::Client.new(region: ENV["REGION"])
user_id = event.dig("requestContext", "authorizer", "claims", "sub")

request_body = event["body"] ? JSON.parse(event["body"]) : nil

puts event.inspect

render_task_id = SecureRandom.uuid
project_id = event["pathParameters"]["project_id"]
status = 'started'

new_task = {
"Status" => "started",
"StartedAt" => Time.now.strftime('%Y-%m-%dT%H:%M:%S.%L%z'),
"StartFrame" => request_body["start_frame"],
"EndFrame" => request_body["end_frame"],
"RenderTaskId" => render_task_id,
"QueueUrl" => sqs.create_queue(queue_name: "RenderTask#{render_task_id}").queue_url
render_task = {
"hk" => render_task_id,
"rk" => "RENDER_TASK",
"data" => status,
"started_at" => Time.now.strftime('%Y-%m-%dT%H:%M:%S.%L%z'),
"start_frame" => request_body["start_frame"],
"end_frame" => request_body["end_frame"]
}

project_render_task = {
"hk" => "project##{project_id}",
"rk" => "render_task##{render_task_id}",
}

begin
res = db.update_item({
table_name: ENV['PROJECTS_TABLE'],
key: {
"ProjectId" => event["pathParameters"]["project_id"]
},
return_values: "ALL_NEW",
update_expression: "set #RenderTasks = list_append(if_not_exists(#RenderTasks, :EmptyList), :RenderTask)",
expression_attribute_names: {
"#RenderTasks" => "RenderTasks"
},
expression_attribute_values: {
":EmptyList" => [],
":RenderTask" => [new_task]
}
})
task = db.put_item(table_name: ENV['PROJECTS_TABLE'], item: render_task)
project_task = db.put_item(table_name: ENV['PROJECTS_TABLE'], item: project_render_task)
new_queue_url = sqs.create_queue(queue_name: "RenderTask#{render_task_id}").queue_url

if new_task["StartFrame"] && new_task["EndFrame"]
if render_task["start_frame"] && render_task["end_frame"]
puts "Sending render messages..."
(new_task["StartFrame"]..new_task["EndFrame"]).each do |frame|
frame_q = Aws::SQS::Queue.new(new_task["QueueUrl"])
(render_task["start_frame"]..render_task["end_frame"]).each do |frame|
frame_q = Aws::SQS::Queue.new(new_queue_url)
frame_q.send_message(
message_body: 'Render Frame Triggered By Render Task',
message_attributes: {
Expand Down
13 changes: 11 additions & 2 deletions src/api/render_tasks/index.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,19 @@ def lambda_handler(event:, context:)
# Get all projects for this user from dynamo
db = Aws::DynamoDB::Client.new(region: ENV['REGION'])
sqs = Aws::SQS::Client.new(region: ENV["REGION"])
user_id = event.dig("requestContext", "authorizer", "claims", "sub")
project_id = event["pathParameters"]["project_id"]

begin
project = db.get_item(table_name: ENV['PROJECTS_TABLE'], key: {"ProjectId" => event["pathParameters"]["project_id"]}).item
{ statusCode: 200, body: JSON.generate(project["RenderTasks"]) }
render_tasks = db.query(
table_name: ENV['PROJECTS_TABLE'],
key_condition_expression: "hk = :project_id AND begins_with(rk, :render_task)",
expression_attribute_values: {
":project_id" => "project##{project_id}",
":render_task" => 'render_task'
}
).items
{ statusCode: 200, body: JSON.generate(render_tasks) }
rescue Aws::DynamoDB::Errors::ServiceError => error
puts error.message
{ statusCode: 400, body: JSON.generate(error.message) }
Expand Down
7 changes: 5 additions & 2 deletions src/api/render_tasks/show.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@ def lambda_handler(event:, context:)
# Get all projects for this user from dynamo
db = Aws::DynamoDB::Client.new(region: ENV['REGION'])
sqs = Aws::SQS::Client.new(region: ENV["REGION"])
user_id = event.dig("requestContext", "authorizer", "claims", "sub")
project_id = event["pathParameters"]["project_id"]
render_task_id = event["pathParameters"]["render_task_id"]

begin
project = db.get_item(table_name: ENV['PROJECTS_TABLE'], key: {"ProjectId" => event["pathParameters"]["project_id"]}).item
render_task = project["RenderTasks"].find{|rt| rt["RenderTaskId"] == event["pathParameters"]["render_task_id"]}
user_project = db.get_item(table_name: ENV['PROJECTS_TABLE'],key: {"hk" => "user##{user_id}", "rk" => "project##{project_id}"}).item
render_task = db.get_item(table_name: ENV['PROJECTS_TABLE'], key: {"hk" => "project##{project_id}", "rk" => "render_task##{render_task_id}"}).item

{ statusCode: 200, body: JSON.generate(render_task) }
rescue Aws::DynamoDB::Errors::ServiceError => error
Expand Down
Binary file modified src/lambda_layer/ruby/.DS_Store
Binary file not shown.
9 changes: 7 additions & 2 deletions src/lambda_layer/ruby/lib/shared.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
require 'shared/model'
require 'shared/models/project'
require 'shared/authenticated_user'

module Shared
def database
@db ||= Aws::DynamoDB::Client.new(region: ENV['REGION'])
end
end

require 'shared/model'
9 changes: 9 additions & 0 deletions src/lambda_layer/ruby/lib/shared/authenticated_user.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module Shared
class AuthenticatedUser
attr_reader :pool_id, :id
def initialize(lambda_event)
authProvider = event.dig("requestContext", "identity", "cognitoAuthenticationProvider")
@pool_id, @id = authProvider.split(":")
end
end
end
2 changes: 1 addition & 1 deletion src/lambda_layer/ruby/lib/shared/model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ module Shared
class Model

end
end
end
9 changes: 9 additions & 0 deletions src/lambda_layer/ruby/lib/shared/models/project.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
require 'shared/model'

module Shared
module Models
class Project < Model

end
end
end