From c316055fc92556f5780074fd8d4bce73ace662a0 Mon Sep 17 00:00:00 2001 From: yuyifan-google Date: Fri, 12 Jun 2020 14:14:35 -0700 Subject: [PATCH 1/3] Add samples for SearchAllResources and SearchAllIamPolicies --- asset/cloud-client/pom.xml | 2 +- .../asset/SearchAllIamPoliciesExample.java | 44 +++++++++ .../asset/SearchAllResourcesExample.java | 51 +++++++++++ .../test/java/com/example/asset/Search.java | 91 +++++++++++++++++++ 4 files changed, 187 insertions(+), 1 deletion(-) create mode 100644 asset/cloud-client/src/main/java/com/example/asset/SearchAllIamPoliciesExample.java create mode 100644 asset/cloud-client/src/main/java/com/example/asset/SearchAllResourcesExample.java create mode 100644 asset/cloud-client/src/test/java/com/example/asset/Search.java diff --git a/asset/cloud-client/pom.xml b/asset/cloud-client/pom.xml index 6cbd76a096a..2f55f60fd63 100644 --- a/asset/cloud-client/pom.xml +++ b/asset/cloud-client/pom.xml @@ -35,7 +35,7 @@ com.google.cloud google-cloud-asset - 1.2.0 + 1.3.0 diff --git a/asset/cloud-client/src/main/java/com/example/asset/SearchAllIamPoliciesExample.java b/asset/cloud-client/src/main/java/com/example/asset/SearchAllIamPoliciesExample.java new file mode 100644 index 00000000000..1ae1c0cf4b7 --- /dev/null +++ b/asset/cloud-client/src/main/java/com/example/asset/SearchAllIamPoliciesExample.java @@ -0,0 +1,44 @@ +/* + * 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.cloud.asset.v1.AssetServiceClient; +import com.google.cloud.asset.v1.AssetServiceClient.SearchAllIamPoliciesPagedResponse; +import com.google.cloud.asset.v1.SearchAllIamPoliciesRequest; + +public class SearchAllIamPoliciesExample { + + public static void searchAllIamPolicies( + String scope, String query, int pageSize, String pageToken) { + SearchAllIamPoliciesRequest request = + SearchAllIamPoliciesRequest.newBuilder() + .setScope(scope) + .setQuery(query) + .setPageSize(pageSize) + .setPageToken(pageToken) + .build(); + try (AssetServiceClient client = AssetServiceClient.create()) { + SearchAllIamPoliciesPagedResponse response = client.searchAllIamPolicies(request); + System.out.println("Search completed successfully: \n" + response.getPage().getValues()); + } catch (Exception e) { + System.out.println("Error during SearchAllIamPolicies: \n" + e.toString()); + } + } +} +// [END asset_quickstart_search_all_iam_policies] diff --git a/asset/cloud-client/src/main/java/com/example/asset/SearchAllResourcesExample.java b/asset/cloud-client/src/main/java/com/example/asset/SearchAllResourcesExample.java new file mode 100644 index 00000000000..9e496694acc --- /dev/null +++ b/asset/cloud-client/src/main/java/com/example/asset/SearchAllResourcesExample.java @@ -0,0 +1,51 @@ +/* + * 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.cloud.asset.v1.AssetServiceClient; +import com.google.cloud.asset.v1.AssetServiceClient.SearchAllResourcesPagedResponse; +import com.google.cloud.asset.v1.SearchAllResourcesRequest; +import java.util.Arrays; + +public class SearchAllResourcesExample { + + public static void searchAllResources( + String scope, + String query, + String[] assetTypes, + int pageSize, + String pageToken, + String orderBy) { + SearchAllResourcesRequest request = + SearchAllResourcesRequest.newBuilder() + .setScope(scope) + .setQuery(query) + .addAllAssetTypes(Arrays.asList(assetTypes)) + .setPageSize(pageSize) + .setPageToken(pageToken) + .setOrderBy(orderBy) + .build(); + try (AssetServiceClient client = AssetServiceClient.create()) { + SearchAllResourcesPagedResponse response = client.searchAllResources(request); + System.out.println("Search completed: \n" + response.getPage().getValues()); + } catch (Exception e) { + System.out.println("Error during SearchAllResources: \n" + e.toString()); + } + } +} +// [END asset_quickstart_search_all_resources] diff --git a/asset/cloud-client/src/test/java/com/example/asset/Search.java b/asset/cloud-client/src/test/java/com/example/asset/Search.java new file mode 100644 index 00000000000..e4f17857ce4 --- /dev/null +++ b/asset/cloud-client/src/test/java/com/example/asset/Search.java @@ -0,0 +1,91 @@ +/* + * 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; + String[] assetTypes = {}; + int pageSize = 0; + String pageToken = ""; + String orderBy = ""; + SearchAllResourcesExample.searchAllResources( + scope, query, assetTypes, pageSize, pageToken, orderBy); + String got = bout.toString(); + assertThat(got).contains(datasetName); + } + + @Test + public void testSearchAllIamPoliciesExample() throws Exception { + String scope = "projects/" + projectId; + String query = "policy:roles/owner"; + int pageSize = 0; + String pageToken = ""; + SearchAllIamPoliciesExample.searchAllIamPolicies(scope, query, pageSize, pageToken); + String got = bout.toString(); + assertThat(got).contains("roles/owner"); + } +} From b502620e25b816256fb556c210435495e1474adf Mon Sep 17 00:00:00 2001 From: yuyifan-google Date: Fri, 12 Jun 2020 18:02:28 -0700 Subject: [PATCH 2/3] Update error handling to catch specific exceptions. --- .../example/asset/SearchAllIamPoliciesExample.java | 14 ++++++++++---- .../example/asset/SearchAllResourcesExample.java | 13 ++++++++++--- 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/asset/cloud-client/src/main/java/com/example/asset/SearchAllIamPoliciesExample.java b/asset/cloud-client/src/main/java/com/example/asset/SearchAllIamPoliciesExample.java index 1ae1c0cf4b7..5635a2ec320 100644 --- a/asset/cloud-client/src/main/java/com/example/asset/SearchAllIamPoliciesExample.java +++ b/asset/cloud-client/src/main/java/com/example/asset/SearchAllIamPoliciesExample.java @@ -17,10 +17,12 @@ 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 { @@ -35,9 +37,13 @@ public static void searchAllIamPolicies( .build(); try (AssetServiceClient client = AssetServiceClient.create()) { SearchAllIamPoliciesPagedResponse response = client.searchAllIamPolicies(request); - System.out.println("Search completed successfully: \n" + response.getPage().getValues()); - } catch (Exception e) { - System.out.println("Error during SearchAllIamPolicies: \n" + e.toString()); + System.out.println("Search completed successfully:\n" + response.getPage().getValues()); + } catch (IOException e) { + System.out.println("Failed to create client:\n" + e.toString()); + } catch (InvalidArgumentException e) { + System.out.println("Invalid request:\n" + e.toString()); + } catch (ApiException e) { + System.out.println("Error during SearchAllIamPolicies:\n" + e.toString()); } } } diff --git a/asset/cloud-client/src/main/java/com/example/asset/SearchAllResourcesExample.java b/asset/cloud-client/src/main/java/com/example/asset/SearchAllResourcesExample.java index 9e496694acc..4f46a70a335 100644 --- a/asset/cloud-client/src/main/java/com/example/asset/SearchAllResourcesExample.java +++ b/asset/cloud-client/src/main/java/com/example/asset/SearchAllResourcesExample.java @@ -17,9 +17,12 @@ 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 { @@ -42,9 +45,13 @@ public static void searchAllResources( .build(); try (AssetServiceClient client = AssetServiceClient.create()) { SearchAllResourcesPagedResponse response = client.searchAllResources(request); - System.out.println("Search completed: \n" + response.getPage().getValues()); - } catch (Exception e) { - System.out.println("Error during SearchAllResources: \n" + e.toString()); + System.out.println("Search completed successfully:\n" + response.getPage().getValues()); + } catch (IOException e) { + System.out.println("Failed to create client:\n" + e.toString()); + } catch (InvalidArgumentException e) { + System.out.println("Invalid request:\n" + e.toString()); + } catch (ApiException e) { + System.out.println("Error during SearchAllResources:\n" + e.toString()); } } } From 1e39a3a2324b0f92d4e942ff4bc1f68eb1d571b9 Mon Sep 17 00:00:00 2001 From: yuyifan-google Date: Fri, 12 Jun 2020 18:52:16 -0700 Subject: [PATCH 3/3] Fix static analysis issues for the newly added files; Remove arguments that are not required by tests. --- .../asset/SearchAllIamPoliciesExample.java | 18 +++++++++---- .../asset/SearchAllResourcesExample.java | 25 +++++++++++-------- .../test/java/com/example/asset/Search.java | 11 ++------ 3 files changed, 30 insertions(+), 24 deletions(-) diff --git a/asset/cloud-client/src/main/java/com/example/asset/SearchAllIamPoliciesExample.java b/asset/cloud-client/src/main/java/com/example/asset/SearchAllIamPoliciesExample.java index 5635a2ec320..202f85af29a 100644 --- a/asset/cloud-client/src/main/java/com/example/asset/SearchAllIamPoliciesExample.java +++ b/asset/cloud-client/src/main/java/com/example/asset/SearchAllIamPoliciesExample.java @@ -26,8 +26,12 @@ public class SearchAllIamPoliciesExample { - public static void searchAllIamPolicies( - String scope, String query, int pageSize, String pageToken) { + // 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) @@ -35,15 +39,19 @@ public static void searchAllIamPolicies( .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("Failed to create client:\n" + e.toString()); + System.out.println(String.format("Failed to create client:%n%s", e.toString())); } catch (InvalidArgumentException e) { - System.out.println("Invalid request:\n" + e.toString()); + System.out.println(String.format("Invalid request:%n%s", e.toString())); } catch (ApiException e) { - System.out.println("Error during SearchAllIamPolicies:\n" + e.toString()); + System.out.println(String.format("Error during SearchAllIamPolicies:%n%s", e.toString())); } } } diff --git a/asset/cloud-client/src/main/java/com/example/asset/SearchAllResourcesExample.java b/asset/cloud-client/src/main/java/com/example/asset/SearchAllResourcesExample.java index 4f46a70a335..fa961008c19 100644 --- a/asset/cloud-client/src/main/java/com/example/asset/SearchAllResourcesExample.java +++ b/asset/cloud-client/src/main/java/com/example/asset/SearchAllResourcesExample.java @@ -27,13 +27,14 @@ public class SearchAllResourcesExample { - public static void searchAllResources( - String scope, - String query, - String[] assetTypes, - int pageSize, - String pageToken, - String orderBy) { + // 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) @@ -43,15 +44,19 @@ public static void searchAllResources( .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("Failed to create client:\n" + e.toString()); + System.out.println(String.format("Failed to create client:%n%s", e.toString())); } catch (InvalidArgumentException e) { - System.out.println("Invalid request:\n" + e.toString()); + System.out.println(String.format("Invalid request:%n%s", e.toString())); } catch (ApiException e) { - System.out.println("Error during SearchAllResources:\n" + e.toString()); + System.out.println(String.format("Error during SearchAllResources:%n%s", e.toString())); } } } diff --git a/asset/cloud-client/src/test/java/com/example/asset/Search.java b/asset/cloud-client/src/test/java/com/example/asset/Search.java index e4f17857ce4..c15c08c4645 100644 --- a/asset/cloud-client/src/test/java/com/example/asset/Search.java +++ b/asset/cloud-client/src/test/java/com/example/asset/Search.java @@ -68,12 +68,7 @@ public void testSearchAllResourcesExample() throws Exception { Thread.sleep(10000); String scope = "projects/" + projectId; String query = "name:" + datasetName; - String[] assetTypes = {}; - int pageSize = 0; - String pageToken = ""; - String orderBy = ""; - SearchAllResourcesExample.searchAllResources( - scope, query, assetTypes, pageSize, pageToken, orderBy); + SearchAllResourcesExample.searchAllResources(scope, query); String got = bout.toString(); assertThat(got).contains(datasetName); } @@ -82,9 +77,7 @@ public void testSearchAllResourcesExample() throws Exception { public void testSearchAllIamPoliciesExample() throws Exception { String scope = "projects/" + projectId; String query = "policy:roles/owner"; - int pageSize = 0; - String pageToken = ""; - SearchAllIamPoliciesExample.searchAllIamPolicies(scope, query, pageSize, pageToken); + SearchAllIamPoliciesExample.searchAllIamPolicies(scope, query); String got = bout.toString(); assertThat(got).contains("roles/owner"); }