Skip to content

Commit

Permalink
Add variable support to Terraform examples (#196)
Browse files Browse the repository at this point in the history
  • Loading branch information
mheap authored Jan 8, 2025
1 parent 4f3e167 commit cce7eb5
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,16 @@ resource "{{ include.presenter.resource_name }}" "my_{{ include.presenter.entity
}
```
{% endif %}

{% if include.presenter.variable_names.size > 0 %}

This example requires the following variables to be added to your manifest. You can specify values at runtime by setting `TF_VAR_name=value`.

```
{% for variable in include.presenter.variable_names -%}
variable "{{ variable }}" {
type = string
}
{% endfor -%}
```
{% endif %}
20 changes: 18 additions & 2 deletions app/_plugins/drops/entity_example/presenters/terraform.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ module Presenters
module Terraform
class Base < Presenters::Base
def data
@data ||= @example_drop.data
@data ||= Utils::VariableReplacer::TerraformData.run(
data: @example_drop.data,
variables: variables
)
end

def target
Expand Down Expand Up @@ -92,6 +95,8 @@ def line(input, depth, eol = "\n")
def quote(input)
return '' if input.nil?

return input if input.is_a?(String) && input.start_with?("var.")

return input if ['true', 'false', true, false].include?(input)

return input if input.is_a?(Numeric)
Expand All @@ -114,7 +119,18 @@ def template_file

class Plugin < Base
def data
@data ||= @example_drop.data.except(*targets.keys)
@data ||= Utils::VariableReplacer::TerraformData.run(
data: @example_drop.data.except(*targets.keys),
variables: variables
)
end

def variable_names
keys = []
variables.each do |k, v|
keys = v['value'].gsub("$","").downcase
end
keys
end
end
end
Expand Down
12 changes: 12 additions & 0 deletions app/_plugins/drops/entity_example/utils/variable_replacer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,18 @@ def replace_variable(variable)
"${{ env \"#{env_variable}\" }}"
end
end

class TerraformData < Data
def replace_variable(variable)
value = super

return nil unless value

env_variable = value.gsub('$', '').downcase
"var.#{env_variable}"
end
end

end
end
end
Expand Down

0 comments on commit cce7eb5

Please sign in to comment.