Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aws
GitHub Repository: aws/aws-cli
Path: blob/develop/awscli/customizations/ec2/paginate.py
1567 views
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
14
15
def register_ec2_page_size_injector(event_emitter):
16
EC2PageSizeInjector().register(event_emitter)
17
18
19
class EC2PageSizeInjector(object):
20
21
# Operations to auto-paginate and their specific whitelists.
22
# Format:
23
# Key: Operation
24
# Value: List of parameters to add to whitelist for that operation.
25
TARGET_OPERATIONS = {
26
"describe-volumes": [],
27
"describe-snapshots": ['OwnerIds', 'RestorableByUserIds']
28
}
29
30
# Parameters which should be whitelisted for every operation.
31
UNIVERSAL_WHITELIST = ['NextToken', 'DryRun', 'PaginationConfig']
32
33
DEFAULT_PAGE_SIZE = 1000
34
35
def register(self, event_emitter):
36
"""Register `inject` for each target operation."""
37
event_template = "calling-command.ec2.%s"
38
for operation in self.TARGET_OPERATIONS:
39
event = event_template % operation
40
event_emitter.register_last(event, self.inject)
41
42
def inject(self, event_name, parsed_globals, call_parameters, **kwargs):
43
"""Conditionally inject PageSize."""
44
if not parsed_globals.paginate:
45
return
46
47
pagination_config = call_parameters.get('PaginationConfig', {})
48
if 'PageSize' in pagination_config:
49
return
50
51
operation_name = event_name.split('.')[-1]
52
53
whitelisted_params = self.TARGET_OPERATIONS.get(operation_name)
54
if whitelisted_params is None:
55
return
56
57
whitelisted_params = whitelisted_params + self.UNIVERSAL_WHITELIST
58
59
for param in call_parameters:
60
if param not in whitelisted_params:
61
return
62
63
pagination_config['PageSize'] = self.DEFAULT_PAGE_SIZE
64
call_parameters['PaginationConfig'] = pagination_config
65
66