-
Notifications
You must be signed in to change notification settings - Fork 2.9k
[asset] feat: add samples for SearchAllResources and SearchAllIamPolicies #3168
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
44 changes: 44 additions & 0 deletions
44
asset/cloud-client/src/main/java/com/example/asset/SearchAllIamPoliciesExample.java
This file contains hidden or 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,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] |
51 changes: 51 additions & 0 deletions
51
asset/cloud-client/src/main/java/com/example/asset/SearchAllResourcesExample.java
This file contains hidden or 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,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()); | ||
} | ||
yuyifan-google marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
// [END asset_quickstart_search_all_resources] |
91 changes: 91 additions & 0 deletions
91
asset/cloud-client/src/test/java/com/example/asset/Search.java
This file contains hidden or 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,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"); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.