Class: CloudFormer::Template

Inherits:
Object
  • Object
show all
Defined in:
lib/cloud_former/template.rb

Instance Method Summary collapse

Constructor Details

#initializeTemplate

Returns a new instance of Template.



4
5
6
7
8
9
10
# File 'lib/cloud_former/template.rb', line 4

def initialize
  @resources = []
  @parameters = []
  @outputs = []
  @conditions = []
  @mappings = {}
end

Instance Method Details

#add_condition(condition) ⇒ Object



24
25
26
# File 'lib/cloud_former/template.rb', line 24

def add_condition(condition)
  @conditions << condition
end

#add_mapping(name, hash) ⇒ Object



28
29
30
# File 'lib/cloud_former/template.rb', line 28

def add_mapping(name, hash)
  @mappings[name] = hash
end

#add_output(key, value) ⇒ Object



20
21
22
# File 'lib/cloud_former/template.rb', line 20

def add_output(key, value)
  @outputs << [key, value]
end

#add_parameter(parameter) ⇒ Object



12
13
14
# File 'lib/cloud_former/template.rb', line 12

def add_parameter(parameter)
  @parameters << parameter
end

#add_resource(resource) ⇒ Object



16
17
18
# File 'lib/cloud_former/template.rb', line 16

def add_resource(resource)
  @resources << resource
end

#dump_jsonObject



32
33
34
35
36
37
38
39
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
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/cloud_former/template.rb', line 32

def dump_json
  res = { 'AWSTemplateFormatVersion' => '2010-09-09' }

  if @parameters.any?
    parameter_res = {}
    @parameters.each do |param|
      parameter_res[param.get_name] = param.dump_json
    end
    res['Parameters'] = parameter_res
  end

  if @conditions.any?
    condition_res = {}
    @conditions.each do |condition|
      condition_res[condition.get_name] = condition.dump_json
    end
    res['Conditions'] = condition_res
  end

  if @mappings.any?
    res['Mappings'] = @mappings
  end

  if @resources.any?
    resource_res = {}
    @resources.each do |resource|
      resource.resource_tree.flatten.each do |r|
        resource_res[r.get_name] = r.dump_json
      end
    end
    res['Resources'] = resource_res
  end

  if @outputs.any?
    output_res = {}
    @outputs.each do |output|
      if output[1].is_a?(Resource) || output[1].is_a?(Parameter)
        output_res[output[0]] = { 'Value' => { 'Ref' => output[1].get_name } }
      elsif output[1].is_a?(Function)
        output_res[output[0]] = { 'Value' => output[1].dump_json }
      else
        output_res[output[0]] = { 'Value' => output[1] }
      end
    end
    res['Outputs'] = output_res
  end

  res
end