Module: SparkleFormation::SparkleAttribute::Terraform

Included in:
SparkleFormation::SparkleStruct::Terraform
Defined in:
lib/sparkle_formation/sparkle_attribute/terraform.rb

Overview

Terraform specific helper implementations

Constant Summary collapse

TERRAFORM_INTRINSIC_FUNCTIONS =
[
  "base64decode",
  "base64encode",
  "base64sha256",
  "cidrhost",
  "cidrnetmask",
  "cidrsubnet",
  "coalesce",
  "compact",
  "concat",
  "distinct",
  "element",
  "file",
  "format",
  "formatlist",
  "index",
  "join",
  "jsonencode",
  "length",
  "list",
  "lower",
  "map",
  "md5",
  "merge",
  "uuid",
  "replace",
  "sha1",
  "sha256",
  "signum",
  "sort",
  "split",
  "trimspace",
  "upper",
]

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(klass) ⇒ Object

Set customized struct behavior



12
13
14
# File 'lib/sparkle_formation/sparkle_attribute/terraform.rb', line 12

def self.included(klass)
  klass.const_set(:CAMEL_KEYS, false)
end

Instance Method Details

#__resource_lookup(name) ⇒ String

Lookup resource based on name and provide formatted reference to the resource with type included.

Parameters:

  • name (String, Symbol)

    resource name

Returns:

  • (String)

    resource name



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/sparkle_formation/sparkle_attribute/terraform.rb', line 126

def __resource_lookup(name)
  return name.to_s if name.to_s.include?(".")
  if root!.key?(:resources) && root!.resources.key?(name)
    resource = root!.resources[name]
    if resource.key?(:type)
      return "#{resource.type}.#{name}"
    end
  end
  if root!.key?(:resource)
    types = root!.resource._keys
    matches = types.find_all do |type|
      root!.resource._set(type).key?(name)
    end
    if matches.size > 1
      raise ArgumentError.new "Non-unique resource name. Multiple type " \
                              "matches for resource `#{name}`"
    elsif matches.size == 1
      return "#{matches.first}.#{name}"
    end
  end
  name.to_s
end

#_depends_on(resource_name) ⇒ Array<String> #_depends_on(resource_names) ⇒ Array<String> #_depends_on(*resource_names) ⇒ Array<String> Also known as: depends_on!

Note:

this will directly modify the struct at its current context to inject depends on structure

Resource dependency generator

Overloads:

  • #_depends_on(resource_name) ⇒ Array<String>

    Parameters:

    • resource_name (String, Symbol)

      logical resource name

  • #_depends_on(resource_names) ⇒ Array<String>

    Parameters:

    • resource_names (Array<String, Symbol>)

      list of logical resource names

  • #_depends_on(*resource_names) ⇒ Array<String>

    Parameters:

    • resource_names (Array<String, Symbol>)

      list of logical resource names

Returns:

  • (Array<String>)


158
159
160
# File 'lib/sparkle_formation/sparkle_attribute/terraform.rb', line 158

def _depends_on(*args)
  _set("depends_on", [args].flatten.compact.map { |s| __attribute_key(s) })
end

#_fn_format(*args) ⇒ SparkleFormation::FunctionStruct

Generate a builtin terraform function



101
102
103
104
105
# File 'lib/sparkle_formation/sparkle_attribute/terraform.rb', line 101

def _fn_format(*args)
  src = ::Kernel.__callee__.to_s
  src = ::Bogo::Utility.camel(src.sub(/(^_|\!$)/, ""), false)
  ::SparkleFormation::TerraformStruct.new(src, *args)
end

#_module(m_name) ⇒ Object Also known as: module!



31
32
33
34
# File 'lib/sparkle_formation/sparkle_attribute/terraform.rb', line 31

def _module(m_name)
  __t_stringish(m_name)
  ::SparkleFormation::TerraformStruct.new("module").set!(__attribute_key(m_name))
end

#_path(p_name) ⇒ Object Also known as: path!



24
25
26
27
# File 'lib/sparkle_formation/sparkle_attribute/terraform.rb', line 24

def _path(p_name)
  __t_stringish(p_name)
  ::SparkleFormation::TerraformStruct.new("path").set!(__attribute_key(p_name))
end

#_resource(r_name) ⇒ Object Also known as: resource!

TODO: Add resource checking before returning structure



52
53
54
55
56
# File 'lib/sparkle_formation/sparkle_attribute/terraform.rb', line 52

def _resource(r_name)
  __t_stringish(r_name)
  r_name = __resource_lookup(r_name)
  ::SparkleFormation::TerraformStruct.new(r_name)
end

#_stack_output(stack_name, output_name) ⇒ Hash Also known as: stack_output!

Reference output value from nested stack

Parameters:

  • stack_name (String, Symbol)

    logical resource name of stack

  • output_name (String, Symbol)

    stack output name

Returns:

  • (Hash)


169
170
171
# File 'lib/sparkle_formation/sparkle_attribute/terraform.rb', line 169

def _stack_output(stack_name, output_name)
  _module(stack_name)._set(output_name)
end

#_terraform_lookup(*args) ⇒ Object Also known as: lookup!



45
46
47
# File 'lib/sparkle_formation/sparkle_attribute/terraform.rb', line 45

def _terraform_lookup(*args)
  ::SparkleFormation::TerraformStruct.new("lookup", *args)
end

#_terraform_self(s_name) ⇒ Object Also known as: self!



38
39
40
41
# File 'lib/sparkle_formation/sparkle_attribute/terraform.rb', line 38

def _terraform_self(s_name)
  __t_stringish(s_name)
  ::SparkleFormation::TerraformStruct.new("self").set!(__attribute_key(s_name))
end

#_var(v_name) ⇒ Object Also known as: var!, parameter!



16
17
18
19
# File 'lib/sparkle_formation/sparkle_attribute/terraform.rb', line 16

def _var(v_name)
  __t_stringish(v_name)
  res = ::SparkleFormation::TerraformStruct.new("var").set!(__attribute_key(v_name))
end