|
| 1 | +
|
| 2 | +/** |
| 3 | + * Copyright 2021 Google Inc. |
| 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 | + |
| 18 | +/** |
| 19 | + * For instructions on how to run the full sample: |
| 20 | + * |
| 21 | + * @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/compute/cloud-client/README.md |
| 22 | + */ |
| 23 | + |
| 24 | +namespace Google\Cloud\Samples\Compute; |
| 25 | + |
| 26 | +# [START compute_images_list_page] |
| 27 | +use Google\Cloud\Compute\V1\ImagesClient; |
| 28 | + |
| 29 | +/** |
| 30 | + * Prints a list of all non-deprecated image names available in a given project, |
| 31 | + * divided into pages as returned by the Compute Engine API. |
| 32 | + * Example: |
| 33 | + * ``` |
| 34 | + * list_images_by_page($projectId, $pageSize); |
| 35 | + * ``` |
| 36 | + * |
| 37 | + * @param string $projectId Project ID or project number of the Cloud project you want to list images from. |
| 38 | + * @param int $pageSize Size of the pages you want the API to return on each call. |
| 39 | + * |
| 40 | + * @throws \Google\ApiCore\ApiException if the remote call fails. |
| 41 | + */ |
| 42 | +function list_images_by_page(string $projectId, int $pageSize = 10) |
| 43 | +{ |
| 44 | + $imagesClient = new ImagesClient(); |
| 45 | + $pageNum = 1; |
| 46 | + // Listing only non-deprecated images to reduce the size of the reply. |
| 47 | + $optionalArgs = ['maxResults' => $pageSize, 'filter' => 'deprecated.state != DEPRECATED']; |
| 48 | + |
| 49 | + /** |
| 50 | + * Use the 'iteratePages()' method of returned response to have more granular control of iteration over |
| 51 | + * paginated results from the API. Each time you want to access the next page, the library retrieves |
| 52 | + * that page from the API. |
| 53 | + */ |
| 54 | + $pagedResponse = $imagesClient->list($projectId, $optionalArgs); |
| 55 | + print("=================== Paginated list of images ===================" . PHP_EOL); |
| 56 | + foreach ($pagedResponse->iteratePages() as $page) { |
| 57 | + printf('Page %s:' . PHP_EOL, $pageNum); |
| 58 | + foreach ($page as $element) { |
| 59 | + printf(' - %s' . PHP_EOL, $element->getName()); |
| 60 | + } |
| 61 | + $pageNum++; |
| 62 | + } |
| 63 | +} |
| 64 | +# [END compute_images_list_page] |
| 65 | + |
| 66 | +require_once __DIR__ . '/../../../../testing/sample_helpers.php'; |
| 67 | +\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv); |
0 commit comments