Module: EC2Launcher::SecurityGroupHandler

Included in:
DSL::Application, DSL::Environment
Defined in:
lib/ec2launcher/security_group_handler.rb

Overview

Helper module for all objects that support EC2 Security Groups.

Instance Method Summary collapse

Instance Method Details

#security_groups(*groups) ⇒ Hash, self

Add or retrieve security groups. Defines the @security_groups instance variable, which contains a Hash of environment names to Arrays of security group names. May define a “default” environment.

Can be defined several different ways:

* String - Adds the named security group to the "default" environment.
* Array - Adds the entire array of security groups to the "default" environment.
* Hash - Keys are environment names (Strings) to security groups. Values of the
         hash can be either a String or an Array. Both are appended to any
         security groups already defined for the environment.


24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/ec2launcher/security_group_handler.rb', line 24

def security_groups(*groups)
  if groups.empty?
    @security_groups
  else
    @security_groups = Hash.new if @security_groups.nil?
    if groups[0].kind_of? Array
      @security_groups[:default] = [] if @security_groups[:default].nil?
      @security_groups[:default] += groups[0]
    elsif groups[0].kind_of? Hash
      groups[0].keys.each do |env_name|
        @security_groups[env_name] = [] if @security_groups[env_name].nil?
        if groups[0][env_name].kind_of? Array
          @security_groups[env_name] += groups[0][env_name]
        else
          @security_groups[env_name] << groups[0][env_name]
        end
      end
    else
      @security_groups[:default] = [] if @security_groups[:default].nil?
      @security_groups[:default] << groups[0].to_s
    end
    self
  end
end

#security_groups_for_environment(environment) ⇒ Object

Retrieves the list of security groups for a given environment. Returns the default set of security groups if the environment isn’t defined. Returns an empty array if the default security group is empty.



52
53
54
55
56
57
58
# File 'lib/ec2launcher/security_group_handler.rb', line 52

def security_groups_for_environment(environment)
  if @security_groups[environment].nil?
    # Environment not found - check default
    return @security_groups[:default].nil? ? [] : @security_groups[:default]
  end
  @security_groups[environment]
end