module SparkleFormation::SparkleAttribute::Google

Google specific helper implementations

Constants

CHARACTER_COLLECTION

Public Class Methods

included(klass) click to toggle source
# File lib/sparkle_formation/sparkle_attribute/google.rb, line 11
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

Public Instance Methods

_env(e_name) click to toggle source

Request deployment manager environment variable

@param e_name [String, Symbol] environment variable name @return [SparkleFormation::JinjaExpressionStruct]

# File lib/sparkle_formation/sparkle_attribute/google.rb, line 108
def _env(e_name)
  __t_stringish(e_name)
  _jinja.env[__attribute_key(e_name)]
end
Also aliased as: env!
_google_dump() click to toggle source
# File lib/sparkle_formation/sparkle_attribute/google.rb, line 15
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
_google_dynamic!(name, *args, &block) click to toggle source

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

@see SparkleFormation::SparkleAttribute#dynamic! @note generate unique names using the `:sparkle_unique` argument

# 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
_jinja() click to toggle source

Jinja function string wrapper

@return [SparkleFormation::JinjaExpressionStruct]

# File lib/sparkle_formation/sparkle_attribute/google.rb, line 97
def _jinja
  ::SparkleFormation::JinjaExpressionStruct.new
end
Also aliased as: jinja!, fn!
_property(p_name) click to toggle source

Access a property value supplied to template

@param p_name [String, Symbol] parameter name @return [SparkleFormation::JinjaExpressionStruct] @todo Provide lookup validation that defined p_name is valid

# File lib/sparkle_formation/sparkle_attribute/google.rb, line 120
def _property(p_name)
  __t_stringish(p_name)
  _jinja.properties[__attribute_key(p_name)]
end
Also aliased as: property!, properties!
_ref(r_name) click to toggle source

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

@param r_name [String, Symbol] resource name @return [SparkleFormation::GoogleStruct]

# File lib/sparkle_formation/sparkle_attribute/google.rb, line 83
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
Also aliased as: ref!
_stack_output(stack_name, output_name) click to toggle source

Reference output value from nested stack

@param stack_name [String, Symbol] logical resource name of stack @param output_name [String, Symbol] stack output name @return [SparkleFormation::JinjaExpressionStruct]

# File lib/sparkle_formation/sparkle_attribute/google.rb, line 143
def _stack_output(stack_name, output_name)
  __t_stringish(stack_name)
  __t_stringish(output_name)
  _ref(stack_name)._set(output_name)
end
Also aliased as: stack_output!
_statement(line) click to toggle source

Generate a statement

@param line [String] @return [SparkleFormation::JinjaStatementStruct]

# File lib/sparkle_formation/sparkle_attribute/google.rb, line 132
def _statement(line)
  ::SparkleFormation::JinjaStatementStruct.new(line)
end
Also aliased as: statement!
env!(e_name)
Alias for: _env
fn!()
Alias for: _jinja
jinja!()
Alias for: _jinja
properties!(p_name)
Alias for: _property
property!(p_name)
Alias for: _property
ref!(r_name)
Alias for: _ref
stack_output!(stack_name, output_name)
Alias for: _stack_output
statement!(line)
Alias for: _statement