This repository was archived by the owner on Dec 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 24
docs(samples): Adding pagination sample. #78
Merged
Merged
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
a27aff8
feat: Adding pagination sample.
m-strzelczyk 85ec4a2
Merge branch 'master' into compute-pagination
m-strzelczyk 4ff53a8
feat: Adding regions and tests to pagination samples
m-strzelczyk c7fe346
chore: lint fix
m-strzelczyk 531a17e
Merge branch 'master' into compute-pagination
parthea 0693ec2
Apply suggestions from code review
m-strzelczyk 6207353
chore: applying review comments.
m-strzelczyk d60a00c
fix: Make tests run in separate projects.
m-strzelczyk 98ade2c
Revert "fix: Make tests run in separate projects."
m-strzelczyk 6665ace
Merge branch 'master' into compute-pagination
m-strzelczyk 856f244
trying to figure out the noxfile_config location
m-strzelczyk aed9059
Revert "trying to figure out the noxfile_config location"
m-strzelczyk e2a3fb8
Testing nox stuff.
m-strzelczyk c26385d
Revert "Testing nox stuff."
m-strzelczyk 7185344
Merge branch 'master' into compute-pagination
m-strzelczyk 5eef04a
Merge branch 'master' into compute-pagination
m-strzelczyk 55d229b
chore: Making the default_values tests be more stable.
m-strzelczyk 6d67628
Merge branch 'compute-pagination' of github.com:googleapis/python-com…
m-strzelczyk 33d998c
Merge branch 'master' into compute-pagination
m-strzelczyk 11a4261
Merge branch 'master' into compute-pagination
m-strzelczyk 16f39a6
Apply suggestions from code review
m-strzelczyk 8b9a8e1
Merge branch 'master' into compute-pagination
m-strzelczyk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| #!/usr/bin/env python | ||
|
|
||
| # Copyright 2021 Google LLC | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License 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. | ||
|
|
||
| # [START compute_images_list_page ] | ||
| # [START compute_images_list ] | ||
| import google.cloud.compute_v1 as compute_v1 | ||
| # [END compute_images_list ] | ||
| # [END compute_images_list_page ] | ||
|
|
||
|
|
||
| # [START compute_images_list ] | ||
| def print_images_list(project: str) -> None: | ||
| """ | ||
| Prints a list of all non-deprecated image names available in given project. | ||
m-strzelczyk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| Args: | ||
| project: project ID or project number of the Cloud project you want to list images from. | ||
|
|
||
| Returns: | ||
| None. | ||
| """ | ||
| images_client = compute_v1.ImagesClient() | ||
| # Listing only non-deprecated images to reduce the size of the reply. | ||
| images_list_request = compute_v1.ListImagesRequest(project=project, max_results=3, | ||
| filter="deprecated.state != DEPRECATED") | ||
|
|
||
| # Although the `max_results` parameter is specified in the request, the iterable returned | ||
| # by the `list()` method hides the pagination mechanic. The library makes multiple | ||
| # requests to the API for you, so you can simply iterate over all the images. | ||
| for img in images_client.list(request=images_list_request): | ||
| print(f" - {img.name}") | ||
| # [END compute_images_list ] | ||
|
|
||
|
|
||
| # [START compute_images_list_page ] | ||
| def print_images_list_by_page(project: str, page_size: int = 10) -> None: | ||
| """ | ||
| Prints a list of all non-deprecated image names available in a given project, | ||
| divided into pages as returned by the Compute Engine API. | ||
|
|
||
| Args: | ||
| project: project ID or project number of the Cloud project you want to list images from. | ||
| page_size: size of the pages you want the API to return on each call. | ||
|
|
||
| Returns: | ||
| None. | ||
| """ | ||
| images_client = compute_v1.ImagesClient() | ||
| # Listing only non-deprecated images to reduce the size of the reply. | ||
| images_list_request = compute_v1.ListImagesRequest(project=project, max_results=page_size, | ||
| filter="deprecated.state != DEPRECATED") | ||
|
|
||
| # Use the `pages` attribute of returned iterable to have more granular control of | ||
| # iteration over paginated results from the API. Each time you want to access the | ||
| # next page, the library retrieves that page from the API. | ||
| for page_num, page in enumerate(images_client.list(request=images_list_request).pages, start=1): | ||
| print(f"Page {page_num}: ") | ||
| for img in page.items: | ||
| print(f" - {img.name}") | ||
| # [END compute_images_list_page ] | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| print("=================== Flat list of images ===================") | ||
| print_images_list('windows-sql-cloud') | ||
| print("================= Paginated list of images ================") | ||
| print_images_list_by_page('windows-sql-cloud', 5) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| # Copyright 2021 Google LLC | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License 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 typing | ||
|
|
||
| from sample_pagination import print_images_list, print_images_list_by_page | ||
|
|
||
| PROJECT = 'windows-sql-cloud' | ||
|
|
||
|
|
||
| def test_pagination(capsys: typing.Any) -> None: | ||
| print_images_list(PROJECT) | ||
| out, _ = capsys.readouterr() | ||
FrodoTheTrue marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| assert(len(out.splitlines()) > 2) | ||
|
|
||
|
|
||
| def test_pagination_page(capsys: typing.Any) -> None: | ||
| print_images_list_by_page(PROJECT, 2) | ||
| out, _ = capsys.readouterr() | ||
| assert("Page 2" in out) | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.