Class: AWS::EC2::SecurityGroup::IpPermissionCollection

Inherits:
Object
  • Object
show all
Defined in:
lib/piculet/ext/ip-permission-collection-ext.rb

Instance Method Summary collapse

Instance Method Details

#aggregateObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/piculet/ext/ip-permission-collection-ext.rb', line 8

def aggregate
  aggregated = nil

  (1..DESC_SECURITY_GROUP_RETRY_TIMES).each do |i|
    begin
      aggregated = {}

      self.each do |perm|
        key = [perm.protocol, perm.port_range]
        aggregated[key] ||= {:ip_ranges => [], :groups => []}
        aggregated[key][:ip_ranges].concat(perm.ip_ranges || [])
        aggregated[key][:groups].concat(perm.groups || [])
      end

      break
    rescue AWS::EC2::Errors::InvalidGroup::NotFound => e
      raise e unless i < DESC_SECURITY_GROUP_RETRY_TIMES
      sleep DESC_SECURITY_GROUP_RETRY_WAIT
    end
  end

  aggregated.map do |key, attrs|
    protocol, port_range = key

    OpenStruct.new({
      :protocol   => protocol,
      :port_range => port_range,
    }.merge(attrs))
  end
end