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..202f85af29a --- /dev/null +++ b/asset/cloud-client/src/main/java/com/example/asset/SearchAllIamPoliciesExample.java @@ -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] 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..fa961008c19 --- /dev/null +++ b/asset/cloud-client/src/main/java/com/example/asset/SearchAllResourcesExample.java @@ -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] 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..c15c08c4645 --- /dev/null +++ b/asset/cloud-client/src/test/java/com/example/asset/Search.java @@ -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"); + } +}