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

#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(document, breakline: false, unescape: false) ⇒ Object



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

def prettify_policy(document, breakline: false, unescape: false)
  json = JSON.pretty_generate(JSON.parse(unescape ? CGI.unescape(document) : 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