Class: Terrafying::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/terrafying/generator.rb

Direct Known Subclasses

RootContext

Constant Summary collapse

REGION =
ENV.fetch("AWS_REGION", "eu-west-1")
PROVIDER_DEFAULTS =
{
  aws: { region: REGION }
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeContext

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

#outputObject (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

#awsObject



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_childrenObject



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_generateObject



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_namesObject



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

#resourcesObject



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