Class: Terrafying::Context
- Inherits:
-
Object
- Object
- Terrafying::Context
- Defined in:
- lib/terrafying/generator.rb
Direct Known Subclasses
Constant Summary collapse
- REGION =
ENV.fetch("AWS_REGION", "eu-west-1")
- PROVIDER_DEFAULTS =
{ aws: { region: REGION } }
Instance Attribute Summary collapse
-
#output ⇒ Object
readonly
Returns the value of attribute output.
Instance Method Summary collapse
- #add!(*c) ⇒ Object
- #aws ⇒ Object
- #data(type, name, spec) ⇒ Object
- #id_of(type, name) ⇒ Object
-
#initialize ⇒ Context
constructor
A new instance of Context.
- #output_of(type, name, value) ⇒ Object
- #output_with_children ⇒ Object
- #pretty_generate ⇒ Object
- #provider(name, spec) ⇒ Object
- #resource(type, name, attributes) ⇒ Object
- #resource_names ⇒ Object
- #resources ⇒ Object
- #template(relative_path, params = {}) ⇒ Object
- #tf_safe(str) ⇒ Object
Constructor Details
#initialize ⇒ Context
Returns a new instance of Context.
52 53 54 55 56 57 |
# File 'lib/terrafying/generator.rb', line 52 def initialize @output = { "resource" => {} } @children = [] end |
Instance Attribute Details
#output ⇒ Object (readonly)
Returns the value of attribute output.
50 51 52 |
# File 'lib/terrafying/generator.rb', line 50 def output @output end |
Instance Method Details
#add!(*c) ⇒ Object
127 128 129 130 |
# File 'lib/terrafying/generator.rb', line 127 def add!(*c) @children.push(*c) c[0] end |
#aws ⇒ Object
59 60 61 |
# File 'lib/terrafying/generator.rb', line 59 def aws @@aws ||= Terrafying::Aws::Ops.new REGION end |
#data(type, name, spec) ⇒ Object
68 69 70 71 72 73 |
# File 'lib/terrafying/generator.rb', line 68 def data(type, name, spec) @output["data"] ||= {} @output["data"][type.to_s] ||= {} @output["data"][type.to_s][name.to_s] = spec id_of(type, name) end |
#id_of(type, name) ⇒ Object
93 94 95 |
# File 'lib/terrafying/generator.rb', line 93 def id_of(type,name) output_of(type, name, "id") end |
#output_of(type, name, value) ⇒ Object
97 98 99 |
# File 'lib/terrafying/generator.rb', line 97 def output_of(type, name, value) Ref.new("#{type}.#{name}.#{value}") end |
#output_with_children ⇒ Object
89 90 91 |
# File 'lib/terrafying/generator.rb', line 89 def output_with_children @children.inject(@output) { |out, c| out.deep_merge(c.output_with_children) } end |
#pretty_generate ⇒ Object
101 102 103 |
# File 'lib/terrafying/generator.rb', line 101 def pretty_generate JSON.pretty_generate(output_with_children) end |
#provider(name, spec) ⇒ Object
63 64 65 66 |
# File 'lib/terrafying/generator.rb', line 63 def provider(name, spec) @output["provider"] ||= {} @output["provider"][name] = spec end |
#resource(type, name, attributes) ⇒ Object
75 76 77 78 79 |
# File 'lib/terrafying/generator.rb', line 75 def resource(type, name, attributes) @output["resource"][type.to_s] ||= {} @output["resource"][type.to_s][name.to_s] = attributes id_of(type, name) end |
#resource_names ⇒ Object
105 106 107 108 109 110 111 112 113 114 |
# File 'lib/terrafying/generator.rb', line 105 def resource_names out = output_with_children ret = [] for type in out["resource"].keys for id in out["resource"][type].keys ret << "#{type}.#{id}" end end ret end |
#resources ⇒ Object
116 117 118 119 120 121 122 123 124 125 |
# File 'lib/terrafying/generator.rb', line 116 def resources out = output_with_children ret = [] for type in out["resource"].keys for id in out["resource"][type].keys ret << "${#{type}.#{id}.id}" end end ret end |
#template(relative_path, params = {}) ⇒ Object
81 82 83 84 85 86 87 |
# File 'lib/terrafying/generator.rb', line 81 def template(relative_path, params = {}) dir = caller_locations[0].path filename = File.join(File.dirname(dir), relative_path) erb = ERB.new(IO.read(filename)) erb.filename = filename erb.result(OpenStruct.new(params).instance_eval { binding }) end |
#tf_safe(str) ⇒ Object
132 133 134 |
# File 'lib/terrafying/generator.rb', line 132 def tf_safe(str) str.gsub(/[\.\s\/\?]/, "-") end |