Skip to content

Commit dfb9ec3

Browse files
[asset] feat: add samples for SearchAllResources and SearchAllIamPolicies (GoogleCloudPlatform#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.
1 parent a282d4e commit dfb9ec3

File tree

3 files changed

+205
-0
lines changed

3 files changed

+205
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Copyright 2020 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.asset;
18+
19+
// [START asset_quickstart_search_all_iam_policies]
20+
import com.google.api.gax.rpc.ApiException;
21+
import com.google.api.gax.rpc.InvalidArgumentException;
22+
import com.google.cloud.asset.v1.AssetServiceClient;
23+
import com.google.cloud.asset.v1.AssetServiceClient.SearchAllIamPoliciesPagedResponse;
24+
import com.google.cloud.asset.v1.SearchAllIamPoliciesRequest;
25+
import java.io.IOException;
26+
27+
public class SearchAllIamPoliciesExample {
28+
29+
// Searches for all the iam policies within the given scope.
30+
public static void searchAllIamPolicies(String scope, String query) {
31+
// TODO(developer): Replace these variables before running the sample.
32+
int pageSize = 0;
33+
String pageToken = "";
34+
35+
SearchAllIamPoliciesRequest request =
36+
SearchAllIamPoliciesRequest.newBuilder()
37+
.setScope(scope)
38+
.setQuery(query)
39+
.setPageSize(pageSize)
40+
.setPageToken(pageToken)
41+
.build();
42+
43+
// Initialize client that will be used to send requests. This client only needs to be created
44+
// once, and can be reused for multiple requests. After completing all of your requests, call
45+
// the "close" method on the client to safely clean up any remaining background resources.
46+
try (AssetServiceClient client = AssetServiceClient.create()) {
47+
SearchAllIamPoliciesPagedResponse response = client.searchAllIamPolicies(request);
48+
System.out.println("Search completed successfully:\n" + response.getPage().getValues());
49+
} catch (IOException e) {
50+
System.out.println(String.format("Failed to create client:%n%s", e.toString()));
51+
} catch (InvalidArgumentException e) {
52+
System.out.println(String.format("Invalid request:%n%s", e.toString()));
53+
} catch (ApiException e) {
54+
System.out.println(String.format("Error during SearchAllIamPolicies:%n%s", e.toString()));
55+
}
56+
}
57+
}
58+
// [END asset_quickstart_search_all_iam_policies]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* Copyright 2020 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.asset;
18+
19+
// [START asset_quickstart_search_all_resources]
20+
import com.google.api.gax.rpc.ApiException;
21+
import com.google.api.gax.rpc.InvalidArgumentException;
22+
import com.google.cloud.asset.v1.AssetServiceClient;
23+
import com.google.cloud.asset.v1.AssetServiceClient.SearchAllResourcesPagedResponse;
24+
import com.google.cloud.asset.v1.SearchAllResourcesRequest;
25+
import java.io.IOException;
26+
import java.util.Arrays;
27+
28+
public class SearchAllResourcesExample {
29+
30+
// Searches for all the resources within the given scope.
31+
public static void searchAllResources(String scope, String query) {
32+
// TODO(developer): Replace these variables before running the sample.
33+
String[] assetTypes = {};
34+
int pageSize = 0;
35+
String pageToken = "";
36+
String orderBy = "";
37+
38+
SearchAllResourcesRequest request =
39+
SearchAllResourcesRequest.newBuilder()
40+
.setScope(scope)
41+
.setQuery(query)
42+
.addAllAssetTypes(Arrays.asList(assetTypes))
43+
.setPageSize(pageSize)
44+
.setPageToken(pageToken)
45+
.setOrderBy(orderBy)
46+
.build();
47+
48+
// Initialize client that will be used to send requests. This client only needs to be created
49+
// once, and can be reused for multiple requests. After completing all of your requests, call
50+
// the "close" method on the client to safely clean up any remaining background resources.
51+
try (AssetServiceClient client = AssetServiceClient.create()) {
52+
SearchAllResourcesPagedResponse response = client.searchAllResources(request);
53+
System.out.println("Search completed successfully:\n" + response.getPage().getValues());
54+
} catch (IOException e) {
55+
System.out.println(String.format("Failed to create client:%n%s", e.toString()));
56+
} catch (InvalidArgumentException e) {
57+
System.out.println(String.format("Invalid request:%n%s", e.toString()));
58+
} catch (ApiException e) {
59+
System.out.println(String.format("Error during SearchAllResources:%n%s", e.toString()));
60+
}
61+
}
62+
}
63+
// [END asset_quickstart_search_all_resources]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
* Copyright 2020 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.asset;
18+
19+
import static com.google.common.truth.Truth.assertThat;
20+
21+
import com.google.cloud.bigquery.BigQuery;
22+
import com.google.cloud.bigquery.BigQuery.DatasetDeleteOption;
23+
import com.google.cloud.bigquery.BigQueryOptions;
24+
import com.google.cloud.bigquery.DatasetId;
25+
import com.google.cloud.bigquery.DatasetInfo;
26+
import com.google.cloud.bigquery.testing.RemoteBigQueryHelper;
27+
import java.io.ByteArrayOutputStream;
28+
import java.io.PrintStream;
29+
import org.junit.After;
30+
import org.junit.Before;
31+
import org.junit.Test;
32+
import org.junit.runner.RunWith;
33+
import org.junit.runners.JUnit4;
34+
35+
/** Tests for search samples. */
36+
@RunWith(JUnit4.class)
37+
@SuppressWarnings("checkstyle:abbreviationaswordinname")
38+
public class Search {
39+
40+
private static final String projectId = System.getenv("GOOGLE_CLOUD_PROJECT");
41+
private static final String datasetName = RemoteBigQueryHelper.generateDatasetName();
42+
private ByteArrayOutputStream bout;
43+
private PrintStream out;
44+
private BigQuery bigquery;
45+
46+
@Before
47+
public void setUp() {
48+
bigquery = BigQueryOptions.getDefaultInstance().getService();
49+
if (bigquery.getDataset(datasetName) == null) {
50+
bigquery.create(DatasetInfo.newBuilder(datasetName).build());
51+
}
52+
bout = new ByteArrayOutputStream();
53+
out = new PrintStream(bout);
54+
System.setOut(out);
55+
}
56+
57+
@After
58+
public void tearDown() {
59+
System.setOut(null);
60+
bout.reset();
61+
DatasetId datasetId = DatasetId.of(bigquery.getOptions().getProjectId(), datasetName);
62+
bigquery.delete(datasetId, DatasetDeleteOption.deleteContents());
63+
}
64+
65+
@Test
66+
public void testSearchAllResourcesExample() throws Exception {
67+
// Wait 10 seconds to let dataset creation event go to CAI
68+
Thread.sleep(10000);
69+
String scope = "projects/" + projectId;
70+
String query = "name:" + datasetName;
71+
SearchAllResourcesExample.searchAllResources(scope, query);
72+
String got = bout.toString();
73+
assertThat(got).contains(datasetName);
74+
}
75+
76+
@Test
77+
public void testSearchAllIamPoliciesExample() throws Exception {
78+
String scope = "projects/" + projectId;
79+
String query = "policy:roles/owner";
80+
SearchAllIamPoliciesExample.searchAllIamPolicies(scope, query);
81+
String got = bout.toString();
82+
assertThat(got).contains("roles/owner");
83+
}
84+
}

0 commit comments

Comments
 (0)