Module: SparkleFormation::Provider::Aws

Defined in:
lib/sparkle_formation/provider/aws.rb

Overview

AWS specific implementation

Instance Method Summary collapse

Instance Method Details

#apply_deep_nesting(*args) {|stack, resource, s_name| ... } ⇒ SparkleFormation::SparkleStruct

Apply deeply nested stacks. This is the new nesting approach and does not bubble parameters up to the root stack. Parameters are isolated to the stack resource itself and output mapping is automatically applied.

Yield Parameters:

  • stack (SparkleFormation)

    stack instance

  • resource (AttributeStruct)

    the stack resource

  • s_name (String)

    stack resource name

Yield Returns:

  • (Hash)

    key/values to be merged into resource properties

Returns:



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/sparkle_formation/provider/aws.rb', line 51

def apply_deep_nesting(*args, &block)
  outputs = collect_outputs
  nested_stacks(:with_resource).each do |stack, resource|
    unless stack.nested_stacks.empty?
      stack.apply_deep_nesting(*args)
    end
    unless stack.root?
      stack.compile.parameters.keys!.each do |parameter_name|
        next if stack.compile.parameters.set!(parameter_name).stack_unique == true
        if !stack.parent.compile.parameters.data![parameter_name].nil?
          resource.properties.parameters.set!(parameter_name, resource.ref!(parameter_name))
        elsif output_name = output_matched?(parameter_name, outputs.keys)
          next if outputs[output_name] == stack
          stack_output = stack.make_output_available(output_name, outputs)
          resource.properties.parameters.set!(parameter_name, stack_output)
        end
      end
    end
  end
  if block_given?
    extract_templates(&block)
  end
  compile
end

#apply_shallow_nesting(*args) {|resource_name, stack| ... } ⇒ SparkleFormation::SparkleStruct

Apply shallow nesting. This style of nesting will bubble parameters up to the root stack. This type of nesting is the original and now deprecated, but remains for compat issues so any existing usage won’t be automatically busted.

Yield Parameters:

  • resource_name (String)

    name of stack resource

  • stack (SparkleFormation)

    nested stack

Yield Returns:

  • (String)

    Remote URL storage for template

Returns:



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/sparkle_formation/provider/aws.rb', line 85

def apply_shallow_nesting(*args, &block)
  parameters = compile[:parameters] ? compile[:parameters]._dump : {}
  output_map = {}
  nested_stacks(:with_resource, :with_name).each do |_stack, stack_resource, stack_name|
    remap_nested_parameters(compile, parameters, stack_name, stack_resource, output_map)
  end
  if block_given?
    extract_templates(&block)
  end
  compile.parameters parameters
  if args.include?(:bubble_outputs)
    outputs_hash = Hash[
      output_map.map do |name, value|
        [name, {"Value" => {"Fn::GetAtt" => value}}]
      end
    ]
    unless compile.outputs.nil?
      compile._merge(compile._klass_new(outputs_hash))
    else
      compile.outputs outputs_hash
    end
  end
  compile
end

#generate_policyHash

Generate policy for stack

Returns:

  • (Hash)


16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/sparkle_formation/provider/aws.rb', line 16

def generate_policy
  statements = []
  compile.resources.keys!.each do |r_name|
    r_object = compile.resources[r_name]
    if r_object["Policy"]
      r_object["Policy"].keys!.each do |effect|
        statements.push(
          "Effect" => effect.to_s.capitalize,
          "Action" => [r_object["Policy"][effect]].flatten.compact.map { |i| "Update:#{i}" },
          "Resource" => "LogicalResourceId/#{r_name}",
          "Principal" => "*",
        )
      end
      r_object.delete!("Policy")
    end
  end
  statements.push(
    "Effect" => "Allow",
    "Action" => "Update:*",
    "Resource" => "*",
    "Principal" => "*",
  )
  Smash.new("Statement" => statements)
end

#list_type?(type) ⇒ TrueClass, FalseClass

Check if type is a list type

Parameters:

  • type (String)

Returns:

  • (TrueClass, FalseClass)


210
211
212
# File 'lib/sparkle_formation/provider/aws.rb', line 210

def list_type?(type)
  type == "CommaDelimitedList" || type.start_with?("List<")
end

#make_output_available(output_name, outputs) ⇒ Object

Extract output to make available for stack parameter usage at the current depth

Parameters:

  • output_name (String)

    name of output

  • outputs (Hash)

    listing of outputs



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/sparkle_formation/provider/aws.rb', line 116

def make_output_available(output_name, outputs)
  bubble_path = outputs[output_name].root_path - root_path
  drip_path = root_path - outputs[output_name].root_path
  bubble_path.each_slice(2) do |base_sparkle, ref_sparkle|
    next unless ref_sparkle
    base_sparkle.compile.outputs.set!(output_name).set!(
      :value, base_sparkle.compile.attr!(
        ref_sparkle.name, "Outputs.#{output_name}"
      )
    )
  end
  if bubble_path.empty?
    if drip_path.size == 1
      parent = drip_path.first.parent
      if parent && parent.compile.parameters.data![output_name]
        return compile.ref!(output_name)
      end
    end
    raise ArgumentError.new "Failed to detect available bubbling path for output `#{output_name}`. " <<
                              "This may be due to a circular dependency! " <<
                              "(Output Path: #{outputs[output_name].root_path.map(&:name).join(" > ")} " <<
                              "Requester Path: #{root_path.map(&:name).join(" > ")})"
  end
  result = compile.attr!(bubble_path.first.name, "Outputs.#{output_name}")
  if drip_path.size > 1
    parent = drip_path.first.parent
    drip_path.unshift(parent) if parent
    drip_path.each_slice(2) do |base_sparkle, ref_sparkle|
      next unless ref_sparkle
      base_sparkle.compile.resources[ref_sparkle.name].properties.parameters.set!(output_name, result)
      ref_sparkle.compile.parameters.set!(output_name) { type "String" } # TODO: <<<<------ type check and prop
      result = compile.ref!(output_name)
    end
  end
  result
end

#remap_nested_parameters(template, parameters, stack_name, stack_resource, output_map) ⇒ TrueClass

Note:

if parameter includes ‘StackUnique` a new parameter will be added to container stack and it will not use outputs

Extract parameters from nested stacks. Check for previous nested stack outputs that match parameter. If match, set parameter to use output. If no match, check container stack parameters for match. If match, set to use ref. If no match, add parameter to container stack parameters and set to use ref.

Parameters:

  • template (Hash)

    template being processed

  • parameters (Hash)

    top level parameter set being built

  • stack_name (String)

    name of stack resource

  • stack_resource (Hash)

    duplicate of stack resource contents

  • output_map (Hash)

    mapping of output names to required stack output access

Returns:

  • (TrueClass)


167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/sparkle_formation/provider/aws.rb', line 167

def remap_nested_parameters(template, parameters, stack_name, stack_resource, output_map)
  stack_parameters = stack_resource.properties.stack.compile.parameters
  unless stack_parameters.nil?
    stack_parameters._dump.each do |pname, pval|
      if pval["StackUnique"]
        check_name = [stack_name, pname].join
      else
        check_name = pname
      end
      if parameters.keys.include?(check_name)
        if list_type?(parameters[check_name]["Type"])
          new_val = {"Fn::Join" => [",", {"Ref" => check_name}]}
        else
          new_val = {"Ref" => check_name}
        end
        template.resources.set!(stack_name).properties.parameters.set!(pname, new_val)
      elsif output_map[check_name]
        template.resources.set!(stack_name).properties.parameters.set!(
          pname, "Fn::GetAtt" => output_map[check_name],
        )
      else
        if list_type?(pval["Type"])
          new_val = {"Fn::Join" => [",", {"Ref" => check_name}]}
        else
          new_val = {"Ref" => check_name}
        end
        template.resources.set!(stack_name).properties.parameters.set!(pname, new_val)
        parameters[check_name] = pval
      end
    end
  end
  unless stack_resource.properties.stack.compile.outputs.nil?
    stack_resource.properties.stack.compile.outputs.keys!.each do |oname|
      output_map[oname] = [stack_name, "Outputs.#{oname}"]
    end
  end
  true
end

#stack_resource_typeString

Returns Type string for AWS CFN stack resource.

Returns:

  • (String)

    Type string for AWS CFN stack resource



9
10
11
# File 'lib/sparkle_formation/provider/aws.rb', line 9

def stack_resource_type
  "AWS::CloudFormation::Stack"
end