Module: Terraspace::Plugin::Expander::Interface

Includes:
Friendly, InferProvider
Included in:
Generic
Defined in:
lib/terraspace/plugin/expander/interface.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Friendly

#friendly_name

Methods included from InferProvider

#provider

Instance Attribute Details

#modObject (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

#appObject



99
# File 'lib/terraspace/plugin/expander/interface.rb', line 99

def app;     Terraspace.app     ; end

#cache_rootObject



114
115
116
# File 'lib/terraspace/plugin/expander/interface.rb', line 114

def cache_root
  Terraspace.cache_root
end

#envObject



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 expand(props={})
  props.each do |key, value|
    props[key] = expansion(value)
  end
  props
end

#expand_string?(string) ⇒ Boolean

interface method

Returns:

  • (Boolean)


48
49
50
# File 'lib/terraspace/plugin/expander/interface.rb', line 48

def expand_string?(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 expand_string?(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

#extraObject



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

#instanceObject Also known as: instance_option



109
110
111
# File 'lib/terraspace/plugin/expander/interface.rb', line 109

def instance
  @mod.options[:instance] || ''
end

#mod_nameObject



95
96
97
# File 'lib/terraspace/plugin/expander/interface.rb', line 95

def mod_name
  @mod.name
end

#projectObject



102
# File 'lib/terraspace/plugin/expander/interface.rb', line 102

def project; Terraspace.project ; end

#regionObject

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

#roleObject



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_instanceObject



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(unexpanded)
  name = unexpanded.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