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



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

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

#add_outputs(attributes) ⇒ Object



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

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

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



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

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



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

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

#add_resource(resource) ⇒ Object



85
86
87
# File 'lib/jets/cfn/builders/interface.rb', line 85

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

#add_resourcesObject



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

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


111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/jets/cfn/builders/interface.rb', line 111

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



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

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
end

#post_process_template(text) ⇒ Object

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



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

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



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

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

#textObject



28
29
30
31
# File 'lib/jets/cfn/builders/interface.rb', line 28

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

#writeObject



17
18
19
20
# File 'lib/jets/cfn/builders/interface.rb', line 17

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