Class: Piculet::EC2Wrapper::SecurityGroupCollection::SecurityGroup::PermissionCollection

Inherits:
Object
  • Object
show all
Includes:
Logger::ClientHelper
Defined in:
lib/piculet/wrapper/permission.rb,
lib/piculet/wrapper/permission-collection.rb

Defined Under Namespace

Classes: Permission

Instance Method Summary collapse

Methods included from Logger::ClientHelper

#log

Constructor Details

#initialize(security_group, direction, options) ⇒ PermissionCollection

Returns a new instance of PermissionCollection.



8
9
10
11
12
13
# File 'lib/piculet/wrapper/permission-collection.rb', line 8

def initialize(security_group, direction, options)
  @security_group = security_group
  @permissions = security_group.send("#{direction}_ip_permissions")
  @direction = direction
  @options = options
end

Instance Method Details

#authorize(protocol, ports, sources, opts = {}) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/piculet/wrapper/permission-collection.rb', line 23

def authorize(protocol, ports, sources, opts = {})
  log(:info, "  authorize #{format_sources(sources)}", opts.fetch(:log_color, :green))

  unless @options.dry_run
    sources = normalize_sources(sources)

    case @direction
    when :ingress
      @security_group.authorize_ingress(protocol, ports, *sources)
      @options.updated = true
    when :egress
      sources.push(:protocol => protocol, :ports => ports)
      @security_group.authorize_egress(*sources)
      @options.updated = true
    end
  end
end

#create(protocol, port_range, dsl) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/piculet/wrapper/permission-collection.rb', line 59

def create(protocol, port_range, dsl)
  dsl_ip_ranges = dsl.ip_ranges || []
  dsl_groups = (dsl.groups || []).map do |i|
    i.kind_of?(Array) ? i : [@options.ec2.owner_id, i]
  end

  sources = dsl_ip_ranges + dsl_groups

  unless sources.empty?
    log(:info, 'Create Permission', :cyan, "#{log_id} > #{protocol} #{port_range}")
    authorize(protocol, port_range, sources, :log_color => :cyan)
  end
end

#eachObject



15
16
17
18
19
20
21
# File 'lib/piculet/wrapper/permission-collection.rb', line 15

def each
  perm_list = @permissions ? @permissions.aggregate : []

  perm_list.each do |perm|
    yield(Permission.new(perm, self, @options))
  end
end

#log_idObject



73
74
75
76
77
78
79
80
81
82
# File 'lib/piculet/wrapper/permission-collection.rb', line 73

def log_id
  vpc = @security_group.vpc_id || :classic
  name = @security_group.name

  if @security_group.owner_id and not @options.ec2.own?(@security_group.owner_id)
    name = "#{@security_group.owner_id}/#{name}"
  end

  "#{vpc} > #{name}(#{@direction})"
end

#revoke(protocol, ports, sources, opts = {}) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/piculet/wrapper/permission-collection.rb', line 41

def revoke(protocol, ports, sources, opts = {})
  log(:info, "  revoke #{format_sources(sources)}", opts.fetch(:log_color, :green))

  unless @options.dry_run
    sources = normalize_sources(sources)

    case @direction
    when :ingress
      @security_group.revoke_ingress(protocol, ports, *sources)
      @options.updated = true
    when :egress
      sources.push(:protocol => protocol, :ports => ports)
      @security_group.revoke_egress(*sources)
      @options.updated = true
    end
  end
end