Class: Jets::Cfn::TemplateBuilders::ParentBuilder

Inherits:
Object
  • Object
show all
Includes:
AwsServices, Interface
Defined in:
lib/jets/cfn/template_builders/parent_builder.rb

Instance Method Summary collapse

Methods included from AwsServices

#cfn, #lambda, #s3, #s3_resource, #stack_exists?, #stack_in_progress?

Methods included from Interface

#add_output, #add_parameter, #add_resource, #build, #post_process_template, #template, #text, #write

Constructor Details

#initialize(options = {}) ⇒ ParentBuilder

Returns a new instance of ParentBuilder.



8
9
10
11
# File 'lib/jets/cfn/template_builders/parent_builder.rb', line 8

def initialize(options={})
  @options = options
  @template = ActiveSupport::HashWithIndifferentAccess.new(Resources: {})
end

Instance Method Details

#add_api_gatewayObject

Each shared stacks has different logic. Handle in ugly case statement until we see the common patterns between them. TODO: clean up the add_shared_stack logical after we figure out the common interface pattern



74
75
76
77
78
79
80
81
82
83
# File 'lib/jets/cfn/template_builders/parent_builder.rb', line 74

def add_api_gateway
  path = "#{Jets.config.project_namespace}-api-gateway.yml"
  map = Jets::Cfn::TemplateMappers::ApiGatewayMapper.new(path, @options[:s3_bucket])

  add_resource(map.logical_id, "AWS::CloudFormation::Stack",
    Properties: { TemplateURL: map.template_url }
  )

  add_output(map.logical_id, Value: "!Ref #{map.logical_id}")
end

#add_api_gateway_deploymentObject



85
86
87
88
89
90
91
92
93
94
95
# File 'lib/jets/cfn/template_builders/parent_builder.rb', line 85

def add_api_gateway_deployment
  path = "#{Jets.config.project_namespace}-api-gateway-deployment.yml"
  map = Jets::Cfn::TemplateMappers::ApiGatewayDeploymentMapper.new(path, @options[:s3_bucket])
  add_resource(map.logical_id, "AWS::CloudFormation::Stack",
    Properties: {
      TemplateURL: map.template_url,
      Parameters: map.parameters
    },
    DependsOn: map.depends_on
  )
end

#add_child_resourcesObject



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/jets/cfn/template_builders/parent_builder.rb', line 40

def add_child_resources
  expression = "#{Jets::Naming.template_path_prefix}-*"
  # IE: path: #{Jets.build_root}/templates/demo-dev-2-comments_controller.yml
  Dir.glob(expression).each do |path|
    next unless File.file?(path)
    next if path =~ /api-gateway/ # treated specially

    # Example of produced code:
    #
    #   map = Jets::Cfn::TemplateMappers::ControllerMapper.new(path, s3_bucket)
    #   map = Jets::Cfn::TemplateMappers::JobMapper.new(path, s3_bucket)
    #   map = Jets::Cfn::TemplateMappers::FunctionMapper.new(path, s3_bucket)
    #
    mapper_class_name = File.basename(path, '.yml').split('_').last
    mapper_class_name = mapper_class_name.classify
    mapper_class = "Jets::Cfn::TemplateMappers::#{mapper_class_name}Mapper".constantize # ControllerMapper or JobMapper
    map = mapper_class.new(path, @options[:s3_bucket])

    # map.logical_id example: PostsController, HardJob, Hello, HelloFunction
    add_resource(map.logical_id, "AWS::CloudFormation::Stack",
      TemplateURL: map.template_url,
      Parameters: map.parameters,
    )
  end

  if @options[:stack_type] == :full and !Jets::Router.routes.empty?
    add_api_gateway
    add_api_gateway_deployment
  end
end

#add_minimal_resourcesObject



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/jets/cfn/template_builders/parent_builder.rb', line 26

def add_minimal_resources
  # variables for minimal-stack.yml
  path = File.expand_path("../templates/minimal-stack.yml", __FILE__)
  variables = {
    policy_name: "lambda-#{Jets.config.project_namespace}-policy",
    role_name: "lambda-#{Jets.config.project_namespace}-role",
  }
  rendered_result = Jets::Erb.result(path, variables)
  minimal_template = YAML.load(rendered_result)

  # minimal_template = YAML.load(IO.read(path))
  @template.deep_merge!(minimal_template)
end

#composeObject

compose is an interface method



14
15
16
17
18
19
# File 'lib/jets/cfn/template_builders/parent_builder.rb', line 14

def compose
  puts "Building parent template."

  add_minimal_resources
  add_child_resources unless @options[:stack_type] == :minimal
end

#template_pathObject

template_path is an interface method



22
23
24
# File 'lib/jets/cfn/template_builders/parent_builder.rb', line 22

def template_path
  Jets::Naming.parent_template_path
end