Class: Jets::Resource::Function
- Includes:
- Environment
- Defined in:
- lib/jets/resource/function.rb,
lib/jets/resource/function/environment.rb
Defined Under Namespace
Modules: Environment
Instance Method Summary collapse
-
#class_properties ⇒ Object
Class properties example:.
- #code_s3_key ⇒ Object
- #combined_properties ⇒ Object
- #default_handler ⇒ Object
- #default_runtime ⇒ Object
- #definition ⇒ Object
-
#finalize_properties!(props) ⇒ Object
Properties managed by Jets with more finality.
-
#full_handler(props) ⇒ Object
Ensure that the handler path is normalized.
- #function_logical_id ⇒ Object
-
#function_name ⇒ Object
Examples: “#Jets.config.project_namespace-sleep_job-perform” “demo-dev-sleep_job-perform”.
-
#function_properties ⇒ Object
Function properties example:.
- #get_runtime(props) ⇒ Object
-
#global_properties ⇒ Object
Global properties example: jets defaults are in jets/default/application.rb.
- #handler ⇒ Object
-
#handler_value(meth) ⇒ Object
Used for node-shim also.
-
#initialize(task) ⇒ Function
constructor
A new instance of Function.
-
#lookup_class_properties(klass) ⇒ Object
Accounts for inherited class_properties.
- #replacements ⇒ Object
Methods included from Environment
#env_properties, #environment, #jets_env
Methods inherited from Base
Constructor Details
#initialize(task) ⇒ Function
Returns a new instance of Function.
6 7 8 9 |
# File 'lib/jets/resource/function.rb', line 6 def initialize(task) @task = task @app_class = task.class_name.to_s end |
Instance Method Details
#class_properties ⇒ Object
Class properties example:
class PostsController < ApplicationController
class_timeout 22
...
end
Also handles iam policy override at the class level. Example:
class_iam_policy("logs:*")
71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/jets/resource/function.rb', line 71 def class_properties # klass is PostsController, HardJob, GameRule, Hello or HelloFunction klass = Jets::Klass.from_task(@task) class_properties = lookup_class_properties(klass) if klass.build_class_iam? iam_policy = Jets::Resource::Iam::ClassRole.new(klass) class_properties[:role] = "!GetAtt #{iam_policy.logical_id}.Arn" end class_properties end |
#code_s3_key ⇒ Object
174 175 176 177 |
# File 'lib/jets/resource/function.rb', line 174 def code_s3_key checksum = Jets::Builders::Md5.checksums["stage/code"] "jets/code/code-#{checksum}.zip" # s3_key end |
#combined_properties ⇒ Object
28 29 30 31 32 33 34 |
# File 'lib/jets/resource/function.rb', line 28 def combined_properties props = env_properties .deep_merge(global_properties) .deep_merge(class_properties) .deep_merge(function_properties) finalize_properties!(props) end |
#default_handler ⇒ Object
147 148 149 150 151 152 153 154 |
# File 'lib/jets/resource/function.rb', line 147 def default_handler map = { node: @task.full_handler(:handler), # IE: handlers/controllers/posts/show.handler python: @task.full_handler(:lambda_handler), # IE: handlers/controllers/posts/show.lambda_handler ruby: handler, # IE: handlers/controllers/posts_controllers.index } map[@task.lang] end |
#default_runtime ⇒ Object
138 139 140 141 142 143 144 145 |
# File 'lib/jets/resource/function.rb', line 138 def default_runtime map = { node: "nodejs8.10", python: "python3.6", ruby: "nodejs8.10", # node shim for ruby support } map[@task.lang] end |
#definition ⇒ Object
11 12 13 14 15 16 17 18 |
# File 'lib/jets/resource/function.rb', line 11 def definition { function_logical_id => { type: "AWS::Lambda::Function", properties: combined_properties } } end |
#finalize_properties!(props) ⇒ Object
Properties managed by Jets with more finality.
124 125 126 127 128 129 130 131 132 |
# File 'lib/jets/resource/function.rb', line 124 def finalize_properties!(props) handler = full_handler(props) runtime = get_runtime(props) props.merge!( function_name: function_name, handler: handler, runtime: runtime, ) end |
#full_handler(props) ⇒ Object
Ensure that the handler path is normalized.
166 167 168 169 170 171 172 |
# File 'lib/jets/resource/function.rb', line 166 def full_handler(props) if props[:handler] handler_value(props[:handler]) else default_handler end end |
#function_logical_id ⇒ Object
20 21 22 |
# File 'lib/jets/resource/function.rb', line 20 def function_logical_id "{namespace}_lambda_function".underscore end |
#function_name ⇒ Object
182 183 184 185 186 187 188 189 190 191 |
# File 'lib/jets/resource/function.rb', line 182 def function_name # Example values: # @app_class: admin/pages_controller # @task.meth: index # method: admin/pages_controller # method: admin-pages_controller-index method = @app_class.underscore method = method.sub('/','-').gsub(/[^0-9a-z\-_]/i, '') + "-#{@task.meth}" "#{Jets.config.project_namespace}-#{method}" end |
#function_properties ⇒ Object
Function properties example:
class PostsController < ApplicationController
timeout 18
def index
...
end
Also handles iam policy override at the function level. Example:
iam_policy("ec2:*")
def new
render json: params.merge(action: "new")
end
114 115 116 117 118 119 120 121 |
# File 'lib/jets/resource/function.rb', line 114 def function_properties properties = @task.properties if @task.build_function_iam? iam_policy = Jets::Resource::Iam::FunctionRole.new(@task) properties[:role] = "!GetAtt #{iam_policy.logical_id}.Arn" end properties end |
#get_runtime(props) ⇒ Object
134 135 136 |
# File 'lib/jets/resource/function.rb', line 134 def get_runtime(props) props[:runtime] || default_runtime end |
#global_properties ⇒ Object
Global properties example: jets defaults are in jets/default/application.rb. Your application’s default config/application.rb then get used. Example:
Jets.application.configure do
config.function = ActiveSupport::OrderedOptions.new
config.function.timeout = 30
config.function.runtime = "nodejs8.10"
config.function.memory_size = 1536
end
46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/jets/resource/function.rb', line 46 def global_properties baseline = { code: { s3_bucket: "!Ref S3Bucket", s3_key: code_s3_key }, role: "!Ref IamRole", environment: { variables: environment }, } appplication_config = Jets.application.config.function.to_h baseline.merge(appplication_config) end |
#handler ⇒ Object
156 157 158 |
# File 'lib/jets/resource/function.rb', line 156 def handler handler_value(@task.meth) # IE: handlers/controllers/posts_controllers.index end |
#handler_value(meth) ⇒ Object
Used for node-shim also
161 162 163 |
# File 'lib/jets/resource/function.rb', line 161 def handler_value(meth) "handlers/#{@task.type.pluralize}/#{@app_class.underscore}.#{meth}" end |
#lookup_class_properties(klass) ⇒ Object
Accounts for inherited class_properties
85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/jets/resource/function.rb', line 85 def lookup_class_properties(klass) all_classes = [] while klass != Object all_classes << klass klass = klass.superclass end class_properties = {} # Go back down class heirachry top to down all_classes.reverse.each do |k| class_properties.merge!(k.class_properties) end class_properties end |
#replacements ⇒ Object
24 25 26 |
# File 'lib/jets/resource/function.rb', line 24 def replacements @task.replacements # has namespace replacement end |