1 2 3 4 5 6 7 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 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
|
module Fog
module AWS
class IAM
class PagedCollection < Fog::Collection
def self.inherited(klass)
klass.send(:attribute, :truncated, :aliases => 'IsTruncated', :type => :boolean)
klass.send(:attribute, :marker, :aliases => 'Marker')
super
end
def each_entry(*args, &block)
to_a.each(*args, &block)
end
def each(options={})
limit = options[:limit] || 100
if !block_given?
self
else
subset = dup.all
subset.each_entry { |f| yield f }
while subset.truncated
subset.
all(:marker => subset.marker, :limit => limit).
each_entry { |f| yield f }
end
self
end
end
protected
def page_params(options={})
marker = options.fetch(:marker) { options.fetch('Marker') { self.marker } }
limit = options.fetch(:limit) { options['MaxItems'] }
params = {}
if marker && !marker.empty?
params.merge!('Marker' => marker)
end
if limit
params.merge!('MaxItems' => limit)
end
params
end
end
end
end
end
|