Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

General solution for paginating AWS endpoints #63

Closed
simonw opened this issue Jan 19, 2022 · 3 comments
Closed

General solution for paginating AWS endpoints #63

simonw opened this issue Jan 19, 2022 · 3 comments
Labels
enhancement New feature or request

Comments

@simonw
Copy link
Owner

simonw commented Jan 19, 2022

I need a general solution for pagination. Maybe something like this:

for policy_name in paginate(iam, "list_role_policies", "PolicyNames", RoleName=role_name):
    # ...

The paginate() function would wrap this pattern, seen elsewhere:

paginator = iam.get_paginator("list_user_policies")
for response in paginator.paginate(UserName=username):
    for policy_name in response["PolicyNames"]:
        # ...

Originally posted by @simonw in #62 (comment)

@simonw simonw added the enhancement New feature or request label Jan 19, 2022
@simonw
Copy link
Owner Author

simonw commented Jan 19, 2022

I think this is it:

def paginate(service, method, list_key, **kwargs):
    paginator = service.get_paginator(method)
    for response in paginator.paginate(**kwargs):
        yield from response[list_key]

@simonw
Copy link
Owner Author

simonw commented Jan 19, 2022

Which simplifies list-users to just:

@cli.command()
@common_output_options
@common_boto3_options
def list_users(nl, csv, tsv, **boto_options):
    "List all users"
    iam = make_client("iam", **boto_options)
    output(
        paginate(iam, "list_users", "Users"),
        (
            "UserName",
            "UserId",
            "Arn",
            "Path",
            "CreateDate",
            "PasswordLastUsed",
            "PermissionsBoundary",
            "Tags",
        ),
        nl,
        csv,
        tsv,
    )

@simonw simonw closed this as completed in fc1e06c Jan 19, 2022
@simonw simonw mentioned this issue Jan 19, 2022
4 tasks
simonw added a commit that referenced this issue Jan 19, 2022
* list-roles command, closes #61
* Refactor list-roles to use paginate() from #63
* Tests for list-roles - a whole lot of mocks
* Documentation for list-roles
* Fixed JSON output in --csv and --tsv
@simonw
Copy link
Owner Author

simonw commented Jan 20, 2022

simonw added a commit that referenced this issue Jan 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant