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 # rubocop:disable Metrics/ClassLength # 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 if !ensure_resources('change') || !valid? opts = { capabilities: %w[CAPABILITY_IAM CAPABILITY_NAMED_IAM] } humidifier_stack.create_change_set(opts) end def deploy(wait = false, parameter_values = {}) return if !ensure_resources('deploy') || !valid? opts = { capabilities: %w[CAPABILITY_IAM CAPABILITY_NAMED_IAM], parameters: parameter_values } humidifier_stack.public_send(wait ? :deploy_and_wait : :deploy, opts) end def parameters @parameters ||= begin parameter_filepath = Reservoir.files_for(name).detect do |filepath| File.basename(filepath, '.yml') == 'parameters' end parameter_filepath ? ParameterList.from(parameter_filepath) : {} end end def resources Reservoir.files_for(name).each_with_object({}) do |filepath, resources| basename = File.basename(filepath, '.yml') # Explicitly skip past parameters so we can pull them out later next if basename == 'parameters' resources.merge!(parse(filepath, basename)) end end def stack_name @stack_name ||= "#{prefix || Reservoir.stack_prefix}#{name}" end def to_cf humidifier_stack.to_cf end def upload return if !ensure_resources('upload') || !valid? 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(action = 'deploy') return true if humidifier_stack.resources.any? puts "Refusing to #{action} 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, parameters: parameters ) 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