Skip to content

Commit 7ff5022

Browse files
authored
fix(asset): sample warnings, upgrade to new snippet format (GoogleCloudPlatform#1311)
1 parent 22dc3a3 commit 7ff5022

9 files changed

+91
-153
lines changed

asset/asset.php

Lines changed: 0 additions & 70 deletions
This file was deleted.

asset/composer.json

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,6 @@
22
"require": {
33
"google/cloud-bigquery": "^1.16.0",
44
"google/cloud-storage": "^1.9",
5-
"google/cloud-asset": "^1.2.0",
6-
"symfony/console": " ^3.0"
7-
},
8-
"autoload": {
9-
"files": [
10-
"src/export_assets.php",
11-
"src/batch_get_assets_history.php",
12-
"src/search_all_resources.php",
13-
"src/search_all_iam_policies.php"
14-
]
5+
"google/cloud-asset": "^1.2.0"
156
}
167
}

asset/src/batch_get_assets_history.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
use Google\Cloud\Asset\V1\TimeWindow;
2424
use Google\Protobuf\Timestamp;
2525

26-
function batch_get_assets_history($projectId, $assetNames)
26+
function batch_get_assets_history(string $projectId, string $assetNames)
2727
{
2828
$client = new AssetServiceClient();
2929
$formattedParent = $client->projectName($projectId);
@@ -36,3 +36,6 @@ function batch_get_assets_history($projectId, $assetNames)
3636
print($resp->serializeToString());
3737
}
3838
# [END asset_quickstart_batch_get_assets_history]
39+
40+
require_once __DIR__ . '/../../testing/sample_helpers.php';
41+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

asset/src/export_assets.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
* @param string $dumpFilePath the file path where the assets will be dumped to.
3030
* e.g.: gs://[bucket-name]/[asset-file-name].
3131
*/
32-
function export_assets($projectId, $dumpFilePath)
32+
function export_assets(string $projectId, string $dumpFilePath)
3333
{
3434
$client = new AssetServiceClient();
3535

@@ -49,3 +49,6 @@ function export_assets($projectId, $dumpFilePath)
4949
}
5050
}
5151
# [END asset_quickstart_export_assets]
52+
53+
require_once __DIR__ . '/../../testing/sample_helpers.php';
54+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

asset/src/search_all_iam_policies.php

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -15,38 +15,39 @@
1515
* limitations under the License.
1616
*/
1717

18-
// Include Google Cloud dependendencies using Composer
19-
require_once __DIR__ . '/../vendor/autoload.php';
20-
21-
if (count($argv) < 2 || count($argv) > 5) {
22-
return printf("Usage: php %s SCOPE [QUERY] [PAGE_SIZE] [PAGE_TOKEN]\n", __FILE__);
23-
}
24-
list($_, $scope) = $argv;
25-
$query = isset($argv[2]) ? $argv[2] : '';
26-
$pageSize = isset($argv[3]) ? (int) $argv[3] : 0;
27-
$pageToken = isset($argv[4]) ? $argv[4] : '';
18+
namespace Google\Cloud\Samples\Asset;
2819

2920
// [START asset_quickstart_search_all_iam_policies]
3021
use Google\Cloud\Asset\V1\AssetServiceClient;
3122

32-
/** Uncomment and populate these variables in your code */
33-
// $scope = 'Scope of the search';
34-
// $query = ''; // (Optional) Query statement
35-
// $pageSize = 0; // (Optional) Size of each result page
36-
// $pageToken = ''; // (Optional) Token produced by the preceding call
37-
38-
// Instantiate a client.
39-
$asset = new AssetServiceClient();
23+
/**
24+
* @param string $scope Scope of the search
25+
* @param string $query (Optional) Query statement
26+
* @param int $pageSize (Optional) Size of each result page
27+
* @param string $pageToken (Optional) Token produced by the preceding call
28+
*/
29+
function search_all_iam_policies(
30+
string $scope,
31+
string $query = '',
32+
int $pageSize = 0,
33+
string $pageToken = ''
34+
) {
35+
// Instantiate a client.
36+
$asset = new AssetServiceClient();
4037

41-
// Run request
42-
$response = $asset->searchAllIamPolicies($scope, [
43-
'query' => $query,
44-
'pageSize' => $pageSize,
45-
'pageToken' => $pageToken
46-
]);
38+
// Run request
39+
$response = $asset->searchAllIamPolicies($scope, [
40+
'query' => $query,
41+
'pageSize' => $pageSize,
42+
'pageToken' => $pageToken
43+
]);
4744

48-
// Print the resources that the policies are set on
49-
foreach ($response->getPage() as $policy) {
50-
print($policy->getResource() . PHP_EOL);
45+
// Print the resources that the policies are set on
46+
foreach ($response->getPage() as $policy) {
47+
print($policy->getResource() . PHP_EOL);
48+
}
5149
}
5250
// [END asset_quickstart_search_all_iam_policies]
51+
52+
require_once __DIR__ . '/../../testing/sample_helpers.php';
53+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

asset/src/search_all_resources.php

Lines changed: 34 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -15,44 +15,45 @@
1515
* limitations under the License.
1616
*/
1717

18-
// Include Google Cloud dependendencies using Composer
19-
require_once __DIR__ . '/../vendor/autoload.php';
20-
21-
if (count($argv) < 2 || count($argv) > 7) {
22-
return printf("Usage: php %s SCOPE [QUERY] [ASSET_TYPES] [PAGE_SIZE] [PAGE_TOKEN] [ORDER_BY]\n", __FILE__);
23-
}
24-
list($_, $scope) = $argv;
25-
$query = isset($argv[2]) ? $argv[2] : '';
26-
$assetTypes = isset($argv[3]) ? $argv[3] : '';
27-
$pageSize = isset($argv[4]) ? (int) $argv[4] : 0;
28-
$pageToken = isset($argv[5]) ? $argv[5] : '';
29-
$orderBy = isset($argv[6]) ? $argv[6] : '';
18+
namespace Google\Cloud\Samples\Asset;
3019

3120
// [START asset_quickstart_search_all_resources]
3221
use Google\Cloud\Asset\V1\AssetServiceClient;
3322

34-
/** Uncomment and populate these variables in your code */
35-
// $scope = 'Scope of the search';
36-
// $query = ''; // (Optional) Query statement
37-
// $assetTypes = ''; // (Optional) Asset types to search for
38-
// $pageSize = 0; // (Optional) Size of each result page
39-
// $pageToken = ''; // (Optional) Token produced by the preceding call
40-
// $orderBy = ''; // (Optional) Fields to sort the results
41-
42-
// Instantiate a client.
43-
$asset = new AssetServiceClient();
23+
/**
24+
* @param string $scope Scope of the search
25+
* @param string $query (Optional) Query statement
26+
* @param string|array $assetTypes (Optional) Asset types to search for
27+
* @param int $pageSize (Optional) Size of each result page
28+
* @param string $pageToken (Optional) Token produced by the preceding call
29+
* @param string $orderBy (Optional) Fields to sort the results
30+
*/
31+
function search_all_resources(
32+
string $scope,
33+
string $query = '',
34+
array $assetTypes = [],
35+
int $pageSize = 0,
36+
string $pageToken = '',
37+
string $orderBy = ''
38+
) {
39+
// Instantiate a client.
40+
$asset = new AssetServiceClient();
4441

45-
// Run request
46-
$response = $asset->searchAllResources($scope, [
47-
'query' => $query,
48-
'assetTypes' => empty($assetTypes) ? [] : explode(',', $assetTypes),
49-
'pageSize' => $pageSize,
50-
'pageToken' => $pageToken,
51-
'orderBy' => $orderBy
52-
]);
42+
// Run request
43+
$response = $asset->searchAllResources($scope, [
44+
'query' => $query,
45+
'assetTypes' => $assetTypes,
46+
'pageSize' => $pageSize,
47+
'pageToken' => $pageToken,
48+
'orderBy' => $orderBy
49+
]);
5350

54-
// Print the resource names in the first page of the result
55-
foreach ($response->getPage() as $resource) {
56-
print($resource->getName() . PHP_EOL);
51+
// Print the resource names in the first page of the result
52+
foreach ($response->getPage() as $resource) {
53+
print($resource->getName() . PHP_EOL);
54+
}
5755
}
5856
// [END asset_quickstart_search_all_resources]
57+
58+
require_once __DIR__ . '/../../testing/sample_helpers.php';
59+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

asset/test/assetSearchTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,20 +53,20 @@ public function testSearchAllResources()
5353
$query = 'name:' . self::$datasetId;
5454

5555
$this->runEventuallyConsistentTest(function () use ($scope, $query) {
56-
$output = $this->runSnippet('search_all_resources', [
56+
$output = $this->runFunctionSnippet('search_all_resources', [
5757
$scope,
5858
$query
5959
]);
6060
$this->assertStringContainsString(self::$datasetId, $output);
61-
}, 10, true);
61+
}, 3, true);
6262
}
6363

6464
public function testSearchAllIamPolicies()
6565
{
6666
$scope = 'projects/' . self::$projectId;
6767
$query = 'policy:roles/owner';
6868

69-
$output = $this->runSnippet('search_all_iam_policies', [
69+
$output = $this->runFunctionSnippet('search_all_iam_policies', [
7070
$scope,
7171
$query
7272
]);

asset/test/assetTest.php

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919

2020
use Google\Cloud\Storage\StorageClient;
2121
use Google\Cloud\TestUtils\TestTrait;
22-
use Google\Cloud\TestUtils\ExecuteCommandTrait;
2322
use Google\Cloud\TestUtils\EventuallyConsistentTestTrait;
2423
use PHPUnit\Framework\TestCase;
2524

@@ -29,10 +28,8 @@
2928
class assetTest extends TestCase
3029
{
3130
use TestTrait;
32-
use ExecuteCommandTrait;
3331
use EventuallyConsistentTestTrait;
3432

35-
private static $commandFile = __DIR__ . '/../asset.php';
3633
private static $storage;
3734
private static $bucketName;
3835
private static $bucket;
@@ -53,9 +50,9 @@ public function testExportAssets()
5350
{
5451
$fileName = 'my-assets.txt';
5552
$dumpFilePath = 'gs://' . self::$bucketName . '/' . $fileName;
56-
$output = $this->runCommand('export', [
57-
'project' => self::$projectId,
58-
'filePath' => $dumpFilePath,
53+
$output = $this->runFunctionSnippet('export_assets', [
54+
'projectId' => self::$projectId,
55+
'dumpFilePath' => $dumpFilePath,
5956
]);
6057
$assetFile = self::$bucket->object($fileName);
6158
$this->assertEquals($assetFile->name(), $fileName);
@@ -67,8 +64,8 @@ public function testBatchGetAssetsHistory()
6764
$assetName = '//storage.googleapis.com/' . self::$bucketName;
6865

6966
$this->runEventuallyConsistentTest(function () use ($assetName) {
70-
$output = $this->runCommand('batch-get-history', [
71-
'project' => self::$projectId,
67+
$output = $this->runFunctionSnippet('batch_get_assets_history', [
68+
'projectId' => self::$projectId,
7269
'assetNames' => [$assetName],
7370
]);
7471

testing/sample_helpers.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,18 @@ function execute_sample(string $file, string $namespace, ?array $argv)
4141
}
4242
require_once $autoloadFile;
4343

44+
// If any parameters are typehinted as "array", explode user input on ","
45+
$parameterReflections = $functionReflection->getParameters();
46+
foreach ($argv as $i => $val) {
47+
$parameterReflection = $parameterReflections[$i];
48+
if (
49+
$parameterReflection->hasType()
50+
&& 'array' === $parameterReflection->getType()->getName()
51+
) {
52+
$argv[$i] = explode(',', $argv[$i]);
53+
}
54+
}
55+
4456
// Run the function
4557
call_user_func_array($functionName, $argv);
4658
}

0 commit comments

Comments
 (0)