From 708fbe0a7616ed16b4d4712f6de0e6def54c9651 Mon Sep 17 00:00:00 2001 From: James Bence Date: Tue, 2 Jul 2013 10:00:00 -0700 Subject: [PATCH] Restore implementation of each/all --- lib/fog/aws/models/rds/snapshots.rb | 34 ++++++++++++++++++++--------- 1 file changed, 24 insertions(+), 10 deletions(-) diff --git a/lib/fog/aws/models/rds/snapshots.rb b/lib/fog/aws/models/rds/snapshots.rb index 751c8c07d..801945ae1 100644 --- a/lib/fog/aws/models/rds/snapshots.rb +++ b/lib/fog/aws/models/rds/snapshots.rb @@ -21,19 +21,33 @@ module Fog super end - # This will return a single page based on the current or provided filters, - # updating the filters with the marker for the next page. Calling this repeatedly - # will iterate through pages. + # This method does NOT return all snapshots. Its implementation deliberately returns a single page + # of results for any one call. It will return a single page based on the current or provided filters, + # updating the filters with the marker for the next page. Calling this repeatedly will iterate + # through pages. See the implementation of each for an example of such iteration. + # + # It is arguably incorrect for the method not to return all snapshots, particularly considering the + # implementation in the corresponding 'elb' files. But this implementation has been released, and + # backwards-compatibility requires leaving it as implemented. def all(filters = filters) self.filters.merge!(filters) - data = [] - begin - result = service.describe_db_snapshots(filters).body['DescribeDBSnapshotsResult'] - self.filters[:marker] = result['Marker'] - data.concat(result['DBSnapshots']) - end while self.filters[:marker] - load(data) + page = service.describe_db_snapshots(filters).body['DescribeDBSnapshotsResult'] + self.filters[:marker] = page['Marker'] + load(page['DBSnapshots']) + end + + # This will execute a block for each snapshot, fetching new pages of snapshots as required. + def each(filters = filters) + if block_given? + begin + page = self.all(filters) + # We need to explicitly use the base 'each' method here on the page, otherwise we get infinite recursion + base_each = Fog::Collection.instance_method(:each) + base_each.bind(page).call { |snapshot| yield snapshot } + end while self.filters[:marker] + end + self end def get(identity)