Skip to content

Commit 091b3c2

Browse files
feat(asset): add sample code for new RPCs (GoogleCloudPlatform#1120)
1 parent 05a51b2 commit 091b3c2

File tree

4 files changed

+190
-2
lines changed

4 files changed

+190
-2
lines changed

asset/composer.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
{
22
"require": {
3+
"google/cloud-bigquery": "^1.16.0",
34
"google/cloud-storage": "^1.9",
4-
"google/cloud-asset": "^1.0.0",
5+
"google/cloud-asset": "^1.2.0",
56
"symfony/console": " ^3.0"
67
},
78
"autoload": {
89
"files": [
910
"src/export_assets.php",
10-
"src/batch_get_assets_history.php"
11+
"src/batch_get_assets_history.php",
12+
"src/search_all_resources.php",
13+
"src/search_all_iam_policies.php"
1114
]
1215
}
1316
}

asset/src/search_all_iam_policies.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
2+
/**
3+
* Copyright 2020 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+
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] : '';
28+
29+
// [START asset_search_all_iam_policies]
30+
use Google\Cloud\Asset\V1\AssetServiceClient;
31+
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();
40+
41+
// Run request
42+
$response = $asset->searchAllIamPolicies($scope, [
43+
'query' => $query,
44+
'pageSize' => $pageSize,
45+
'pageToken' => $pageToken
46+
]);
47+
48+
// Print the resources that the policies are set on
49+
foreach ($response->getPage() as $policy) {
50+
print($policy->getResource() . PHP_EOL);
51+
}
52+
// [END asset_search_all_iam_policies]

asset/src/search_all_resources.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
2+
/**
3+
* Copyright 2020 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+
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] : '';
30+
31+
// [START asset_search_all_resources]
32+
use Google\Cloud\Asset\V1\AssetServiceClient;
33+
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();
44+
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+
]);
53+
54+
// Print the resource names in the first page of the result
55+
foreach ($response->getPage() as $resource) {
56+
print($resource->getName() . PHP_EOL);
57+
}
58+
// [END asset_search_all_resources]

asset/test/assetSearchTest.php

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
2+
/**
3+
* Copyright 2020 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+
namespace Google\Cloud\Samples\Asset;
19+
20+
use Google\Cloud\BigQuery\BigQueryClient;
21+
use Google\Cloud\TestUtils\TestTrait;
22+
use Google\Cloud\TestUtils\EventuallyConsistentTestTrait;
23+
use PHPUnit\Framework\TestCase;
24+
25+
/**
26+
* Unit Tests for asset search commands.
27+
*/
28+
class assetSearchTest extends TestCase
29+
{
30+
use TestTrait;
31+
use EventuallyConsistentTestTrait;
32+
33+
private static $datasetId;
34+
private static $dataset;
35+
36+
public static function setUpBeforeClass()
37+
{
38+
$client = new BigQueryClient([
39+
'projectId' => self::$projectId,
40+
]);
41+
self::$datasetId = sprintf('temp_dataset_%s', time());
42+
self::$dataset = $client->createDataset(self::$datasetId);
43+
}
44+
45+
public static function tearDownAfterClass()
46+
{
47+
self::$dataset->delete();
48+
}
49+
50+
public function testSearchAllResources()
51+
{
52+
$scope = 'projects/' . self::$projectId;
53+
$query = 'name:' . self::$datasetId;
54+
55+
$this->runEventuallyConsistentTest(function () use ($scope, $query) {
56+
$output = $this->runSnippet('search_all_resources', [
57+
$scope,
58+
$query
59+
]);
60+
$this->assertContains(self::$datasetId, $output);
61+
}, 10, true);
62+
}
63+
64+
public function testSearchAllIamPolicies()
65+
{
66+
$scope = 'projects/' . self::$projectId;
67+
$query = 'policy:roles/owner';
68+
69+
$output = $this->runSnippet('search_all_iam_policies', [
70+
$scope,
71+
$query
72+
]);
73+
$this->assertContains(self::$projectId, $output);
74+
}
75+
}

0 commit comments

Comments
 (0)