class BBC::Cosmos::Tools::Cloudformation::Generator

Constants

RESERVED_MAIN_STACK_PARAMS

Public Class Methods

component_exists?(component, config) click to toggle source
# File lib/bbc/cosmos/tools/cloudformation/generator.rb, line 11
def self.component_exists?(component, config)
  config.resources["cloudformation"]["components"][component]
end
create(component, config, options, to_json = false, &block) click to toggle source
# File lib/bbc/cosmos/tools/cloudformation/generator.rb, line 19
def self.create(component, config, options, to_json = false, &block)
  template = CfnDsl::CloudFormationTemplate.new
  template.declare(&block)

  if template.checkRefs || CfnDsl::Errors.errors?
    raise_error template
  else
    hash = JSON.parse(template.to_json)
    hash["Parameters"] ||= {}

    if component_exists?(component, config) && stack_exists?(component, config, options[:stack])
      component_parameters(config.resources, options, component).each do |key, value|
        hash["Parameters"][key]["Default"] = value if hash["Parameters"][key]
      end
    end

    hash.delete_if { |k, v| k == "Parameters" && v == {} }

    to_json ? JSON.pretty_generate(hash) : hash
  end
end
parameters(data, main_stack = true) click to toggle source
# File lib/bbc/cosmos/tools/cloudformation/generator.rb, line 41
def self.parameters(data, main_stack = true)
  if data["Parameters"]
    data["Parameters"].reduce({}) do |object, (key, value)|
      object.tap do |o|
        o[key] = value["Default"] unless reserved_main_stack_param_for(key, main_stack) && value["Default"].nil?
      end
    end
  else
    {}
  end
end
raise_error(template) click to toggle source
# File lib/bbc/cosmos/tools/cloudformation/generator.rb, line 53
def self.raise_error(template)
  if CfnDsl::Errors.errors?
    fail("Errors in template: #{CfnDsl::Errors.errors.join(' # ')}")
  elsif (invalid_references = template.checkRefs)
    fail("Invalid Refs: #{invalid_references.join(' # ')}")
  end
end
stack_exists?(component, config, stack) click to toggle source
# File lib/bbc/cosmos/tools/cloudformation/generator.rb, line 15
def self.stack_exists?(component, config, stack)
  config.resources["cloudformation"]["components"][component][stack]
end

Private Class Methods

component_parameters(resources, options, component) click to toggle source
# File lib/bbc/cosmos/tools/cloudformation/generator.rb, line 78
def self.component_parameters(resources, options, component)
  stack_data = component_stack_details(resources, options, component)
  variants(stack_data, options[:variant])
end
component_stack_details(resources, options, component) click to toggle source
# File lib/bbc/cosmos/tools/cloudformation/generator.rb, line 63
def self.component_stack_details(resources, options, component)
  fail("cloudformation: -> components: node is missing from the environment resource file") unless resources.key?("cloudformation") && resources["cloudformation"].key?("components")

  components = resources["cloudformation"]["components"]
  fail("'#{component}' node missing from cloudformation: -> components: nodes") unless components.key?(component)
  component = components[component]

  fail("'#{options[:stack]}' stack node missing from cloudformation: -> components: -> #{component}:") unless component.key?(options[:stack])
  component[options[:stack]]
end
reserved_main_stack_param_for(key, main_stack) click to toggle source
# File lib/bbc/cosmos/tools/cloudformation/generator.rb, line 83
def self.reserved_main_stack_param_for(key, main_stack)
  main_stack && RESERVED_MAIN_STACK_PARAMS.include?(key)
end
variants(component_meta, variant = nil) click to toggle source
# File lib/bbc/cosmos/tools/cloudformation/generator.rb, line 74
def self.variants(component_meta, variant = nil)
  component_meta.key?("variants") ? component_meta["variants"][variant] : component_meta.fetch("parameters", {})
end