module Humidifier module Reservoir # Represents a CloudFormation stack. This contains all of the logic for # interfacing with humidifier to deploy stacks, validate them, and display # them. class Stack # Represents an exported resource in a stack for use in cross-stack # references. Export = Struct.new(:name, :attribute) do def value if attribute.is_a?(String) Humidifier.fn.get_att([name, attribute]) else Humidifier.ref(name) end end end attr_reader :name, :pattern, :prefix, :exports def initialize(name, pattern: nil, prefix: nil) @name = name @pattern = pattern @prefix = prefix @exports = [] end def create_change_set return unless ensure_resources valid? opts = { capabilities: %w[CAPABILITY_IAM CAPABILITY_NAMED_IAM] } humidifier_stack.create_change_set(opts) end def deploy(wait = false) return unless ensure_resources valid? opts = { capabilities: %w[CAPABILITY_IAM CAPABILITY_NAMED_IAM] } humidifier_stack.public_send(wait ? :deploy_and_wait : :deploy, opts) end def resources Reservoir.files_for(name).each_with_object({}) do |filepath, resources| resources.merge!(parse(filepath, File.basename(filepath, '.yml'))) end end def stack_name @stack_name ||= "#{prefix || Reservoir.stack_prefix}#{name}" end def to_cf humidifier_stack.to_cf end def upload humidifier_stack.upload end def valid? humidifier_stack.valid? rescue Aws::CloudFormation::Errors::AccessDenied raise Error, <<~MSG The authenticated AWS profile does not have the requisite permissions to run this command. Ensure the profile has cloudformation:ValidateTemplate. MSG end private def ensure_resources return true if humidifier_stack.resources.any? puts "Refusing to deploy stack #{humidifier_stack.name} with no " \ 'resources' false end def humidifier_stack Humidifier::Stack.new( name: stack_name, description: "Resources for #{stack_name}", resources: resources, outputs: outputs ) end def outputs exports.each_with_object({}) do |export, exported| exported[export.name] = Humidifier::Output.new( value: export.value, export_name: export.name ) end end def parse(filepath, type) mapping = Reservoir.mapping_for(type) return {} if mapping.nil? loaded = YAML.load_file(filepath) return {} unless loaded loaded.each_with_object({}) do |(name, attributes), resources| next if pattern && name !~ pattern attribute = attributes.delete('export') exports << Export.new(name, attribute) if attribute resources[name] = mapping.resource_for(name, attributes) end end end end end