Class: Jets::Resource::ChildStack::Shared

Inherits:
AppClass show all
Defined in:
lib/jets/resource/child_stack/shared.rb

Instance Method Summary collapse

Methods inherited from AppClass

#app_logical_id, common_parameters, #controller?, #controller_params, #current_app_class, #depends_on_params, #parameters, #scoped_routes

Methods inherited from Base

#outputs, #template_url

Methods inherited from Base

#replacements, #resource

Constructor Details

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

Returns a new instance of Shared.



8
9
10
11
# File 'lib/jets/resource/child_stack/shared.rb', line 8

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

Instance Method Details

#child_propertiesObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/jets/resource/child_stack/shared.rb', line 25

def child_properties
  props = {
    template_url: template_url,
  }

  props[:parameters] = common_parameters # common child parameters
  # add depends on parameters
  depends_on.each do |dependency|
    dependency_outputs(dependency).each do |output|
      dependency_class = dependency.to_s.classify
      props[:parameters][output] = "!GetAtt #{dependency_class}.Outputs.#{output}"
    end
  end if depends_on

  props
end

#common_parametersObject



42
43
44
45
46
47
# File 'lib/jets/resource/child_stack/shared.rb', line 42

def common_parameters
  {
    IamRole: "!GetAtt IamRole.Arn",
    S3Bucket: "!Ref S3Bucket",
  }
end

#current_shared_classObject

IE: app/resource.rb => Resource Returns Resource class object in the example



70
71
72
73
74
75
76
77
# File 'lib/jets/resource/child_stack/shared.rb', line 70

def current_shared_class
  templates_prefix = "#{Jets::Naming.template_path_prefix}-shared-"
  @path.sub(templates_prefix, '')
    .sub(/\.yml$/,'')
    .gsub('-','/')
    .classify
    .constantize # returns actual class
end

#definitionObject



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/jets/resource/child_stack/shared.rb', line 13

def definition
  logical_id = shared_logical_id
  definition = {
    logical_id => {
      type: "AWS::CloudFormation::Stack",
      properties: child_properties
    }
  }
  definition[logical_id][:depends_on] = depends_on if depends_on
  definition
end

#dependency_outputs(dependency) ⇒ Object

Returns output keys associated with the stack. They are the resource logical ids.



50
51
52
# File 'lib/jets/resource/child_stack/shared.rb', line 50

def dependency_outputs(dependency)
  dependency.to_s.classify.constantize.output_keys
end

#depends_onObject



54
55
56
57
# File 'lib/jets/resource/child_stack/shared.rb', line 54

def depends_on
  return unless current_shared_class.depends_on
  current_shared_class.depends_on.map { |x| x.to_s.singularize.camelize }
end

#resources?Boolean

Tells us if there are any resources defined in the shared class.

Returns: Boolean

Returns:

  • (Boolean)


82
83
84
# File 'lib/jets/resource/child_stack/shared.rb', line 82

def resources?
  current_shared_class.build?
end

#shared_logical_idObject

map the path to a camelized logical_id. Example:

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


62
63
64
65
66
# File 'lib/jets/resource/child_stack/shared.rb', line 62

def shared_logical_id
  regexp = Regexp.new(".*#{Jets.config.project_namespace}-shared-") # remove the shared
  shared_name = @path.sub(regexp, '').sub('.yml', '')
  shared_name.underscore.camelize
end

#template_filenameObject



86
87
88
# File 'lib/jets/resource/child_stack/shared.rb', line 86

def template_filename
  "#{Jets.config.project_namespace}-shared-#{current_shared_class.to_s.underscore}.yml"
end