Module: Jets::Cfn::Builders::Interface

Extended by:
Memoist
Included in:
ApiDeploymentBuilder, ApiGatewayBuilder, BaseChildBuilder, ParentBuilder
Defined in:
lib/jets/cfn/builders/interface.rb

Instance Method Summary collapse

Instance Method Details

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



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

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

#add_outputs(attributes) ⇒ Object



74
75
76
77
78
# File 'lib/jets/cfn/builders/interface.rb', line 74

def add_outputs(attributes)
  attributes.each do |name,value|
    add_output(name.to_s.camelize, Value: value)
  end
end

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



67
68
69
70
71
72
# File 'lib/jets/cfn/builders/interface.rb', line 67

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

#add_parameters(attributes) ⇒ Object



61
62
63
64
65
# File 'lib/jets/cfn/builders/interface.rb', line 61

def add_parameters(attributes)
  attributes.each do |name,value|
    add_parameter(name.to_s.camelize, Description: value)
  end
end

#add_resource(resource) ⇒ Object



95
96
97
# File 'lib/jets/cfn/builders/interface.rb', line 95

def add_resource(resource)
  add_template_resource(resource.logical_id, resource.type, resource.attributes)
end

#add_resourcesObject



85
86
87
88
89
90
91
92
93
# File 'lib/jets/cfn/builders/interface.rb', line 85

def add_resources
  @app_class.tasks.each do |task|
    task.associated_resources.each do |associated|
      resource = Jets::Resource.new(associated.definition, task.replacements)
      add_resource(resource)
      add_resource(resource.permission)
    end
  end
end

#add_template_resource(logical_id, type, options) ⇒ Object

The add_resource method can take an options Hash with both with either top level attributes or properties.

Example:

Top level options:

add_template_resource("MyId", "AWS::ApiGateway::RestApi",
  type: "AWS::ApiGateway::RestApi",
  properties: {
    name: "my-api"
  },
  depends_on: ["AnotherResource"]
)

Simple options with properties only:

add_template_resource("MyId", "AWS::CloudFormationStack",
  template_url: "template_url",
  parameters: {},
)


121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/jets/cfn/builders/interface.rb', line 121

def add_template_resource(logical_id, type, options)
  options = Jets::Camelizer.transform(options)

  attributes = if options.include?('Type')
                 base = { 'Type' => type }
                 base.merge(options) # options are top-level attributes
               else
                 {
                   'Type' => type,
                   'Properties' => options # options are properties
                 }
               end

  template['Resources'][logical_id] = attributes
end

#buildObject



10
11
12
13
14
15
16
17
# File 'lib/jets/cfn/builders/interface.rb', line 10

def build
  # Do not bother building or writing the template unless there are functions defined
  return if @app_class && !@app_class.build?

  compose # must be implemented by subclass
  write
  self
end

#cook_templateObject



32
33
34
35
36
# File 'lib/jets/cfn/builders/interface.rb', line 32

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

#post_process_template(text) ⇒ Object

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



46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/jets/cfn/builders/interface.rb', line 46

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')
    elsif line.include?('- "!') # IE: - "!GetAtt Foo.Arn"
       # IamRole: - "!GetAtt Foo.Arn" => - !GetAtt Foo.Arn
      line.sub(/- "(.*)"/, '- \1')
    else
      line
    end
  end
  results.join("\n") + "\n"
end

#textObject



38
39
40
41
# File 'lib/jets/cfn/builders/interface.rb', line 38

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

#writeObject



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/jets/cfn/builders/interface.rb', line 19

def write
  if  self.is_a? Paged
    range.each do |page_num|
      turn_to_page(page_num)
      FileUtils.mkdir_p(File.dirname(template_path))
      IO.write(template_path, text)    
    end
  else
    FileUtils.mkdir_p(File.dirname(template_path))
    IO.write(template_path, text)          
  end
end