Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[asset] feat: add samples for SearchAllResources and SearchAllIamPolicies #3168

Merged
merged 3 commits into from
Jun 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion asset/cloud-client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-asset</artifactId>
<version>1.2.0</version>
<version>1.3.0</version>
</dependency>
<!-- [END asset_java_dependencies] -->

Expand Down
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]
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]
84 changes: 84 additions & 0 deletions asset/cloud-client/src/test/java/com/example/asset/Search.java
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");
}
}