Class: Jets::Resource::ChildStack::AppClass

Inherits:
Base
  • Object
show all
Includes:
CommonParameters
Defined in:
lib/jets/resource/child_stack/app_class.rb

Direct Known Subclasses

Shared

Class Method Summary collapse

Instance Method Summary collapse

Methods included from CommonParameters

#common_parameters

Methods inherited from Base

#template_url

Methods inherited from Base

#replacements, #resource

Constructor Details

#initialize(s3_bucket, options = {}) ⇒ AppClass

Returns a new instance of AppClass.



10
11
12
13
# File 'lib/jets/resource/child_stack/app_class.rb', line 10

def initialize(s3_bucket, options={})
  super
  @path = options[:path]
end

Class Method Details

.common_parametersObject



63
64
65
66
67
68
69
70
# File 'lib/jets/resource/child_stack/app_class.rb', line 63

def self.common_parameters
  parameters = {
    IamRole: "!GetAtt IamRole.Arn",
    S3Bucket: "!Ref S3Bucket",
  }
  parameters[:GemLayer] = "!Ref GemLayer" unless Jets.poly_only?
  parameters
end

Instance Method Details

#add_stagger_depends_on(stacks) ⇒ Object

For staggering. We’re abusing depends_on to slow down the update rate.

For this type of depends_on, there are no template parameters or outputs. To use the normal depends at we would have to make app classes adhere to what Jets::Stack::Depends requires. This is mainly dependency_outputs and output_keys for each class right now. It would not be that difficult but is not needed. So we create the Jets::Stack::Depends::Item objects directly.



49
50
51
52
53
54
# File 'lib/jets/resource/child_stack/app_class.rb', line 49

def add_stagger_depends_on(stacks)
  stack_names = stacks.map { |s| s.current_app_class.to_s.underscore }
  items = stack_names.map { |name| Jets::Stack::Depends::Item.new(name) }
  @stagger_depends_on ||= []
  @stagger_depends_on += items.flatten
end

#all_depends_onObject

Always returns an Array, could be empty



37
38
39
40
41
# File 'lib/jets/resource/child_stack/app_class.rb', line 37

def all_depends_on
  depends_on = current_app_class.depends_on || [] # contains Depends::Items
  stagger_depends_on = @stagger_depends_on  || [] # contains Depends::Items
  depends_on + stagger_depends_on
end

#api_resource_page(parameter) ⇒ Object



95
96
97
# File 'lib/jets/resource/child_stack/app_class.rb', line 95

def api_resource_page(parameter)
  ApiResource::Page.logical_id(parameter)
end

#app_logical_idObject

map the path to a camelized logical_id. Example:

/tmp/jets/demo/templates/demo-dev-2-posts_controller.yml to
PostsController


139
140
141
142
143
# File 'lib/jets/resource/child_stack/app_class.rb', line 139

def app_logical_id
  regexp = Regexp.new(".*#{Jets.config.project_namespace}-app-")
  controller_name = @path.sub(regexp, '').sub('.yml', '')
  controller_name.underscore.camelize
end

#authorizer_output(desc) ⇒ Object



99
100
101
102
103
# File 'lib/jets/resource/child_stack/app_class.rb', line 99

def authorizer_output(desc)
  authorizer_stack, authorizer_logical_id = desc.split('.')
  # IE: MainAuthorizer.Outputs.ProtectAuthorizer
  "#{authorizer_stack}.Outputs.#{authorizer_logical_id}"
end

#controller?Boolean

Returns:

  • (Boolean)


113
114
115
# File 'lib/jets/resource/child_stack/app_class.rb', line 113

def controller?
  @path.include?('_controller.yml')
end

#controller_paramsObject



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/jets/resource/child_stack/app_class.rb', line 72

def controller_params
  return {} if Jets::Router.routes.empty?

  params = {
    RestApi: "!GetAtt ApiGateway.Outputs.RestApi",
  }

  template_path = @path
  template = Jets::Cfn::BuiltTemplate.get(template_path)
  template['Parameters'].each do |p,data|
    case p
    when /Resource$/ # AWS::ApiGateway::Resource in api-resources templates. IE: demo-dev-api-resources-2.yml
      params[p] = "!GetAtt #{api_resource_page(p)}.Outputs.#{p}"
    when /Authorizer$/ # AWS::ApiGateway::Authorizer in authorizers templates. IE: demo-dev-authorizers.yml
      # Description contains metadata to get the Authorizer logical id
      params[p] = "!GetAtt #{authorizer_output(data["Description"])}"
    when 'RootResourceId'
      params[p] = "!GetAtt ApiGateway.Outputs.RootResourceId"
    end
  end
  params
end

#current_app_classObject



127
128
129
130
131
132
133
134
# File 'lib/jets/resource/child_stack/app_class.rb', line 127

def current_app_class
  templates_prefix = "#{Jets::Naming.template_path_prefix}-app-"
  @path.sub(templates_prefix, '')
    .sub(/\.yml$/,'')
    .gsub('-','/')
    .camelize
    .constantize
end

#definitionObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/jets/resource/child_stack/app_class.rb', line 15

def definition
  logical_id = app_logical_id
  defintion = {
    logical_id => {
      type: "AWS::CloudFormation::Stack",
      properties: {
        template_url: template_url,
        parameters: parameters,
      }
    }
  }
  defintion[logical_id][:depends_on] = depends.stack_list if depends
  defintion
end

#dependsObject



30
31
32
33
# File 'lib/jets/resource/child_stack/app_class.rb', line 30

def depends
  return if all_depends_on.empty?
  Jets::Stack::Depends.new(all_depends_on)
end

#job?Boolean

Returns:

  • (Boolean)


117
118
119
# File 'lib/jets/resource/child_stack/app_class.rb', line 117

def job?
  @path.include?('_job.yml')
end

#outputsObject



105
106
107
108
109
110
111
# File 'lib/jets/resource/child_stack/app_class.rb', line 105

def outputs
  if controller? or job?
    {}
  else
    super # { logical_id => "!Ref #{logical_id}" } in base.rb
  end
end

#parametersObject



56
57
58
59
60
61
# File 'lib/jets/resource/child_stack/app_class.rb', line 56

def parameters
  params = self.class.common_parameters
  params.merge!(controller_params) if controller?
  params.merge!(depends.params) if depends
  params
end

#scoped_routesObject



121
122
123
124
125
# File 'lib/jets/resource/child_stack/app_class.rb', line 121

def scoped_routes
  @routes ||= Jets::Router.routes.select do |route|
    route.controller_name == current_app_class.to_s
  end
end

#template_filenameObject



145
146
147
# File 'lib/jets/resource/child_stack/app_class.rb', line 145

def template_filename
  @path
end