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
- #app ⇒ Object
- #cache_root ⇒ Object
- #env ⇒ Object
-
#expand(props = {}) ⇒ Object
Handles list of objects.
-
#expand_string?(string) ⇒ Boolean
interface method.
-
#expansion(string) ⇒ Object
Handles single string.
- #extra ⇒ Object
- #initialize(mod) ⇒ Object
- #instance ⇒ Object (also: #instance_option)
- #mod_name ⇒ Object
- #project ⇒ Object
-
#region ⇒ Object
So default config works: config.cache_dir = “:REGION/:ENV/:BUILD_DIR” For when folks configure it with the http backend for non-cloud providers The double slash // will be replace with a single slash in expander/interface.rb.
- #role ⇒ Object
-
#strip(string) ⇒ Object
remove leading and trailing common separators.
- #type_instance ⇒ Object
- #var_value(unexpanded) ⇒ 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
#app ⇒ Object
99 |
# File 'lib/terraspace/plugin/expander/interface.rb', line 99 def app; Terraspace.app ; end |
#cache_root ⇒ Object
114 115 116 |
# File 'lib/terraspace/plugin/expander/interface.rb', line 114 def cache_root Terraspace.cache_root end |
#env ⇒ Object
100 |
# File 'lib/terraspace/plugin/expander/interface.rb', line 100 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 |
#expand_string?(string) ⇒ Boolean
interface method
48 49 50 |
# File 'lib/terraspace/plugin/expander/interface.rb', line 48 def (string) true 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 45 |
# File 'lib/terraspace/plugin/expander/interface.rb', line 35 def expansion(string) return string unless string.is_a?(String) # in case of nil return string unless (string) string = string.dup vars = string.scan(/:\w+/) # => [":ENV", ":BUILD_DIR"] vars.each do |var| string.gsub!(var, var_value(var)) end strip(string) end |
#extra ⇒ Object
101 |
# File 'lib/terraspace/plugin/expander/interface.rb', line 101 def extra; Terraspace.extra ; 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
109 110 111 |
# File 'lib/terraspace/plugin/expander/interface.rb', line 109 def instance @mod.[:instance] || '' end |
#mod_name ⇒ Object
95 96 97 |
# File 'lib/terraspace/plugin/expander/interface.rb', line 95 def mod_name @mod.name end |
#project ⇒ Object
102 |
# File 'lib/terraspace/plugin/expander/interface.rb', line 102 def project; Terraspace.project ; end |
#region ⇒ Object
So default config works:
config.cache_dir = ":REGION/:ENV/:BUILD_DIR"
For when folks configure it with the http backend for non-cloud providers The double slash // will be replace with a single slash in expander/interface.rb
122 123 124 |
# File 'lib/terraspace/plugin/expander/interface.rb', line 122 def region '' end |
#role ⇒ Object
103 |
# File 'lib/terraspace/plugin/expander/interface.rb', line 103 def role; Terraspace.role ; 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:
:REGION/:ENV/:BUILD_DIR/
s3 backend key:
:REGION/:ENV/:BUILD_DIR/terraform.tfstate
workspace:
:MOD_NAME-:ENV-:REGION-:INSTANCE
71 72 73 74 75 76 77 |
# File 'lib/terraspace/plugin/expander/interface.rb', line 71 def strip(string) string.sub(%r{/+$},'') # only remove trailing / or else /home/ec2-user => home/ec2-user .sub(/:\/\//, 'TMP_KEEP_HTTP') # so we can keep ://. IE: https:// or http:// .gsub(%r{/+},'/') # remove double slashes are more. IE: // -> / Useful of region is '' in generic expander .sub('TMP_KEEP_HTTP', '://') # restore :// IE: https:// or http:// .sub(/^-+/,'').sub(/-+$/,'') # remove leading and trailing - end |
#type_instance ⇒ Object
105 106 107 |
# File 'lib/terraspace/plugin/expander/interface.rb', line 105 def type_instance [type, instance].reject { |s| s.blank? }.join('-') end |
#var_value(unexpanded) ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/terraspace/plugin/expander/interface.rb', line 79 def var_value() name = .sub(':','') downcase = name.downcase if respond_to?(downcase) value = send(downcase).to_s elsif !ENV[name].blank? return ENV[name] else return '' end if downcase == "namespace" && Terraspace.config.layering.enable_names.expansion value = friendly_name(value) end value end |