Class: Ufo::Cfn::Stack::CustomProperties

Inherits:
Object
  • Object
show all
Defined in:
lib/ufo/cfn/stack/custom_properties.rb

Instance Method Summary collapse

Constructor Details

#initialize(template, stack_name) ⇒ CustomProperties

Returns a new instance of CustomProperties.



3
4
5
# File 'lib/ufo/cfn/stack/custom_properties.rb', line 3

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

Instance Method Details

#applyObject



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

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/config/cfn/default.yml is now CamelCase



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

def camelize(properties)
  if ENV['UFO_CAMELIZE'] == '0'
    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

#cfnObject



57
58
59
60
61
62
63
64
65
66
# File 'lib/ufo/cfn/stack/custom_properties.rb', line 57

def cfn
  folder = "#{Ufo.root}/.ufo/config/cfn/"
  paths = ["base", Ufo.env].map do |file|
    "#{folder}/#{file}.yml"
  end
  paths.inject({}) do |result, path|
    data = yaml_load(path)
    result.merge(data)
  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}


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

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

#yaml_load(path) ⇒ Object



68
69
70
71
72
73
# File 'lib/ufo/cfn/stack/custom_properties.rb', line 68

def yaml_load(path)
  return {} unless File.exist?(path)
  text = RenderMePretty.result(path)
  result = YAML.load(text) # yaml file can contain nothing but comments
  result.is_a?(Hash) ? result.deep_symbolize_keys : {}
end