module Terraspace::Plugin::Expander::Interface

Attributes

mod[R]

Public Class Methods

new(mod) click to toggle source
# File lib/terraspace/plugin/expander/interface.rb, line 15
def initialize(mod)
  @mod = mod
end

Public Instance Methods

cache_root() click to toggle source
# File lib/terraspace/plugin/expander/interface.rb, line 96
def cache_root
  Terraspace.cache_root
end
env() click to toggle source
# File lib/terraspace/plugin/expander/interface.rb, line 83
def env
  Terraspace.env
end
expand(props={}) click to toggle source

Handles list of objects. Calls expansion to handle each string expansion.

# File lib/terraspace/plugin/expander/interface.rb, line 20
def expand(props={})
  props.each do |key, value|
    props[key] = expansion(value)
  end
  props
end
expansion(string) click to toggle source

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
# 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
instance() click to toggle source
# File lib/terraspace/plugin/expander/interface.rb, line 91
def instance
  @mod.options[:instance] || ''
end
Also aliased as: instance_option
instance_option()
Alias for: instance
mod_name() click to toggle source
# File lib/terraspace/plugin/expander/interface.rb, line 79
def mod_name
  @mod.name
end
strip(string) click to toggle source

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
# 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() click to toggle source
# File lib/terraspace/plugin/expander/interface.rb, line 87
def type_instance
  [type, instance].reject { |s| s.blank? }.join('-')
end
var_value(name) click to toggle source
# 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