Module: Terraspace::Plugin::Expander::Interface
- Includes:
- InferProvider
- Included in:
- Generic
- Defined in:
- lib/terraspace/plugin/expander/interface.rb
Instance Attribute Summary collapse
-
#mod ⇒ Object
readonly
Returns the value of attribute mod.
Instance Method Summary collapse
- #cache_root ⇒ Object
- #env ⇒ Object
-
#expand(props = {}) ⇒ Object
Handles list of objects.
-
#expansion(string) ⇒ Object
Handles single string.
- #initialize(mod) ⇒ Object
- #instance ⇒ Object (also: #instance_option)
- #mod_name ⇒ Object
-
#strip(string) ⇒ Object
remove leading and trailing common separators.
- #type_instance ⇒ Object
- #var_value(name) ⇒ Object
Methods included from InferProvider
Instance Attribute Details
#mod ⇒ Object (readonly)
Returns the value of attribute mod.
13 14 15 |
# File 'lib/terraspace/plugin/expander/interface.rb', line 13 def mod @mod end |
Instance Method Details
#cache_root ⇒ Object
91 92 93 |
# File 'lib/terraspace/plugin/expander/interface.rb', line 91 def cache_root Terraspace.cache_root end |
#env ⇒ Object
78 79 80 |
# File 'lib/terraspace/plugin/expander/interface.rb', line 78 def env Terraspace.env end |
#expand(props = {}) ⇒ Object
Handles list of objects. Calls expansion to handle each string expansion.
19 20 21 22 23 24 |
# File 'lib/terraspace/plugin/expander/interface.rb', line 19 def (props={}) props.each do |key, value| props[key] = expansion(value) end props end |
#expansion(string) ⇒ Object
Handles single string
Replaces variables denoted by colon in front with actual values. Example:
:REGION/:ENV/:BUILD_DIR/terraform.tfstate
>
us-west-2/dev/stacks/wordpress/terraform.tfstate
34 35 36 37 38 39 40 41 42 43 |
# File 'lib/terraspace/plugin/expander/interface.rb', line 34 def expansion(string) return string unless string.is_a?(String) # in case of nil string = string.dup vars = string.scan(/:\w+/) # => [":ENV", ":BUILD_DIR"] vars.each do |var| string.gsub!(var, var_value(var)) end strip(string) end |
#initialize(mod) ⇒ Object
14 15 16 |
# File 'lib/terraspace/plugin/expander/interface.rb', line 14 def initialize(mod) @mod = mod end |
#instance ⇒ Object Also known as: instance_option
86 87 88 |
# File 'lib/terraspace/plugin/expander/interface.rb', line 86 def instance @mod.[:instance] || '' end |
#mod_name ⇒ Object
74 75 76 |
# File 'lib/terraspace/plugin/expander/interface.rb', line 74 def mod_name @mod.name end |
#strip(string) ⇒ Object
remove leading and trailing common separators.
This is useful for when INSTANCE is not set. Note: BUILD_DIR includes INSTANCE
Examples:
cache_dir:
:CACHE_ROOT/:REGION/:ENV/:BUILD_DIR/
s3 backend key:
:REGION/:ENV/:BUILD_DIR/terraform.tfstate
workspace:
:MOD_NAME-:ENV-:REGION-:INSTANCE
64 65 66 67 |
# File 'lib/terraspace/plugin/expander/interface.rb', line 64 def strip(string) string.sub(/^-+/,'').sub(/-+$/,'') # remove leading and trailing - .sub(%r{/+$},'') # only remove trailing / or else /home/ec2-user => home/ec2-user end |
#type_instance ⇒ Object
82 83 84 |
# File 'lib/terraspace/plugin/expander/interface.rb', line 82 def type_instance [type, instance].reject { |s| s.blank? }.join('-') end |
#var_value(name) ⇒ Object
69 70 71 72 |
# File 'lib/terraspace/plugin/expander/interface.rb', line 69 def var_value(name) name = name.sub(':','').downcase send(name) end |