Class: Jets::Cfn::TemplateBuilders::FunctionProperties::BaseBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/jets/cfn/template_builders/function_properties/base_builder.rb

Direct Known Subclasses

NodeBuilder, PythonBuilder, RubyBuilder

Instance Method Summary collapse

Constructor Details

#initialize(task) ⇒ BaseBuilder

Returns a new instance of BaseBuilder.



10
11
12
# File 'lib/jets/cfn/template_builders/function_properties/base_builder.rb', line 10

def initialize(task)
  @task = task
end

Instance Method Details

#class_propertiesObject

Class properties example:

class PostsController < ApplicationController
  class_timeout 22
  ...
end

Also handles iam policy override at the class level. Example:

class_iam_policy("logs:*")


87
88
89
90
91
92
93
94
95
96
# File 'lib/jets/cfn/template_builders/function_properties/base_builder.rb', line 87

def class_properties
  # klass is PostsController, HardJob, GameRule, Hello or HelloFunction
  klass = Jets::Klass.from_task(@task)
  class_properties = klass.class_properties
  if klass.class_iam_policy
    map = Jets::Cfn::TemplateMappers::IamPolicy::ClassPolicyMapper.new(klass)
    class_properties[:Role] = "!GetAtt #{map.logical_id}.Arn"
  end
  Jets::Pascalize.pascalize(class_properties.deep_stringify_keys)
end

#env_file_propertiesObject



122
123
124
125
# File 'lib/jets/cfn/template_builders/function_properties/base_builder.rb', line 122

def env_file_properties
  env_vars = Jets::Dotenv.load!(true)
  Jets::Pascalize.pascalize(environment: { variables: env_vars })
end

#finalize_properties!(props) ⇒ Object

Add properties managed by Jets.



27
28
29
30
31
32
33
34
35
# File 'lib/jets/cfn/template_builders/function_properties/base_builder.rb', line 27

def finalize_properties!(props)
  handler = full_handler(props)
  runtime = get_runtime(props)
  props.merge!(
    "FunctionName" => map.function_name,
    "Handler" => handler,
    "Runtime" => runtime,
  )
end

#full_handler(props) ⇒ Object

Ensure that the handler path is normalized.



42
43
44
45
46
47
48
# File 'lib/jets/cfn/template_builders/function_properties/base_builder.rb', line 42

def full_handler(props)
  if props["Handler"]
    map.handler_value(props["Handler"])
  else
    default_handler
  end
end

#function_propertiesObject

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


113
114
115
116
117
118
119
120
# File 'lib/jets/cfn/template_builders/function_properties/base_builder.rb', line 113

def function_properties
  properties = @task.properties
  if @task.iam_policy
    map = Jets::Cfn::TemplateMappers::IamPolicy::FunctionPolicyMapper.new(@task)
    properties[:Role] = "!GetAtt #{map.logical_id}.Arn"
  end
  Jets::Pascalize.pascalize(properties.deep_stringify_keys)
end

#get_runtime(props) ⇒ Object



37
38
39
# File 'lib/jets/cfn/template_builders/function_properties/base_builder.rb', line 37

def get_runtime(props)
  props["Runtime"] || default_runtime
end

#global_propertiesObject

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 = 10
  config.function.runtime = "nodejs8.10"
  config.function.memory_size = 1536
end


60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/jets/cfn/template_builders/function_properties/base_builder.rb', line 60

def global_properties
  baseline = {
    Code: {
      S3Bucket: {Ref: "S3Bucket"}, # from child stack
      S3Key: map.code_s3_key
    },
    Role: { Ref: "IamRole" },
    Environment: { Variables: map.environment },
  }.deep_stringify_keys

  app_config_props = Jets.application.config.function.to_h
  app_config_props = Jets::Pascalize.pascalize(app_config_props.deep_stringify_keys)

  baseline.deep_merge(app_config_props)
end

#mapObject



14
15
16
# File 'lib/jets/cfn/template_builders/function_properties/base_builder.rb', line 14

def map
  @map ||= Jets::Cfn::TemplateMappers::LambdaFunctionMapper.new(@task)
end

#propertiesObject



18
19
20
21
22
23
24
# File 'lib/jets/cfn/template_builders/function_properties/base_builder.rb', line 18

def properties
  props = env_file_properties
    .deep_merge(global_properties)
    .deep_merge(class_properties)
    .deep_merge(function_properties)
  finalize_properties!(props)
end