From 62df2f920893269875701345f15a64d5aa12e0ec Mon Sep 17 00:00:00 2001 From: Peter Zheng Date: Fri, 27 Aug 2021 13:31:10 -0700 Subject: [PATCH 1/6] CloudAset: Add ListAssets sample code. --- asset/src/list_assets.php | 48 +++++++++++++++++++++++++++++++++++++++ asset/test/assetTest.php | 12 ++++++++++ 2 files changed, 60 insertions(+) create mode 100644 asset/src/list_assets.php diff --git a/asset/src/list_assets.php b/asset/src/list_assets.php new file mode 100644 index 0000000000..f4fccaf607 --- /dev/null +++ b/asset/src/list_assets.php @@ -0,0 +1,48 @@ +listAssets("projects/$project"); + + // Print the asset names in the first page of the result + foreach ($response->getPage() as $asset) { + print($asset->getName() . PHP_EOL); + } +} +// [END asset_quickstart_list_assets] + +// The following 2 lines are only needed to run the samples +require_once __DIR__ . '/../../testing/sample_helpers.php'; +\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv); diff --git a/asset/test/assetTest.php b/asset/test/assetTest.php index c5161633f4..f0610a2895 100644 --- a/asset/test/assetTest.php +++ b/asset/test/assetTest.php @@ -59,6 +59,18 @@ public function testExportAssets() $assetFile->delete(); } + public function testListAssets() + { + $assetName = '//storage.googleapis.com/' . self::$bucketName; + $this->runEventuallyConsistentTest(function () use ($assetName) { + $output = $this->runFunctionSnippet('list_assets', [ + 'projectId' => self::$projectId, + ]); + + $this->assertStringContainsString($assetName, $output); + }, 10, true); + } + public function testBatchGetAssetsHistory() { $assetName = '//storage.googleapis.com/' . self::$bucketName; From aa54bc93d04cf6375299ff1363d759a2a85c6978 Mon Sep 17 00:00:00 2001 From: Peter Zheng Date: Fri, 27 Aug 2021 13:36:08 -0700 Subject: [PATCH 2/6] Fix ListAssets code comments. --- asset/src/list_assets.php | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/asset/src/list_assets.php b/asset/src/list_assets.php index f4fccaf607..bca9acae70 100644 --- a/asset/src/list_assets.php +++ b/asset/src/list_assets.php @@ -1,6 +1,6 @@ listAssets("projects/$project"); - // Print the asset names in the first page of the result + // Print the asset names in the result foreach ($response->getPage() as $asset) { print($asset->getName() . PHP_EOL); } From a7d9675f113b1a46c0dd36b2a13f882bd00f9d9c Mon Sep 17 00:00:00 2001 From: Peter Zheng Date: Fri, 27 Aug 2021 14:22:38 -0700 Subject: [PATCH 3/6] CloudAsset: Fix ListAssets sample code --- asset/src/list_assets.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/asset/src/list_assets.php b/asset/src/list_assets.php index bca9acae70..fdbaff7d2a 100644 --- a/asset/src/list_assets.php +++ b/asset/src/list_assets.php @@ -29,7 +29,7 @@ function list_assets(string $projectId) $client = new AssetServiceClient(); // Run request - $response = $client->listAssets("projects/$project"); + $response = $client->listAssets("projects/$projectId"); // Print the asset names in the result foreach ($response->getPage() as $asset) { From 4cc3871bf4c17a142111d9ebfa72afc51e5920b2 Mon Sep 17 00:00:00 2001 From: Peter Zheng Date: Fri, 27 Aug 2021 15:02:42 -0700 Subject: [PATCH 4/6] CloudAsset: Fix ListAssets sample code --- asset/src/list_assets.php | 12 +++++++++--- asset/test/assetTest.php | 4 +++- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/asset/src/list_assets.php b/asset/src/list_assets.php index fdbaff7d2a..02d2cc06fa 100644 --- a/asset/src/list_assets.php +++ b/asset/src/list_assets.php @@ -21,15 +21,21 @@ use Google\Cloud\Asset\V1\AssetServiceClient; /** - * @param string $projectId the project Id for list assets. + * @param string $projectId Tthe project Id for list assets. + * @param string|array $assetTypes (Optional) Asset types to list for. + * @param int $pageSize (Optional) Size of one result page. */ -function list_assets(string $projectId) +function list_assets(string $projectId, array $assetTypes = [], int $pageSize) { // Instantiate a client. $client = new AssetServiceClient(); // Run request - $response = $client->listAssets("projects/$projectId"); + $response = $client->listAssets( + "projects/$projectId", [ + 'assetTypes' => $assetTypes, + 'pageSize' => $pageSize, + ]); // Print the asset names in the result foreach ($response->getPage() as $asset) { diff --git a/asset/test/assetTest.php b/asset/test/assetTest.php index f0610a2895..d5c5f1ca2f 100644 --- a/asset/test/assetTest.php +++ b/asset/test/assetTest.php @@ -64,7 +64,9 @@ public function testListAssets() $assetName = '//storage.googleapis.com/' . self::$bucketName; $this->runEventuallyConsistentTest(function () use ($assetName) { $output = $this->runFunctionSnippet('list_assets', [ - 'projectId' => self::$projectId, + 'projectId' => self::$projectId, + 'assetTypes' => ['storage.buckets'], + 'pageSize' => 1000, ]); $this->assertStringContainsString($assetName, $output); From d0f0433859b1454a30532fbef25d7030b0b42f8d Mon Sep 17 00:00:00 2001 From: Peter Zheng Date: Fri, 27 Aug 2021 15:58:04 -0700 Subject: [PATCH 5/6] CloudAsset: Fix sample code for ListAssets --- asset/test/assetTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/asset/test/assetTest.php b/asset/test/assetTest.php index d5c5f1ca2f..a2fc70d62d 100644 --- a/asset/test/assetTest.php +++ b/asset/test/assetTest.php @@ -65,7 +65,7 @@ public function testListAssets() $this->runEventuallyConsistentTest(function () use ($assetName) { $output = $this->runFunctionSnippet('list_assets', [ 'projectId' => self::$projectId, - 'assetTypes' => ['storage.buckets'], + 'assetTypes' => ['storage.googleapis.com/Bucket'], 'pageSize' => 1000, ]); From 260216f440116ab239d1d0e3eec2ec3deb2e48cb Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Mon, 20 Sep 2021 12:39:16 -0600 Subject: [PATCH 6/6] Update asset/test/assetTest.php --- asset/test/assetTest.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/asset/test/assetTest.php b/asset/test/assetTest.php index a2fc70d62d..18cf4eaad9 100644 --- a/asset/test/assetTest.php +++ b/asset/test/assetTest.php @@ -64,9 +64,9 @@ public function testListAssets() $assetName = '//storage.googleapis.com/' . self::$bucketName; $this->runEventuallyConsistentTest(function () use ($assetName) { $output = $this->runFunctionSnippet('list_assets', [ - 'projectId' => self::$projectId, - 'assetTypes' => ['storage.googleapis.com/Bucket'], - 'pageSize' => 1000, + 'projectId' => self::$projectId, + 'assetTypes' => ['storage.googleapis.com/Bucket'], + 'pageSize' => 1000, ]); $this->assertStringContainsString($assetName, $output);