tflat 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8857b58d1633ffb77eda5e12f302113a5741ee5057fb17c32dd1190ba510ee58
4
- data.tar.gz: 5cc3cbdccd719aee57517c79369cce8938f845af117a2d778ab8c6628dd498d0
3
+ metadata.gz: f7f0542a87cf91985a9d85fbb008a929c8476f8ee6c5bf140e35c30a049bf521
4
+ data.tar.gz: 03b4b5f6f38256fc3d9db1ed1b0d23331f4195e1787e527d1702497f2b45f97c
5
5
  SHA512:
6
- metadata.gz: 6e559851c0c04dfa89ca8836feee4a2d7a213eb7aa16cc742cf651b0bc974c333277a879a0a94320bf1cbfd99f43587c31d2d4c197273c173b6c78ec8d227490
7
- data.tar.gz: 62d8944b2f6955eee6defead0095d790a6942f0c9694d750d4ccfff0c1705b95d7d5b3ccb289c47d7db5e03b9e82b1cc919c2a7aaafb1bb7378f387852609512
6
+ metadata.gz: 3902861c7d4683a40a07ae308f65ed0b69856b649294e005e76a108b93189d6b4adf242c527cbde2e7236ae9a7454d4ce494788ef7b33ce276b4ff9b231503d8
7
+ data.tar.gz: 9d89086124665920668439b094c81ebe7bd10cb9adabd3618370ab6bfabe0d65ceefbda82123bed30f04bae3e118ecb1d77ce70e5b0756951affc22b23c6e347
data/README.md CHANGED
@@ -1,10 +1,10 @@
1
- # TFlat
1
+ # TFlat = Terraform + Subdirectories + Ruby (ERB)
2
2
 
3
3
  Aren't you tired of copy and pasting?
4
4
 
5
- I love Terraform, but HCL really gets in the way sometimes.
5
+ I love [Terraform](https://www.terraform.io/), but HCL really gets in the way sometimes.
6
6
 
7
- How many times you wish you could just write a simple IF or CASE statement inside a .tf file? Any attempt of having minimal flow control with HCL results in a massive oneliner mess. Sometimes it feels like writing PERL one-liners. Hard to read equals hard to debug.
7
+ How many times you wish you could just write a simple IF or CASE statement inside a `.tf` file? Any attempt of having minimal flow control with HCL results in a massive oneliner mess. Sometimes it feels like writing PERL one-liners. Hard to read equals hard to debug.
8
8
 
9
9
  Also, you can't use subfolders with Terraform, so you often end up at one of the three scenarios below:
10
10
  - You have a few `.tf` files with a lot of code in it. Ugly and not organized.
@@ -15,6 +15,11 @@ TFlat does 2 things to solve this problem:
15
15
  * Separate your Terraform code in subdirectories.
16
16
  * It allows you to write Ruby code in `.tf` files using ERB templates. Hurray!
17
17
 
18
+ Jump to:
19
+ * [Installation](#installation)
20
+ * [Usage](#usage)
21
+
22
+
18
23
  ## How does TFlat make Terraform read subdirectories?
19
24
  It doesn't. This is what happens when you run it:
20
25
  1. Create a folder `.tflat` inside the current folder if it doesn't exist yet. If it does exist, delete all files from this folder non-recursively.
@@ -92,10 +97,15 @@ template = "<%= file('files/userdata.tpl') %>"
92
97
  # Let Terraform load the file content using the 'f' helper method (quoting nightmare!)
93
98
  template = "${file("<%= f('files/userdata.tpl') %>")}"
94
99
  ```
100
+
101
+ **IMPORTANT:** The file path must be relative to the project's root folder!
102
+
95
103
  It is up to you to choose what you like. Try both and look inside `.tflat/main.tf` to see the difference between the two ways.
96
104
 
97
105
  ## Installation
106
+ 1. [Download and install Terraform](https://www.terraform.io/intro/getting-started/install.html). Make sure the `terraform` command is in your **$PATH**.
98
107
 
108
+ 2. Install the gem:
99
109
  ```
100
110
  $ gem install tflat
101
111
  ```
@@ -112,6 +122,27 @@ tflat plan
112
122
  ```
113
123
  That's it!
114
124
 
125
+ ## Using TFVARS inside the ERB template
126
+ If you store your variables in JSON format inside a file named `terraform.tfvars.json`, those variables will be automatically available for you as a HASH named `@variables` (with string keys). For example:
127
+
128
+ ```
129
+ # terraform.tfvars.json
130
+ {
131
+ "app_domain": "example.com"
132
+ }
133
+
134
+ # files/swarm-stack.yml
135
+ version: "3.7"
136
+ services:
137
+ ...
138
+ myapp:
139
+ ...
140
+ environment:
141
+ - DOMAIN_NAME=<%= @variables['app_domain'] %>
142
+ ...
143
+ ```
144
+
145
+ That way you don't have to deal with Terraform templates!
115
146
 
116
147
  ## Contributing
117
148
 
@@ -2,6 +2,7 @@ require "tflat/version"
2
2
  require 'erb'
3
3
  require 'fileutils'
4
4
  require 'ptools'
5
+ require 'json'
5
6
 
6
7
  module Tflat
7
8
  class Terraform
@@ -19,6 +20,7 @@ module Tflat
19
20
  return
20
21
  end
21
22
  setup
23
+ read_variables
22
24
  print "- [tflat] Generating files..."
23
25
  flatten_directories
24
26
  parse_erb
@@ -35,6 +37,11 @@ module Tflat
35
37
  end
36
38
  end
37
39
 
40
+ def read_variables
41
+ return unless File.file?('terraform.tfvars.json')
42
+ @variables = JSON.parse(File.read 'terraform.tfvars.json')
43
+ end
44
+
38
45
  def flatten_directories
39
46
  all_files.each do |entry|
40
47
  new_name = entry.sub(/^#{directory}\//,'').gsub('/', '#')
@@ -1,3 +1,3 @@
1
1
  module Tflat
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.2"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tflat
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Paulo Arruda
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-11-10 00:00:00.000000000 Z
11
+ date: 2018-11-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: ptools