Module: Terraforming::Util
- Included in:
- Resource::DBParameterGroup, Resource::DBSecurityGroup, Resource::DBSubnetGroup, Resource::EC2, Resource::ELB, Resource::IAMGroup, Resource::IAMGroupPolicy, Resource::IAMInstanceProfile, Resource::IAMPolicy, Resource::IAMRole, Resource::IAMRolePolicy, Resource::IAMUser, Resource::IAMUserPolicy, Resource::NetworkACL, Resource::RDS, Resource::Route53Record, Resource::Route53Zone, Resource::S3, Resource::SecurityGroup, Resource::Subnet, Resource::VPC
- Defined in:
- lib/terraforming/util.rb
Instance Method Summary collapse
- #apply_template(client, erb) ⇒ Object
- #generate_tfstate(resources, tfstate_base = nil) ⇒ Object
- #name_from_tag(resource, default_name) ⇒ Object
- #normalize_module_name(name) ⇒ Object
- #prettify_policy(policy_document, breakline = false) ⇒ Object
- #template_path(template_name) ⇒ Object
- #tfstate_skeleton ⇒ Object
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..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.(File.dirname(__FILE__)), "template", template_name) << ".erb" end |
#tfstate_skeleton ⇒ Object
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 |