Class: Ufo::Stack::CustomProperties

Inherits:
Object
  • Object
show all
Includes:
Ufo::Settings
Defined in:
lib/ufo/stack/custom_properties.rb

Instance Method Summary collapse

Methods included from Ufo::Settings

#cfn, #network, #settings

Constructor Details

#initialize(template, stack_name) ⇒ CustomProperties

Returns a new instance of CustomProperties.



5
6
7
# File 'lib/ufo/stack/custom_properties.rb', line 5

def initialize(template, stack_name)
  @template, @stack_name = template, stack_name
end

Instance Method Details

#applyObject



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/ufo/stack/custom_properties.rb', line 9

def apply
  customizations = camelize(cfn)
  @template["Resources"].each do |logical_id, attrs|
    custom_props = customizations[logical_id]
    next unless custom_props
    attrs["Properties"].deeper_merge!(custom_props, {overwrite_arrays: true})
  end

  substitute_variables!(@template["Resources"])
  @template
end

#camelize(properties) ⇒ Object

Keep backward compatiablity but encouraging CamelCase now because in the ufo init generator the .ufo/settings/cfn/default.yml is now CamelCase



23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/ufo/stack/custom_properties.rb', line 23

def camelize(properties)
  if ENV['UFO_CAMELIZE'] == '0' || settings[:auto_camelize] == false # provide a way to quickly test full camelize disable
    return properties.deep_stringify_keys
  end

  # transform keys: camelize
  properties.deep_stringify_keys.deep_transform_keys do |key|
    if key == key.upcase # trying to generalize special rule for dns.TTL
      key # leave key alone if key is already in all upcase
    else
      key.camelize
    end
  end
end

#substitute_variables!(properties) ⇒ Object

Substitute special variables that cannot be baked into the template because they are dynamically assigned. Only one special variable:

{stack_name}


42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/ufo/stack/custom_properties.rb', line 42

def substitute_variables!(properties)
  # transform values and substitute for special values
  # https://stackoverflow.com/questions/34595142/process-nested-hash-to-convert-all-values-to-strings
  #
  # Examples:
  #   "{stack_name}.stag.boltops.com." => development-demo-web.stag.boltops.com.
  #   "{stack_name}.stag.boltops.com." => dev-demo-web-2.stag.boltops.com.
  properties.deep_merge(properties) do |_,_,v|
    if v.is_a?(String)
      v.sub!('{stack_name}', @stack_name) # need shebang, updating in-place
    else
      v
    end
  end
  properties
end