Skip to content

Commit

Permalink
Fix Fog::Compute::Google::Servers#all paging
Browse files Browse the repository at this point in the history
If there are more instances than will fit in a page a next_page_token is
returned.  This has to be passed in to subsequent calls in order to
retrieve the full list of servers.
  • Loading branch information
agrare committed Sep 27, 2023
1 parent c0b13ce commit bd4fa07
Showing 1 changed file with 22 additions and 8 deletions.
30 changes: 22 additions & 8 deletions lib/fog/compute/google/models/servers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,31 @@ def all(zone: nil, filter: nil, max_results: nil,
:page_token => page_token
}

if zone
data = service.list_servers(zone, **opts).to_h[:items] || []
else
data = []
service.list_aggregated_servers(**opts).items.each_value do |scoped_lst|
if scoped_lst && scoped_lst.instances
data.concat(scoped_lst.instances.map(&:to_h))
items = []
next_page_token = nil

loop do
if zone
data = service.list_servers(zone, **opts)
next_items = data.to_h[:items] || []
items.concat(next_items)
next_page_token = data.next_page_token
else
data = service.list_aggregated_servers(**opts)
data.items.each_value do |scoped_lst|
if scoped_lst && scoped_lst.instances
items.concat(scoped_lst.instances.map(&:to_h))
end
end
next_page_token = data.next_page_token
end

break if next_page_token.nil? || next_page_token.empty?

opts[:page_token] = next_page_token
end
load(data)

load(items)
end

# TODO: This method needs to take self_links as well as names
Expand Down

0 comments on commit bd4fa07

Please sign in to comment.