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



133
134
135
136
# File 'lib/terrafying/generator.rb', line 133

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



74
75
76
77
78
79
# File 'lib/terrafying/generator.rb', line 74

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



99
100
101
# File 'lib/terrafying/generator.rb', line 99

def id_of(type,name)
  output_of(type, name, "id")
end

#output_of(type, name, value) ⇒ Object



103
104
105
# File 'lib/terrafying/generator.rb', line 103

def output_of(type, name, value)
  Ref.new("#{type}.#{name}.#{value}")
end

#output_with_childrenObject



95
96
97
# File 'lib/terrafying/generator.rb', line 95

def output_with_children
  @children.inject(@output) { |out, c| out.deep_merge(c.output_with_children) }
end

#pretty_generateObject



107
108
109
# File 'lib/terrafying/generator.rb', line 107

def pretty_generate
  JSON.pretty_generate(output_with_children)
end

#provider(name, spec) ⇒ Object



63
64
65
66
67
68
69
70
71
72
# File 'lib/terrafying/generator.rb', line 63

def provider(name, spec)
  @output["provider"] ||= {}
  @output["provider"][name] = spec

  if spec[:alias]
    "#{name}.#{spec[:alias]}"
  else
    name.to_s
  end
end

#resource(type, name, attributes) ⇒ Object



81
82
83
84
85
# File 'lib/terrafying/generator.rb', line 81

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



111
112
113
114
115
116
117
118
119
120
# File 'lib/terrafying/generator.rb', line 111

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



122
123
124
125
126
127
128
129
130
131
# File 'lib/terrafying/generator.rb', line 122

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



87
88
89
90
91
92
93
# File 'lib/terrafying/generator.rb', line 87

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



138
139
140
# File 'lib/terrafying/generator.rb', line 138

def tf_safe(str)
  str.gsub(/[\.\s\/\?]/, "-")
end