Skip to content

Commit cceda32

Browse files
authored
Add UUID for IAM tests (GoogleCloudPlatform#3610)
Fixes GoogleCloudPlatform#3605 > It's a good idea to open an issue first for discussion. - [ ] I have followed [Sample Format Guide](https://github.com/GoogleCloudPlatform/java-docs-samples/blob/master/SAMPLE_FORMAT.md) - [ ] `pom.xml` parent set to latest `shared-configuration` - [ ] Appropriate changes to README are included in PR - [ ] API's need to be enabled to test (tell us) - [ ] Environment Variables need to be set (ask us to set them) - [ ] **Tests** pass: `mvn clean verify` **required** - [ ] **Lint** passes: `mvn -P lint checkstyle:check` **required** - [ ] **Static Analysis**: `mvn -P lint clean compile pmd:cpd-check spotbugs:check` **advisory only** - [ ] Please **merge** this PR for me once it is approved.
1 parent a743970 commit cceda32

File tree

9 files changed

+46
-68
lines changed

9 files changed

+46
-68
lines changed

iam/api-client/src/main/java/iam/snippets/CreateServiceAccount.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,9 @@
3131
public class CreateServiceAccount {
3232

3333
// Creates a service account.
34-
public static void createServiceAccount(String projectId) {
34+
public static void createServiceAccount(String projectId, String serviceAccountName) {
3535
// String projectId = "my-project-id";
36+
// String serviceAccountName = "my-service-account-name";
3637

3738
Iam service = null;
3839
try {
@@ -46,7 +47,7 @@ public static void createServiceAccount(String projectId) {
4647
ServiceAccount serviceAccount = new ServiceAccount();
4748
serviceAccount.setDisplayName("your-display-name");
4849
CreateServiceAccountRequest request = new CreateServiceAccountRequest();
49-
request.setAccountId("your-service-account-name");
50+
request.setAccountId(serviceAccountName);
5051
request.setServiceAccount(serviceAccount);
5152

5253
serviceAccount =

iam/api-client/src/main/java/iam/snippets/CreateServiceAccountKey.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,9 @@
3131
public class CreateServiceAccountKey {
3232

3333
// Creates a key for a service account.
34-
public static void createKey(String projectId) {
34+
public static void createKey(String projectId, String serviceAccountName) {
3535
// String projectId = "my-project-id";
36+
// String serviceAccountName = "my-service-account-name";
3637

3738
Iam service = null;
3839
try {
@@ -42,16 +43,15 @@ public static void createKey(String projectId) {
4243
return;
4344
}
4445

46+
String serviceAccountEmail = serviceAccountName + "@" + projectId + ".iam.gserviceaccount.com";
4547
try {
4648
ServiceAccountKey key =
4749
service
4850
.projects()
4951
.serviceAccounts()
5052
.keys()
5153
.create(
52-
"projects/-/serviceAccounts/your-service-account-name@"
53-
+ projectId
54-
+ ".iam.gserviceaccount.com",
54+
"projects/-/serviceAccounts/" + serviceAccountEmail,
5555
new CreateServiceAccountKeyRequest())
5656
.execute();
5757

iam/api-client/src/main/java/iam/snippets/DeleteServiceAccount.java

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,9 @@
2929
public class DeleteServiceAccount {
3030

3131
// Deletes a service account.
32-
public static void deleteServiceAccount(String projectId) {
32+
public static void deleteServiceAccount(String projectId, String serviceAccountName) {
3333
// String projectId = "my-project-id";
34+
// String serviceAccountName = "my-service-account-name";
3435

3536
Iam service = null;
3637
try {
@@ -40,22 +41,15 @@ public static void deleteServiceAccount(String projectId) {
4041
return;
4142
}
4243

44+
String serviceAccountEmail = serviceAccountName + "@" + projectId + ".iam.gserviceaccount.com";
4345
try {
4446
service
4547
.projects()
4648
.serviceAccounts()
47-
.delete(
48-
"projects/-/serviceAccounts/"
49-
+ "your-service-account-name@"
50-
+ projectId
51-
+ ".iam.gserviceaccount.com")
49+
.delete("projects/-/serviceAccounts/" + serviceAccountEmail)
5250
.execute();
5351

54-
System.out.println(
55-
"Deleted service account: "
56-
+ "your-service-account-name@"
57-
+ projectId
58-
+ ".iam.gserviceaccount.com");
52+
System.out.println("Deleted service account: " + serviceAccountEmail);
5953
} catch (IOException e) {
6054
System.out.println("Unable to delete service account: \n" + e.toString());
6155
}

iam/api-client/src/main/java/iam/snippets/DeleteServiceAccountKey.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,9 @@
3131
public class DeleteServiceAccountKey {
3232

3333
// Deletes a service account key.
34-
public static void deleteKey(String projectId) {
34+
public static void deleteKey(String projectId, String serviceAccountName) {
3535
// String projectId = "my-project-id";
36+
// String serviceAccountName = "my-service-account-name";
3637

3738
Iam service = null;
3839
try {
@@ -42,18 +43,15 @@ public static void deleteKey(String projectId) {
4243
return;
4344
}
4445

46+
String serviceAccountEmail = serviceAccountName + "@" + projectId + ".iam.gserviceaccount.com";
4547
try {
4648
// First, get the name of the key using List() or Get()
4749
List<ServiceAccountKey> keys =
4850
service
4951
.projects()
5052
.serviceAccounts()
5153
.keys()
52-
.list(
53-
"projects/-/serviceAccounts/"
54-
+ "your-service-account-name@"
55-
+ projectId
56-
+ ".iam.gserviceaccount.com")
54+
.list("projects/-/serviceAccounts/" + serviceAccountEmail)
5755
.execute()
5856
.getKeys();
5957
String keyToDelete = keys.get(0).getName();

iam/api-client/src/main/java/iam/snippets/DisableServiceAccount.java

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,9 @@
3030
public class DisableServiceAccount {
3131

3232
// Disables a service account.
33-
public static void disableServiceAccount(String projectId) {
33+
public static void disableServiceAccount(String projectId, String serviceAccountName) {
3434
// String projectId = "my-project-id";
35+
// String serviceAccountName = "my-service-account-name";
3536

3637
Iam service = null;
3738
try {
@@ -41,24 +42,16 @@ public static void disableServiceAccount(String projectId) {
4142
return;
4243
}
4344

45+
String serviceAccountEmail = serviceAccountName + "@" + projectId + ".iam.gserviceaccount.com";
4446
try {
4547
DisableServiceAccountRequest request = new DisableServiceAccountRequest();
4648
service
4749
.projects()
4850
.serviceAccounts()
49-
.disable(
50-
"projects/-/serviceAccounts/"
51-
+ "your-service-account-name@"
52-
+ projectId
53-
+ ".iam.gserviceaccount.com",
54-
request)
51+
.disable("projects/-/serviceAccounts/" + serviceAccountEmail, request)
5552
.execute();
5653

57-
System.out.println(
58-
"Disabled service account: "
59-
+ "your-service-account-name@"
60-
+ projectId
61-
+ ".iam.gserviceaccount.com");
54+
System.out.println("Disabled service account: " + serviceAccountEmail);
6255
} catch (IOException e) {
6356
System.out.println("Unable to disable service account: \n" + e.toString());
6457
}

iam/api-client/src/main/java/iam/snippets/EnableServiceAccount.java

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,9 @@
3030
public class EnableServiceAccount {
3131

3232
// Enables a service account.
33-
public static void enableServiceAccount(String projectId) {
33+
public static void enableServiceAccount(String projectId, String serviceAccountName) {
3434
// String projectId = "my-project-id";
35+
// String serviceAccountName = "my-service-account-name";
3536

3637
Iam service = null;
3738
try {
@@ -41,24 +42,16 @@ public static void enableServiceAccount(String projectId) {
4142
return;
4243
}
4344

45+
String serviceAccountEmail = serviceAccountName + "@" + projectId + ".iam.gserviceaccount.com";
4446
try {
4547
EnableServiceAccountRequest request = new EnableServiceAccountRequest();
4648
service
4749
.projects()
4850
.serviceAccounts()
49-
.enable(
50-
"projects/-/serviceAccounts/"
51-
+ "your-service-account-name@"
52-
+ projectId
53-
+ ".iam.gserviceaccount.com",
54-
request)
51+
.enable("projects/-/serviceAccounts/" + serviceAccountEmail, request)
5552
.execute();
5653

57-
System.out.println(
58-
"Enabled service account: "
59-
+ "your-service-account-name@"
60-
+ projectId
61-
+ ".iam.gserviceaccount.com");
54+
System.out.println("Enabled service account: " + serviceAccountEmail);
6255
} catch (IOException e) {
6356
System.out.println("Unable to enable service account: \n" + e.toString());
6457
}

iam/api-client/src/main/java/iam/snippets/ListServiceAccountKeys.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,9 @@
3131
public class ListServiceAccountKeys {
3232

3333
// Lists all keys for a service account.
34-
public static void listKeys(String projectId) {
34+
public static void listKeys(String projectId, String serviceAccountName) {
3535
// String projectId = "my-project-id";
36+
// String serviceAccountName = "my-service-account-name";
3637

3738
Iam service = null;
3839
try {
@@ -42,17 +43,14 @@ public static void listKeys(String projectId) {
4243
return;
4344
}
4445

46+
String serviceAccountEmail = serviceAccountName + "@" + projectId + ".iam.gserviceaccount.com";
4547
try {
4648
List<ServiceAccountKey> keys =
4749
service
4850
.projects()
4951
.serviceAccounts()
5052
.keys()
51-
.list(
52-
"projects/-/serviceAccounts/"
53-
+ "your-service-account-name@"
54-
+ projectId
55-
+ ".iam.gserviceaccount.com")
53+
.list("projects/-/serviceAccounts/" + serviceAccountEmail)
5654
.execute()
5755
.getKeys();
5856

iam/api-client/src/main/java/iam/snippets/RenameServiceAccount.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,9 @@
3030
public class RenameServiceAccount {
3131

3232
// Changes a service account's display name.
33-
public static void renameServiceAccount(String projectId) {
33+
public static void renameServiceAccount(String projectId, String serviceAccountName) {
3434
// String projectId = "my-project-id";
35+
// String serviceAccountName = "my-service-account-name";
3536

3637
Iam service = null;
3738
try {
@@ -41,17 +42,14 @@ public static void renameServiceAccount(String projectId) {
4142
return;
4243
}
4344

45+
String serviceAccountEmail = serviceAccountName + "@" + projectId + ".iam.gserviceaccount.com";
4446
try {
4547
// First, get a service account using List() or Get()
4648
ServiceAccount serviceAccount =
4749
service
4850
.projects()
4951
.serviceAccounts()
50-
.get(
51-
"projects/-/serviceAccounts/"
52-
+ "your-service-account-name@"
53-
+ projectId
54-
+ ".iam.gserviceaccount.com")
52+
.get("projects/-/serviceAccounts/" + serviceAccountEmail)
5553
.execute();
5654

5755
// Then you can update the display name

iam/api-client/src/test/java/iam/snippets/ServiceAccountTests.java

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import java.io.ByteArrayOutputStream;
2323
import java.io.PrintStream;
24+
import java.util.UUID;
2425
import org.junit.After;
2526
import org.junit.Before;
2627
import org.junit.BeforeClass;
@@ -36,6 +37,8 @@ public class ServiceAccountTests {
3637

3738
private ByteArrayOutputStream bout;
3839
private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT");
40+
private static final String SERVICE_ACCOUNT =
41+
"service-account-" + UUID.randomUUID().toString().substring(0, 8);
3942

4043
private static void requireEnvVar(String varName) {
4144
assertNotNull(
@@ -63,9 +66,9 @@ public void tearDown() {
6366

6467
@Test
6568
public void stage1_testServiceAccountCreate() {
66-
CreateServiceAccount.createServiceAccount(PROJECT_ID);
69+
CreateServiceAccount.createServiceAccount(PROJECT_ID, SERVICE_ACCOUNT);
6770
String got = bout.toString();
68-
assertThat(got, containsString("Created service account: your-service-account-name"));
71+
assertThat(got, containsString("Created service account: " + SERVICE_ACCOUNT));
6972
}
7073

7174
@Test
@@ -77,49 +80,49 @@ public void stage1_testServiceAccountsList() {
7780

7881
@Test
7982
public void stage2_testServiceAccountRename() {
80-
RenameServiceAccount.renameServiceAccount(PROJECT_ID);
83+
RenameServiceAccount.renameServiceAccount(PROJECT_ID, SERVICE_ACCOUNT);
8184
String got = bout.toString();
8285
assertThat(got, containsString("Updated display name"));
8386
}
8487

8588
@Test
8689
public void stage2_testServiceAccountKeyCreate() {
87-
CreateServiceAccountKey.createKey(PROJECT_ID);
90+
CreateServiceAccountKey.createKey(PROJECT_ID, SERVICE_ACCOUNT);
8891
String got = bout.toString();
8992
assertThat(got, containsString("Created key:"));
9093
}
9194

9295
@Test
9396
public void stage2_testServiceAccountKeysList() {
94-
ListServiceAccountKeys.listKeys(PROJECT_ID);
97+
ListServiceAccountKeys.listKeys(PROJECT_ID, SERVICE_ACCOUNT);
9598
String got = bout.toString();
9699
assertThat(got, containsString("Key:"));
97100
}
98101

99102
@Test
100103
public void stage3_testServiceAccountKeyDelete() {
101-
DeleteServiceAccountKey.deleteKey(PROJECT_ID);
104+
DeleteServiceAccountKey.deleteKey(PROJECT_ID, SERVICE_ACCOUNT);
102105
String got = bout.toString();
103106
assertThat(got, containsString("Deleted key:"));
104107
}
105108

106109
@Test
107110
public void stage4_testDisableServiceAccount() {
108-
DisableServiceAccount.disableServiceAccount(PROJECT_ID);
111+
DisableServiceAccount.disableServiceAccount(PROJECT_ID, SERVICE_ACCOUNT);
109112
String got = bout.toString();
110113
assertThat(got, containsString("Disabled service account:"));
111114
}
112115

113116
@Test
114117
public void stage5_testEnableServiceAccount() {
115-
EnableServiceAccount.enableServiceAccount(PROJECT_ID);
118+
EnableServiceAccount.enableServiceAccount(PROJECT_ID, SERVICE_ACCOUNT);
116119
String got = bout.toString();
117120
assertThat(got, containsString("Enabled service account:"));
118121
}
119122

120123
@Test
121124
public void stage6_testServiceAccountDelete() {
122-
DeleteServiceAccount.deleteServiceAccount(PROJECT_ID);
125+
DeleteServiceAccount.deleteServiceAccount(PROJECT_ID, SERVICE_ACCOUNT);
123126
String got = bout.toString();
124127
assertThat(got, containsString("Deleted service account:"));
125128
}

0 commit comments

Comments
 (0)