Skip to content

Commit a78aae9

Browse files
authored
moved files from "update IAM policy samples to use GAPIC library" PR (GoogleCloudPlatform#9319)
* moved all files from "update IAM policy samples to use GAPIC library" PR to snippets module * merge with main, create TestPermissionsTest * remove unused test case * added license header * fix lint * fix lint
1 parent e3be391 commit a78aae9

14 files changed

+75
-38
lines changed

iam/api-client/pom.xml

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -109,12 +109,6 @@
109109
<artifactId>google-iam-admin</artifactId>
110110
<scope>compile</scope>
111111
</dependency>
112-
<dependency>
113-
<groupId>com.google.cloud</groupId>
114-
<artifactId>google-cloud-resourcemanager</artifactId>
115-
<scope>compile</scope>
116-
</dependency>
117-
118112
</dependencies>
119113

120114
<build>
@@ -124,7 +118,7 @@
124118
<artifactId>exec-maven-plugin</artifactId>
125119
<version>3.1.1</version>
126120
<configuration>
127-
<mainClass>iam.snippets.Quickstart</mainClass>
121+
<mainClass>iam.snippets.CreateServiceAccount</mainClass>
128122
</configuration>
129123
</plugin>
130124
</plugins>
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/* Copyright 2024 Google LLC
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
package iam.snippets;
17+
18+
import static org.hamcrest.MatcherAssert.assertThat;
19+
import static org.hamcrest.core.StringContains.containsString;
20+
import static org.junit.Assert.assertNotNull;
21+
22+
import java.io.ByteArrayOutputStream;
23+
import java.io.PrintStream;
24+
import org.junit.After;
25+
import org.junit.Before;
26+
import org.junit.BeforeClass;
27+
import org.junit.Test;
28+
import org.junit.runner.RunWith;
29+
import org.junit.runners.JUnit4;
30+
31+
32+
@RunWith(JUnit4.class)
33+
public class TestPermissionsTest {
34+
private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT");
35+
private ByteArrayOutputStream bout;
36+
37+
private static void requireEnvVar(String varName) {
38+
assertNotNull(
39+
System.getenv(varName),
40+
String.format("Environment variable '%s' is required to perform these tests.", varName));
41+
}
42+
43+
@BeforeClass
44+
public static void checkRequirementsAndInitServiceAccount() {
45+
requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS");
46+
requireEnvVar("GOOGLE_CLOUD_PROJECT");
47+
}
48+
49+
@Before
50+
public void beforeTest() {
51+
bout = new ByteArrayOutputStream();
52+
System.setOut(new PrintStream(bout));
53+
}
54+
55+
@After
56+
public void tearDown() {
57+
System.setOut(null);
58+
bout.reset();
59+
}
60+
61+
@Test
62+
public void testTestPermissions() {
63+
TestPermissions.testPermissions("projects/" + PROJECT_ID);
64+
String got = bout.toString();
65+
assertThat(
66+
got,
67+
containsString("Of the permissions listed in the request, the caller has the following: "));
68+
}
69+
}

iam/snippets/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@
6161
<artifactId>google-iam-admin</artifactId>
6262
<scope>compile</scope>
6363
</dependency>
64+
<dependency>
65+
<groupId>com.google.cloud</groupId>
66+
<artifactId>google-cloud-resourcemanager</artifactId>
67+
<scope>compile</scope>
68+
</dependency>
6469

6570
<!-- Test dependencies -->
6671
<dependency>

iam/api-client/src/main/java/iam/snippets/AddBinding.java renamed to iam/snippets/src/main/java/AddBinding.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@
1313
* limitations under the License.
1414
*/
1515

16-
package iam.snippets;
17-
1816
// [START iam_modify_policy_add_role]
1917

2018
import com.google.iam.v1.Binding;

iam/api-client/src/main/java/iam/snippets/AddMember.java renamed to iam/snippets/src/main/java/AddMember.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@
1313
* limitations under the License.
1414
*/
1515

16-
package iam.snippets;
17-
1816
// [START iam_modify_policy_add_member]
1917
import com.google.iam.v1.Binding;
2018
import com.google.iam.v1.Policy;

iam/api-client/src/main/java/iam/snippets/GetProjectPolicy.java renamed to iam/snippets/src/main/java/GetProjectPolicy.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@
1313
* limitations under the License.
1414
*/
1515

16-
package iam.snippets;
17-
1816
// [START iam_get_policy]
1917
import com.google.cloud.resourcemanager.v3.ProjectsClient;
2018
import com.google.iam.admin.v1.ProjectName;

iam/api-client/src/main/java/iam/snippets/GetServiceAccountPolicy.java renamed to iam/snippets/src/main/java/GetServiceAccountPolicy.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@
1313
* limitations under the License.
1414
*/
1515

16-
package iam.snippets;
17-
1816
// [START iam_service_account_get_policy]
1917

2018
import com.google.cloud.iam.admin.v1.IAMClient;

iam/api-client/src/main/java/iam/snippets/Quickstart.java renamed to iam/snippets/src/main/java/Quickstart.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@
1313
* limitations under the License.
1414
*/
1515

16-
package iam.snippets;
17-
1816
// [START iam_quickstart]
1917

2018
import com.google.cloud.iam.admin.v1.IAMClient;

iam/api-client/src/main/java/iam/snippets/RemoveMember.java renamed to iam/snippets/src/main/java/RemoveMember.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@
1313
* limitations under the License.
1414
*/
1515

16-
package iam.snippets;
17-
1816
// [START iam_modify_policy_remove_member]
1917
import com.google.iam.v1.Binding;
2018
import com.google.iam.v1.Policy;

iam/api-client/src/main/java/iam/snippets/SetProjectPolicy.java renamed to iam/snippets/src/main/java/SetProjectPolicy.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@
1313
* limitations under the License.
1414
*/
1515

16-
package iam.snippets;
17-
1816
// [START iam_set_policy]
1917
import com.google.cloud.resourcemanager.v3.ProjectsClient;
2018
import com.google.iam.admin.v1.ProjectName;

iam/api-client/src/main/java/iam/snippets/SetServiceAccountPolicy.java renamed to iam/snippets/src/main/java/SetServiceAccountPolicy.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@
1313
* limitations under the License.
1414
*/
1515

16-
package iam.snippets;
17-
1816
// [START iam_service_account_set_policy]
1917
import com.google.cloud.iam.admin.v1.IAMClient;
2018
import com.google.iam.admin.v1.ServiceAccountName;

iam/api-client/src/test/java/iam/snippets/AccessTests.java renamed to iam/snippets/src/test/java/AccessTests.java

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,7 @@
1313
* limitations under the License.
1414
*/
1515

16-
package iam.snippets;
17-
1816
import static org.hamcrest.MatcherAssert.assertThat;
19-
import static org.hamcrest.core.StringContains.containsString;
2017
import static org.junit.Assert.assertNotNull;
2118

2219
import com.google.cloud.iam.admin.v1.IAMClient;
@@ -185,13 +182,4 @@ public void testRemoveMember() {
185182
}
186183
assertThat("policy should not contain member", memberRemoved);
187184
}
188-
189-
@Test
190-
public void testTestPermissions() {
191-
TestPermissions.testPermissions("projects/" + PROJECT_ID);
192-
String got = bout.toString();
193-
assertThat(
194-
got,
195-
containsString("Of the permissions listed in the request, the caller has the following: "));
196-
}
197185
}

iam/api-client/src/test/java/iam/snippets/QuickstartTests.java renamed to iam/snippets/src/test/java/QuickstartTests.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@
1313
* limitations under the License.
1414
*/
1515

16-
package iam.snippets;
17-
1816
import static org.hamcrest.CoreMatchers.not;
1917
import static org.hamcrest.MatcherAssert.assertThat;
2018
import static org.hamcrest.core.IsCollectionContaining.hasItem;

iam/snippets/src/test/java/ServiceAccountTests.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323

2424
import com.google.cloud.iam.admin.v1.IAMClient;
2525
import com.google.cloud.testing.junit4.MultipleAttemptsRule;
26-
import com.google.common.collect.Lists;
2726
import com.google.iam.admin.v1.ServiceAccount;
2827
import com.google.iam.admin.v1.ServiceAccountKey;
2928
import java.io.ByteArrayOutputStream;

0 commit comments

Comments
 (0)