Sitelet https://github.com/Bryan420/aws-cli/commit/63f89c153573e28ad3b64a6dc1d01be9bfbfda91
Skip to content

Commit 63f89c1

Browse files
Default ec2 page size to 1000
If this value isn't set, EC2 will not paginate at all. Instead, they will attempt to return all values to you at once. Naturally, this can cause significant delay if you have a ton of resources.
1 parent b0b0951 commit 63f89c1

6 files changed

Lines changed: 124 additions & 4 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"type": "bugfix",
3+
"category": "ec2",
4+
"description": "Sets MaxResults to default value of 1000."
5+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Copyright 2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License"). You
4+
# may not use this file except in compliance with the License. A copy of
5+
# the License is located at
6+
#
7+
# http://aws.amazon.com/apache2.0/
8+
#
9+
# or in the "license" file accompanying this file. This file is
10+
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
11+
# ANY KIND, either express or implied. See the License for the specific
12+
# language governing permissions and limitations under the License.
13+
DEFAULT_MAX_RESULTS = 1000
14+
15+
16+
def set_max_results_default(parsed_args, parsed_globals, **kwargs):
17+
"""
18+
In order to have EC2 return results you can paginate you need to inject
19+
the `MaxItems` parameter. In the CLI we should be setting this by default
20+
to avoid users having to wait exceedingly long times for the full results.
21+
"""
22+
23+
# The check for page size validates that the operation is a pagination
24+
# operation.
25+
if parsed_globals.paginate and hasattr(parsed_args, 'page_size') and \
26+
parsed_args.page_size is None and parsed_args.max_results is None:
27+
parsed_args.page_size = DEFAULT_MAX_RESULTS

‎awscli/handlers.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
from awscli.customizations.ec2.protocolarg import register_protocol_args
4545
from awscli.customizations.ec2.runinstances import register_runinstances
4646
from awscli.customizations.ec2.secgroupsimplify import register_secgroup
47+
from awscli.customizations.ec2.paginate import set_max_results_default
4748
from awscli.customizations.ecr import register_ecr_commands
4849
from awscli.customizations.emr.emr import emr_initialize
4950
from awscli.customizations.gamelift import register_gamelift_commands
@@ -102,6 +103,7 @@ def awscli_initialize(event_handlers):
102103
ec2_add_priv_launch_key)
103104
register_parse_global_args(event_handlers)
104105
register_pagination(event_handlers)
106+
event_handlers.register('operation-args-parsed.ec2.*', set_max_results_default)
105107
register_secgroup(event_handlers)
106108
register_bundleinstance(event_handlers)
107109
s3_plugin_initialize(event_handlers)

‎tests/functional/ec2/test_describe_instances.py‎

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
1212
# ANY KIND, either express or implied. See the License for the specific
1313
# language governing permissions and limitations under the License.
14+
from awscli.customizations.ec2.paginate import DEFAULT_MAX_RESULTS
1415
from awscli.testutils import BaseAWSCommandParamsTest
1516

1617

@@ -20,26 +21,35 @@ class TestDescribeInstances(BaseAWSCommandParamsTest):
2021

2122
def test_no_params(self):
2223
cmdline = self.prefix
23-
result = {}
24+
result = {'MaxResults': DEFAULT_MAX_RESULTS}
2425
self.assert_params_for_cmd(cmdline, result)
2526

2627
def test_instance_id(self):
2728
args = ' --instance-ids i-12345678'
2829
cmdline = self.prefix + args
29-
result = {'InstanceIds': ['i-12345678']}
30+
result = {
31+
'InstanceIds': ['i-12345678'],
32+
'MaxResults': DEFAULT_MAX_RESULTS
33+
}
3034
self.assert_params_for_cmd(cmdline, result)
3135

3236
def test_instance_ids(self):
3337
args = ' --instance-ids i-12345678 i-87654321'
3438
cmdline = self.prefix + args
35-
result = {'InstanceIds': ['i-12345678', 'i-87654321']}
39+
result = {
40+
'InstanceIds': ['i-12345678', 'i-87654321'],
41+
'MaxResults': DEFAULT_MAX_RESULTS
42+
}
3643
self.assert_params_for_cmd(cmdline, result)
3744

3845
def test_instance_ids_alternate(self):
3946
# Not required, but will still work if you use JSON.
4047
args = ' --instance-ids ["i-12345678","i-87654321"]'
4148
cmdline = self.prefix + args
42-
result = {'InstanceIds': ['i-12345678', 'i-87654321']}
49+
result = {
50+
'InstanceIds': ['i-12345678', 'i-87654321'],
51+
'MaxResults': DEFAULT_MAX_RESULTS
52+
}
4353
self.assert_params_for_cmd(cmdline, result)
4454

4555
def test_filter_json(self):
@@ -50,6 +60,7 @@ def test_filter_json(self):
5060
{'Name': 'group-name',
5161
'Values': ['foobar']},
5262
],
63+
'MaxResults': DEFAULT_MAX_RESULTS
5364
}
5465
self.assert_params_for_cmd(cmdline, result)
5566

@@ -61,6 +72,7 @@ def test_filter_simple(self):
6172
{'Name': 'group-name',
6273
'Values': ['foobar']},
6374
],
75+
'MaxResults': DEFAULT_MAX_RESULTS
6476
}
6577
self.assert_params_for_cmd(cmdline, result)
6678

@@ -72,6 +84,7 @@ def test_filter_values(self):
7284
{'Name': 'group-name',
7385
'Values': ['foobar', 'fiebaz']},
7486
],
87+
'MaxResults': DEFAULT_MAX_RESULTS
7588
}
7689
self.assert_params_for_cmd(cmdline, result)
7790

@@ -86,6 +99,7 @@ def test_multiple_filters(self):
8699
{'Name': 'instance-id',
87100
'Values': ['i-12345']},
88101
],
102+
'MaxResults': DEFAULT_MAX_RESULTS
89103
}
90104
self.assert_params_for_cmd(cmdline, result)
91105

@@ -101,6 +115,7 @@ def test_multiple_filters_alternate(self):
101115
{'Name': 'instance-id',
102116
'Values': ['i-12345']},
103117
],
118+
'MaxResults': DEFAULT_MAX_RESULTS
104119
}
105120
self.assert_params_for_cmd(cmdlist, result)
106121

@@ -110,6 +125,11 @@ def test_page_size(self):
110125
result = {'MaxResults': 10}
111126
self.assert_params_for_cmd(cmdline, result)
112127

128+
def test_page_size_default(self):
129+
cmdline = self.prefix
130+
result = {'MaxResults': DEFAULT_MAX_RESULTS}
131+
self.assert_params_for_cmd(cmdline, result)
132+
113133

114134
if __name__ == "__main__":
115135
unittest.main()
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Copyright 2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License"). You
4+
# may not use this file except in compliance with the License. A copy of
5+
# the License is located at
6+
#
7+
# http://aws.amazon.com/apache2.0/
8+
#
9+
# or in the "license" file accompanying this file. This file is
10+
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
11+
# ANY KIND, either express or implied. See the License for the specific
12+
# language governing permissions and limitations under the License.
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# Copyright 2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License"). You
4+
# may not use this file except in compliance with the License. A copy of
5+
# the License is located at
6+
#
7+
# http://aws.amazon.com/apache2.0/
8+
#
9+
# or in the "license" file accompanying this file. This file is
10+
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
11+
# ANY KIND, either express or implied. See the License for the specific
12+
# language governing permissions and limitations under the License.
13+
import mock
14+
15+
from awscli.customizations.ec2.paginate import set_max_results_default
16+
from awscli.customizations.ec2.paginate import DEFAULT_MAX_RESULTS
17+
from awscli.testutils import unittest
18+
19+
20+
class TestSetMaxResult(unittest.TestCase):
21+
def setUp(self):
22+
self.parsed_args = mock.Mock()
23+
self.parsed_globals = mock.Mock()
24+
25+
self.parsed_args.max_results = None
26+
self.parsed_args.page_size = None
27+
self.parsed_globals.paginate = True
28+
29+
def test_default_is_set(self):
30+
set_max_results_default(self.parsed_args, self.parsed_globals)
31+
self.assertEqual(self.parsed_args.page_size, DEFAULT_MAX_RESULTS)
32+
33+
def test_page_size_isnt_overwritten(self):
34+
page_size = DEFAULT_MAX_RESULTS - 10
35+
self.parsed_args.page_size = page_size
36+
set_max_results_default(self.parsed_args, self.parsed_globals)
37+
self.assertEqual(self.parsed_args.page_size, page_size)
38+
39+
def test_max_results_isnt_overwritten(self):
40+
max_results = DEFAULT_MAX_RESULTS - 10
41+
self.parsed_args.max_results = max_results
42+
set_max_results_default(self.parsed_args, self.parsed_globals)
43+
self.assertEqual(self.parsed_args.max_results, max_results)
44+
self.assertEqual(self.parsed_args.page_size, None)
45+
46+
def test_no_paginate_disables_default(self):
47+
self.parsed_globals.paginate = False
48+
set_max_results_default(self.parsed_args, self.parsed_globals)
49+
self.assertEqual(self.parsed_args.page_size, None)
50+
51+
def test_only_applies_if_page_size_is_present(self):
52+
del self.parsed_args.page_size
53+
set_max_results_default(self.parsed_args, self.parsed_globals)
54+
self.assertFalse(hasattr(self.parsed_args, 'page_size'))

0 commit comments

Comments
 (0)