Class: SparkleFormation

Inherits:
Object
  • Object
show all
Extended by:
Utils::AnimalStrings
Includes:
Bogo::Memoization, Utils::AnimalStrings
Defined in:
lib/sparkle_formation/sparkle_formation.rb,
lib/sparkle_formation.rb,
lib/sparkle_formation/aws.rb,
lib/sparkle_formation/error.rb,
lib/sparkle_formation/utils.rb,
lib/sparkle_formation/sparkle.rb,
lib/sparkle_formation/version.rb,
lib/sparkle_formation/translation.rb,
lib/sparkle_formation/sparkle_struct.rb,
lib/sparkle_formation/translation/heat.rb,
lib/sparkle_formation/sparkle_attribute.rb,
lib/sparkle_formation/sparkle_collection.rb,
lib/sparkle_formation/translation/rackspace.rb

Overview

Formation container

Defined Under Namespace

Modules: SparkleAttribute, Utils Classes: Aws, Cache, Error, Registry, Sparkle, SparkleCollection, SparkleStruct, Translation

Constant Summary collapse

SparklePack =

Alias for interfacing naming

Sparkle
VERSION =

Current library version

Gem::Version.new('1.1.0')
IGNORE_DIRECTORIES =

Returns directory names to ignore.

Returns:

  • (Array<String>)

    directory names to ignore

[
  'components',
  'dynamics',
  'registry'
]
DEFAULT_STACK_RESOURCE =

Returns default stack resource name.

Returns:

  • (String)

    default stack resource name

'AWS::CloudFormation::Stack'
VALID_STACK_RESOURCES =

Returns collection of valid stack resource types.

Returns:

  • (Array<String>)

    collection of valid stack resource types

[DEFAULT_STACK_RESOURCE]
ALLOWED_GENERATION_PARAMETERS =
['type', 'default', 'description', 'multiple', 'prompt_when_nested']
VALID_GENERATION_PARAMETER_TYPES =
['String', 'Number']

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Utils::AnimalStrings

camel, snake

Constructor Details

#initialize(name, options = {}) { ... } ⇒ SparkleFormation

Create new instance

Parameters:

  • name (String, Symbol)

    name of formation

  • options (Hash) (defaults to: {})

    options

Options Hash (options):

  • :sparkle_path (String)

    custom base path

  • :components_directory (String)

    custom components path

  • :dynamics_directory (String)

    custom dynamics path

  • :registry_directory (String)

    custom registry path

  • :parameters (Hash)

    parameters for stack generation

  • :disable_aws_builtins (Truthy, Falsey)

    do not load builtins

Yields:

  • base context



333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
# File 'lib/sparkle_formation/sparkle_formation.rb', line 333

def initialize(name, options={}, &block)
  @name = name.to_sym
  @component_paths = []
  @sparkle = SparkleCollection.new
  @sparkle.set_root(
    Sparkle.new(
      Smash.new.tap{|h|
        s_path = options.fetch(:sparkle_path,
          self.class.custom_paths[:sparkle_path]
        )
        if(s_path)
          h[:root] = s_path
        end
      }
    )
  )
  unless(options[:disable_aws_builtins])
    require 'sparkle_formation/aws'
    SfnAws.load!
  end
  @parameters = set_generation_parameters!(
    options.fetch(:compile_time_parameters,
      options.fetch(:parameters, {})
    )
  )
  @stack_resource_types = (
    VALID_STACK_RESOURCES +
    options.fetch(:stack_resource_types, [])
  ).uniq
  @components = Smash.new
  @load_order = []
  @overrides = []
  @parent = options[:parent]
  if(block)
    load_block(block)
  end
  @compiled = nil
end

Instance Attribute Details

#compile_stateHash

Returns state hash for compile time parameters.

Returns:

  • (Hash)

    state hash for compile time parameters



320
321
322
# File 'lib/sparkle_formation/sparkle_formation.rb', line 320

def compile_state
  @compile_state
end

#componentsArray (readonly)

Returns components to load.

Returns:

  • (Array)

    components to load



310
311
312
# File 'lib/sparkle_formation/sparkle_formation.rb', line 310

def components
  @components
end

#components_directoryString (readonly)

Returns components path.

Returns:

  • (String)

    components path



304
305
306
# File 'lib/sparkle_formation/sparkle_formation.rb', line 304

def components_directory
  @components_directory
end

#dynamics_directoryString (readonly)

Returns dynamics path.

Returns:

  • (String)

    dynamics path



306
307
308
# File 'lib/sparkle_formation/sparkle_formation.rb', line 306

def dynamics_directory
  @dynamics_directory
end

#load_orderArray (readonly)

Returns order of loading.

Returns:

  • (Array)

    order of loading



312
313
314
# File 'lib/sparkle_formation/sparkle_formation.rb', line 312

def load_order
  @load_order
end

#nameSymbol

Returns name of formation.

Returns:

  • (Symbol)

    name of formation



298
299
300
# File 'lib/sparkle_formation/sparkle_formation.rb', line 298

def name
  @name
end

#parametersHash (readonly)

Returns parameters for stack generation.

Returns:

  • (Hash)

    parameters for stack generation



314
315
316
# File 'lib/sparkle_formation/sparkle_formation.rb', line 314

def parameters
  @parameters
end

#parentSparkleFormation

Returns parent stack.

Returns:



316
317
318
# File 'lib/sparkle_formation/sparkle_formation.rb', line 316

def parent
  @parent
end

#registry_directoryString (readonly)

Returns registry path.

Returns:

  • (String)

    registry path



308
309
310
# File 'lib/sparkle_formation/sparkle_formation.rb', line 308

def registry_directory
  @registry_directory
end

#sparkleSparkle (readonly)

Returns parts store.

Returns:



300
301
302
# File 'lib/sparkle_formation/sparkle_formation.rb', line 300

def sparkle
  @sparkle
end

#sparkle_pathString (readonly)

Returns base path.

Returns:

  • (String)

    base path



302
303
304
# File 'lib/sparkle_formation/sparkle_formation.rb', line 302

def sparkle_path
  @sparkle_path
end

#stack_resource_typesArray<String> (readonly)

Returns valid stack resource types.

Returns:

  • (Array<String>)

    valid stack resource types



318
319
320
# File 'lib/sparkle_formation/sparkle_formation.rb', line 318

def stack_resource_types
  @stack_resource_types
end

Class Method Details

.build(base = nil) { ... } ⇒ SparkleStruct

Execute given block within struct context

Parameters:

Yields:

  • block to execute

Returns:



112
113
114
115
116
117
118
119
120
# File 'lib/sparkle_formation/sparkle_formation.rb', line 112

def build(base=nil, &block)
  if(base || block.nil?)
    struct = base || SparkleStruct.new
    struct.instance_exec(&block)
    struct
  else
    block
  end
end

.builtin_insert(dynamic_name, struct, *args, &block) ⇒ SparkleStruct

Insert a builtin dynamic into a context

Parameters:

  • dynamic_name (String, Symbol)

    dynamic name

  • struct (SparkleStruct)

    context for insertion

  • args (Object)

    parameters for dynamic

Returns:



254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
# File 'lib/sparkle_formation/sparkle_formation.rb', line 254

def builtin_insert(dynamic_name, struct, *args, &block)
  if(defined?(SfnAws) && lookup_key = SfnAws.registry_key(dynamic_name))
    _name, _config = *args
    _config ||= {}
    return unless _name
    resource_name = "#{_name}_#{_config.delete(:resource_name_suffix) || dynamic_name}".to_sym
    struct.resources.set!(resource_name)
    new_resource = struct.resources[resource_name]
    new_resource.type lookup_key
    properties = new_resource.properties
    config_keys = _config.keys.zip(_config.keys.map{|k| snake(k).to_s.tr('_', '')})
    SfnAws.resource(dynamic_name, :properties).each do |prop_name|
      key = (config_keys.detect{|k| k.last == snake(prop_name).to_s.tr('_', '')} || []).first
      value = _config[key] if key
      if(value)
        if(value.is_a?(Proc))
          properties[prop_name].to_sym.instance_exec(&value)
        else
          properties.set!(prop_name, value)
        end
      end
    end
    new_resource.instance_exec(&block) if block
    new_resource
  end
end

.compile(path, *args) ⇒ Hashish, SparkleStruct

Compile file

Parameters:

  • path (String)

    path to file

  • args (Object)

    use :sparkle to return struct. provide Hash to pass through when compiling (=> {})

Returns:



93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/sparkle_formation/sparkle_formation.rb', line 93

def compile(path, *args)
  opts = args.detect{|i| i.is_a?(Hash) } || {}
  if(spath = (opts.delete(:sparkle_path) || SparkleFormation.sparkle_path))
    container = Sparkle.new(:root => spath)
    path = container.get(:template, path)[:path]
  end
  formation = self.instance_eval(IO.read(path), path, 1)
  if(args.delete(:sparkle))
    formation
  else
    formation.compile(opts)._dump
  end
end

.components_path=(path = nil) ⇒ String Also known as: components_path

Get/set path to component files

Parameters:

  • path (String) (defaults to: nil)

    path to component files

Returns:

  • (String)

    path to component files



55
56
57
58
59
60
# File 'lib/sparkle_formation/sparkle_formation.rb', line 55

def components_path=(path=nil)
  if(path)
    custom_paths[:components_directory] = path
  end
  custom_paths[:components_directory]
end

.custom_pathsHashish

Returns custom paths.

Returns:

  • (Hashish)

    custom paths



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

def custom_paths
  @_paths ||= SparkleStruct.hashish.new
  @_paths
end

.dynamic(name, args = {}) { ... } ⇒ TrueClass

Define and register new dynamic

Examples:

metadata describes dynamic parameters for _config hash:
:item_name => {:description => 'Defines item name', :type => 'String'}

Parameters:

  • name (String, Symbol)

    name of dynamic

  • args (Hash) (defaults to: {})

    dynamic metadata

Options Hash (args):

  • :parameters (Hash)

    description of _config parameters

Yields:

  • dynamic block

Returns:

  • (TrueClass)


169
170
171
172
173
174
175
# File 'lib/sparkle_formation/sparkle_formation.rb', line 169

def dynamic(name, args={}, &block)
  @dynamics ||= SparkleStruct.hashish.new
  dynamics[name] = SparkleStruct.hashish[
    :block, block, :args, SparkleStruct.hashish[args.map(&:to_a)]
  ]
  true
end

.dynamic_info(name) ⇒ Hashish Also known as: dynamic_information

Metadata for dynamic

Parameters:

  • name (String, Symbol)

    dynamic name

Returns:

  • (Hashish)

    metadata information



181
182
183
184
185
186
187
# File 'lib/sparkle_formation/sparkle_formation.rb', line 181

def dynamic_info(name)
  if(dynamics[name])
    dynamics[name][:args] ||= SparkleStruct.hashish.new
  else
    raise KeyError.new("No dynamic registered with provided name (#{name})")
  end
end

.dynamicsHashish

Returns loaded dynamics.

Returns:

  • (Hashish)

    loaded dynamics



26
27
28
# File 'lib/sparkle_formation/sparkle_formation.rb', line 26

def dynamics
  @dynamics ||= SparkleStruct.hashish.new
end

.dynamics_path=(path = nil) ⇒ String Also known as: dynamics_path

Get/set path to dynamic files

Parameters:

  • path (String) (defaults to: nil)

    path to dynamic files

Returns:

  • (String)

    path to dynamic files



67
68
69
70
71
72
# File 'lib/sparkle_formation/sparkle_formation.rb', line 67

def dynamics_path=(path=nil)
  if(path)
    custom_paths[:dynamics_directory] = path
  end
  custom_paths[:dynamics_directory]
end

.from_hash(hash) ⇒ SparkleStruct

Note:

will do best effort on camel key auto discovery

Convert hash to SparkleStruct instance

Parameters:

  • hash (Hashish)

Returns:



286
287
288
289
290
291
292
# File 'lib/sparkle_formation/sparkle_formation.rb', line 286

def from_hash(hash)
  struct = SparkleStruct.new
  struct._camel_keys_set(:auto_discovery)
  struct._load(hash)
  struct._camel_keys_set(nil)
  struct
end

.insert(dynamic_name, struct, *args, &block) ⇒ SparkleStruct

Insert a dynamic into a context

Parameters:

  • dynamic_name (String, Symbol)

    dynamic name

  • struct (SparkleStruct)

    context for insertion

  • args (Object)

    parameters for dynamic

Returns:



196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/sparkle_formation/sparkle_formation.rb', line 196

def insert(dynamic_name, struct, *args, &block)
  result = false
  begin
    dyn = struct._self.sparkle.get(:dynamic, dynamic_name)
    raise dyn if dyn.is_a?(Exception)
    result = struct.instance_exec(*args, &dyn[:block])
    if(block_given?)
      result.instance_exec(&block)
    end
    result = struct
  rescue Error::NotFound::Dynamic
    result = builtin_insert(dynamic_name, struct, *args, &block)
  end
  unless(result)
    raise "Failed to locate requested dynamic block for insertion: #{dynamic_name} (valid: #{struct._self.sparkle.dynamics.keys.sort.join(', ')})"
  end
  result
end

.load_component(path) ⇒ SparkleStruct

Load component

Parameters:

  • path (String)

    path to component

Returns:



126
127
128
129
# File 'lib/sparkle_formation/sparkle_formation.rb', line 126

def load_component(path)
  self.instance_eval(IO.read(path), path, 1)
  @_struct
end

.load_dynamics!(directory) ⇒ TrueClass

Load all dynamics within a directory

Parameters:

  • directory (String)

Returns:

  • (TrueClass)


135
136
137
138
139
140
141
142
143
144
145
# File 'lib/sparkle_formation/sparkle_formation.rb', line 135

def load_dynamics!(directory)
  @loaded_dynamics ||= []
  Dir.glob(File.join(directory, '*.rb')).each do |dyn|
    dyn = File.expand_path(dyn)
    next if @loaded_dynamics.include?(dyn)
    self.instance_eval(IO.read(dyn), dyn, 1)
    @loaded_dynamics << dyn
  end
  @loaded_dynamics.uniq!
  true
end

.load_registry!(directory) ⇒ TrueClass

Load all registry entries within a directory

Parameters:

  • directory (String)

Returns:

  • (TrueClass)


151
152
153
154
155
156
157
# File 'lib/sparkle_formation/sparkle_formation.rb', line 151

def load_registry!(directory)
  Dir.glob(File.join(directory, '*.rb')).each do |reg|
    reg = File.expand_path(reg)
    require reg
  end
  true
end

.nest(template, struct, *args, &block) ⇒ SparkleStruct

Note:

if symbol is provided for template, double underscores will be used for directory separator and dashes will match underscores

Nest a template into a context

Parameters:

  • template (String, Symbol)

    template to nest

  • struct (SparkleStruct)

    context for nesting

  • args (String, Symbol)

    stringified and underscore joined for name

Returns:



223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# File 'lib/sparkle_formation/sparkle_formation.rb', line 223

def nest(template, struct, *args, &block)
  options = args.detect{|i| i.is_a?(Hash)}
  if(options)
    args.delete(options)
  else
    options = {}
  end
  to_nest = struct._self.sparkle.get(:template, template)
  resource_name = (args.empty? ? template.to_s.gsub('__', '_') : args.map{|a| Bogo::Utility.snake(a)}.join('_')).to_sym
  nested_template = self.compile(to_nest[:path], :sparkle)
  nested_template.parent = struct._self
  nested_template.name = Bogo::Utility.camel(resource_name)
  if(options[:parameters])
    nested_template.compile_state = options[:parameters]
  end
  struct.resources.set!(resource_name) do
    type DEFAULT_STACK_RESOURCE
  end
  struct.resources[resource_name].properties.stack nested_template
  if(block_given?)
    struct.resources[resource_name].instance_exec(&block)
  end
  struct.resources[resource_name]
end

.registry_path=(path = nil) ⇒ String Also known as: registry_path

Get/set path to registry files

Parameters:

  • path (String) (defaults to: nil)

    path to registry files

Returns:

  • (String)

    path to registry files



79
80
81
82
83
84
# File 'lib/sparkle_formation/sparkle_formation.rb', line 79

def registry_path=(path=nil)
  if(path)
    custom_paths[:registry_directory] = path
  end
  custom_paths[:registry_directory]
end

.sparkle_path=(path = nil) ⇒ String Also known as: sparkle_path

Get/set path to sparkle directory

Parameters:

  • path (String) (defaults to: nil)

    path to directory

Returns:

  • (String)

    path to directory



40
41
42
43
44
45
46
47
48
# File 'lib/sparkle_formation/sparkle_formation.rb', line 40

def sparkle_path=(path=nil)
  if(path)
    custom_paths[:sparkle_path] = path
    custom_paths[:components_directory] = File.join(path, 'components')
    custom_paths[:dynamics_directory] = File.join(path, 'dynamics')
    custom_paths[:registry_directory] = File.join(path, 'registry')
  end
  custom_paths[:sparkle_path]
end

Instance Method Details

#apply_deep_nesting(*args) {|stack, resource, s_name| ... } ⇒ Hash

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:

  • (Hash)

    dumped stack



620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
# File 'lib/sparkle_formation/sparkle_formation.rb', line 620

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
    stack.compile.parameters.keys!.each do |parameter_name|
      if(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
  if(block_given?)
    extract_templates(&block)
  end
  compile.dump!
end

#apply_nesting(*args, &block) ⇒ Hash

Note:

see specific version for expected block parameters

Apply nesting logic to stack

Parameters:

  • nest_type (Symbol)

    values: :shallow, :deep (default: :deep)

Returns:

  • (Hash)

    dumped stack



602
603
604
605
606
607
608
# File 'lib/sparkle_formation/sparkle_formation.rb', line 602

def apply_nesting(*args, &block)
  if(args.include?(:shallow))
    apply_shallow_nesting(&block)
  else
    apply_deep_nesting(&block)
  end
end

#apply_shallow_nesting(*args) {|resource_name, stack| ... } ⇒ Hash

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:

  • (Hash)


723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
# File 'lib/sparkle_formation/sparkle_formation.rb', line 723

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
  extract_templates(&block)
  compile.parameters parameters
  if(args.include?(:bubble_outputs))
    outputs_hash = Hash[
      output_map do |name, value|
        [name, {'Value' => {'Fn::GetAtt' => value}}]
      end
    ]
    if(compile.outputs)
      compile._merge(SparkleStruct.new(outputs_hash))
    else
      compile.outputs output_hash
    end
  end
  compile.dump!
end

#block(block) ⇒ TrueClass Also known as: load_block

Add block to load order

Parameters:

  • block (Proc)

Returns:

  • (TrueClass)


454
455
456
457
458
# File 'lib/sparkle_formation/sparkle_formation.rb', line 454

def block(block)
  @components[:__base__] = block
  @load_order << :__base__
  true
end

#collect_outputs(*args) ⇒ Smash<output_name:SparkleFormation>

Returns:

  • (Smash<output_name:SparkleFormation>)


747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
# File 'lib/sparkle_formation/sparkle_formation.rb', line 747

def collect_outputs(*args)
  if(args.include?(:force) || root?)
    if(compile.outputs)
      outputs = Smash[
        compile.outputs.keys!.zip(
          [self] * compile.outputs.keys!.size
        )
      ]
    else
      outputs = Smash.new
    end
    nested_stacks.each do |nested_stack|
      outputs = nested_stack.collect_outputs(:force).merge(outputs)
    end
    outputs
  else
    root.collect_outputs(:force)
  end
end

#compile(args = {}) ⇒ SparkleStruct

Compile the formation

Parameters:

  • args (Hash) (defaults to: {})

Options Hash (args):

  • :state (Hash)

    local state parameters

Returns:



492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
# File 'lib/sparkle_formation/sparkle_formation.rb', line 492

def compile(args={})
  if(args.has_key?(:state))
    @compile_state = args[:state]
    unmemoize(:compile)
  end
  memoize(:compile) do
    set_compile_time_parameters!
    compiled = SparkleStruct.new
    compiled._set_self(self)
    if(compile_state)
      compiled.set_state!(compile_state)
    end
    @load_order.each do |key|
      self.class.build(compiled, &components[key])
    end
    @overrides.each do |override|
      if(override[:args] && !override[:args].empty?)
        compiled._set_state(override[:args])
      end
      self.class.build(compiled, &override[:block])
    end
    if(compile_state)
      compiled.outputs.compile_state.value MultiJson.dump(compile_state)
    end
    compiled
  end
end

#compile_time_parameter_setter {|| ... } ⇒ Proc, NilClass

Get or set the compile time parameter setting block. If a get request the ancestor path will be searched to root

Yields:

  • block to set compile time parameters

Yield Parameters:

Returns:

  • (Proc, NilClass)


412
413
414
415
416
417
418
419
420
421
422
# File 'lib/sparkle_formation/sparkle_formation.rb', line 412

def compile_time_parameter_setter(&block)
  if(block)
    @compile_time_parameter_setter = block
  else
    if(@compile_time_parameter_setter)
      @compile_time_parameter_setter
    else
      parent.nil? ? nil : parent.compile_time_parameter_setter
    end
  end
end

#dumpHash

Returns dumped hash.

Returns:

  • (Hash)

    dumped hash



819
820
821
# File 'lib/sparkle_formation/sparkle_formation.rb', line 819

def dump
  MultiJson.load(self.to_json)
end

#extract_templates {|stack, resource, s_name| ... } ⇒ Object

Extract and process nested stacks

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



697
698
699
# File 'lib/sparkle_formation/sparkle_formation.rb', line 697

def extract_templates(&block)
  stack_template_extractor(nested_stacks(:with_resource, :with_name), &block)
end

#generate_policyHash

TODO:

this is very AWS specific, so make this easy for swapping

Generate policy for stack

Returns:

  • (Hash)


572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
# File 'lib/sparkle_formation/sparkle_formation.rb', line 572

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

#includes_policies?(stack_hash = nil) ⇒ TrueClass, FalseClass

Returns policies defined.

Returns:

  • (TrueClass, FalseClass)

    policies defined



561
562
563
564
565
566
# File 'lib/sparkle_formation/sparkle_formation.rb', line 561

def includes_policies?(stack_hash=nil)
  stack_hash = compile.dump! unless stack_hash
  stack_hash.fetch('Resources', {}).any? do |name, resource|
    resource.has_key?('Policy')
  end
end

#isolated_nests?(stack_hash = nil) ⇒ TrueClass, FalseClass

Returns includes only nested stacks.

Returns:

  • (TrueClass, FalseClass)

    includes only nested stacks



553
554
555
556
557
558
# File 'lib/sparkle_formation/sparkle_formation.rb', line 553

def isolated_nests?(stack_hash=nil)
  stack_hash = compile.dump! unless stack_hash
  stack_hash.fetch('Resources', {}).all? do |name, resource|
    stack_resource_type?(resource['Type'])
  end
end

#load(*args) ⇒ self

Load components into instance

Parameters:

  • args (String, Symbol)

    Symbol component names or String paths

Returns:

  • (self)


465
466
467
468
469
470
471
472
473
474
475
476
# File 'lib/sparkle_formation/sparkle_formation.rb', line 465

def load(*args)
  args.each do |thing|
    key = File.basename(thing.to_s).sub('.rb', '')
    if(thing.is_a?(String))
      components[key] = self.class.load_component(thing)
    else
      components[key] = sparkle.get(:component, thing)[:block]
    end
    @load_order << key
  end
  self
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



658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
# File 'lib/sparkle_formation/sparkle_formation.rb', line 658

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

#nested?(stack_hash = nil) ⇒ TrueClass, FalseClass

Returns includes nested stacks.

Returns:

  • (TrueClass, FalseClass)

    includes nested stacks



545
546
547
548
549
550
# File 'lib/sparkle_formation/sparkle_formation.rb', line 545

def nested?(stack_hash=nil)
  stack_hash = compile.dump! unless stack_hash
  !!stack_hash.fetch('Resources', {}).detect do |r_name, resource|
    stack_resource_type?(resource['Type'])
  end
end

#nested_stacks(*args) ⇒ Array<SparkleFormation>

Returns:



529
530
531
532
533
534
535
536
537
538
539
540
541
542
# File 'lib/sparkle_formation/sparkle_formation.rb', line 529

def nested_stacks(*args)
  compile.resources.keys!.map do |key|
    if(stack_resource_type?(compile.resources[key].type))
      result = [compile.resources[key].properties.stack]
      if(args.include?(:with_resource))
        result.push(compile[:resources][key])
      end
      if(args.include?(:with_name))
        result.push(key)
      end
      result.size == 1 ? result.first : result
    end
  end.compact
end

#output_matched?(p_name, output_names) ⇒ String, NilClass

Note:

will auto downcase name prior to comparison

Check if parameter name matches an output name

Parameters:

  • p_name (String, Symbol)

    parameter name

  • output_names (Array<String>)

    list of available outputs

Returns:

  • (String, NilClass)

    matching output name



646
647
648
649
650
# File 'lib/sparkle_formation/sparkle_formation.rb', line 646

def output_matched?(p_name, output_names)
  output_names.detect do |o_name|
    Bogo::Utility.snake(o_name).tr('_', '') == Bogo::Utility.snake(p_name).tr('_', '')
  end
end

#overrides(args = {}) { ... } ⇒ Object

Registers block into overrides

Parameters:

  • args (Hash) (defaults to: {})

    optional arguments to provide state

Yields:

  • override block



482
483
484
485
# File 'lib/sparkle_formation/sparkle_formation.rb', line 482

def overrides(args={}, &block)
  @overrides << {:args => args, :block => block}
  self
end

#recompileSparkleStruct

Clear compiled stack if cached and perform compilation again

Returns:



523
524
525
526
# File 'lib/sparkle_formation/sparkle_formation.rb', line 523

def recompile
  unmemoize(:compile)
  compile
end

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

Note:

if parameter has 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)


781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
# File 'lib/sparkle_formation/sparkle_formation.rb', line 781

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(parameters[check_name]['Type'] == 'CommaDelimitedList')
          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(pval['Type'] == 'CommaDelimitedList')
          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

#rootSparkleFormation

Returns root stack.

Returns:



381
382
383
384
385
386
387
# File 'lib/sparkle_formation/sparkle_formation.rb', line 381

def root
  if(parent)
    parent.root
  else
    self
  end
end

#root?TrueClass, FalseClass

Returns current stack is root.

Returns:

  • (TrueClass, FalseClass)

    current stack is root



399
400
401
# File 'lib/sparkle_formation/sparkle_formation.rb', line 399

def root?
  root == self
end

#root_pathArray<SparkleFormation] path to root

Returns Array<SparkleFormation] path to root.

Returns:



390
391
392
393
394
395
396
# File 'lib/sparkle_formation/sparkle_formation.rb', line 390

def root_path
  if(parent)
    [*parent.root_path, self].compact
  else
    [self]
  end
end

#set_compile_time_parameters!Object

Set the compile time parameters for the stack if the setter proc is available



426
427
428
429
430
# File 'lib/sparkle_formation/sparkle_formation.rb', line 426

def set_compile_time_parameters!
  if(compile_time_parameter_setter)
    compile_time_parameter_setter.call(self)
  end
end

#set_generation_parameters!(params) ⇒ Hash

Validation parameters used for template generation to ensure they are in the expected format

Parameters:

  • params (Hash)

    parameter set

Returns:

  • (Hash)

    parameter set



438
439
440
441
442
443
444
445
446
447
448
# File 'lib/sparkle_formation/sparkle_formation.rb', line 438

def set_generation_parameters!(params)
  params.each do |name, value|
    unless(value.is_a?(Hash))
      raise TypeError.new("Expecting `Hash` type. Received `#{value.class}`")
    end
    if(key = value.keys.detect{|k| !ALLOWED_GENERATION_PARAMETERS.include?(k.to_s) })
      raise ArgumentError.new("Invalid generation parameter key provided `#{key}`")
    end
  end
  params
end

#stack_resource_type?(type) ⇒ TrueClass, FalseClass

Check if type is a registered stack type

Parameters:

  • type (String)

Returns:

  • (TrueClass, FalseClass)


376
377
378
# File 'lib/sparkle_formation/sparkle_formation.rb', line 376

def stack_resource_type?(type)
  stack_resource_types.include?(type)
end

#stack_template_extractor(x_stacks, &block) ⇒ Object

Run the stack extraction

Parameters:



704
705
706
707
708
709
710
711
712
# File 'lib/sparkle_formation/sparkle_formation.rb', line 704

def stack_template_extractor(x_stacks, &block)
  x_stacks.each do |stack, resource, s_name|
    unless(stack.nested_stacks.empty?)
      stack_template_extractor(stack.nested_stacks(:with_resource, :with_name), &block)
    end
    resource.properties.set!(:stack, stack.compile.dump!)
    block.call(s_name, stack, resource)
  end
end

#to_json(*args) ⇒ String

Returns dumped hash JSON.

Returns:

  • (String)

    dumped hash JSON



824
825
826
# File 'lib/sparkle_formation/sparkle_formation.rb', line 824

def to_json(*args)
  MultiJson.dump(compile.dump!, *args)
end