Class: Humidifier::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/humidifier/config.rb,
lib/humidifier/config/mapper.rb,
lib/humidifier/config/mapping.rb

Overview

A container for user params

Defined Under Namespace

Classes: Mapper, Mapping

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfig

Returns a new instance of Config.



27
28
29
30
# File 'lib/humidifier/config.rb', line 27

def initialize
  @mappings = {}
  @stack_path = "."
end

Instance Attribute Details

#force_uploadObject

If true, always upload the CloudFormation template to the configured S3 destination. A useful option if you're going to be deploying multiple copies of a template or you just generally want a backup.



9
10
11
# File 'lib/humidifier/config.rb', line 9

def force_upload
  @force_upload
end

#s3_bucketObject

The S3 bucket to which to deploy CloudFormation templates when always_upload is set to true or the template is too big for a string literal.



14
15
16
# File 'lib/humidifier/config.rb', line 14

def s3_bucket
  @s3_bucket
end

#s3_prefixObject

An optional prefix for the JSON file names.



17
18
19
# File 'lib/humidifier/config.rb', line 17

def s3_prefix
  @s3_prefix
end

#stack_pathObject

The path to the various directories containing the YAML files representing stacks. If blank, it's assumed to be the current working direction from which the CLI is executing.



22
23
24
# File 'lib/humidifier/config.rb', line 22

def stack_path
  @stack_path
end

#stack_prefixObject

An optional prefix for the stack names before they get uploaded to AWS.



25
26
27
# File 'lib/humidifier/config.rb', line 25

def stack_prefix
  @stack_prefix
end

Instance Method Details

#files_for(name) ⇒ Object



32
33
34
# File 'lib/humidifier/config.rb', line 32

def files_for(name)
  Dir["#{stack_path}/#{name}/*.yml"]
end

#map(type, opts = {}, &block) ⇒ Object

#map is a declaration of a link between a file name and a mapper configuration. It is used to declare the manner in which a set of attributes read from a resource file is converted into instantiations of that type.

For more information about the mapping DSL and how attributes get converted into props, see the Humidifier::Config::Mapper class.

== Basic mapping

For the most basic of mappings, you can just map a file name to a resource, which effectively means that each attribute you provide must map directly to an AWS attribute for that resource, and that no additional attributes will be provided. For example, the following code indicates that files named routes.yml will contain AWS::EC2::Route resources, and that every attribute read will directly correspond to one from AWS:

Humidifier.configure do |config|
  config.map :routes, to: 'EC2::Route'
end

== Using the DSL

For mappings for which you want to use the Humidifier::Config::Mapper DSL, you can pass a block which will get used to create a new mapper class. This is useful for shorter mapper declarations. For example, in the following code we map files named instance_profiles.yml to AWS::IAM::InstanceProfile resources. In that mapping we default the path prop to "/" and we allow the roles prop to just pass a list of roles as an array, which we then convert into CloudFormation references.

Humidifier.configure do |config|
  config.map :instance_profiles, to: 'IAM::InstanceProfile' do
    defaults do |_|
      { path: '/' }
    end

    attribute :roles do |names|
      { roles: names.map { |name| Humidifier.ref(name) } }
    end
  end
end

== Reusing mappers

Finally, if you want to pull the mapper out for reuse, testing, or just separation of code, you can pass the :using key with a mapper as a value. This will cause the given file type to be mapped using whatever class you provided. For example, the following code creates a mapper that automatically tags the resource with the logical name from the stack. It then configures network ACLs to use that so that all network ACL resource declarations automatically have a tag on them with their name.

class NameToTag < Humidifier::Config::Mapper
  defaults do |logical_name|
    { tags: [{ key: 'Name', value: logical_name }] }
  end
end

Humidifier.configure do |config|
  config.map :network_acls, to: 'EC2::NetworkAcl', using: NameToTag
end


99
100
101
# File 'lib/humidifier/config.rb', line 99

def map(type, opts = {}, &block)
  mappings[type.to_sym] = Mapping.new(opts, &block)
end

#mapping_for(type) ⇒ Object



103
104
105
# File 'lib/humidifier/config.rb', line 103

def mapping_for(type)
  mappings[type.to_sym]
end

#stack_namesObject



115
116
117
118
119
# File 'lib/humidifier/config.rb', line 115

def stack_names
  Dir["#{stack_path}/*"].each_with_object([]) do |name, names|
    names << File.basename(name) if File.directory?(name)
  end
end