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) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/terraforming/util.rb', line 20

def generate_tfstate(resources)
  tfstate = {
    "version" => 1,
    "serial" => 1,
    "modules" => [
      {
        "path" => [
          "root"
        ],
        "outputs" => {},
        "resources" => 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

#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