Skip to content

Commit

Permalink
Merge pull request #1971 from JordonPhillips/ec2-pagination
Browse files Browse the repository at this point in the history
Default ec2 page size to 1000
  • Loading branch information
JordonPhillips committed May 13, 2016
2 parents 2665308 + 63f89c1 commit 784e9fc
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changes/next-release/bugfix-ec2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"type": "bugfix",
"category": "ec2",
"description": "Sets MaxResults to default value of 1000."
}
27 changes: 27 additions & 0 deletions awscli/customizations/ec2/paginate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Copyright 2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"). You
# may not use this file except in compliance with the License. A copy of
# the License is located at
#
# http://aws.amazon.com/apache2.0/
#
# or in the "license" file accompanying this file. This file is
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
# ANY KIND, either express or implied. See the License for the specific
# language governing permissions and limitations under the License.
DEFAULT_MAX_RESULTS = 1000


def set_max_results_default(parsed_args, parsed_globals, **kwargs):
"""
In order to have EC2 return results you can paginate you need to inject
the `MaxItems` parameter. In the CLI we should be setting this by default
to avoid users having to wait exceedingly long times for the full results.
"""

# The check for page size validates that the operation is a pagination
# operation.
if parsed_globals.paginate and hasattr(parsed_args, 'page_size') and \
parsed_args.page_size is None and parsed_args.max_results is None:
parsed_args.page_size = DEFAULT_MAX_RESULTS
2 changes: 2 additions & 0 deletions awscli/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
from awscli.customizations.ec2.protocolarg import register_protocol_args
from awscli.customizations.ec2.runinstances import register_runinstances
from awscli.customizations.ec2.secgroupsimplify import register_secgroup
from awscli.customizations.ec2.paginate import set_max_results_default
from awscli.customizations.ecr import register_ecr_commands
from awscli.customizations.emr.emr import emr_initialize
from awscli.customizations.gamelift import register_gamelift_commands
Expand Down Expand Up @@ -102,6 +103,7 @@ def awscli_initialize(event_handlers):
ec2_add_priv_launch_key)
register_parse_global_args(event_handlers)
register_pagination(event_handlers)
event_handlers.register('operation-args-parsed.ec2.*', set_max_results_default)
register_secgroup(event_handlers)
register_bundleinstance(event_handlers)
s3_plugin_initialize(event_handlers)
Expand Down
28 changes: 24 additions & 4 deletions tests/functional/ec2/test_describe_instances.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
# ANY KIND, either express or implied. See the License for the specific
# language governing permissions and limitations under the License.
from awscli.customizations.ec2.paginate import DEFAULT_MAX_RESULTS
from awscli.testutils import BaseAWSCommandParamsTest


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

def test_no_params(self):
cmdline = self.prefix
result = {}
result = {'MaxResults': DEFAULT_MAX_RESULTS}
self.assert_params_for_cmd(cmdline, result)

def test_instance_id(self):
args = ' --instance-ids i-12345678'
cmdline = self.prefix + args
result = {'InstanceIds': ['i-12345678']}
result = {
'InstanceIds': ['i-12345678'],
'MaxResults': DEFAULT_MAX_RESULTS
}
self.assert_params_for_cmd(cmdline, result)

def test_instance_ids(self):
args = ' --instance-ids i-12345678 i-87654321'
cmdline = self.prefix + args
result = {'InstanceIds': ['i-12345678', 'i-87654321']}
result = {
'InstanceIds': ['i-12345678', 'i-87654321'],
'MaxResults': DEFAULT_MAX_RESULTS
}
self.assert_params_for_cmd(cmdline, result)

def test_instance_ids_alternate(self):
# Not required, but will still work if you use JSON.
args = ' --instance-ids ["i-12345678","i-87654321"]'
cmdline = self.prefix + args
result = {'InstanceIds': ['i-12345678', 'i-87654321']}
result = {
'InstanceIds': ['i-12345678', 'i-87654321'],
'MaxResults': DEFAULT_MAX_RESULTS
}
self.assert_params_for_cmd(cmdline, result)

def test_filter_json(self):
Expand All @@ -50,6 +60,7 @@ def test_filter_json(self):
{'Name': 'group-name',
'Values': ['foobar']},
],
'MaxResults': DEFAULT_MAX_RESULTS
}
self.assert_params_for_cmd(cmdline, result)

Expand All @@ -61,6 +72,7 @@ def test_filter_simple(self):
{'Name': 'group-name',
'Values': ['foobar']},
],
'MaxResults': DEFAULT_MAX_RESULTS
}
self.assert_params_for_cmd(cmdline, result)

Expand All @@ -72,6 +84,7 @@ def test_filter_values(self):
{'Name': 'group-name',
'Values': ['foobar', 'fiebaz']},
],
'MaxResults': DEFAULT_MAX_RESULTS
}
self.assert_params_for_cmd(cmdline, result)

Expand All @@ -86,6 +99,7 @@ def test_multiple_filters(self):
{'Name': 'instance-id',
'Values': ['i-12345']},
],
'MaxResults': DEFAULT_MAX_RESULTS
}
self.assert_params_for_cmd(cmdline, result)

Expand All @@ -101,6 +115,7 @@ def test_multiple_filters_alternate(self):
{'Name': 'instance-id',
'Values': ['i-12345']},
],
'MaxResults': DEFAULT_MAX_RESULTS
}
self.assert_params_for_cmd(cmdlist, result)

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

def test_page_size_default(self):
cmdline = self.prefix
result = {'MaxResults': DEFAULT_MAX_RESULTS}
self.assert_params_for_cmd(cmdline, result)


if __name__ == "__main__":
unittest.main()
12 changes: 12 additions & 0 deletions tests/unit/customizations/ec2/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Copyright 2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"). You
# may not use this file except in compliance with the License. A copy of
# the License is located at
#
# http://aws.amazon.com/apache2.0/
#
# or in the "license" file accompanying this file. This file is
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
# ANY KIND, either express or implied. See the License for the specific
# language governing permissions and limitations under the License.
54 changes: 54 additions & 0 deletions tests/unit/customizations/ec2/test_paginate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# Copyright 2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"). You
# may not use this file except in compliance with the License. A copy of
# the License is located at
#
# http://aws.amazon.com/apache2.0/
#
# or in the "license" file accompanying this file. This file is
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
# ANY KIND, either express or implied. See the License for the specific
# language governing permissions and limitations under the License.
import mock

from awscli.customizations.ec2.paginate import set_max_results_default
from awscli.customizations.ec2.paginate import DEFAULT_MAX_RESULTS
from awscli.testutils import unittest


class TestSetMaxResult(unittest.TestCase):
def setUp(self):
self.parsed_args = mock.Mock()
self.parsed_globals = mock.Mock()

self.parsed_args.max_results = None
self.parsed_args.page_size = None
self.parsed_globals.paginate = True

def test_default_is_set(self):
set_max_results_default(self.parsed_args, self.parsed_globals)
self.assertEqual(self.parsed_args.page_size, DEFAULT_MAX_RESULTS)

def test_page_size_isnt_overwritten(self):
page_size = DEFAULT_MAX_RESULTS - 10
self.parsed_args.page_size = page_size
set_max_results_default(self.parsed_args, self.parsed_globals)
self.assertEqual(self.parsed_args.page_size, page_size)

def test_max_results_isnt_overwritten(self):
max_results = DEFAULT_MAX_RESULTS - 10
self.parsed_args.max_results = max_results
set_max_results_default(self.parsed_args, self.parsed_globals)
self.assertEqual(self.parsed_args.max_results, max_results)
self.assertEqual(self.parsed_args.page_size, None)

def test_no_paginate_disables_default(self):
self.parsed_globals.paginate = False
set_max_results_default(self.parsed_args, self.parsed_globals)
self.assertEqual(self.parsed_args.page_size, None)

def test_only_applies_if_page_size_is_present(self):
del self.parsed_args.page_size
set_max_results_default(self.parsed_args, self.parsed_globals)
self.assertFalse(hasattr(self.parsed_args, 'page_size'))

0 comments on commit 784e9fc

Please sign in to comment.