Class: CloudFormer::Template

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTemplate

Returns a new instance of Template.



9
10
11
12
13
14
15
# File 'lib/cloud_former/template.rb', line 9

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

Instance Attribute Details

#conditionsObject (readonly)

Returns the value of attribute conditions.



6
7
8
# File 'lib/cloud_former/template.rb', line 6

def conditions
  @conditions
end

#mappingsObject (readonly)

Returns the value of attribute mappings.



7
8
9
# File 'lib/cloud_former/template.rb', line 7

def mappings
  @mappings
end

#outputsObject (readonly)

Returns the value of attribute outputs.



5
6
7
# File 'lib/cloud_former/template.rb', line 5

def outputs
  @outputs
end

#parametersObject (readonly)

Returns the value of attribute parameters.



4
5
6
# File 'lib/cloud_former/template.rb', line 4

def parameters
  @parameters
end

#resourcesObject (readonly)

Returns the value of attribute resources.



3
4
5
# File 'lib/cloud_former/template.rb', line 3

def resources
  @resources
end

Instance Method Details

#add_condition(condition) ⇒ Object



43
44
45
46
# File 'lib/cloud_former/template.rb', line 43

def add_condition(condition)
  raise "Overwriting condition with name #{condition.get_name}" if @conditions.any? { |c| c.get_name == condition.get_name }
  @conditions << condition
end

#add_mapping(name, hash) ⇒ Object



54
55
56
# File 'lib/cloud_former/template.rb', line 54

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

#add_output(key, value) ⇒ Object



39
40
41
# File 'lib/cloud_former/template.rb', line 39

def add_output(key, value)
  @outputs << { key: key, value: value }
end

#add_parameter(parameter) ⇒ Object



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

def add_parameter(parameter)
  raise "Overwriting parameter with name #{parameter.get_name}" if @parameters.any? { |p| p.get_name == parameter.get_name }
  @parameters << parameter
end

#add_resource(resource) ⇒ Object



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

def add_resource(resource)
  raise "Overwriting resource with name #{resource.get_name}" if @resources.any? { |r| r.get_name == resource.get_name }
  @resources << resource
end

#condition(name) ⇒ Object



48
49
50
51
52
# File 'lib/cloud_former/template.rb', line 48

def condition(name)
  cond = @conditions.find { |c| c.get_name == name }
  raise "Condition #{name} not found" unless cond
  cond
end

#dump_jsonObject



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/cloud_former/template.rb', line 58

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[:value].is_a?(Resource) || output[:value].is_a?(Parameter)
        output_res[output[:key]] = { 'Value' => { 'Ref' => output[:value].get_name } }
      elsif output[:value].is_a?(Function)
        output_res[output[:key]] = { 'Value' => output[:value].dump_json }
      else
        output_res[output[:key]] = { 'Value' => output[:value] }
      end
    end
    res['Outputs'] = output_res
  end

  res
end

#parameter(name) ⇒ Object



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

def parameter(name)
  param = @parameters.find { |p| p.get_name == name }
  raise "Parameter #{name} not found" unless param
  param
end

#resource(name) ⇒ Object



33
34
35
36
37
# File 'lib/cloud_former/template.rb', line 33

def resource(name)
  res = @resources.find { |r| r.get_name == name }
  raise "Resource #{name} not found" unless res
  res
end