Skip to content

Commit 301fc8a

Browse files
chore(samples): Add BatchGetEffectiveIamPolicy code samples (#1325)
* feat: add BatchGetEffectiveIamPolicy code samples * refactor: Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
1 parent fca0ce5 commit 301fc8a

File tree

3 files changed

+120
-0
lines changed

3 files changed

+120
-0
lines changed

java-asset/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ Samples are in the [`samples/`](https://github.com/googleapis/java-asset/tree/ma
111111
| Analyze Iam Policy Longrunning Bigquery Example | [source code](https://github.com/googleapis/java-asset/blob/main/samples/snippets/src/main/java/com/example/asset/AnalyzeIamPolicyLongrunningBigqueryExample.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-asset&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/asset/AnalyzeIamPolicyLongrunningBigqueryExample.java) |
112112
| Analyze Iam Policy Longrunning Gcs Example | [source code](https://github.com/googleapis/java-asset/blob/main/samples/snippets/src/main/java/com/example/asset/AnalyzeIamPolicyLongrunningGcsExample.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-asset&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/asset/AnalyzeIamPolicyLongrunningGcsExample.java) |
113113
| Batch Get Assets History Example | [source code](https://github.com/googleapis/java-asset/blob/main/samples/snippets/src/main/java/com/example/asset/BatchGetAssetsHistoryExample.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-asset&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/asset/BatchGetAssetsHistoryExample.java) |
114+
| Batch Get Effective Iam Policy Example | [source code](https://github.com/googleapis/java-asset/blob/main/samples/snippets/src/main/java/com/example/asset/BatchGetEffectiveIamPolicyExample.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-asset&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/asset/BatchGetEffectiveIamPolicyExample.java) |
114115
| Create Feed Example | [source code](https://github.com/googleapis/java-asset/blob/main/samples/snippets/src/main/java/com/example/asset/CreateFeedExample.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-asset&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/asset/CreateFeedExample.java) |
115116
| Delete Feed Example | [source code](https://github.com/googleapis/java-asset/blob/main/samples/snippets/src/main/java/com/example/asset/DeleteFeedExample.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-asset&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/asset/DeleteFeedExample.java) |
116117
| Export Assets Bigquery Example | [source code](https://github.com/googleapis/java-asset/blob/main/samples/snippets/src/main/java/com/example/asset/ExportAssetsBigqueryExample.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-asset&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/asset/ExportAssetsBigqueryExample.java) |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright 2022 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_batch_get_effective_iam_policy]
20+
// Imports the Google Cloud client library
21+
22+
import com.google.api.gax.rpc.ApiException;
23+
import com.google.cloud.asset.v1.AssetServiceClient;
24+
import com.google.cloud.asset.v1.BatchGetEffectiveIamPoliciesRequest;
25+
import com.google.cloud.asset.v1.BatchGetEffectiveIamPoliciesResponse;
26+
import java.io.IOException;
27+
import java.util.Arrays;
28+
29+
/** Batch get effective iam policy example. */
30+
public class BatchGetEffectiveIamPolicyExample {
31+
32+
/**
33+
* Batch get effective iam policies specified list of resources within accessible scope, such as a
34+
* project, folder or organization.
35+
*
36+
* @param resourceNames a string array denoting full resource names.
37+
* @param scope a string denoting scope, which can be a Project, Folder or Organization.
38+
*/
39+
public static void batchGetEffectiveIamPolicies(String[] resourceNames, String scope) {
40+
BatchGetEffectiveIamPoliciesRequest request =
41+
BatchGetEffectiveIamPoliciesRequest.newBuilder()
42+
.setScope(scope)
43+
.addAllNames(Arrays.asList(resourceNames))
44+
.build();
45+
try (AssetServiceClient client = AssetServiceClient.create()) {
46+
BatchGetEffectiveIamPoliciesResponse response = client.batchGetEffectiveIamPolicies(request);
47+
System.out.println("BatchGetEffectiveIamPolicies completed successfully:\n" + response);
48+
} catch (IOException e) {
49+
System.out.println("Failed to create client:\n" + e);
50+
} catch (ApiException e) {
51+
System.out.println("Error during BatchGetEffectiveIamPolicies:\n" + e);
52+
}
53+
}
54+
}
55+
// [END asset_quickstart_batch_get_effective_iam_policy]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* Copyright 2022 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 java.io.ByteArrayOutputStream;
22+
import java.io.PrintStream;
23+
import org.junit.After;
24+
import org.junit.Before;
25+
import org.junit.Test;
26+
import org.junit.runner.RunWith;
27+
import org.junit.runners.JUnit4;
28+
29+
/** Tests for batch get effective iam policy sample. */
30+
@RunWith(JUnit4.class)
31+
@SuppressWarnings("checkstyle:abbreviationaswordinname")
32+
public class BatchGetEffectiveIamPolicy {
33+
34+
private static final String projectId = System.getenv("GOOGLE_CLOUD_PROJECT");
35+
private static final String scope = "projects/" + projectId;
36+
private static final String[] resourceNames = {
37+
"//cloudresourcemanager.googleapis.com/projects/" + projectId
38+
};
39+
40+
private ByteArrayOutputStream bout;
41+
private PrintStream originalPrintStream;
42+
43+
@Before
44+
public void setUp() {
45+
bout = new ByteArrayOutputStream();
46+
PrintStream out = new PrintStream(bout);
47+
originalPrintStream = System.out;
48+
System.setOut(out);
49+
}
50+
51+
@After
52+
public void tearDown() {
53+
// restores print statements in the original method
54+
System.out.flush();
55+
System.setOut(originalPrintStream);
56+
}
57+
58+
@Test
59+
public void testBatchGetEffectiveIamPolicyExample() {
60+
BatchGetEffectiveIamPolicyExample.batchGetEffectiveIamPolicies(resourceNames, scope);
61+
String got = bout.toString();
62+
assertThat(got).contains(resourceNames[0]);
63+
}
64+
}

0 commit comments

Comments
 (0)