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.
40 41 42 43 44 45 |
# File 'lib/terrafying/generator.rb', line 40 def initialize @output = { "resource" => {} } @children = [] end |
Instance Attribute Details
#output ⇒ Object (readonly)
Returns the value of attribute output.
38 39 40 |
# File 'lib/terrafying/generator.rb', line 38 def output @output end |
Instance Method Details
#add!(*c) ⇒ Object
115 116 117 118 |
# File 'lib/terrafying/generator.rb', line 115 def add!(*c) @children.push(*c) c[0] end |
#aws ⇒ Object
47 48 49 |
# File 'lib/terrafying/generator.rb', line 47 def aws @@aws ||= Terrafying::Aws::Ops.new REGION end |
#data(type, name, spec) ⇒ Object
56 57 58 59 60 61 |
# File 'lib/terrafying/generator.rb', line 56 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
81 82 83 |
# File 'lib/terrafying/generator.rb', line 81 def id_of(type,name) output_of(type, name, "id") end |
#output_of(type, name, value) ⇒ Object
85 86 87 |
# File 'lib/terrafying/generator.rb', line 85 def output_of(type, name, value) Ref.new("#{type}.#{name}.#{value}") end |
#output_with_children ⇒ Object
77 78 79 |
# File 'lib/terrafying/generator.rb', line 77 def output_with_children @children.inject(@output) { |out, c| out.deep_merge(c.output_with_children) } end |
#pretty_generate ⇒ Object
89 90 91 |
# File 'lib/terrafying/generator.rb', line 89 def pretty_generate JSON.pretty_generate(output_with_children) end |
#provider(name, spec) ⇒ Object
51 52 53 54 |
# File 'lib/terrafying/generator.rb', line 51 def provider(name, spec) @output["provider"] ||= {} @output["provider"][name] = spec end |
#resource(type, name, attributes) ⇒ Object
63 64 65 66 67 |
# File 'lib/terrafying/generator.rb', line 63 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
93 94 95 96 97 98 99 100 101 102 |
# File 'lib/terrafying/generator.rb', line 93 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
104 105 106 107 108 109 110 111 112 113 |
# File 'lib/terrafying/generator.rb', line 104 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
69 70 71 72 73 74 75 |
# File 'lib/terrafying/generator.rb', line 69 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
120 121 122 |
# File 'lib/terrafying/generator.rb', line 120 def tf_safe(str) str.gsub(/[\.\s\/\?]/, "-") end |