Module: Jets::Cfn::TemplateBuilders::Interface

Included in:
ApiGatewayBuilder, ApiGatewayDeploymentBuilder, BaseChildBuilder, ParentBuilder
Defined in:
lib/jets/cfn/template_builders/interface.rb

Instance Method Summary collapse

Instance Method Details

#add_output(name, options = {}) ⇒ Object



86
87
88
89
# File 'lib/jets/cfn/template_builders/interface.rb', line 86

def add_output(name, options={})
  @template[:Outputs] ||= {}
  @template[:Outputs][name.camelize] = options
end

#add_parameter(name, options = {}) ⇒ Object



79
80
81
82
83
84
# File 'lib/jets/cfn/template_builders/interface.rb', line 79

def add_parameter(name, options={})
  defaults = { Type: "String" }
  options = defaults.merge(options)
  @template[:Parameters] ||= {}
  @template[:Parameters][name.camelize] = options
end

#add_resource(logical_id, type, options) ⇒ Object

Example:

Simple options with properties only: add_resource(“MyId”, “AWS::CloudFormationStack”,

TemplateURL: "template_url",
Parameters: {},

)

More complicated options: add_resource(“MyId”, “AWS::ApiGateway::RestApi”,

Properties: {
  Name: "my-api"
},
DependsOn: ["AnotherResource"]

)



64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/jets/cfn/template_builders/interface.rb', line 64

def add_resource(logical_id, type, options)
  base = { Type: type }

  options = if options.include?(:Properties)
              base.merge(options)
            else
              {
                Type: type,
                Properties: options # options are properties
              }
            end

  @template[:Resources][logical_id] = options
end

#buildObject



7
8
9
10
11
12
13
# File 'lib/jets/cfn/template_builders/interface.rb', line 7

def build
  return if @app_klass && @app_klass.tasks.empty? # do not bother building
    #or writing the template unless there are functions defined

  compose # must be implemented by subclass
  write
end

#post_process_template(text) ⇒ Object

post process the text so that “!Ref IamRole” => !Ref IamRole We strip the surrounding quotes



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/jets/cfn/template_builders/interface.rb', line 34

def post_process_template(text)
  results = text.split("\n").map do |line|
    if line.include?(': "!') # IE: IamRole: "!Ref IamRole",
       # IamRole: "!Ref IamRole" => IamRole: !Ref IamRole
      line.sub(/: "(.*)"/, ': \1')
    else
      line
    end
  end
  results.join("\n") + "\n"
end

#templateObject



20
21
22
23
24
# File 'lib/jets/cfn/template_builders/interface.rb', line 20

def template
  # need the to_hash or the YAML dump has
  #  !ruby/hash:ActiveSupport::HashWithIndifferentAccess
  @template.to_hash
end

#textObject



26
27
28
29
# File 'lib/jets/cfn/template_builders/interface.rb', line 26

def text
  text = YAML.dump(template)
  post_process_template(text)
end

#writeObject



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

def write
  FileUtils.mkdir_p(File.dirname(template_path))
  IO.write(template_path, text)
end