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.
Class Method Summary collapse
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.
- #key_exists_spec_differs(key, name, spec) ⇒ Object
- #local(name, value) ⇒ Object
- #output_of(type, name, key) ⇒ 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
- #var(name, spec) ⇒ Object
Constructor Details
#initialize ⇒ Context
Returns a new instance of Context.
183 184 185 186 187 188 |
# File 'lib/terrafying/generator.rb', line 183 def initialize @output = { "resource" => {} } @children = [] end |
Instance Attribute Details
#output ⇒ Object (readonly)
Returns the value of attribute output.
181 182 183 |
# File 'lib/terrafying/generator.rb', line 181 def output @output end |
Class Method Details
Instance Method Details
#add!(*c) ⇒ Object
288 289 290 291 |
# File 'lib/terrafying/generator.rb', line 288 def add!(*c) @children.push(*c) c[0] end |
#aws ⇒ Object
190 191 192 |
# File 'lib/terrafying/generator.rb', line 190 def aws @@aws ||= Terrafying::Aws::Ops.new REGION end |
#data(type, name, spec) ⇒ Object
225 226 227 228 229 230 231 232 |
# File 'lib/terrafying/generator.rb', line 225 def data(type, name, spec) @output["data"] ||= {} @output["data"][type.to_s] ||= {} raise "Data already exists #{type.to_s}.#{name.to_s}" if @output["data"][type.to_s].has_key? name.to_s @output["data"][type.to_s][name.to_s] = spec RootRef.new(kind: :data, type: type, name: name) end |
#id_of(type, name) ⇒ Object
254 255 256 |
# File 'lib/terrafying/generator.rb', line 254 def id_of(type,name) output_of(type, name, "id") end |
#key_exists_spec_differs(key, name, spec) ⇒ Object
203 204 205 |
# File 'lib/terrafying/generator.rb', line 203 def key_exists_spec_differs(key, name, spec) @providers.key?(key) && spec != @providers[key][name.to_s] end |
#local(name, value) ⇒ Object
207 208 209 210 211 212 213 214 |
# File 'lib/terrafying/generator.rb', line 207 def local(name, value) @output["locals"] ||= {} raise "Local already exists #{name.to_s}" if @output["locals"].has_key? name.to_s @output["locals"][name.to_s] = value RootRef.new(kind: :local, name: name) end |
#output_of(type, name, key) ⇒ Object
258 259 260 |
# File 'lib/terrafying/generator.rb', line 258 def output_of(type, name, key) RootRef.new(kind: :resource, type: type, name: name)[key] end |
#output_with_children ⇒ Object
250 251 252 |
# File 'lib/terrafying/generator.rb', line 250 def output_with_children @children.inject(@output) { |out, c| out.deep_merge(c.output_with_children) } end |
#pretty_generate ⇒ Object
262 263 264 |
# File 'lib/terrafying/generator.rb', line 262 def pretty_generate JSON.pretty_generate(output_with_children) end |
#provider(name, spec) ⇒ Object
194 195 196 197 198 199 200 201 |
# File 'lib/terrafying/generator.rb', line 194 def provider(name, spec) key = [name, spec[:alias]].compact.join('.') @providers ||= {} raise "Duplicate provider configuration detected for #{key}" if key_exists_spec_differs(key, name, spec) @providers[key] = { name.to_s => spec } @output['provider'] = @providers.values key end |
#resource(type, name, attributes) ⇒ Object
234 235 236 237 238 239 240 |
# File 'lib/terrafying/generator.rb', line 234 def resource(type, name, attributes) @output["resource"][type.to_s] ||= {} raise "Resource already exists #{type.to_s}.#{name.to_s}" if @output["resource"][type.to_s].has_key? name.to_s @output["resource"][type.to_s][name.to_s] = attributes RootRef.new(kind: :resource, type: type, name: name) end |
#resource_names ⇒ Object
266 267 268 269 270 271 272 273 274 275 |
# File 'lib/terrafying/generator.rb', line 266 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
277 278 279 280 281 282 283 284 285 286 |
# File 'lib/terrafying/generator.rb', line 277 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
242 243 244 245 246 247 248 |
# File 'lib/terrafying/generator.rb', line 242 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
293 294 295 |
# File 'lib/terrafying/generator.rb', line 293 def tf_safe(str) str.gsub(/[\.\s\/\?]/, "-") end |
#var(name, spec) ⇒ Object
216 217 218 219 220 221 222 223 |
# File 'lib/terrafying/generator.rb', line 216 def var(name, spec) @output["variable"] ||= {} raise "Var already exists #{name.to_s}" if @output["variable"].has_key? name.to_s @output["variable"][name.to_s] = spec RootRef.new(kind: :var, name: name) end |