Class: CloudFormer::Template
- Inherits:
-
Object
- Object
- CloudFormer::Template
- Defined in:
- lib/cloud_former/template.rb
Instance Attribute Summary collapse
-
#conditions ⇒ Object
readonly
Returns the value of attribute conditions.
-
#mappings ⇒ Object
readonly
Returns the value of attribute mappings.
-
#outputs ⇒ Object
readonly
Returns the value of attribute outputs.
-
#parameters ⇒ Object
readonly
Returns the value of attribute parameters.
-
#resources ⇒ Object
readonly
Returns the value of attribute resources.
Instance Method Summary collapse
- #add_condition(condition) ⇒ Object
- #add_mapping(name, hash) ⇒ Object
- #add_output(key, value) ⇒ Object
- #add_parameter(parameter) ⇒ Object
- #add_resource(*args, &block) ⇒ Object
- #condition(name) ⇒ Object
- #dump_json ⇒ Object
-
#initialize ⇒ Template
constructor
A new instance of Template.
- #parameter(name) ⇒ Object
- #resource(name) ⇒ Object
Constructor Details
#initialize ⇒ Template
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
#conditions ⇒ Object (readonly)
Returns the value of attribute conditions.
6 7 8 |
# File 'lib/cloud_former/template.rb', line 6 def conditions @conditions end |
#mappings ⇒ Object (readonly)
Returns the value of attribute mappings.
7 8 9 |
# File 'lib/cloud_former/template.rb', line 7 def mappings @mappings end |
#outputs ⇒ Object (readonly)
Returns the value of attribute outputs.
5 6 7 |
# File 'lib/cloud_former/template.rb', line 5 def outputs @outputs end |
#parameters ⇒ Object (readonly)
Returns the value of attribute parameters.
4 5 6 |
# File 'lib/cloud_former/template.rb', line 4 def parameters @parameters end |
#resources ⇒ Object (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
53 54 55 56 |
# File 'lib/cloud_former/template.rb', line 53 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
64 65 66 |
# File 'lib/cloud_former/template.rb', line 64 def add_mapping(name, hash) @mappings[name] = hash end |
#add_output(key, value) ⇒ Object
49 50 51 |
# File 'lib/cloud_former/template.rb', line 49 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(*args, &block) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/cloud_former/template.rb', line 28 def add_resource(*args, &block) if args.size == 1 resource = args.first elsif args.size == 2 type, name = *args klass = CloudFormer.type_to_class(type) resource = klass.new(name, &block) else raise ArgumentErrror, "wrong number of args (#{args.size} for 1..2)" end raise "Overwriting resource with name #{resource.get_name}" if @resources.any? { |r| r.get_name == resource.get_name } @resources << resource resource end |
#condition(name) ⇒ Object
58 59 60 61 62 |
# File 'lib/cloud_former/template.rb', line 58 def condition(name) cond = @conditions.find { |c| c.get_name == name } raise "Condition #{name} not found" unless cond cond end |
#dump_json ⇒ Object
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 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/cloud_former/template.rb', line 68 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
43 44 45 46 47 |
# File 'lib/cloud_former/template.rb', line 43 def resource(name) res = @resources.find { |r| r.get_name == name } raise "Resource #{name} not found" unless res res end |