class Terrafying::Context

Constants

PROVIDER_DEFAULTS
REGION

Attributes

output[R]

Public Class Methods

bundle(&block) click to toggle source
# File lib/terrafying/generator.rb, line 169
def self.bundle(&block)
  ctx = Context.new
  ctx.instance_eval(&block)
  ctx
end
new() click to toggle source
# File lib/terrafying/generator.rb, line 177
def initialize
  @output = {
    'resource' => {}
  }
  @children = []
end

Public Instance Methods

add!(*c) click to toggle source
# File lib/terrafying/generator.rb, line 314
def add!(*c)
  @children.push(*c)
  c[0]
end
aws() click to toggle source
# File lib/terrafying/generator.rb, line 184
def aws
  @@aws ||= Terrafying::Aws::Ops.new REGION
end
data(type, name, spec) click to toggle source
# File lib/terrafying/generator.rb, line 239
def data(type, name, spec)
  @output['data'] ||= {}
  @output['data'][type.to_s] ||= {}

  raise "Data already exists #{type}.#{name}" if @output['data'][type.to_s].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) click to toggle source
# File lib/terrafying/generator.rb, line 280
def id_of(type, name)
  output_of(type, name, 'id')
end
key_exists_spec_differs(key, name, spec) click to toggle source
# File lib/terrafying/generator.rb, line 217
def key_exists_spec_differs(key, name, spec)
  @providers.key?(key) && spec != @providers[key][name.to_s]
end
local(name, value) click to toggle source
# File lib/terrafying/generator.rb, line 221
def local(name, value)
  @output['locals'] ||= {}

  raise "Local already exists #{name}" if @output['locals'].key? name.to_s

  @output['locals'][name.to_s] = value
  RootRef.new(kind: :local, name: name)
end
output_of(type, name, key) click to toggle source
# File lib/terrafying/generator.rb, line 284
def output_of(type, name, key)
  RootRef.new(kind: :resource, type: type, name: name)[key]
end
output_with_children() click to toggle source
# File lib/terrafying/generator.rb, line 276
def output_with_children
  @children.inject(@output) { |out, c| out.deep_merge(c.output_with_children) }
end
pretty_generate() click to toggle source
# File lib/terrafying/generator.rb, line 288
def pretty_generate
  JSON.pretty_generate(output_with_children)
end
provider(name, spec) click to toggle source
# File lib/terrafying/generator.rb, line 188
def provider(name, spec)
  key = provider_key(name, spec)
  @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
provider_key(name, spec) click to toggle source
# File lib/terrafying/generator.rb, line 198
def provider_key(name, spec)
  [name, spec[:alias]].compact.join('.')
end
required_provider(name, spec) click to toggle source
# File lib/terrafying/generator.rb, line 202
def required_provider(name, spec)
  @output['terraform'] ||= {}
  @output['terraform']['required_providers'] ||= {}
  raise "Duplicate required_provider configuration detected for #{name}" if @output['terraform']['required_providers'].key? name.to_s

  @output['terraform']['required_providers'][name.to_s] = spec
end
required_version(version) click to toggle source
# File lib/terrafying/generator.rb, line 210
def required_version(version)
  @output['terraform'] ||= {}
  raise "required_version already configure" if @output['terraform']['required_version']

  @output['terraform']['required_version'] = "#{version}"
end
resource(type, name, attributes) click to toggle source
# File lib/terrafying/generator.rb, line 249
def resource(type, name, attributes)
  @output['resource'][type.to_s] ||= {}

  raise "Resource already exists #{type}.#{name}" if @output['resource'][type.to_s].key? name.to_s

  @output['resource'][type.to_s][name.to_s] = attributes
  RootRef.new(kind: :resource, type: type, name: name)
end
resource_names() click to toggle source
# File lib/terrafying/generator.rb, line 292
def resource_names
  out = output_with_children
  ret = []
  out['resource'].keys.each do |type|
    out['resource'][type].keys.each do |id|
      ret << "#{type}.#{id}"
    end
  end
  ret
end
resources() click to toggle source
# File lib/terrafying/generator.rb, line 303
def resources
  out = output_with_children
  ret = []
  out['resource'].keys.each do |type|
    out['resource'][type].keys.each do |id|
      ret << "${#{type}.#{id}.id}"
    end
  end
  ret
end
template(relative_path, params = {}) click to toggle source
# File lib/terrafying/generator.rb, line 268
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_module(name, spec) click to toggle source
# File lib/terrafying/generator.rb, line 258
def tf_module(name, spec)
  @output['module'] ||= {}

  raise "Module already exists #{name}" if @output['module'].key? name.to_s

  @output['module'][name.to_s] = spec

  RootRef.new(kind: :module, name: name)
end
tf_safe(str) click to toggle source
# File lib/terrafying/generator.rb, line 319
def tf_safe(str)
  str.gsub(%r{[\.\s/\?]}, '-').gsub(%r{\*}, "star")
end
var(name, spec) click to toggle source
# File lib/terrafying/generator.rb, line 230
def var(name, spec)
  @output['variable'] ||= {}

  raise "Var already exists #{name}" if @output['variable'].key? name.to_s

  @output['variable'][name.to_s] = spec
  RootRef.new(kind: :var, name: name)
end