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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 48bac62d1bd59cb46a67cfcc05d0a3a92a5d2300
4
- data.tar.gz: 3ca5b7f8ce549c3a498bd688e8b9e2b83df16f35
3
+ metadata.gz: 199d05f5cf8be0a612bac993592c3ad7e83a17a4
4
+ data.tar.gz: a0707bbc1b18022812c9ac513b479031cf6008cf
5
5
  SHA512:
6
- metadata.gz: 05208a74a1873f77c36647a90edc1407fe5856b54bb41398f14839256bc8c47152a3ae49c59062ac145d29b71ccb388c1f52bec5ab370377dc61f7e878396453
7
- data.tar.gz: 7666c567d1a7c0973ae06b9df0dbf155d6feae5f31ebb2bbb8bc53da5cb4f6cc16c7e11b08591b155aabfa2f2a4c627832735a788f25b187688bc42436ea38f5
6
+ metadata.gz: c0add51ad60f5be109c7c94cc6acdc60ff16e7fdef8182e94486847be280c5fcb4d0341dd0394b64e1e042500be36c8335c78e0f86ed4e38d3c22afa3284db77
7
+ data.tar.gz: b7c9ebd034973e49d589d53619bbd55d5969d29b1945f58991f7b8addc5c4c6dd8abf7a8168f051ef9e5f08f010479927e39cf010978c23c57974d28e9cb67bb
@@ -1,3 +1,6 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.3
3
+
1
4
  Metrics/LineLength:
2
5
  Max: 119
3
6
  Exclude:
@@ -1,3 +1,9 @@
1
+ ## 0.3.0 (2018-02-02)
2
+
3
+ Features:
4
+
5
+ - Partials can now be rendered by calling `render 'path/to/partial.erb'` in the template.
6
+
1
7
  ## 0.2.0 (2018-01-29)
2
8
 
3
9
  Features:
data/Gemfile CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  source "https://rubygems.org"
2
4
 
3
5
  gemspec
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
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "bundler/gem_tasks"
2
4
  require "rspec/core/rake_task"
3
5
 
@@ -1 +1,3 @@
1
+ # frozen_string_literal: true
2
+
1
3
  gem "terraform-template-renderer", path: "../"
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: ..
3
3
  specs:
4
- terraform-template-renderer (0.2.0)
4
+ terraform-template-renderer (0.3.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -0,0 +1,2 @@
1
+ First partial <%= @partial_key_1 %>
2
+ <%= render "partials/string2.erb" -%>
@@ -0,0 +1 @@
1
+ Second partial <%= @partial_key_2 %>
@@ -0,0 +1,3 @@
1
+ KEY1 [<%=@key1%>]
2
+ KEY2 [<%=@key2%>]
3
+ <%= render "partials/string1.erb" -%>
@@ -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
+ }
@@ -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 <<~EOF
10
+ puts <<~HELP
9
11
  Usage: render-template <path_to_erb_template>
10
12
 
11
13
  Reads json blob with params from STDIN
12
- EOF
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.exist?(template_path)
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,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require_relative "terraform_template_renderer/binding"
2
4
  require_relative "terraform_template_renderer/renderer"
3
5
  require_relative "terraform_template_renderer/version"
@@ -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
- @erb_template.result(template_binding(json_variables))
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 { |binding_object| add_params_to_object(binding_object, JSON.parse(json_variables)) }.bind
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)
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module TerraformTemplateRenderer
2
- VERSION = "0.2.0"
4
+ VERSION = "0.3.0"
3
5
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require_relative "lib/terraform_template_renderer/version"
2
4
 
3
5
  Gem::Specification.new do |spec|
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.2.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-01-29 00:00:00.000000000 Z
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