-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[asset] feat: add samples for SearchAllResources and SearchAllIamPoli…
…cies (#3168) * Add samples for SearchAllResources and SearchAllIamPolicies * Update error handling to catch specific exceptions. * Fix static analysis issues for the newly added files; Remove arguments that are not required by tests.
- Loading branch information
1 parent
1885c3a
commit 731190e
Showing
3 changed files
with
205 additions
and
0 deletions.
There are no files selected for viewing
58 changes: 58 additions & 0 deletions
58
asset/src/main/java/com/example/asset/SearchAllIamPoliciesExample.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
package com.example.asset; | ||
|
||
// [START asset_quickstart_search_all_iam_policies] | ||
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())); | ||
} | ||
} | ||
} | ||
// [END asset_quickstart_search_all_iam_policies] |
63 changes: 63 additions & 0 deletions
63
asset/src/main/java/com/example/asset/SearchAllResourcesExample.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
package com.example.asset; | ||
|
||
// [START asset_quickstart_search_all_resources] | ||
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.SearchAllResourcesPagedResponse; | ||
import com.google.cloud.asset.v1.SearchAllResourcesRequest; | ||
import java.io.IOException; | ||
import java.util.Arrays; | ||
|
||
public class SearchAllResourcesExample { | ||
|
||
// Searches for all the resources within the given scope. | ||
public static void searchAllResources(String scope, String query) { | ||
// TODO(developer): Replace these variables before running the sample. | ||
String[] assetTypes = {}; | ||
int pageSize = 0; | ||
String pageToken = ""; | ||
String orderBy = ""; | ||
|
||
SearchAllResourcesRequest request = | ||
SearchAllResourcesRequest.newBuilder() | ||
.setScope(scope) | ||
.setQuery(query) | ||
.addAllAssetTypes(Arrays.asList(assetTypes)) | ||
.setPageSize(pageSize) | ||
.setPageToken(pageToken) | ||
.setOrderBy(orderBy) | ||
.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()) { | ||
SearchAllResourcesPagedResponse response = client.searchAllResources(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 SearchAllResources:%n%s", e.toString())); | ||
} | ||
} | ||
} | ||
// [END asset_quickstart_search_all_resources] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
package com.example.asset; | ||
|
||
import static com.google.common.truth.Truth.assertThat; | ||
|
||
import com.google.cloud.bigquery.BigQuery; | ||
import com.google.cloud.bigquery.BigQuery.DatasetDeleteOption; | ||
import com.google.cloud.bigquery.BigQueryOptions; | ||
import com.google.cloud.bigquery.DatasetId; | ||
import com.google.cloud.bigquery.DatasetInfo; | ||
import com.google.cloud.bigquery.testing.RemoteBigQueryHelper; | ||
import java.io.ByteArrayOutputStream; | ||
import java.io.PrintStream; | ||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.JUnit4; | ||
|
||
/** Tests for search samples. */ | ||
@RunWith(JUnit4.class) | ||
@SuppressWarnings("checkstyle:abbreviationaswordinname") | ||
public class Search { | ||
|
||
private static final String projectId = System.getenv("GOOGLE_CLOUD_PROJECT"); | ||
private static final String datasetName = RemoteBigQueryHelper.generateDatasetName(); | ||
private ByteArrayOutputStream bout; | ||
private PrintStream out; | ||
private BigQuery bigquery; | ||
|
||
@Before | ||
public void setUp() { | ||
bigquery = BigQueryOptions.getDefaultInstance().getService(); | ||
if (bigquery.getDataset(datasetName) == null) { | ||
bigquery.create(DatasetInfo.newBuilder(datasetName).build()); | ||
} | ||
bout = new ByteArrayOutputStream(); | ||
out = new PrintStream(bout); | ||
System.setOut(out); | ||
} | ||
|
||
@After | ||
public void tearDown() { | ||
System.setOut(null); | ||
bout.reset(); | ||
DatasetId datasetId = DatasetId.of(bigquery.getOptions().getProjectId(), datasetName); | ||
bigquery.delete(datasetId, DatasetDeleteOption.deleteContents()); | ||
} | ||
|
||
@Test | ||
public void testSearchAllResourcesExample() throws Exception { | ||
// Wait 10 seconds to let dataset creation event go to CAI | ||
Thread.sleep(10000); | ||
String scope = "projects/" + projectId; | ||
String query = "name:" + datasetName; | ||
SearchAllResourcesExample.searchAllResources(scope, query); | ||
String got = bout.toString(); | ||
assertThat(got).contains(datasetName); | ||
} | ||
|
||
@Test | ||
public void testSearchAllIamPoliciesExample() throws Exception { | ||
String scope = "projects/" + projectId; | ||
String query = "policy:roles/owner"; | ||
SearchAllIamPoliciesExample.searchAllIamPolicies(scope, query); | ||
String got = bout.toString(); | ||
assertThat(got).contains("roles/owner"); | ||
} | ||
} |