Skip to content

Commit 8a1fc4c

Browse files
author
Gildas Le Nadan
committed
Add logic to support pagination in resource listing
1 parent bcda5e5 commit 8a1fc4c

File tree

2 files changed

+47
-14
lines changed

2 files changed

+47
-14
lines changed

lib/awspec/helper/finder/backup.rb

Lines changed: 45 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,62 @@ module Awspec::Helper
44
module Finder
55
module Backup
66
def find_backup_vault(id)
7-
res = backup_client.list_backup_vaults
8-
res.backup_vault_list.select do |v|
9-
v.backup_vault_name == id || v.backup_vault_arn == id
10-
end.single_resource(id)
7+
selected = []
8+
req = {}
9+
loop do
10+
res = backup_client.list_backup_vaults(req)
11+
selected += res.backup_vault_list.select do |v|
12+
v.backup_vault_name == id || v.backup_vault_arn == id
13+
end
14+
break if res.next_token.nil?
15+
16+
req[:next_token] = res.next_token
17+
end
18+
selected.single_resource(id)
1119
rescue Aws::Backup::Errors::ResourceNotFoundException
1220
nil
1321
end
1422

1523
def find_backup_plan(id)
16-
res = backup_client.list_backup_plans
17-
res.backup_plans_list.select do |p|
18-
p.backup_plan_name == id || p.backup_plan_arn == id || p.backup_plan_id == id
19-
end.single_resource(id)
24+
selected = []
25+
req = {}
26+
loop do
27+
res = backup_client.list_backup_plans(req)
28+
selected += res.backup_plans_list.select do |p|
29+
p.backup_plan_name == id || p.backup_plan_arn == id || p.backup_plan_id == id
30+
end
31+
break if res.next_token.nil?
32+
33+
req[:next_token] = res.next_token
34+
end
35+
selected.single_resource(id)
2036
rescue Aws::Backup::Errors::ResourceNotFoundException
2137
nil
2238
end
2339

2440
def find_backup_selection(id)
41+
backup_plans = []
42+
req = {}
43+
loop do
44+
res = backup_client.list_backup_plans(req)
45+
backup_plans += res.backup_plans_list.map { |p| p.backup_plan_id }
46+
break if res.next_token.nil?
47+
48+
req[:next_token] = res.next_token
49+
end
50+
2551
selected = []
26-
plans = backup_client.list_backup_plans
27-
plans.backup_plans_list.each do |p|
28-
res = backup_client.list_backup_selections({ backup_plan_id: p.backup_plan_id })
29-
selected += res.backup_selections_list.select do |s|
30-
s.selection_id == id || s.selection_name == id
52+
next_token = nil
53+
54+
backup_plans.each do |plan_id|
55+
loop do
56+
res = backup_client.list_backup_selections({ backup_plan_id: plan_id, next_token: next_token })
57+
selected += res.backup_selections_list.select do |s|
58+
s.selection_id == id || s.selection_name == id
59+
end
60+
break if res.next_token.nil?
61+
62+
next_token = res.next_token
3163
end
3264
end
3365
selected.single_resource(id)

lib/awspec/stub/backup_selection.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@
2626
creation_date: Time.new(2016, 10, 4, 9, 00, 00, '+00:00'),
2727
creator_request_id: nil,
2828
iam_role_arn: 'arn:aws:iam::111122223333:role/service-role/my-backup-service-role'
29-
]
29+
],
30+
next_token: nil
3031
}
3132
}
3233
}

0 commit comments

Comments
 (0)