From a1bc3423eb578b8549bbb2772bd4e1dea4daa3cc Mon Sep 17 00:00:00 2001 From: Adam Grare Date: Tue, 3 Oct 2023 11:56:06 -0400 Subject: [PATCH] Fix missing paging on models `#all` methods Most of the models' `#all` methods are missing paging which limits the number of results to a maximum of 500. --- lib/fog/compute/google/models/addresses.rb | 25 ++++++++++++------ lib/fog/compute/google/models/disk_types.rb | 24 ++++++++++++----- lib/fog/compute/google/models/disks.rb | 24 ++++++++++++----- lib/fog/compute/google/models/firewalls.rb | 13 ++++++++-- .../compute/google/models/forwarding_rules.rb | 26 ++++++++++++------- .../compute/google/models/global_addresses.rb | 13 ++++++++-- .../google/models/global_forwarding_rules.rb | 13 ++++++++-- .../google/models/http_health_checks.rb | 15 ++++++++--- lib/fog/compute/google/models/images.rb | 23 ++++++++++------ .../google/models/instance_group_managers.rb | 26 ++++++++++++------- .../compute/google/models/instance_groups.rb | 22 ++++++++++------ 11 files changed, 158 insertions(+), 66 deletions(-) diff --git a/lib/fog/compute/google/models/addresses.rb b/lib/fog/compute/google/models/addresses.rb index 2232bdab8b..69f66f0a10 100644 --- a/lib/fog/compute/google/models/addresses.rb +++ b/lib/fog/compute/google/models/addresses.rb @@ -11,16 +11,25 @@ def all(region: nil, filter: nil, max_results: nil, order_by: nil, page_token: n :order_by => order_by, :page_token => page_token } - - if region - data = service.list_addresses(region, **opts).items || [] - else - data = [] - service.list_aggregated_addresses(**opts).items.each_value do |scoped_list| - data.concat(scoped_list.addresses) if scoped_list && scoped_list.addresses + items = [] + next_page_token = nil + loop do + if region + data = service.list_addresses(region, **opts) + next_items = data.items || [] + items.concat(next_items) + next_page_token = data.next_page_token + else + data = service.list_aggregated_addresses(**opts) + data.items.each_value do |scoped_list| + items.concat(scoped_list.addresses) if scoped_list && scoped_list.addresses + 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.map(&:to_h)) + load(items.map(&:to_h)) end def get(identity, region = nil) diff --git a/lib/fog/compute/google/models/disk_types.rb b/lib/fog/compute/google/models/disk_types.rb index 88021d89cc..e2ac936bf9 100644 --- a/lib/fog/compute/google/models/disk_types.rb +++ b/lib/fog/compute/google/models/disk_types.rb @@ -12,15 +12,25 @@ def all(zone: nil, filter: nil, max_results: nil, :order_by => order_by, :page_token => page_token } - if zone - data = service.list_disk_types(zone, **opts).items - else - data = [] - service.list_aggregated_disk_types(**opts).items.each_value do |scoped_lst| - data.concat(scoped_lst.disk_types) if scoped_lst && scoped_lst.disk_types + items = [] + next_page_token = nil + loop do + if zone + data = service.list_disk_types(zone, **opts) + next_items = data.items || [] + items.concat(next_items) + next_page_token = data.next_page_token + else + data = service.list_aggregated_disk_types(**opts) + data.items.each_value do |scoped_lst| + items.concat(scoped_lst.disk_types) if scoped_lst && scoped_lst.disk_types + 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.map(&:to_h)) + load(items.map(&:to_h)) end def get(identity, zone = nil) diff --git a/lib/fog/compute/google/models/disks.rb b/lib/fog/compute/google/models/disks.rb index f71d540a47..325508a1f1 100644 --- a/lib/fog/compute/google/models/disks.rb +++ b/lib/fog/compute/google/models/disks.rb @@ -12,15 +12,25 @@ def all(zone: nil, filter: nil, max_results: nil, order_by: nil, :order_by => order_by, :page_token => page_token } - if zone - data = service.list_disks(zone, **opts).items || [] - else - data = [] - service.list_aggregated_disks(**opts).items.each_value do |scoped_list| - data.concat(scoped_list.disks) if scoped_list.disks + items = [] + next_page_token = nil + loop do + if zone + data = service.list_disks(zone, **opts) + next_items = data.items || [] + items.concat(next_items) + next_page_token = data.next_page_token + else + data = service.list_aggregated_disks(**opts) + data.items.each_value do |scoped_list| + items.concat(scoped_list.disks) if scoped_list && scoped_list.disks + 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.map(&:to_h)) + load(items.map(&:to_h)) end def get(identity, zone = nil) diff --git a/lib/fog/compute/google/models/firewalls.rb b/lib/fog/compute/google/models/firewalls.rb index 3cdc8605bf..c5cd850df2 100644 --- a/lib/fog/compute/google/models/firewalls.rb +++ b/lib/fog/compute/google/models/firewalls.rb @@ -5,8 +5,17 @@ class Firewalls < Fog::Collection model Fog::Compute::Google::Firewall def all(opts = {}) - data = service.list_firewalls(**opts).to_h[:items] - load(data || []) + items = [] + next_page_token = nil + loop do + data = service.list_firewalls(**opts) + next_items = data.to_h[:items] || [] + items.concat(next_items) + next_page_token = data.next_page_token + break if next_page_token.nil? || next_page_token.empty? + opts[:page_token] = next_page_token + end + load(items) end def get(identity) diff --git a/lib/fog/compute/google/models/forwarding_rules.rb b/lib/fog/compute/google/models/forwarding_rules.rb index 3c61367ed2..b53f807bd1 100644 --- a/lib/fog/compute/google/models/forwarding_rules.rb +++ b/lib/fog/compute/google/models/forwarding_rules.rb @@ -11,19 +11,25 @@ def all(region: nil, filter: nil, max_results: nil, order_by: nil, page_token: n :order_by => order_by, :page_token => page_token } - - if region - data = service.list_forwarding_rules(region, **opts).items || [] - else - data = [] - service.list_aggregated_forwarding_rules(**opts).items - .each_value do |scoped_list| - if scoped_list && scoped_list.forwarding_rules - data.concat(scoped_list.forwarding_rules) + items = [] + next_page_token = nil + loop do + if region + data = service.list_forwarding_rules(region, **opts) + next_items = data.items || [] + items.concat(next_items) + next_page_token = data.next_page_token + else + data = service.list_aggregated_forwarding_rules(**opts) + data.items.each_value do |scoped_list| + items.concat(scoped_list.forwarding_rules) if scoped_list && scoped_list.forwarding_rules 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.map(&:to_h)) + load(items.map(&:to_h)) end def get(identity, region = nil) diff --git a/lib/fog/compute/google/models/global_addresses.rb b/lib/fog/compute/google/models/global_addresses.rb index ebb05579e8..29ab993efd 100644 --- a/lib/fog/compute/google/models/global_addresses.rb +++ b/lib/fog/compute/google/models/global_addresses.rb @@ -5,8 +5,17 @@ class GlobalAddresses < Fog::Collection model Fog::Compute::Google::GlobalAddress def all(options = {}) - data = service.list_global_addresses(**options).to_h[:items] || [] - load(data) + items = [] + next_page_token = nil + loop do + data = service.list_global_addresses(**options) + next_items = data.to_h[:items] || [] + items.concat(next_items) + next_page_token = data.next_page_token + break if next_page_token.nil? || next_page_token.empty? + options[:page_token] = next_page_token + end + load(items) end def get(identity) diff --git a/lib/fog/compute/google/models/global_forwarding_rules.rb b/lib/fog/compute/google/models/global_forwarding_rules.rb index c44845832e..46fe5f9a6d 100644 --- a/lib/fog/compute/google/models/global_forwarding_rules.rb +++ b/lib/fog/compute/google/models/global_forwarding_rules.rb @@ -5,8 +5,17 @@ class GlobalForwardingRules < Fog::Collection model Fog::Compute::Google::GlobalForwardingRule def all(opts = {}) - data = service.list_global_forwarding_rules(**opts).to_h[:items] || [] - load(data) + items = [] + next_page_token = nil + loop do + data = service.list_global_forwarding_rules(**opts) + next_items = data.to_h[:items] || [] + items.concat(next_items) + next_page_token = data.next_page_token + break if next_page_token.nil? || next_page_token.empty? + opts[:page_token] = next_page_token + end + load(items) end def get(identity) diff --git a/lib/fog/compute/google/models/http_health_checks.rb b/lib/fog/compute/google/models/http_health_checks.rb index eedc3f2102..eb373b785c 100644 --- a/lib/fog/compute/google/models/http_health_checks.rb +++ b/lib/fog/compute/google/models/http_health_checks.rb @@ -4,9 +4,18 @@ class Google class HttpHealthChecks < Fog::Collection model Fog::Compute::Google::HttpHealthCheck - def all(_filters = {}) - data = service.list_http_health_checks.to_h[:items] || [] - load(data) + def all(opts = {}) + items = [] + next_page_token = nil + loop do + data = service.list_http_health_checks(**opts) + next_items = data.to_h[:items] || [] + items.concat(next_items) + next_page_token = data.next_page_token + break if next_page_token.nil? || next_page_token.empty? + opts[:page_token] = next_page_token + end + load(items) end def get(identity) diff --git a/lib/fog/compute/google/models/images.rb b/lib/fog/compute/google/models/images.rb index 1bfeea99ca..9f72d21e85 100644 --- a/lib/fog/compute/google/models/images.rb +++ b/lib/fog/compute/google/models/images.rb @@ -24,12 +24,21 @@ class Images < Fog::Collection windows-sql-cloud ).freeze - def all - data = all_projects.map do |project| + def all(opts = {}) + items = [] + all_projects.each do |project| begin - images = service.list_images(project).items || [] - # Keep track of the project in which we found the image(s) - images.map { |img| img.to_h.merge(:project => project) } + next_page_token = nil + loop do + opts[:page_token] = next_page_token + data = service.list_images(project, **opts) + images = data.items&.map(&:to_h) || [] + # Keep track of the project in which we found the image(s) + images.each { |img| img.merge!(:project => project) } + items.concat(images) + next_page_token = data.next_page_token + break if next_page_token.nil? || next_page_token.empty? + end rescue ::Google::Apis::ClientError => e raise e unless e.status_code == 404 # Not everyone has access to every Global Project. Requests @@ -37,9 +46,7 @@ def all next end end - data = data.flatten - - load(data) + load(items) end # Only return the non-deprecated list of images diff --git a/lib/fog/compute/google/models/instance_group_managers.rb b/lib/fog/compute/google/models/instance_group_managers.rb index 17844160aa..833357f8b5 100644 --- a/lib/fog/compute/google/models/instance_group_managers.rb +++ b/lib/fog/compute/google/models/instance_group_managers.rb @@ -12,17 +12,25 @@ def all(zone: nil, filter: nil, max_results: nil, :order_by => order_by, :page_token => page_token } - - if zone - data = service.list_instance_group_managers(zone, **opts).items || [] - else - data = [] - service.list_aggregated_instance_group_managers(**opts).items.each_value do |group| - data.concat(group.instance_group_managers) if group.instance_group_managers + items = [] + next_page_token = nil + loop do + if zone + data = service.list_instance_group_managers(zone, **opts) + next_items = data.items || [] + items.concat(next_items) + next_page_token = data.next_page_token + else + data = service.list_aggregated_instance_group_managers(**opts) + data.items.each_value do |group| + items.concat(group.instance_group_managers) if group && group.instance_group_managers + 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.map(&:to_h)) + load(items.map(&:to_h)) end def get(identity, zone = nil) diff --git a/lib/fog/compute/google/models/instance_groups.rb b/lib/fog/compute/google/models/instance_groups.rb index 8518604e47..3630b63081 100644 --- a/lib/fog/compute/google/models/instance_groups.rb +++ b/lib/fog/compute/google/models/instance_groups.rb @@ -11,17 +11,23 @@ def all(zone: nil, filter: nil, max_results: nil, order_by: nil, page_token: nil :order_by => order_by, :page_token => page_token } - - if zone - data = service.list_instance_groups(zone).items || [] - else - data = [] - service.list_aggregated_instance_groups(opts).items.each_value do |group| - data.concat(group.instance_groups) if group && group.instance_groups + items = [] + next_page_token = nil + loop do + if zone + data = service.list_instance_groups(zone).items || [] + else + data = service.list_aggregated_instance_groups(opts) + data.items.each_value do |group| + items.concat(group.instance_groups) if group && group.instance_groups + 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.map(&:to_h)) + load(items.map(&:to_h)) end def get(identity, zone = nil)