Module: SparkleFormation::Provider::Google

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

Overview

Google specific implementation

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(klass) ⇒ Object

Properly remap dumping methods



93
94
95
96
97
98
# File 'lib/sparkle_formation/provider/google.rb', line 93

def self.extended(klass)
  klass.instance_eval do
    alias :non_google_dump :dump
    alias :dump :google_dump
  end
end

.included(klass) ⇒ Object

Properly remap dumping methods



85
86
87
88
89
90
# File 'lib/sparkle_formation/provider/google.rb', line 85

def self.included(klass)
  klass.class_eval do
    alias_method :non_google_dump, :dump
    alias_method :dump, :google_dump
  end
end

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:



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/sparkle_formation/provider/google.rb', line 124

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, self)
        # NOTE: Only set value if not already explicitly set
        if resource.properties._set(parameter_name).nil?
          resource.properties._set(parameter_name, stack_output)
        end
      end
    end
  end
  if block_given?
    extract_templates(&block)
  end
  self
end

#apply_shallow_nesting(*args, &block) ⇒ Object

Forcibly disable shallow nesting as support for it with Google templates doesn't really make much sense.



149
150
151
# File 'lib/sparkle_formation/provider/google.rb', line 149

def apply_shallow_nesting(*args, &block)
  raise NotImplementedError.new "Shallow nesting is not supported for this provider!"
end

#generate_policyHash

Generate policy for stack

Returns:

  • (Hash)


110
111
112
# File 'lib/sparkle_formation/provider/google.rb', line 110

def generate_policy
  {}
end

#generate_template_files(r_name, r_stack, dump_copy) ⇒ String

Sets stack template files into target copy and extracts parameters into schema files if available

Parameters:

  • r_name (String)

    name used for template file name

  • r_stack (Hash)

    template to store

  • dump_copy (Smash)

    translated dump

Returns:

  • (String)

    new type for stack



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/sparkle_formation/provider/google.rb', line 42

def generate_template_files(r_name, r_stack, dump_copy)
  f_name = "#{r_name}.jinja"
  r_parameters = r_stack.delete("parameters")
  dump_copy[:imports].push(
    Smash.new(
      :name => f_name,
      :content => r_stack,
    )
  )
  if r_parameters
    dump_copy[:imports].push(
      Smash.new(
        :name => "#{f_name}.schema",
        :content => Smash.new.tap { |schema|
          schema.set(:info, :title, "#{f_name} template")
          schema.set(:info, :description, "#{f_name} template schema")
          schema.set(:properties, r_parameters)
        },
      )
    )
  end
  f_name
end

#google_dumpHash

Customized dump to break out templates into consumable structures for passing to the deployment manager API

Returns:

  • (Hash)


70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/sparkle_formation/provider/google.rb', line 70

def google_dump
  result = non_google_dump
  if root?
    dump_copy = Smash.new(:imports => [])
    google_template_extractor(result, dump_copy)
    dump_copy.set(:config, :content, result)
    dump_copy.set(:config, :content, :imports,
                  dump_copy[:imports].map { |i| i[:name] })
    dump_copy.to_hash
  else
    result
  end
end

#google_template_extractor(template_hash, dump_copy, parent_names = []) ⇒ Smash

Extract nested stack templates and store in root level files

Parameters:

  • template_hash (Hash)

    template hash to process

  • dump_copy (Smash)

    translated dump

  • parent_names (Array<String>) (defaults to: [])

    name of parent resources

Returns:

  • (Smash)

    dump_copy



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/sparkle_formation/provider/google.rb', line 19

def google_template_extractor(template_hash, dump_copy, parent_names = [])
  template_hash.fetch("resources", []).each do |t_resource|
    if t_resource["type"] == stack_resource_type
      full_names = parent_names + [t_resource["name"]]
      stack = t_resource["properties"].delete("stack")
      if t_resource["properties"].empty?
        t_resource.delete("properties")
      end
      google_template_extractor(stack, dump_copy, full_names)
      new_type = generate_template_files(full_names.join("-"), stack, dump_copy)
      t_resource["type"] = new_type
    end
  end
  dump_copy
end

#make_output_available(output_name, outputs, source_stack) ⇒ Hash

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

  • source_stack (SparkleFormation)

    requesting stack

Returns:

  • (Hash)

    reference to output value (used for setting parameter)



160
161
162
163
164
165
166
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
# File 'lib/sparkle_formation/provider/google.rb', line 160

def make_output_available(output_name, outputs, source_stack)
  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._stack_output(
        ref_sparkle.name, output_name
      )
    )
  end
  if bubble_path.empty?
    if drip_path.size == 1
      parent = drip_path.first.parent
      if parent && !parent.compile.parameters._set(output_name).nil?
        return compile.parameter!(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 = source_stack.compile._stack_output(bubble_path.first.name, 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.value._set(output_name, result)
      ref_sparkle.compile.parameters._set(output_name).type "string" # TODO: <<<<------ type check and prop
      result = compile._parameter(output_name)
    end
  end
  result
end

#nested?(*_) ⇒ Boolean

Always return as nested since nesting is our final form

Returns:

  • (Boolean)


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

def nested?(*_)
  true
end

#stack_resource_typeString

Note:

Nested templates aren't defined as a specific type thus no "real" type exists. So we'll create a custom one!

Returns Type string for Google Deployment Manager stack resource

Returns:

  • (String)

    Type string for Google Deployment Manager stack resource



103
104
105
# File 'lib/sparkle_formation/provider/google.rb', line 103

def stack_resource_type
  "sparkleformation.stack"
end