Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add variable support to Terraform examples #196

Merged
merged 1 commit into from
Jan 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading