Module: Terraforming::Util

Instance Method Summary collapse

Instance Method Details

#apply_template(client, erb) ⇒ Object



3
4
5
# File 'lib/terraforming/util.rb', line 3

def apply_template(client, erb)
  ERB.new(open(template_path(erb)).read, nil, "-").result(binding)
end

#generate_tfstate(resources, tfstate_base = nil) ⇒ Object



20
21
22
23
24
25
# File 'lib/terraforming/util.rb', line 20

def generate_tfstate(resources, tfstate_base = nil)
  tfstate = tfstate_base || tfstate_skeleton
  tfstate["serial"] = tfstate["serial"] + 1
  tfstate["modules"][0]["resources"] = tfstate["modules"][0]["resources"].merge(resources)
  JSON.pretty_generate(tfstate)
end

#name_from_tag(resource, default_name) ⇒ Object



7
8
9
10
# File 'lib/terraforming/util.rb', line 7

def name_from_tag(resource, default_name)
  name_tag = resource.tags.find { |tag| tag.key == "Name" }
  name_tag ? name_tag.value : default_name
end

#normalize_module_name(name) ⇒ Object



12
13
14
# File 'lib/terraforming/util.rb', line 12

def normalize_module_name(name)
  name.gsub(/[^a-zA-Z0-9_-]/, "-")
end

#prettify_policy(policy_document, breakline = false) ⇒ Object



27
28
29
30
31
32
33
34
35
# File 'lib/terraforming/util.rb', line 27

def prettify_policy(policy_document, breakline = false)
  json = JSON.pretty_generate(JSON.parse(CGI.unescape(policy_document)))

  if breakline
    json[-1] != "\n" ? json << "\n" : json
  else
    json.strip
  end
end

#template_path(template_name) ⇒ Object



16
17
18
# File 'lib/terraforming/util.rb', line 16

def template_path(template_name)
  File.join(File.expand_path(File.dirname(__FILE__)), "template", template_name) << ".erb"
end

#tfstate_skeletonObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/terraforming/util.rb', line 37

def tfstate_skeleton
  {
    "version" => 1,
    "serial" => 0,
    "modules" => [
      {
        "path" => [
          "root"
        ],
        "outputs" => {},
        "resources" => {},
      }
    ]
  }
end