Skip to content

Commit 6eaf989

Browse files
authored
Fix Aws::EC2::Resource not to set max_results automatically when the options contains the parameter that cannot be used with the parameter max_results (#3029)
1 parent f598ca7 commit 6eaf989

File tree

3 files changed

+204
-4
lines changed

3 files changed

+204
-4
lines changed

gems/aws-sdk-ec2/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
Unreleased Changes
22
------------------
33

4+
* Issue - Fix Aws::EC2::Resource not to set max_results automatically when the options contains the parameter that cannot be used with the parameter max_results.
5+
46
1.457.0 (2024-05-13)
57
------------------
68

gems/aws-sdk-ec2/lib/aws-sdk-ec2/customizations/resource.rb

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,38 @@ module EC2
99
# to ensure results are paginated.
1010
module ResourcePaginationFix
1111
def images(options = {})
12-
options[:max_results] ||= 1000
12+
# Prevent the error:
13+
# The parameter imageIdsSet cannot be used with the parameter maxResults
14+
if options[:image_ids].nil? || options[:image_ids].empty?
15+
options[:max_results] ||= 1000
16+
end
1317
super(options)
1418
end
1519

1620
def instances(options = {})
17-
options[:max_results] ||= 1000
21+
# Prevent the error:
22+
# The parameter instancesSet cannot be used with the parameter maxResults
23+
if options[:instance_ids].nil? || options[:instance_ids].empty?
24+
options[:max_results] ||= 1000
25+
end
1826
super(options)
1927
end
2028

2129
def snapshots(options = {})
22-
options[:max_results] ||= 1000
30+
# Prevent the error:
31+
# The parameter snapshotSet cannot be used with the parameter maxResults
32+
if options[:snapshot_ids].nil? || options[:snapshot_ids].empty?
33+
options[:max_results] ||= 1000
34+
end
2335
super(options)
2436
end
2537

2638
def volumes(options = {})
27-
options[:max_results] ||= 1000
39+
# Prevent the error:
40+
# The parameter volumeIdsSet cannot be used with the parameter maxResults
41+
if options[:volume_ids].nil? || options[:volume_ids].empty?
42+
options[:max_results] ||= 1000
43+
end
2844
super(options)
2945
end
3046
end

gems/aws-sdk-ec2/spec/resource_spec.rb

Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,188 @@ module EC2
1010

1111
let(:ec2) { Resource.new(client: client) }
1212

13+
describe '#images' do
14+
15+
it 'sets max_results parameter when options without image_ids' do
16+
expect(ec2.client).to receive(:describe_images).with(
17+
{
18+
filters: [
19+
{
20+
name: 'image-type',
21+
values: ['machine'],
22+
},
23+
],
24+
max_results: 1000,
25+
}
26+
).and_call_original
27+
28+
ec2.images(
29+
filters: [
30+
{
31+
name: 'image-type',
32+
values: ['machine'],
33+
},
34+
],
35+
).to_a
36+
end
37+
38+
it 'does not set max_results parameter when options with image_ids' do
39+
expect(ec2.client).to receive(:describe_images).with(
40+
{
41+
filters: [
42+
{
43+
name: 'image-type',
44+
values: ['machine'],
45+
},
46+
],
47+
image_ids: ['ami-12345678', 'ami-87654321'],
48+
}
49+
).and_call_original
50+
51+
ec2.images(
52+
filters: [
53+
{
54+
name: 'image-type',
55+
values: ['machine'],
56+
},
57+
],
58+
image_ids: ['ami-12345678', 'ami-87654321'],
59+
).to_a
60+
end
61+
62+
end
63+
64+
describe '#instances' do
65+
66+
it 'sets max_results parameter when options without instance_ids' do
67+
expect(ec2.client).to receive(:describe_instances).with(
68+
{
69+
filters: [
70+
{
71+
name: 'image-id',
72+
values: ['ami-12345678', 'ami-87654321'],
73+
},
74+
],
75+
max_results: 1000,
76+
}
77+
).and_call_original
78+
79+
ec2.instances(
80+
filters: [
81+
{
82+
name: 'image-id',
83+
values: ['ami-12345678', 'ami-87654321'],
84+
},
85+
],
86+
).to_a
87+
end
88+
89+
it 'does not set max_results parameter when options with instance_ids' do
90+
expect(ec2.client).to receive(:describe_instances).with(
91+
{
92+
filters: [
93+
{
94+
name: 'image-id',
95+
values: ['ami-12345678', 'ami-87654321'],
96+
},
97+
],
98+
instance_ids: ['ami-12345678', 'ami-87654321'],
99+
}
100+
).and_call_original
101+
102+
ec2.instances(
103+
filters: [
104+
{
105+
name: 'image-id',
106+
values: ['ami-12345678', 'ami-87654321'],
107+
},
108+
],
109+
instance_ids: ['ami-12345678', 'ami-87654321'],
110+
).to_a
111+
end
112+
113+
end
114+
115+
describe '#snapshots' do
116+
117+
it 'sets max_results parameter when options without snapshot_ids' do
118+
expect(ec2.client).to receive(:describe_snapshots).with(
119+
{
120+
owner_ids: ['self'],
121+
max_results: 1000,
122+
}
123+
).and_call_original
124+
125+
ec2.snapshots(
126+
owner_ids: ['self'],
127+
).to_a
128+
end
129+
130+
it 'does not set max_results parameter when options with snapshot_ids' do
131+
expect(ec2.client).to receive(:describe_snapshots).with(
132+
{
133+
snapshot_ids: [
134+
'snap-12345678',
135+
'snap-87654321',
136+
],
137+
}
138+
).and_call_original
139+
140+
ec2.snapshots(
141+
snapshot_ids: [
142+
'snap-12345678',
143+
'snap-87654321',
144+
],
145+
).to_a
146+
end
147+
148+
end
149+
150+
describe '#volumes' do
151+
152+
it 'sets max_results parameter when options without volume_ids' do
153+
expect(ec2.client).to receive(:describe_volumes).with(
154+
{
155+
filters: [
156+
{
157+
name: 'snapshot-id',
158+
values: ['snap-12345678'],
159+
},
160+
],
161+
max_results: 1000,
162+
}
163+
).and_call_original
164+
165+
ec2.volumes(
166+
filters: [
167+
{
168+
name: 'snapshot-id',
169+
values: ['snap-12345678'],
170+
},
171+
],
172+
).to_a
173+
end
174+
175+
it 'does not set max_results parameter when options with volume_ids' do
176+
expect(ec2.client).to receive(:describe_volumes).with(
177+
{
178+
volume_ids: [
179+
'snap-12345678',
180+
'snap-87654321',
181+
],
182+
}
183+
).and_call_original
184+
185+
ec2.volumes(
186+
volume_ids: [
187+
'snap-12345678',
188+
'snap-87654321',
189+
],
190+
).to_a
191+
end
192+
193+
end
194+
13195
describe '#create_tags' do
14196

15197
it 'returns a batch of created tags, the product of ids and tags' do

0 commit comments

Comments
 (0)