Skip to content

Commit fa1f97d

Browse files
EshaantheMelonrsamborski
authored andcommitted
feat(compute): new samples for listing images and pagination (GoogleCloudPlatform#1466)
1 parent 350cec1 commit fa1f97d

File tree

3 files changed

+150
-0
lines changed

3 files changed

+150
-0
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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]
27+
use Google\Cloud\Compute\V1\ImagesClient;
28+
29+
/**
30+
* Prints a list of all non-deprecated image names available in given project.
31+
* Example:
32+
* ```
33+
* list_all_images($projectId);
34+
* ```
35+
*
36+
* @param string $projectId Project ID or project number of the Cloud project you want to list images from.
37+
*
38+
* @throws \Google\ApiCore\ApiException if the remote call fails.
39+
*/
40+
function list_all_images(string $projectId)
41+
{
42+
$imagesClient = new ImagesClient();
43+
// Listing only non-deprecated images to reduce the size of the reply.
44+
$optionalArgs = ['maxResults' => 100, 'filter' => 'deprecated.state != DEPRECATED'];
45+
46+
/**
47+
* Although the maxResults parameter is specified in the request, the iterateAllElements() method
48+
* hides the pagination mechanic. The library makes multiple requests to the API for you,
49+
* so you can simply iterate over all the images.
50+
*/
51+
$pagedResponse = $imagesClient->list($projectId, $optionalArgs);
52+
print("=================== Flat list of images ===================" . PHP_EOL);
53+
foreach ($pagedResponse->iterateAllElements() as $element) {
54+
printf(' - %s' . PHP_EOL, $element->getName());
55+
}
56+
}
57+
# [END compute_images_list]
58+
59+
require_once __DIR__ . '/../../../../testing/sample_helpers.php';
60+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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);

compute/cloud-client/instances/test/instancesTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,4 +170,27 @@ public function testSetUsageExportBucketCustomPrefix()
170170
]);
171171
$this->assertStringContainsString('project `' . self::$projectId . '` is disabled', $output);
172172
}
173+
174+
public function testListAllImages()
175+
{
176+
$output = $this->runFunctionSnippet('list_all_images', [
177+
'projectId' => 'windows-sql-cloud',
178+
]);
179+
180+
$this->assertStringContainsString('sql-2012-enterprise-windows', $output);
181+
$arr = explode(PHP_EOL, $output);
182+
$this->assertGreaterThanOrEqual(2, count($arr));
183+
}
184+
185+
public function testListImagesByPage()
186+
{
187+
$output = $this->runFunctionSnippet('list_images_by_page', [
188+
'projectId' => 'windows-sql-cloud',
189+
]);
190+
191+
$this->assertStringContainsString('sql-2012-enterprise-windows', $output);
192+
$this->assertStringContainsString('Page 2', $output);
193+
$arr = explode(PHP_EOL, $output);
194+
$this->assertGreaterThanOrEqual(2, count($arr));
195+
}
173196
}

0 commit comments

Comments
 (0)