Class: Sfn::Planner::Aws::Translator

Inherits:
SparkleFormation::Translation
  • Object
show all
Defined in:
lib/sfn/planner/aws.rb

Overview

Customized translator to dereference template

Constant Summary collapse

MAP =
{}
REF_MAPPING =
{}
FN_MAPPING =
{}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(template_hash, args = {}) ⇒ Translator

Override to init flagged array



21
22
23
24
# File 'lib/sfn/planner/aws.rb', line 21

def initialize(template_hash, args={})
  super
  @flagged = []
end

Instance Attribute Details

#flaggedArray<String> (readonly)

Returns flagged items for value replacement.

Returns:

  • (Array<String>)

    flagged items for value replacement



18
19
20
# File 'lib/sfn/planner/aws.rb', line 18

def flagged
  @flagged
end

Instance Method Details

#apply_function(hash, funcs = []) ⇒ Hash

Note:

also allows ‘Ref’ within funcs to provide mapping replacements using the REF_MAPPING constant

Apply function if possible

Parameters:

  • hash (Hash)
  • funcs (Array) (defaults to: [])

    allowed functions

Returns:

  • (Hash)


50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/sfn/planner/aws.rb', line 50

def apply_function(hash, funcs=[])
  k,v = hash.first
  if(hash.size == 1 && (k.start_with?('Fn') || k == 'Ref') && (funcs.empty? || funcs.include?(k)))
    case k
    when 'Fn::Join'
      v.last.join(v.first)
    when 'Fn::FindInMap'
      map_holder = mappings[v[0]]
      if(map_holder)
        map_item = map_holder[dereference(v[1])]
        if(map_item)
          map_item[v[2]]
        else
          raise "Failed to find mapping item! (#{v[0]} -> #{v[1]})"
        end
      else
        raise "Failed to find mapping! (#{v[0]})"
      end
    when 'Fn::GetAtt'
      func.include?('DEREF') ? dereference(hash) : hash
    when 'Ref'
      if(funcs.include?('DEREF'))
        dereference(hash)
      else
        {'Ref' => self.class.const_get(:REF_MAPPING).fetch(v, v)}
      end
    else
      hash
    end
  else
    hash
  end
end

#dereference(hash) ⇒ Hash, String

Override the parent dereference behavior to return junk value on flagged resource match

Parameters:

  • hash (Hash)

Returns:

  • (Hash, String)


89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/sfn/planner/aws.rb', line 89

def dereference(hash)
  result = nil
  if(hash.is_a?(Hash))
    if(hash.keys.first == 'Ref' && flagged?(hash.values.first))
      result = RUNTIME_MODIFIED
    elsif(hash.keys.first == 'Fn::GetAtt')
      if(hash.values.last.last.start_with?('Outputs.'))
        if(flagged?(hash.values.join('_')))
          result = RUNTIME_MODIFIED
        end
      elsif(flagged?(hash.values.first))
        result = RUNTIME_MODIFIED
      end
    end
  end
  result = result.nil? ? super : result
  unless(result.is_a?(Enumerable))
    result = result.to_s
  end
  result
end

#flag_ref(ref_name) ⇒ Array<String>

Flag a reference as modified

Parameters:

  • ref_name (String)

Returns:

  • (Array<String>)


30
31
32
33
# File 'lib/sfn/planner/aws.rb', line 30

def flag_ref(ref_name)
  @flagged << ref_name.to_s
  @flagged.uniq!
end

#flagged?(name) ⇒ TrueClass, FalseClass

Check if resource name is flagged

Parameters:

  • name (String)

Returns:

  • (TrueClass, FalseClass)


39
40
41
# File 'lib/sfn/planner/aws.rb', line 39

def flagged?(name)
  @flagged.include?(name.to_s)
end