Class: Jets::Cfn::Builder::Nested
- Inherits:
-
Object
- Object
- Jets::Cfn::Builder::Nested
show all
- Includes:
- Interface
- Defined in:
- lib/jets/cfn/builder/nested.rb
Instance Method Summary
collapse
Methods included from Interface
#add_description, #add_output, #add_outputs, #add_parameter, #add_parameters, #add_resource, #add_resources, #add_template_resource, #build, #build?, #template, #text, #write
#camelize
Constructor Details
#initialize(app_class) ⇒ Nested
The app_class is can be a controller, job or anonymous function class. IE: PostsController, HardJob
8
9
10
11
|
# File 'lib/jets/cfn/builder/nested.rb', line 8
def initialize(app_class)
@app_class = app_class
@template = ActiveSupport::HashWithIndifferentAccess.new(Resources: {})
end
|
Instance Method Details
#add_class_iam_policy ⇒ Object
85
86
87
88
89
90
|
# File 'lib/jets/cfn/builder/nested.rb', line 85
def add_class_iam_policy
return unless build_class_iam_policy?
resource = Jets::Cfn::Resource::Iam::ClassRole.new(@app_class)
add_resource(resource)
end
|
#add_common_parameters ⇒ Object
18
19
20
21
22
23
24
25
26
27
|
# File 'lib/jets/cfn/builder/nested.rb', line 18
def add_common_parameters
common_parameters = Jets::Cfn::Params::Common.parameters
common_parameters.each do |k,_|
add_parameter(k)
end
depends_on_params.each do |output_key, _|
add_parameter(output_key)
end
end
|
#add_function(definition) ⇒ Object
#add_function_iam_policy(definition) ⇒ Object
105
106
107
108
109
110
|
# File 'lib/jets/cfn/builder/nested.rb', line 105
def add_function_iam_policy(definition)
return unless definition.build_function_iam?
resource = Jets::Cfn::Resource::Iam::FunctionRole.new(definition)
add_resource(resource)
end
|
#add_functions ⇒ Object
37
38
39
40
41
42
43
44
|
# File 'lib/jets/cfn/builder/nested.rb', line 37
def add_functions
validate_function_names!
if Jets.one_lambda_per_controller? && @app_class.to_s.ends_with?("Controller")
one_lambda_per_controller
else
one_lambda_per_method
end
end
|
#build_class_iam_policy? ⇒ Boolean
92
93
94
95
96
97
98
99
100
101
102
103
|
# File 'lib/jets/cfn/builder/nested.rb', line 92
def build_class_iam_policy?
should_build = false
klass = @app_class
while klass && klass != Object
if klass&.build_class_iam?
should_build = true
break
end
klass = klass.superclass
end
should_build
end
|
#depends_on_params ⇒ Object
29
30
31
32
33
34
35
|
# File 'lib/jets/cfn/builder/nested.rb', line 29
def depends_on_params
return {} if Jets.one_lambda_for_all_controllers? && @app_class.to_s.ends_with?("Controller")
return {} if @app_class.is_a?(Hash)
return {} unless @app_class.depends_on
depends = Jets::Stack::Depends.new(@app_class.depends_on)
depends.params
end
|
#one_lambda_per_controller ⇒ Object
#one_lambda_per_method ⇒ Object
46
47
48
49
50
51
52
|
# File 'lib/jets/cfn/builder/nested.rb', line 46
def one_lambda_per_method
add_class_iam_policy
@app_class.definitions.each do |definition|
add_function(definition)
add_function_iam_policy(definition)
end
end
|
#scoped_routes ⇒ Object
routes scoped to this controller template.
79
80
81
82
83
|
# File 'lib/jets/cfn/builder/nested.rb', line 79
def scoped_routes
@routes ||= Jets::Router.routes.select do |route|
route.controller_name == @app_class.to_s
end
end
|
#validate_function_names! ⇒ Object
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
|
# File 'lib/jets/cfn/builder/nested.rb', line 112
def validate_function_names!
invalids = @app_class.definitions.reject do |definition|
definition.meth.to_s =~ /^[a-zA-Z][a-zA-Z0-9_]/
end
return if invalids.empty?
list = invalids.map do |definition|
" #{definition.class_name}##{definition.meth}" end.join("\n")
puts "ERROR: Detected invalid AWS Lambda function names".color(:red)
puts <<~EOL
Lambda function names must start with a letter and can only contain letters, numbers, and underscores.
Invalid function names:
#{list}
EOL
exit 1
end
|