terraform-template-renderer 0.2.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.rubocop.yml +3 -0
- data/CHANGELOG.md +6 -0
- data/Gemfile +2 -0
- data/README.md +21 -0
- data/Rakefile +2 -0
- data/examples/Gemfile +2 -0
- data/examples/Gemfile.lock +1 -1
- data/examples/simple_partials/partials/string1.erb +2 -0
- data/examples/simple_partials/partials/string2.erb +1 -0
- data/examples/simple_partials/simple_partials.erb +3 -0
- data/examples/simple_partials/simple_partials.tf +23 -0
- data/exe/render-template +6 -4
- data/lib/terraform_template_renderer.rb +2 -0
- data/lib/terraform_template_renderer/binding.rb +17 -0
- data/lib/terraform_template_renderer/renderer.rb +15 -3
- data/lib/terraform_template_renderer/version.rb +3 -1
- data/terraform-template-renderer.gemspec +2 -0
- metadata +6 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 199d05f5cf8be0a612bac993592c3ad7e83a17a4
|
4
|
+
data.tar.gz: a0707bbc1b18022812c9ac513b479031cf6008cf
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c0add51ad60f5be109c7c94cc6acdc60ff16e7fdef8182e94486847be280c5fcb4d0341dd0394b64e1e042500be36c8335c78e0f86ed4e38d3c22afa3284db77
|
7
|
+
data.tar.gz: b7c9ebd034973e49d589d53619bbd55d5969d29b1945f58991f7b8addc5c4c6dd8abf7a8168f051ef9e5f08f010479927e39cf010978c23c57974d28e9cb67bb
|
data/.rubocop.yml
CHANGED
data/CHANGELOG.md
CHANGED
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -23,6 +23,27 @@ provider](https://www.terraform.io/docs/providers/external/data_source.html) to
|
|
23
23
|
terraform passes in the variables to render as a json blob to stdin as described above and expects a json blob
|
24
24
|
back.
|
25
25
|
|
26
|
+
## Rendering Partials
|
27
|
+
|
28
|
+
Inside any template you can render a partial template by calling the render method.
|
29
|
+
|
30
|
+
For example if you have a partial template in filepath `partials/partial.erb`:
|
31
|
+
|
32
|
+
This is a partial, Hello <%= name %>
|
33
|
+
|
34
|
+
And your template is
|
35
|
+
|
36
|
+
My favourite colour is <%= colour %>
|
37
|
+
<%= render 'partials/partial.erb' %>
|
38
|
+
|
39
|
+
And you execute the command as
|
40
|
+
|
41
|
+
echo '{ "colour": "purple", "name": "Jonathan" }' | template_renderer path_to_my_template
|
42
|
+
|
43
|
+
You will produce
|
44
|
+
|
45
|
+
{ "rendered": "My favourite colour is purple\nThis is a partial, Hello Jonathan\n" }
|
46
|
+
|
26
47
|
## ERB Template notes
|
27
48
|
|
28
49
|
Trim mode is enabled and the trim character is a hyphen `-`.
|
data/Rakefile
CHANGED
data/examples/Gemfile
CHANGED
data/examples/Gemfile.lock
CHANGED
@@ -0,0 +1 @@
|
|
1
|
+
Second partial <%= @partial_key_2 %>
|
@@ -0,0 +1,23 @@
|
|
1
|
+
terraform {
|
2
|
+
required_version = "~> 0.11.1"
|
3
|
+
}
|
4
|
+
|
5
|
+
provider "external" {
|
6
|
+
version = "~> 1.0.0"
|
7
|
+
}
|
8
|
+
|
9
|
+
data "external" "simple_partials" {
|
10
|
+
program = ["bundle", "exec", "render-template", "simple_partials.erb"]
|
11
|
+
|
12
|
+
query {
|
13
|
+
key1 = "value1"
|
14
|
+
key2 = "value2"
|
15
|
+
partial_key_1 = "partial_value_1"
|
16
|
+
partial_key_2 = "partial_value_2"
|
17
|
+
}
|
18
|
+
}
|
19
|
+
|
20
|
+
resource "local_file" "simple_partials" {
|
21
|
+
content = "${data.external.simple_partials.result.rendered}"
|
22
|
+
filename = "sample_output"
|
23
|
+
}
|
data/exe/render-template
CHANGED
@@ -1,15 +1,17 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
2
|
|
3
|
+
# frozen_string_literal: true
|
4
|
+
|
3
5
|
require "bundler/setup"
|
4
6
|
require "json"
|
5
7
|
require_relative "../lib/terraform_template_renderer"
|
6
8
|
|
7
9
|
def help
|
8
|
-
puts <<~
|
10
|
+
puts <<~HELP
|
9
11
|
Usage: render-template <path_to_erb_template>
|
10
12
|
|
11
13
|
Reads json blob with params from STDIN
|
12
|
-
|
14
|
+
HELP
|
13
15
|
end
|
14
16
|
|
15
17
|
if ARGV.empty?
|
@@ -24,7 +26,7 @@ if ["-h", "--help", "-help", "help"].include?(template_path)
|
|
24
26
|
exit 0
|
25
27
|
end
|
26
28
|
|
27
|
-
unless File.
|
29
|
+
unless File.file?(template_path)
|
28
30
|
STDERR.puts "ERB Template #{template_path} does not exist"
|
29
31
|
exit 2
|
30
32
|
end
|
@@ -32,7 +34,7 @@ end
|
|
32
34
|
template = File.read(template_path)
|
33
35
|
json_params = STDIN.read
|
34
36
|
|
35
|
-
renderer = TerraformTemplateRenderer::Renderer.new(template)
|
37
|
+
renderer = TerraformTemplateRenderer::Renderer.new(template, File.dirname(template_path))
|
36
38
|
rendered_template = renderer.render(json_params)
|
37
39
|
json_output = { rendered: rendered_template }.to_json
|
38
40
|
puts json_output
|
@@ -1,5 +1,15 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module TerraformTemplateRenderer
|
4
|
+
# Provides a Binding context which we can add arbitrary params to (which will become instance
|
5
|
+
# variables for the templates when they get rendered). Also provides a method to render
|
6
|
+
# partial templates which will pass through itself as the binding context for the partial
|
7
|
+
# template
|
2
8
|
class Binding
|
9
|
+
def initialize(template_path)
|
10
|
+
@template_path = template_path
|
11
|
+
end
|
12
|
+
|
3
13
|
def add_param(key, value)
|
4
14
|
instance_variable_set("@#{key}", value)
|
5
15
|
end
|
@@ -7,5 +17,12 @@ module TerraformTemplateRenderer
|
|
7
17
|
def bind
|
8
18
|
binding
|
9
19
|
end
|
20
|
+
|
21
|
+
def render(partial_path)
|
22
|
+
path_to_partial = File.join(@template_path, partial_path)
|
23
|
+
renderer = Renderer.new(File.read(path_to_partial), @template_path)
|
24
|
+
|
25
|
+
renderer.render_with_binding(self)
|
26
|
+
end
|
10
27
|
end
|
11
28
|
end
|
@@ -1,23 +1,35 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require "erb"
|
2
4
|
require "json"
|
3
5
|
|
4
6
|
module TerraformTemplateRenderer
|
7
|
+
# Renderer to remder an ERB template with variables taken from a json string instead of
|
8
|
+
# from the original binding
|
5
9
|
class Renderer
|
6
|
-
def initialize(template)
|
10
|
+
def initialize(template, template_path)
|
7
11
|
# The third argument enables trim mode using a hyphen
|
8
12
|
@erb_template = ERB.new(template, nil, "-")
|
13
|
+
@template_path = template_path
|
9
14
|
end
|
10
15
|
|
11
16
|
# The passed in json_variables needs to be a JSON object (not array), all the keys will be used
|
12
17
|
# as variables in the templates
|
13
18
|
def render(json_variables)
|
14
|
-
|
19
|
+
binding_ = template_binding(json_variables)
|
20
|
+
render_with_binding(binding_)
|
21
|
+
end
|
22
|
+
|
23
|
+
def render_with_binding(binding_)
|
24
|
+
@erb_template.result(binding_.bind)
|
15
25
|
end
|
16
26
|
|
17
27
|
private
|
18
28
|
|
19
29
|
def template_binding(json_variables)
|
20
|
-
Binding.new.tap
|
30
|
+
Binding.new(@template_path).tap do |binding_object|
|
31
|
+
add_params_to_object(binding_object, JSON.parse(json_variables))
|
32
|
+
end
|
21
33
|
end
|
22
34
|
|
23
35
|
def add_params_to_object(object, params)
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: terraform-template-renderer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jonathan Harden
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-
|
11
|
+
date: 2018-02-02 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: aruba
|
@@ -121,6 +121,10 @@ files:
|
|
121
121
|
- examples/simple_list/simple_list.tf
|
122
122
|
- examples/simple_map/simple_map.erb
|
123
123
|
- examples/simple_map/simple_map.tf
|
124
|
+
- examples/simple_partials/partials/string1.erb
|
125
|
+
- examples/simple_partials/partials/string2.erb
|
126
|
+
- examples/simple_partials/simple_partials.erb
|
127
|
+
- examples/simple_partials/simple_partials.tf
|
124
128
|
- examples/simple_strings/simple_strings.erb
|
125
129
|
- examples/simple_strings/simple_strings.tf
|
126
130
|
- exe/render-template
|