Skip to content

Commit 924301d

Browse files
authored
core: allow custom roles (#2863)
Role previously mandated that all role strings starts with "role/". If the given string doesn't start with the prefix, Role prepends it. This is incorrect, since custom roles have format "projects/X/roles/Y". This PR makes Role a little smarter at prepending: we only prepend if the given string contains no slash; this keeps the previously-valid use valid. Fixes #2687.
1 parent b6125eb commit 924301d

2 files changed

Lines changed: 25 additions & 15 deletions

File tree

google-cloud-core/src/main/java/com/google/cloud/Role.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,15 +82,22 @@ public static Role owner() {
8282
/**
8383
* Returns a new role given its string value.
8484
*
85-
* @param value the string value for the role, for example, {@code "roles/viewer"},
86-
* {@code "roles/editor"}, or {@code "roles/owner"}. If this value does not start with the
87-
* role prefix {@code roles/}, the prefix is prepended.
85+
* <p>If the value contains no slash character ({@code '/'}), the prefix {@code "roles/""} is
86+
* prepended. This slightly simplifies usage for <a
87+
* href="https://cloud.google.com/iam/docs/understanding-roles>"predefined roles</a>. For <a
88+
* href="https://cloud.google.com/iam/docs/creating-custom-roles">custom roles</a>, call this
89+
* method with the fully-qualified name, eg {@code "projects/XXX/roles/YYY"}.
90+
*
91+
* @param value the string value for the role
8892
* @see <a href="https://cloud.google.com/iam/docs/viewing-grantable-roles">Viewing the Grantable
8993
* Roles on Resources</a>
9094
*/
9195
public static Role of(String value) {
9296
checkNotNull(value);
93-
return new Role(value.startsWith(ROLE_PREFIX) ? value : ROLE_PREFIX + value);
97+
if (!value.contains("/")) {
98+
value = ROLE_PREFIX + value;
99+
}
100+
return new Role(value);
94101
}
95102

96103
@Override

google-cloud-core/src/test/java/com/google/cloud/RoleTest.java

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
package com.google.cloud;
1818

19-
import static org.junit.Assert.assertEquals;
19+
import static com.google.common.truth.Truth.assertThat;
2020

2121
import org.junit.Test;
2222

@@ -28,28 +28,31 @@ public class RoleTest {
2828

2929
@Test
3030
public void testOf() {
31-
assertEquals("roles/viewer", VIEWER.getValue());
32-
assertEquals("roles/editor", EDITOR.getValue());
33-
assertEquals("roles/owner", OWNER.getValue());
31+
assertThat(VIEWER.getValue()).isEqualTo("roles/viewer");
32+
assertThat(EDITOR.getValue()).isEqualTo("roles/editor");
33+
assertThat(OWNER.getValue()).isEqualTo("roles/owner");
3434
compareRoles(VIEWER, Role.of("roles/viewer"));
3535
compareRoles(EDITOR, Role.of("roles/editor"));
3636
compareRoles(OWNER, Role.of("roles/owner"));
37+
38+
String customRole = "projects/foo/roles/bar";
39+
assertThat(Role.of(customRole).getValue()).isEqualTo(customRole);
3740
}
3841

3942

4043
@Test
4144
public void testViewer() {
42-
assertEquals("roles/viewer", Role.viewer().getValue());
45+
assertThat(Role.viewer().getValue()).isEqualTo("roles/viewer");
4346
}
4447

4548
@Test
4649
public void testEditor() {
47-
assertEquals("roles/editor", Role.editor().getValue());
50+
assertThat(Role.editor().getValue()).isEqualTo("roles/editor");
4851
}
4952

5053
@Test
5154
public void testOwner() {
52-
assertEquals("roles/owner", Role.owner().getValue());
55+
assertThat(Role.owner().getValue()).isEqualTo("roles/owner");
5356
}
5457

5558
@Test(expected = NullPointerException.class)
@@ -58,9 +61,9 @@ public void testOfNullValue() {
5861
}
5962

6063
private void compareRoles(Role expected, Role actual) {
61-
assertEquals(expected, actual);
62-
assertEquals(expected.getValue(), actual.getValue());
63-
assertEquals(expected.hashCode(), actual.hashCode());
64-
assertEquals(expected.toString(), actual.toString());
64+
assertThat(actual).isEqualTo(expected);
65+
assertThat(actual.getValue()).isEqualTo(expected.getValue());
66+
assertThat(actual.hashCode()).isEqualTo(expected.hashCode());
67+
assertThat(actual.toString()).isEqualTo(expected.toString());
6568
}
6669
}

0 commit comments

Comments
 (0)