import com.google.api.gax.rpc.ApiException;
import com.google.api.gax.rpc.InvalidArgumentException;
import com.google.cloud.asset.v1.AssetServiceClient;
import com.google.cloud.asset.v1.AssetServiceClient.SearchAllIamPoliciesPagedResponse;
import com.google.cloud.asset.v1.SearchAllIamPoliciesRequest;
import java.io.IOException;
public class SearchAllIamPoliciesExample {
// Searches for all the iam policies within the given scope.
public static void searchAllIamPolicies(String scope, String query) {
// TODO(developer): Replace these variables before running the sample.
int pageSize = 0;
String pageToken = "";
SearchAllIamPoliciesRequest request =
SearchAllIamPoliciesRequest.newBuilder()
.setScope(scope)
.setQuery(query)
.setPageSize(pageSize)
.setPageToken(pageToken)
.build();
// Initialize client that will be used to send requests. This client only needs to be created
// once, and can be reused for multiple requests. After completing all of your requests, call
// the "close" method on the client to safely clean up any remaining background resources.
try (AssetServiceClient client = AssetServiceClient.create()) {
SearchAllIamPoliciesPagedResponse response = client.searchAllIamPolicies(request);
System.out.println("Search completed successfully:\n" + response.getPage().getValues());
} catch (IOException e) {
System.out.println(String.format("Failed to create client:%n%s", e.toString()));
} catch (InvalidArgumentException e) {
System.out.println(String.format("Invalid request:%n%s", e.toString()));
} catch (ApiException e) {
System.out.println(String.format("Error during SearchAllIamPolicies:%n%s", e.toString()));
}
}
}
use Google\Cloud\Asset\V1\AssetServiceClient;
/**
* @param string $scope Scope of the search
* @param string $query (Optional) Query statement
* @param int $pageSize (Optional) Size of each result page
* @param string $pageToken (Optional) Token produced by the preceding call
*/
function search_all_iam_policies(
string $scope,
string $query = '',
int $pageSize = 0,
string $pageToken = ''
) {
// 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);
}
}
from google.cloud import asset_v1
# TODO scope = 'Scope of the search'
# TODO query = 'Query statement'
# TODO page_size = Size of each result page
client = asset_v1.AssetServiceClient()
response = client.search_all_iam_policies(
request={"scope": scope, "query": query, "page_size": page_size}
)
for policy in response:
print(policy)
break