modulator 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +11 -0
- data/.rspec +3 -0
- data/.travis.yml +7 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +7 -0
- data/Gemfile.lock +87 -0
- data/LICENSE.txt +21 -0
- data/README.md +43 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/gateway/gateway.rb +123 -0
- data/lib/gateway/routes/console.rb +89 -0
- data/lib/lambda/aws_lambda_handler.rb +140 -0
- data/lib/lambda/aws_stack_builder.rb +223 -0
- data/lib/lambda/aws_stack_uploader.rb +147 -0
- data/lib/modulator.rb +114 -0
- data/lib/utils.rb +101 -0
- data/lib/version.rb +3 -0
- data/modulator.gemspec +42 -0
- metadata +203 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: e7dc8b70cbed5a8393011232c81278535c22415e6f2cef565b68f1f74a50b69f
|
4
|
+
data.tar.gz: d68ee3491a171bbe19a717ea8cd85e5cbdb1376d6abb27c8294dc987aee54b6a
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: ddd88590bb797341ee9c0b27f1ecfe5e76650740b660581ee0f1be5a89ced5fc3aee8fe957de1938dceb163c6d6d8e450da6102cb25688b17c6ae61f57e39507
|
7
|
+
data.tar.gz: 8376dd8b6ba36dfa66813d1319273b0c85dffedf482c15bc0e1bd55a84f8d437c317d90813f4c0f8f87b715e75e365c73d6d80aeca7b011c72c84cb5b71ed67c
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at damir.roso@nih.gov. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: http://contributor-covenant.org
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,87 @@
|
|
1
|
+
GIT
|
2
|
+
remote: https://github.com/damir/humidifier
|
3
|
+
revision: 81a75bbd4b47aadaaed5f967fb3527a2175dec82
|
4
|
+
specs:
|
5
|
+
humidifier (2.15.0)
|
6
|
+
|
7
|
+
PATH
|
8
|
+
remote: .
|
9
|
+
specs:
|
10
|
+
modulator (0.1.1)
|
11
|
+
aws-sdk-cloudformation
|
12
|
+
aws-sdk-s3
|
13
|
+
puma
|
14
|
+
rerun
|
15
|
+
roda
|
16
|
+
rubyzip
|
17
|
+
|
18
|
+
GEM
|
19
|
+
remote: https://rubygems.org/
|
20
|
+
specs:
|
21
|
+
aws-eventstream (1.0.3)
|
22
|
+
aws-partitions (1.154.0)
|
23
|
+
aws-sdk-cloudformation (1.19.0)
|
24
|
+
aws-sdk-core (~> 3, >= 3.48.2)
|
25
|
+
aws-sigv4 (~> 1.1)
|
26
|
+
aws-sdk-core (3.48.6)
|
27
|
+
aws-eventstream (~> 1.0, >= 1.0.2)
|
28
|
+
aws-partitions (~> 1.0)
|
29
|
+
aws-sigv4 (~> 1.1)
|
30
|
+
jmespath (~> 1.0)
|
31
|
+
aws-sdk-kms (1.17.0)
|
32
|
+
aws-sdk-core (~> 3, >= 3.48.2)
|
33
|
+
aws-sigv4 (~> 1.1)
|
34
|
+
aws-sdk-s3 (1.36.1)
|
35
|
+
aws-sdk-core (~> 3, >= 3.48.2)
|
36
|
+
aws-sdk-kms (~> 1)
|
37
|
+
aws-sigv4 (~> 1.0)
|
38
|
+
aws-sigv4 (1.1.0)
|
39
|
+
aws-eventstream (~> 1.0, >= 1.0.2)
|
40
|
+
diff-lcs (1.3)
|
41
|
+
ffi (1.10.0)
|
42
|
+
jmespath (1.4.0)
|
43
|
+
listen (3.1.5)
|
44
|
+
rb-fsevent (~> 0.9, >= 0.9.4)
|
45
|
+
rb-inotify (~> 0.9, >= 0.9.7)
|
46
|
+
ruby_dep (~> 1.2)
|
47
|
+
puma (3.12.1)
|
48
|
+
rack (2.0.7)
|
49
|
+
rack-test (1.1.0)
|
50
|
+
rack (>= 1.0, < 3)
|
51
|
+
rake (10.5.0)
|
52
|
+
rb-fsevent (0.10.3)
|
53
|
+
rb-inotify (0.10.0)
|
54
|
+
ffi (~> 1.0)
|
55
|
+
rerun (0.13.0)
|
56
|
+
listen (~> 3.0)
|
57
|
+
roda (3.19.0)
|
58
|
+
rack
|
59
|
+
rspec (3.8.0)
|
60
|
+
rspec-core (~> 3.8.0)
|
61
|
+
rspec-expectations (~> 3.8.0)
|
62
|
+
rspec-mocks (~> 3.8.0)
|
63
|
+
rspec-core (3.8.0)
|
64
|
+
rspec-support (~> 3.8.0)
|
65
|
+
rspec-expectations (3.8.3)
|
66
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
67
|
+
rspec-support (~> 3.8.0)
|
68
|
+
rspec-mocks (3.8.0)
|
69
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
70
|
+
rspec-support (~> 3.8.0)
|
71
|
+
rspec-support (3.8.0)
|
72
|
+
ruby_dep (1.5.0)
|
73
|
+
rubyzip (1.2.2)
|
74
|
+
|
75
|
+
PLATFORMS
|
76
|
+
ruby
|
77
|
+
|
78
|
+
DEPENDENCIES
|
79
|
+
bundler (~> 2.0)
|
80
|
+
humidifier!
|
81
|
+
modulator!
|
82
|
+
rack-test (~> 1.1)
|
83
|
+
rake (~> 10.0)
|
84
|
+
rspec (~> 3.0)
|
85
|
+
|
86
|
+
BUNDLED WITH
|
87
|
+
2.0.1
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2019 Damir Roso
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
# Modulator
|
2
|
+
|
3
|
+
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/modulator`. To experiment with that code, run `bin/console` for an interactive prompt.
|
4
|
+
|
5
|
+
TODO: Delete this and the text above, and describe your gem
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'modulator'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install modulator
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
TODO: Write usage instructions here
|
26
|
+
|
27
|
+
## Development
|
28
|
+
|
29
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
30
|
+
|
31
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
32
|
+
|
33
|
+
## Contributing
|
34
|
+
|
35
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/damir/modulator. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
36
|
+
|
37
|
+
## License
|
38
|
+
|
39
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
40
|
+
|
41
|
+
## Code of Conduct
|
42
|
+
|
43
|
+
Everyone interacting in the Modulator project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/modulator/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "modulator"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,123 @@
|
|
1
|
+
require 'roda'
|
2
|
+
require 'modulator'
|
3
|
+
|
4
|
+
class Gateway < Roda
|
5
|
+
plugin :json
|
6
|
+
plugin :hooks
|
7
|
+
plugin :json_parser
|
8
|
+
plugin :pass
|
9
|
+
plugin :all_verbs
|
10
|
+
plugin :multi_route
|
11
|
+
plugin :default_headers, 'Content-Type'=>'application/json'
|
12
|
+
|
13
|
+
my_dir = Pathname.new(__FILE__).dirname
|
14
|
+
my_dir.glob('routes/*.rb').each{|file| require_relative file}
|
15
|
+
DUMMY_AWS_EVENT = Utils.load_json my_dir.parent.join('../spec/lambda/aws/event.json')
|
16
|
+
|
17
|
+
before do
|
18
|
+
@time = Time.now
|
19
|
+
puts
|
20
|
+
puts "Method: #{env['REQUEST_METHOD']}"
|
21
|
+
puts "Path: #{request.path}"
|
22
|
+
puts "Payload: #{request.params}" if request.params.any?
|
23
|
+
puts "Query string: #{env['QUERY_STRING']}" unless env['QUERY_STRING'].size.zero?
|
24
|
+
@headers = Hash[*env.select {|k,v| k.start_with? 'HTTP_'}
|
25
|
+
.collect {|k,v| [k.sub(/^HTTP_/, ''), v]}
|
26
|
+
.collect {|k,v| [k.split('_').collect(&:capitalize).join('-'), v]}
|
27
|
+
.sort
|
28
|
+
.flatten]
|
29
|
+
puts "Headers: #{@headers}"
|
30
|
+
end
|
31
|
+
|
32
|
+
after do |res|
|
33
|
+
puts "Matched path: #{request.matched_path}"
|
34
|
+
puts "Status: #{response.status}"
|
35
|
+
puts ("Took: #{Time.now - @time} seconds")
|
36
|
+
end
|
37
|
+
|
38
|
+
plugin :error_handler do |e|
|
39
|
+
puts
|
40
|
+
error = {error: e.class, message: e.message, backtrace: e.backtrace&.take(10)}
|
41
|
+
puts "FAILED: #{error}"
|
42
|
+
error
|
43
|
+
end
|
44
|
+
|
45
|
+
route do |r|
|
46
|
+
r.root do
|
47
|
+
{working_dir: Pathname.getwd}
|
48
|
+
end
|
49
|
+
|
50
|
+
r.multi_route # tools routes
|
51
|
+
|
52
|
+
# process lambda configs
|
53
|
+
Modulator::LAMBDAS.each do |lambda_name, lambda_config|
|
54
|
+
# puts "* Registering #{lambda_name}"
|
55
|
+
# pp lambda_config
|
56
|
+
|
57
|
+
# module and wrapper config
|
58
|
+
Modulator.set_env lambda_config
|
59
|
+
|
60
|
+
# build route
|
61
|
+
@path_params = {}
|
62
|
+
build_route(r, ENV['gateway_verb'], ENV['gateway_path'].split('/'))
|
63
|
+
end
|
64
|
+
|
65
|
+
# return 404 if no route is found, the loop will return 200 otherwise
|
66
|
+
r.halt
|
67
|
+
end
|
68
|
+
|
69
|
+
def build_route(r, verb, fragments)
|
70
|
+
# end of path, tail is gone: [] -> nil
|
71
|
+
return unless fragments
|
72
|
+
|
73
|
+
# execute when all fragments are processed
|
74
|
+
execute_lambda(r, verb) and return if fragments.empty?
|
75
|
+
|
76
|
+
# traverse fragments
|
77
|
+
fragment = fragments.first
|
78
|
+
tail = fragments[1..-1] || []
|
79
|
+
|
80
|
+
# dynamic fragment
|
81
|
+
if fragment.start_with?(':')
|
82
|
+
r.on String do |value|
|
83
|
+
fragment = fragment[1..-1]
|
84
|
+
@path_params[fragment] = value
|
85
|
+
build_route(r, verb, tail)
|
86
|
+
r.pass # continue loop
|
87
|
+
end
|
88
|
+
|
89
|
+
# static fragment
|
90
|
+
else
|
91
|
+
r.on fragment do
|
92
|
+
build_route(r, verb, tail)
|
93
|
+
r.pass # continue loop
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
def execute_lambda(r, verb)
|
99
|
+
r.send(verb.downcase) do
|
100
|
+
puts "Path params: #{@path_params}"
|
101
|
+
|
102
|
+
# build aws event
|
103
|
+
aws_event = DUMMY_AWS_EVENT.merge(
|
104
|
+
'pathParameters' => @path_params,
|
105
|
+
'queryStringParameters' => {},
|
106
|
+
'headers' => @headers,
|
107
|
+
'body' => request.params.to_json
|
108
|
+
)
|
109
|
+
|
110
|
+
# build aws context
|
111
|
+
aws_context = {}
|
112
|
+
|
113
|
+
# execute lambda
|
114
|
+
result = Dir.chdir(opts[:app_dir] || '.') do
|
115
|
+
AwsLambdaHandler.call(event: aws_event, context: aws_context)
|
116
|
+
end
|
117
|
+
|
118
|
+
# render
|
119
|
+
response.status = result[:statusCode]
|
120
|
+
result[:body]
|
121
|
+
end
|
122
|
+
end
|
123
|
+
end
|
@@ -0,0 +1,89 @@
|
|
1
|
+
require 'aws-sdk-cloudformation'
|
2
|
+
|
3
|
+
# console API
|
4
|
+
Gateway.route('console') do |r|
|
5
|
+
|
6
|
+
client = Aws::CloudFormation::Client.new
|
7
|
+
app_name = (opts[:app_dir] || Pathname.getwd.basename.to_s).camelize
|
8
|
+
|
9
|
+
# helpers
|
10
|
+
def capture_output
|
11
|
+
previous_stdout, $stdout = $stdout, StringIO.new
|
12
|
+
previous_stderr, $stderr = $stderr, StringIO.new
|
13
|
+
yield
|
14
|
+
$stdout.string if $stdout.string.size > 0
|
15
|
+
$stderr.string if $stderr.string.size > 0
|
16
|
+
ensure
|
17
|
+
$stdout = previous_stdout
|
18
|
+
$stderr = previous_stderr
|
19
|
+
end
|
20
|
+
|
21
|
+
def render_command_result(command_result, command_output)
|
22
|
+
if command_result
|
23
|
+
command_result.to_hash
|
24
|
+
else
|
25
|
+
{'aws-sdk-cloudformation': command_output.split("\n").first}
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
r.on 'stack' do
|
30
|
+
bucket_name = ENV['S3BUCKET'] || 'modulator-lambdas'
|
31
|
+
payload = request.params.symbolize_keys
|
32
|
+
command_result = nil
|
33
|
+
|
34
|
+
r.get 'events' do
|
35
|
+
resp = client.describe_stack_events(stack_name: app_name, next_token: @headers['X-Next-Token'])
|
36
|
+
response['X-Next-Token'] = resp.next_token
|
37
|
+
resp.stack_events.map(&:to_hash)
|
38
|
+
end
|
39
|
+
|
40
|
+
r.on 'init' do
|
41
|
+
serializer = :yaml
|
42
|
+
content_type = 'text/html'
|
43
|
+
|
44
|
+
r.on 'json' do
|
45
|
+
serializer = :json
|
46
|
+
content_type = 'application/json'
|
47
|
+
r.pass
|
48
|
+
end
|
49
|
+
|
50
|
+
# init stack
|
51
|
+
stack = Modulator.init_stack(
|
52
|
+
app_name: app_name,
|
53
|
+
bucket: bucket_name,
|
54
|
+
timeout: 15
|
55
|
+
)
|
56
|
+
|
57
|
+
# add stack parameters if found in payload
|
58
|
+
bucket_name[:parameters]&.each do |param|
|
59
|
+
stack.add_parameter(param[:key],
|
60
|
+
description: param[:description],
|
61
|
+
type: param[:type],
|
62
|
+
value: param[:value]
|
63
|
+
)
|
64
|
+
end
|
65
|
+
|
66
|
+
r.post 'valid' do
|
67
|
+
command_output = capture_output do
|
68
|
+
command_result = stack.valid?
|
69
|
+
end
|
70
|
+
render_command_result(command_result, command_output)
|
71
|
+
end
|
72
|
+
|
73
|
+
r.post 'deploy' do
|
74
|
+
command_output = capture_output do
|
75
|
+
command_result = stack.deploy(
|
76
|
+
parameters: bucket_name[:parameters]&.map{|param| {parameter_key: param[:key], parameter_value: param[:value]}},
|
77
|
+
capabilities: ['CAPABILITY_IAM']
|
78
|
+
)
|
79
|
+
end
|
80
|
+
render_command_result(command_result, command_output)
|
81
|
+
end
|
82
|
+
|
83
|
+
r.post do
|
84
|
+
response['Content-Type'] = content_type
|
85
|
+
template = stack.to_cf(serializer)
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
@@ -0,0 +1,140 @@
|
|
1
|
+
require 'pathname'
|
2
|
+
|
3
|
+
module AwsLambdaHandler
|
4
|
+
module_function
|
5
|
+
|
6
|
+
# select event handler
|
7
|
+
def call(event:, context:)
|
8
|
+
# TODO: implement handlers for other event types based on some event key, like AwsS3EventHandler
|
9
|
+
AwsApiGatewayEventHandler.call(event: event, context: context)
|
10
|
+
end
|
11
|
+
|
12
|
+
# helpers
|
13
|
+
def symbolize_keys(obj)
|
14
|
+
case obj
|
15
|
+
when Hash
|
16
|
+
hash = {}
|
17
|
+
obj.each {|k, v| hash[k.to_sym] = symbolize_keys(v)}
|
18
|
+
hash
|
19
|
+
when Array
|
20
|
+
obj.map {|x| symbolize_keys(x)}
|
21
|
+
else
|
22
|
+
obj
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
module AwsApiGatewayEventHandler
|
28
|
+
module_function
|
29
|
+
|
30
|
+
NUMBER_REGEX = /\A[+-]?\d+(\.[\d]+)?\z/
|
31
|
+
|
32
|
+
def call(event:, context:)
|
33
|
+
wrapper_params = {}
|
34
|
+
|
35
|
+
if ENV['wrapper_name']
|
36
|
+
# module init
|
37
|
+
require Pathname.getwd.join(ENV['wrapper_path']).to_s
|
38
|
+
wrapper = Object.const_get(ENV['wrapper_name'])
|
39
|
+
mod_method = ENV['wrapper_method']
|
40
|
+
|
41
|
+
# check arity
|
42
|
+
if wrapper.method(mod_method).parameters != [[:keyreq, :event], [:keyreq, :context]]
|
43
|
+
raise("#{wrapper}.#{mod_method} should accept event and context keyword arguments")
|
44
|
+
end
|
45
|
+
|
46
|
+
# print call info
|
47
|
+
puts "Calling wrapper #{wrapper}.#{mod_method}"
|
48
|
+
wrapper_result = wrapper.send(mod_method, event: event, context: context)
|
49
|
+
|
50
|
+
if wrapper_result.is_a?(Hash)
|
51
|
+
# block with custom status and body
|
52
|
+
return {
|
53
|
+
statusCode: wrapper_result[:status],
|
54
|
+
body: JSON.generate(wrapper_result[:body])
|
55
|
+
} if wrapper_result[:status]
|
56
|
+
|
57
|
+
# or set params
|
58
|
+
wrapper_params = wrapper_result
|
59
|
+
|
60
|
+
elsif !wrapper_result
|
61
|
+
# block if result is false/nil
|
62
|
+
return {
|
63
|
+
statusCode: 403,
|
64
|
+
body: JSON.generate(forbidden: "#{wrapper}.#{mod_method}")
|
65
|
+
} if !wrapper_result
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
# module init
|
70
|
+
require Pathname.getwd.join(ENV['module_path']).to_s
|
71
|
+
mod = Object.const_get(ENV['module_name'])
|
72
|
+
mod_method = ENV['module_method']
|
73
|
+
|
74
|
+
# gateway def
|
75
|
+
verb = ENV['gateway_verb']
|
76
|
+
path = ENV['gateway_path']
|
77
|
+
|
78
|
+
# print call info
|
79
|
+
method_signature = mod.method(mod_method).parameters
|
80
|
+
puts "Resolving #{verb.to_s.upcase} #{path} to #{mod}.#{mod_method} with #{method_signature}"
|
81
|
+
|
82
|
+
# cast path parameters to numbers
|
83
|
+
path_params = event['pathParameters'].each_with_object({}) do |(key, value), params|
|
84
|
+
if matcher = NUMBER_REGEX.match(value)
|
85
|
+
value = matcher[1] ? Float(value) : value.to_i
|
86
|
+
end
|
87
|
+
params[key] = value
|
88
|
+
end
|
89
|
+
|
90
|
+
# merge wrapper params
|
91
|
+
path_params.merge!(wrapper_params)
|
92
|
+
|
93
|
+
# call the module method
|
94
|
+
result =
|
95
|
+
if ['GET', 'DELETE'].include?(verb)
|
96
|
+
mod.send(mod_method, *path_params.values)
|
97
|
+
|
98
|
+
elsif verb == 'POST'
|
99
|
+
payload = AwsLambdaHandler.symbolize_keys(JSON.parse(event['body']))
|
100
|
+
method_signature.each do |arg_type, arg_name| # [[:req, :id], [:key, :pet]]
|
101
|
+
payload = {arg_name => payload} if arg_type == :key # scope payload to first named argument
|
102
|
+
end
|
103
|
+
|
104
|
+
# we can override GET to POST without payload, ruby will break if **payload is resolved from {}
|
105
|
+
if payload.any?
|
106
|
+
mod.send(mod_method, *path_params.values, **payload)
|
107
|
+
else
|
108
|
+
mod.send(mod_method, *path_params.values)
|
109
|
+
end
|
110
|
+
|
111
|
+
else
|
112
|
+
raise 'Verb should be GET, POST or DELETE'
|
113
|
+
end
|
114
|
+
|
115
|
+
# set status and body values
|
116
|
+
if result.nil?
|
117
|
+
status = 404
|
118
|
+
body = nil # it will print json null string
|
119
|
+
else
|
120
|
+
status = result[:status] ? result[:status] : 200
|
121
|
+
body = result[:body] ? result[:body] : result
|
122
|
+
end
|
123
|
+
|
124
|
+
# return result
|
125
|
+
return {statusCode: status, body: JSON.generate(body)}
|
126
|
+
|
127
|
+
# print and return error
|
128
|
+
rescue => e
|
129
|
+
puts output = {
|
130
|
+
statusCode: 500,
|
131
|
+
body: JSON.generate(
|
132
|
+
error: {
|
133
|
+
class: e.class,
|
134
|
+
message: e.message
|
135
|
+
}.merge(ENV['debug'] ? {backtrace: e.backtrace.take(20)} : {})
|
136
|
+
)
|
137
|
+
}
|
138
|
+
output
|
139
|
+
end
|
140
|
+
end
|