|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +# Copyright 2021 Google LLC |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | + |
| 17 | +# [START compute_images_list_page ] |
| 18 | +# [START compute_images_list ] |
| 19 | +import google.cloud.compute_v1 as compute_v1 |
| 20 | +# [END compute_images_list ] |
| 21 | +# [END compute_images_list_page ] |
| 22 | + |
| 23 | + |
| 24 | +# [START compute_images_list ] |
| 25 | +def print_images_list(project: str) -> None: |
| 26 | + """ |
| 27 | + Prints a list of all non-deprecated image names available in given project. |
| 28 | +
|
| 29 | + Args: |
| 30 | + project: project ID or project number of the Cloud project you want to list images from. |
| 31 | +
|
| 32 | + Returns: |
| 33 | + None. |
| 34 | + """ |
| 35 | + images_client = compute_v1.ImagesClient() |
| 36 | + # Listing only non-deprecated images to reduce the size of the reply. |
| 37 | + images_list_request = compute_v1.ListImagesRequest(project=project, max_results=3, |
| 38 | + filter="deprecated.state != DEPRECATED") |
| 39 | + |
| 40 | + # Although the `max_results` parameter is specified in the request, the iterable returned |
| 41 | + # by the `list()` method hides the pagination mechanic. The library makes multiple |
| 42 | + # requests to the API for you, so you can simply iterate over all the images. |
| 43 | + for img in images_client.list(request=images_list_request): |
| 44 | + print(f" - {img.name}") |
| 45 | +# [END compute_images_list ] |
| 46 | + |
| 47 | + |
| 48 | +# [START compute_images_list_page ] |
| 49 | +def print_images_list_by_page(project: str, page_size: int = 10) -> None: |
| 50 | + """ |
| 51 | + Prints a list of all non-deprecated image names available in a given project, |
| 52 | + divided into pages as returned by the Compute Engine API. |
| 53 | +
|
| 54 | + Args: |
| 55 | + project: project ID or project number of the Cloud project you want to list images from. |
| 56 | + page_size: size of the pages you want the API to return on each call. |
| 57 | +
|
| 58 | + Returns: |
| 59 | + None. |
| 60 | + """ |
| 61 | + images_client = compute_v1.ImagesClient() |
| 62 | + # Listing only non-deprecated images to reduce the size of the reply. |
| 63 | + images_list_request = compute_v1.ListImagesRequest(project=project, max_results=page_size, |
| 64 | + filter="deprecated.state != DEPRECATED") |
| 65 | + |
| 66 | + # Use the `pages` attribute of returned iterable to have more granular control of |
| 67 | + # iteration over paginated results from the API. Each time you want to access the |
| 68 | + # next page, the library retrieves that page from the API. |
| 69 | + for page_num, page in enumerate(images_client.list(request=images_list_request).pages, start=1): |
| 70 | + print(f"Page {page_num}: ") |
| 71 | + for img in page.items: |
| 72 | + print(f" - {img.name}") |
| 73 | +# [END compute_images_list_page ] |
| 74 | + |
| 75 | + |
| 76 | +if __name__ == '__main__': |
| 77 | + print("=================== Flat list of images ===================") |
| 78 | + print_images_list('windows-sql-cloud') |
| 79 | + print("================= Paginated list of images ================") |
| 80 | + print_images_list_by_page('windows-sql-cloud', 5) |
0 commit comments