Module: Terraspace::Plugin::Expander::Interface
- Includes:
- Friendly, 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 Friendly
Methods included from InferProvider
Instance Attribute Details
#mod ⇒ Object (readonly)
Returns the value of attribute mod.
14 15 16 |
# File 'lib/terraspace/plugin/expander/interface.rb', line 14 def mod @mod end |
Instance Method Details
#cache_root ⇒ Object
96 97 98 |
# File 'lib/terraspace/plugin/expander/interface.rb', line 96 def cache_root Terraspace.cache_root end |
#env ⇒ Object
83 84 85 |
# File 'lib/terraspace/plugin/expander/interface.rb', line 83 def env Terraspace.env end |
#expand(props = {}) ⇒ Object
Handles list of objects. Calls expansion to handle each string expansion.
20 21 22 23 24 25 |
# File 'lib/terraspace/plugin/expander/interface.rb', line 20 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
35 36 37 38 39 40 41 42 43 44 |
# File 'lib/terraspace/plugin/expander/interface.rb', line 35 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
15 16 17 |
# File 'lib/terraspace/plugin/expander/interface.rb', line 15 def initialize(mod) @mod = mod end |
#instance ⇒ Object Also known as: instance_option
91 92 93 |
# File 'lib/terraspace/plugin/expander/interface.rb', line 91 def instance @mod.[:instance] || '' end |
#mod_name ⇒ Object
79 80 81 |
# File 'lib/terraspace/plugin/expander/interface.rb', line 79 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
65 66 67 68 |
# File 'lib/terraspace/plugin/expander/interface.rb', line 65 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
87 88 89 |
# File 'lib/terraspace/plugin/expander/interface.rb', line 87 def type_instance [type, instance].reject { |s| s.blank? }.join('-') end |
#var_value(name) ⇒ Object
70 71 72 73 74 75 76 77 |
# File 'lib/terraspace/plugin/expander/interface.rb', line 70 def var_value(name) name = name.sub(':','').downcase value = send(name) if name == "namespace" && Terraspace.config.layering.enable_names.expansion value = friendly_name(value) end value end |