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

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

Instance Method Summary collapse

Instance Method Details

#add_description(desc) ⇒ Object



53
54
55
# File 'lib/jets/cfn/builders/interface.rb', line 53

def add_description(desc)
  @template[:Description] = desc
end

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



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

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

#add_outputs(attributes) ⇒ Object



70
71
72
73
74
# File 'lib/jets/cfn/builders/interface.rb', line 70

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

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



63
64
65
66
67
68
# File 'lib/jets/cfn/builders/interface.rb', line 63

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

#add_parameters(attributes) ⇒ Object



57
58
59
60
61
# File 'lib/jets/cfn/builders/interface.rb', line 57

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

#add_resource(resource) ⇒ Object



91
92
93
# File 'lib/jets/cfn/builders/interface.rb', line 91

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

#add_resourcesObject



81
82
83
84
85
86
87
88
89
# File 'lib/jets/cfn/builders/interface.rb', line 81

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: {},
)


117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/jets/cfn/builders/interface.rb', line 117

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

#build(parent = false) ⇒ Object



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

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

  if @options.nil? || @options[:templates] || @options[:stack_type] != :minimal || parent
    compose # must be implemented by subclass
  end
  write
end

#post_process_template(text) ⇒ Object

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



38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/jets/cfn/builders/interface.rb', line 38

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

#templateObject



24
25
26
27
28
# File 'lib/jets/cfn/builders/interface.rb', line 24

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

#textObject



30
31
32
33
# File 'lib/jets/cfn/builders/interface.rb', line 30

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

#writeObject



19
20
21
22
# File 'lib/jets/cfn/builders/interface.rb', line 19

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