Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions asset/composer.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
{
"require": {
"google/cloud-bigquery": "^1.16.0",
"google/cloud-storage": "^1.9",
"google/cloud-asset": "^1.0.0",
"google/cloud-asset": "^1.2.0",
"symfony/console": " ^3.0"
},
"autoload": {
"files": [
"src/export_assets.php",
"src/batch_get_assets_history.php"
"src/batch_get_assets_history.php",
"src/search_all_resources.php",
"src/search_all_iam_policies.php"
]
}
}
52 changes: 52 additions & 0 deletions asset/src/search_all_iam_policies.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php
/**
* Copyright 2020 Google LLC.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

// Include Google Cloud dependendencies using Composer
require_once __DIR__ . '/../vendor/autoload.php';

if (count($argv) < 2 || count($argv) > 5) {
return printf("Usage: php %s SCOPE [QUERY] [PAGE_SIZE] [PAGE_TOKEN]\n", __FILE__);
}
list($_, $scope) = $argv;
$query = isset($argv[2]) ? $argv[2] : '';
$pageSize = isset($argv[3]) ? (int) $argv[3] : 0;
$pageToken = isset($argv[4]) ? $argv[4] : '';

// [START asset_search_all_iam_policies]
use Google\Cloud\Asset\V1\AssetServiceClient;

/** Uncomment and populate these variables in your code */
// $scope = 'Scope of the search';
// $query = ''; // (Optional) Query statement
// $pageSize = 0; // (Optional) Size of each result page
// $pageToken = ''; // (Optional) Token produced by the preceding call

// Instantiate a client.
$asset = new AssetServiceClient();

// Run request
$response = $asset->searchAllIamPolicies($scope, [
'query' => $query,
'pageSize' => $pageSize,
'pageToken' => $pageToken
]);

// Print the resources that the policies are set on
foreach ($response->getPage() as $policy) {
print($policy->getResource() . PHP_EOL);
}
// [END asset_search_all_iam_policies]
58 changes: 58 additions & 0 deletions asset/src/search_all_resources.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php
/**
* Copyright 2020 Google LLC.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

// Include Google Cloud dependendencies using Composer
require_once __DIR__ . '/../vendor/autoload.php';

if (count($argv) < 2 || count($argv) > 7) {
return printf("Usage: php %s SCOPE [QUERY] [ASSET_TYPES] [PAGE_SIZE] [PAGE_TOKEN] [ORDER_BY]\n", __FILE__);
}
list($_, $scope) = $argv;
$query = isset($argv[2]) ? $argv[2] : '';
$assetTypes = isset($argv[3]) ? $argv[3] : '';
$pageSize = isset($argv[4]) ? (int) $argv[4] : 0;
$pageToken = isset($argv[5]) ? $argv[5] : '';
$orderBy = isset($argv[6]) ? $argv[6] : '';

// [START asset_search_all_resources]
use Google\Cloud\Asset\V1\AssetServiceClient;

/** Uncomment and populate these variables in your code */
// $scope = 'Scope of the search';
// $query = ''; // (Optional) Query statement
// $assetTypes = ''; // (Optional) Asset types to search for
// $pageSize = 0; // (Optional) Size of each result page
// $pageToken = ''; // (Optional) Token produced by the preceding call
// $orderBy = ''; // (Optional) Fields to sort the results

// Instantiate a client.
$asset = new AssetServiceClient();

// Run request
$response = $asset->searchAllResources($scope, [
'query' => $query,
'assetTypes' => empty($assetTypes) ? [] : explode(',', $assetTypes),
'pageSize' => $pageSize,
'pageToken' => $pageToken,
'orderBy' => $orderBy
]);

// Print the resource names in the first page of the result
foreach ($response->getPage() as $resource) {
print($resource->getName() . PHP_EOL);
}
// [END asset_search_all_resources]
75 changes: 75 additions & 0 deletions asset/test/assetSearchTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php
/**
* Copyright 2020 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace Google\Cloud\Samples\Asset;

use Google\Cloud\BigQuery\BigQueryClient;
use Google\Cloud\TestUtils\TestTrait;
use Google\Cloud\TestUtils\EventuallyConsistentTestTrait;
use PHPUnit\Framework\TestCase;

/**
* Unit Tests for asset search commands.
*/
class assetSearchTest extends TestCase
{
use TestTrait;
use EventuallyConsistentTestTrait;

private static $datasetId;
private static $dataset;

public static function setUpBeforeClass()
{
$client = new BigQueryClient([
'projectId' => self::$projectId,
]);
self::$datasetId = sprintf('temp_dataset_%s', time());
self::$dataset = $client->createDataset(self::$datasetId);
}

public static function tearDownAfterClass()
{
self::$dataset->delete();
}

public function testSearchAllResources()
{
$scope = 'projects/' . self::$projectId;
$query = 'name:' . self::$datasetId;

$this->runEventuallyConsistentTest(function () use ($scope, $query) {
$output = $this->runSnippet('search_all_resources', [
$scope,
$query
]);
$this->assertContains(self::$datasetId, $output);
}, 10, true);
}

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

$output = $this->runSnippet('search_all_iam_policies', [
$scope,
$query
]);
$this->assertContains(self::$projectId, $output);
}
}