Skip to content

Commit 462910e

Browse files
committed
Add factory methods for primitive roles, other minor fixes
1 parent 1431f0d commit 462910e

12 files changed

Lines changed: 73 additions & 25 deletions

File tree

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

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import com.google.common.collect.ImmutableMap;
2525
import com.google.common.collect.ImmutableSet;
2626
import com.google.common.collect.Lists;
27+
import com.google.common.io.BaseEncoding;
2728
import com.google.protobuf.ByteString;
2829

2930
import java.io.Serializable;
@@ -39,10 +40,10 @@
3940
import java.util.Set;
4041

4142
/**
42-
* Base class for Identity and Access Management (IAM) policies. IAM policies are used to specify
43-
* access settings for Cloud Platform resources. A policy is a map of bindings. A binding assigns
44-
* a set of identities to a role, where the identities can be user accounts, Google groups, Google
45-
* domains, and service accounts. A role is a named list of permissions defined by IAM.
43+
* Class for Identity and Access Management (IAM) policies. IAM policies are used to specify access
44+
* settings for Cloud Platform resources. A policy is a map of bindings. A binding assigns a set of
45+
* identities to a role, where the identities can be user accounts, Google groups, Google domains,
46+
* and service accounts. A role is a named list of permissions defined by IAM.
4647
*
4748
* @see <a href="https://cloud.google.com/iam/reference/rest/v1/Policy">Policy</a>
4849
*/
@@ -88,7 +89,8 @@ protected Policy fromPb(com.google.iam.v1.Policy policyPb) {
8889
}
8990
return builder()
9091
.bindings(bindings)
91-
.etag(policyPb.getEtag().size() == 0 ? null : policyPb.getEtag().toStringUtf8())
92+
.etag(policyPb.getEtag().isEmpty() ? null
93+
: BaseEncoding.base64().encode(policyPb.getEtag().toByteArray()))
9294
.version(policyPb.getVersion())
9395
.build();
9496
}
@@ -106,7 +108,7 @@ protected com.google.iam.v1.Policy toPb(Policy policy) {
106108
}
107109
policyBuilder.addAllBindings(bindingPbList);
108110
if (policy.etag != null) {
109-
policyBuilder.setEtag(ByteString.copyFromUtf8(policy.etag));
111+
policyBuilder.setEtag(ByteString.copyFrom(BaseEncoding.base64().decode(policy.etag)));
110112
}
111113
policyBuilder.setVersion(policy.version);
112114
return policyBuilder.build();
@@ -250,14 +252,14 @@ public Builder toBuilder() {
250252
}
251253

252254
/**
253-
* The map of bindings that comprises the policy.
255+
* Returns the map of bindings that comprises the policy.
254256
*/
255257
public Map<Role, Set<Identity>> bindings() {
256258
return bindings;
257259
}
258260

259261
/**
260-
* The policy's etag.
262+
* Returns the policy's etag.
261263
*
262264
* <p>Etags are used for optimistic concurrency control as a way to help prevent simultaneous
263265
* updates of a policy from overwriting each other. It is strongly suggested that systems make
@@ -272,8 +274,8 @@ public String etag() {
272274
}
273275

274276
/**
275-
* Sets the version of the policy. The default version is 0, meaning only the "owner", "editor",
276-
* and "viewer" roles are permitted. If the version is 1, you may also use other roles.
277+
* Returns the version of the policy. The default version is 0, meaning only the "owner",
278+
* "editor", and "viewer" roles are permitted. If the version is 1, you may also use other roles.
277279
*/
278280
public int version() {
279281
return version;

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
*
2828
* @see <a href="https://cloud.google.com/iam/docs/viewing-grantable-roles">Viewing the Grantable
2929
* Roles on Resources</a>
30+
* @see <a href="https://cloud.google.com/iam/docs/understanding-roles">Understanding Roles</a>
3031
*/
3132
public final class Role implements Serializable {
3233

@@ -47,6 +48,36 @@ public String value() {
4748
return value;
4849
}
4950

51+
/**
52+
* Returns the viewer role. Encapsulates the permission for read-only actions that preserve state
53+
* of a resource.
54+
*
55+
* @see <a href="https://cloud.google.com/iam/docs/understanding-roles">Understanding Roles</a>
56+
*/
57+
public static Role viewer() {
58+
return of("viewer");
59+
}
60+
61+
/**
62+
* Returns the editor role. Encapsulates all viewer's permissions and permissions for actions that
63+
* modify the state of a resource.
64+
*
65+
* @see <a href="https://cloud.google.com/iam/docs/understanding-roles">Understanding Roles</a>
66+
*/
67+
public static Role editor() {
68+
return of("editor");
69+
}
70+
71+
/**
72+
* Returns the owner role. Encapsulates all editor's permissions and permissions to manage access
73+
* control for a resource or manage the billing options for a project.
74+
*
75+
* @see <a href="https://cloud.google.com/iam/docs/understanding-roles">Understanding Roles</a>
76+
*/
77+
public static Role owner() {
78+
return of("owner");
79+
}
80+
5081
/**
5182
* Returns a new role given its string value.
5283
*

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@ public class PolicyTest {
4343
Identity.serviceAccount("[email protected]");
4444
private static final Identity GROUP = Identity.group("[email protected]");
4545
private static final Identity DOMAIN = Identity.domain("google.com");
46-
private static final Role VIEWER = Role.of("viewer");
47-
private static final Role EDITOR = Role.of("editor");
48-
private static final Role OWNER = Role.of("owner");
46+
private static final Role VIEWER = Role.viewer();
47+
private static final Role EDITOR = Role.editor();
48+
private static final Role OWNER = Role.owner();
4949
private static final Map<Role, ImmutableSet<Identity>> BINDINGS = ImmutableMap.of(
5050
VIEWER,
5151
ImmutableSet.of(USER, SERVICE_ACCOUNT, ALL_USERS),

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,21 @@ public void testOf() {
3636
compareRoles(OWNER, Role.of("roles/owner"));
3737
}
3838

39+
@Test
40+
public void testViewer() {
41+
assertEquals("roles/viewer", Role.viewer().value());
42+
}
43+
44+
@Test
45+
public void testEditor() {
46+
assertEquals("roles/editor", Role.editor().value());
47+
}
48+
49+
@Test
50+
public void testOwner() {
51+
assertEquals("roles/owner", Role.owner().value());
52+
}
53+
3954
@Test(expected = NullPointerException.class)
4055
public void testOfNullValue() {
4156
Role.of(null);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public class SerializationTest extends BaseSerializationTest {
3939
private static final SigningException SIGNING_EXCEPTION =
4040
new SigningException("message", BASE_SERVICE_EXCEPTION);
4141
private static final RetryParams RETRY_PARAMS = RetryParams.defaultInstance();
42-
private static final Role SOME_ROLE = Role.of("viewer");
42+
private static final Role SOME_ROLE = Role.viewer();
4343
private static final Policy SOME_IAM_POLICY = Policy.builder().build();
4444
private static final WaitForOption CHECKING_PERIOD =
4545
WaitForOption.checkEvery(42, TimeUnit.SECONDS);

google-cloud-examples/src/main/java/com/google/cloud/examples/resourcemanager/snippets/ModifyPolicy.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public static void main(String... args) {
4949
// Add a viewer
5050
Policy.Builder modifiedPolicy = policy.toBuilder();
5151
Identity newViewer = Identity.user("<insert user's email address here>");
52-
modifiedPolicy.addIdentity(Role.of("roles/viewer"), newViewer);
52+
modifiedPolicy.addIdentity(Role.viewer(), newViewer);
5353

5454
// Write policy
5555
Policy updatedPolicy = project.replacePolicy(modifiedPolicy.build());

google-cloud-resourcemanager/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ Policy policy = project.getPolicy();
186186
// Add a viewer
187187
Policy.Builder modifiedPolicy = policy.toBuilder();
188188
Identity newViewer = Identity.user("<insert user's email address here>");
189-
modifiedPolicy.addIdentity(Role.of("roles/viewer"), newViewer);
189+
modifiedPolicy.addIdentity(Role.viewer(), newViewer);
190190

191191
// Write policy
192192
Policy updatedPolicy = project.replacePolicy(modifiedPolicy.build());

google-cloud-resourcemanager/src/main/java/com/google/cloud/resourcemanager/PolicyMarshaller.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ final class PolicyMarshaller
3838

3939
private PolicyMarshaller() {}
4040

41-
private static class Builder extends com.google.cloud.Policy.Builder {
41+
private static class Builder extends Policy.Builder {
4242

4343
private Builder(Map<Role, Set<Identity>> bindings, String etag, Integer version) {
4444
bindings(bindings);

google-cloud-resourcemanager/src/main/java/com/google/cloud/resourcemanager/ResourceManager.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ public static ProjectListOption fields(ProjectField... fields) {
303303
* <pre> {@code
304304
* Policy currentPolicy = resourceManager.getPolicy("my-project-id");
305305
* Policy modifiedPolicy = current.toBuilder()
306-
* .removeIdentity(Role.of("roles/viewer"), Identity.user("[email protected]"))
306+
* .removeIdentity(Role.viewer(), Identity.user("[email protected]"))
307307
* .build();
308308
* Policy newPolicy = resourceManager.replacePolicy("my-project-id", modified);
309309
* }

google-cloud-resourcemanager/src/test/java/com/google/cloud/resourcemanager/PolicyMarshallerTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ public class PolicyMarshallerTest {
3535
Identity.serviceAccount("[email protected]");
3636
private static final Identity GROUP = Identity.group("[email protected]");
3737
private static final Identity DOMAIN = Identity.domain("google.com");
38-
private static final Role VIEWER = Role.of("viewer");
39-
private static final Role EDITOR = Role.of("editor");
40-
private static final Role OWNER = Role.of("owner");
38+
private static final Role VIEWER = Role.viewer();
39+
private static final Role EDITOR = Role.editor();
40+
private static final Role OWNER = Role.owner();
4141
private static final Role SOME_ROLE = Role.of("roles/some-role");
4242
private static final Policy SIMPLE_POLICY = Policy.builder()
4343
.addIdentity(OWNER, USER)

0 commit comments

Comments
 (0)