Class: Jets::Cfn::Builder::Nested

Inherits:
Object
  • Object
show all
Includes:
Interface
Defined in:
lib/jets/cfn/builder/nested.rb

Direct Known Subclasses

Authorizer, Controller, Function, Job, OneController, Rule, Shared

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

Methods included from Util::Camelize

#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_policyObject



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_parametersObject



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



66
67
68
69
70
71
72
73
74
75
76
# File 'lib/jets/cfn/builder/nested.rb', line 66

def add_function(definition)
  resource = Jets::Cfn::Resource::Lambda::Function.new(definition)
  add_resource(resource)
  # apigw lambda permission is also added to the controller template next to the function
  route = Jets::Router.find_by_definition(definition)
  if route
    # Creates a permission more directly to set principal apigateway.amazonaws.com
    method = Jets::Cfn::Resource::ApiGateway::Method.new(route)
    add_resource(method.permission)
  end
end

#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_functionsObject



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

Returns:

  • (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_paramsObject



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_controllerObject



54
55
56
57
58
59
60
61
62
63
64
# File 'lib/jets/cfn/builder/nested.rb', line 54

def one_lambda_per_controller
  add_class_iam_policy
  definition = Jets::Lambda::Definition.new(@app_class, "lambda_handler",
    lang: :ruby,
  )
  add_function(definition)
  unless Jets::Router.no_routes?
    controller = Jets::Cfn::Resource::Lambda::Function::Controller.new(definition)
    add_resource(controller.permission)
  end
end

#one_lambda_per_methodObject



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_routesObject

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

#template_pathObject

interface method



14
15
16
# File 'lib/jets/cfn/builder/nested.rb', line 14

def template_path
  Jets::Names.app_template_path(@app_class)
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}" # IE: PostsController#index
  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