Module: SparkleFormation::SparkleAttribute::Google

Included in:
SparkleFormation::SparkleStruct::Google
Defined in:
lib/sparkle_formation/sparkle_attribute/google.rb

Overview

Google specific helper implementations

Constant Summary

CHARACTER_COLLECTION =
('a'..'z').to_a

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(klass) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/sparkle_formation/sparkle_attribute/google.rb', line 12

def self.included(klass)
  klass.const_set(:CAMEL_STYLE, :no_leading)

  klass.class_eval do
    def _google_dump
      result = _non_google_attribute_struct_dump
      if(_parent.nil?)
        sparkle_root = {}
        if(result.key?('resources') && result['resources'].is_a?(Hash))
          resources = result.delete('resources') || {}
          sparkle_root = (resources.delete(_self.name) || {}).fetch('properties', {})
          result['resources'] = resources.map do |r_name, r_content|
            r_content.merge('name' => r_name)
          end
          outputs = result.delete('outputs') || {}
          result['outputs'] = outputs.map do |o_name, o_content|
            o_content.merge('name' => o_name)
          end
          if(_self.parent.nil?)
            result = {
              'resources' => [{
                'name' => _self.name,
                'type' => _self.stack_resource_type,
                'properties' => {
                  'stack' => result
                }.merge(sparkle_root)
              }]
            }
          end
        end
      end
      result
    end
    alias_method :_non_google_attribute_struct_dump, :_dump
    alias_method :_dump, :_google_dump
    alias_method :dump!, :_google_dump
    alias_method :_non_google_dynamic!, :dynamic!
    alias_method :dynamic!, :_google_dynamic!
  end
end

Instance Method Details

#_env(e_name) ⇒ SparkleFormation::JinjaExpressionStruct Also known as: env!

Request deployment manager environment variable

Parameters:

  • e_name (String, Symbol)

    environment variable name

Returns:



107
108
109
110
# File 'lib/sparkle_formation/sparkle_attribute/google.rb', line 107

def _env(e_name)
  __t_stringish(e_name)
  _jinja.env[__attribute_key(e_name)]
end

#_google_dynamic!(name, *args, &block) ⇒ Object

Note:

generate unique names using the :sparkle_unique argument

Customized dynamic to provide automatic unique name generation for built in resources



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/sparkle_formation/sparkle_attribute/google.rb', line 58

def _google_dynamic!(name, *args, &block)
  if(args.delete(:sparkle_unique))
    seed = Zlib.crc32(_self.root_path.map(&:name).join('-'))
    gen = Random.new(seed)
    suffix = Array.new(10) do
      CHARACTER_COLLECTION.at(
        gen.rand(CHARACTER_COLLECTION.size
        )
      )
    end.join
    config_hash = args.detect{|a| a.is_a?(Hash)}
    unless(config_hash)
      config_hash = {}
      args.push(config_hash)
    end
    config_hash[:resource_name_suffix] = "-#{suffix}"
    args[0] = args.first.to_s.tr('_', '-').downcase
  end
  _non_google_dynamic!(name, *args, &block)
end

#_jinjaSparkleFormation::JinjaExpressionStruct Also known as: jinja!, fn!

Jinja function string wrapper



97
98
99
# File 'lib/sparkle_formation/sparkle_attribute/google.rb', line 97

def _jinja
  ::SparkleFormation::JinjaExpressionStruct.new
end

#_property(p_name) ⇒ SparkleFormation::JinjaExpressionStruct Also known as: property!, properties!

TODO:

Provide lookup validation that defined p_name is valid

Access a property value supplied to template

Parameters:

  • p_name (String, Symbol)

    parameter name

Returns:



118
119
120
121
# File 'lib/sparkle_formation/sparkle_attribute/google.rb', line 118

def _property(p_name)
  __t_stringish(p_name)
  _jinja.properties[__attribute_key(p_name)]
end

#_ref(r_name) ⇒ SparkleFormation::GoogleStruct Also known as: ref!

Reference generator. Will lookup defined resource name to validate exist.

Parameters:

  • r_name (String, Symbol)

    resource name

Returns:



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

def _ref(r_name)
  __t_stringish(r_name)
  if(_root.resources.set!(r_name).nil?)
    ::Kernel.raise ::SparkleFormation::Error::NotFound::Resource.new(:name => r_name)
  else
    ::SparkleFormation::GoogleStruct.new('ref').set!(__attribute_key(r_name))
  end
end

#_stack_output(stack_name, output_name) ⇒ SparkleFormation::JinjaExpressionStruct Also known as: stack_output!

Reference output value from nested stack

Parameters:

  • stack_name (String, Symbol)

    logical resource name of stack

  • output_name (String, Symbol)

    stack output name

Returns:



139
140
141
142
143
# File 'lib/sparkle_formation/sparkle_attribute/google.rb', line 139

def _stack_output(stack_name, output_name)
  __t_stringish(stack_name)
  __t_stringish(output_name)
  _ref(stack_name)._set(output_name)
end

#_statement(line) ⇒ SparkleFormation::JinjaStatementStruct Also known as: statement!

Generate a statement

Parameters:

  • line (String)

Returns:



129
130
131
# File 'lib/sparkle_formation/sparkle_attribute/google.rb', line 129

def _statement(line)
  ::SparkleFormation::JinjaStatementStruct.new(line)
end