Skip to content

Commit 53d2792

Browse files
author
Ajay Kannan
committed
---
yaml --- r: 1725 b: refs/heads/master c: e44e12a h: refs/heads/master i: 1723: d45c1b3
1 parent 29536f0 commit 53d2792

10 files changed

Lines changed: 361 additions & 488 deletions

File tree

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
refs/heads/master: c96469b6a135619708f08526c2f0d5cc50396c80
2+
refs/heads/master: e44e12a2b996d1649704a9ce124f670c23bd6c7e
33
refs/heads/travis: e21ee7b88a5edc3f3d8c71f90c3fc32abf7e8dd6
44
refs/heads/gh-pages: d1b373c30c176edc08692348167bec3a244bb823
55
refs/heads/bigquery: 762fa5830e6c398c0396177e3e7fd243bd62cfc3

trunk/gcloud-java-core/src/main/java/com/google/gcloud/IamPolicy.java renamed to trunk/gcloud-java-core/src/main/java/com/google/gcloud/BaseIamPolicy.java

Lines changed: 51 additions & 194 deletions
Original file line numberDiff line numberDiff line change
@@ -21,29 +21,28 @@
2121
import com.google.common.collect.ImmutableList;
2222

2323
import java.io.Serializable;
24-
import java.util.Arrays;
25-
import java.util.LinkedList;
24+
import java.util.HashMap;
2625
import java.util.List;
26+
import java.util.Map;
2727
import java.util.Objects;
2828

2929
/**
30-
* An Identity and Access Management (IAM) policy. It is used to specify access control policies for
31-
* Cloud Platform resources. A Policy consists of a list of ACLs (also known as bindings in Cloud
32-
* IAM documentation). An ACL binds a list of identities to a role, where the identities can be user
33-
* accounts, Google groups, Google domains, and service accounts. A role is a named list of
34-
* permissions defined by IAM.
30+
* Base class for Identity and Access Management (IAM) policies. IAM policies are used to specify
31+
* access settings for Cloud Platform resources. A Policy consists of a list of bindings. An binding
32+
* assigns a list of identities to a role, where the identities can be user accounts, Google groups,
33+
* Google domains, and service accounts. A role is a named list of permissions defined by IAM.
3534
*
3635
* @see <a href="https://cloud.google.com/iam/reference/rest/v1/Policy">Policy</a>
3736
*/
38-
public class IamPolicy implements Serializable {
37+
public abstract class BaseIamPolicy<R> implements Serializable {
3938

40-
static final long serialVersionUID = 1114489978726897720L;
39+
private static final long serialVersionUID = 1114489978726897720L;
4140

42-
private final List<Acl> acls;
41+
private final Map<R, List<Identity>> bindings;
4342
private final String etag;
4443
private final int version;
4544

46-
public static class Identity implements Serializable {
45+
public static final class Identity implements Serializable {
4746

4847
private static final long serialVersionUID = 30811617560110848L;
4948

@@ -85,7 +84,7 @@ public enum Type {
8584
DOMAIN
8685
}
8786

88-
Identity(Type type, String id) {
87+
private Identity(Type type, String id) {
8988
this.type = type;
9089
this.id = id;
9190
}
@@ -178,177 +177,38 @@ public boolean equals(Object obj) {
178177
}
179178
}
180179

181-
/**
182-
* An ACL binds a list of identities to a role, where the identities can be user accounts, Google
183-
* groups, Google domains, and service accounts. A role is a named list of permissions defined by
184-
* IAM.
185-
*
186-
* @see <a href="https://cloud.google.com/iam/reference/rest/v1/Policy#Binding">Binding</a>
187-
*/
188-
public static class Acl implements Serializable {
189-
190-
private static final long serialVersionUID = 3954282899483745158L;
191-
192-
private final List<Identity> identities;
193-
private final String role;
194-
195-
/**
196-
* An ACL builder.
197-
*/
198-
public static class Builder {
199-
private final List<Identity> members = new LinkedList<>();
200-
private String role;
201-
202-
Builder(String role) {
203-
this.role = role;
204-
}
205-
206-
/**
207-
* Sets the role associated with this ACL.
208-
*/
209-
public Builder role(String role) {
210-
this.role = role;
211-
return this;
212-
}
213-
214-
/**
215-
* Replaces the builder's list of identities with the given list.
216-
*/
217-
public Builder identities(List<Identity> identities) {
218-
this.members.clear();
219-
this.members.addAll(identities);
220-
return this;
221-
}
222-
223-
/**
224-
* Adds one or more identities to the list of identities associated with the ACL.
225-
*/
226-
public Builder addIdentity(Identity first, Identity... others) {
227-
members.add(first);
228-
members.addAll(Arrays.asList(others));
229-
return this;
230-
}
231-
232-
/**
233-
* Removes the specified identity from the ACL.
234-
*/
235-
public Builder removeIdentity(Identity identity) {
236-
members.remove(identity);
237-
return this;
238-
}
239-
240-
public Acl build() {
241-
return new Acl(this);
242-
}
243-
}
244-
245-
Acl(Builder builder) {
246-
identities = ImmutableList.copyOf(checkNotNull(builder.members));
247-
role = checkNotNull(builder.role);
248-
}
249-
250-
/**
251-
* Returns the list of identities associated with this ACL.
252-
*/
253-
public List<Identity> identities() {
254-
return identities;
255-
}
256-
257-
/**
258-
* Returns the role associated with this ACL.
259-
*/
260-
public String role() {
261-
return role;
262-
}
263-
264-
/**
265-
* Returns an ACL builder for the specific role type.
266-
*
267-
* @param role string representing the role, without the "roles/" prefix. An example of a valid
268-
* legacy role is "viewer". An example of a valid service-specific role is
269-
* "pubsub.publisher".
270-
*/
271-
public static Builder builder(String role) {
272-
return new Builder(role);
273-
}
274-
275-
/**
276-
* Returns an ACL for the role type and list of identities provided.
277-
*
278-
* @param role string representing the role, without the "roles/" prefix. An example of a valid
279-
* legacy role is "viewer". An example of a valid service-specific role is
280-
* "pubsub.publisher".
281-
* @param members list of identities associated with the role.
282-
*/
283-
public static Acl of(String role, List<Identity> members) {
284-
return new Acl(new Builder(role).identities(members));
285-
}
286-
287-
/**
288-
* Returns an ACL for the role type and identities provided.
289-
*
290-
* @param role string representing the role, without the "roles/" prefix. An example of a valid
291-
* legacy role is "viewer". An example of a valid service-specific role is
292-
* "pubsub.publisher".
293-
* @param first identity associated with the role.
294-
* @param others any other identities associated with the role.
295-
*/
296-
public static Acl of(String role, Identity first, Identity... others) {
297-
return new Acl(new Builder(role).addIdentity(first, others));
298-
}
299-
300-
public Builder toBuilder() {
301-
return new Builder(role).identities(identities);
302-
}
303-
304-
@Override
305-
public int hashCode() {
306-
return Objects.hash(identities, role);
307-
}
308-
309-
@Override
310-
public boolean equals(Object obj) {
311-
if (!(obj instanceof Acl)) {
312-
return false;
313-
}
314-
Acl other = (Acl) obj;
315-
return Objects.equals(identities, other.identities()) && Objects.equals(role, other.role());
316-
}
317-
}
318-
319180
/**
320181
* Builder for an IAM Policy.
321182
*/
322-
public static class Builder {
183+
protected abstract static class BaseBuilder<R, B extends BaseBuilder<R, B>> {
323184

324-
private final List<Acl> acls = new LinkedList<>();
185+
private final Map<R, List<Identity>> bindings = new HashMap<>();
325186
private String etag;
326187
private int version;
327188

328189
/**
329-
* Replaces the builder's list of ACLs with the given list of ACLs.
190+
* Replaces the builder's list of bindings with the given list of bindings.
330191
*/
331-
public Builder acls(List<Acl> acls) {
332-
this.acls.clear();
333-
this.acls.addAll(acls);
334-
return this;
192+
public B bindings(Map<R, List<Identity>> bindings) {
193+
this.bindings.clear();
194+
this.bindings.putAll(bindings);
195+
return self();
335196
}
336197

337198
/**
338-
* Adds one or more ACLs to the policy.
199+
* Adds one or more bindings to the policy.
339200
*/
340-
public Builder addAcl(Acl first, Acl... others) {
341-
acls.add(first);
342-
acls.addAll(Arrays.asList(others));
343-
return this;
201+
public B addBinding(R role, List<Identity> identities) {
202+
bindings.put(role, ImmutableList.copyOf(identities));
203+
return self();
344204
}
345205

346206
/**
347207
* Removes the specified ACL.
348208
*/
349-
public Builder removeAcl(Acl acl) {
350-
acls.remove(acl);
351-
return this;
209+
public B removeBinding(R role) {
210+
bindings.remove(role);
211+
return self();
352212
}
353213

354214
/**
@@ -362,35 +222,40 @@ public Builder removeAcl(Acl acl) {
362222
* applied to the same version of the policy. If no etag is provided in the call to
363223
* setIamPolicy, then the existing policy is overwritten blindly.
364224
*/
365-
public Builder etag(String etag) {
225+
protected B etag(String etag) {
366226
this.etag = etag;
367-
return this;
227+
return self();
368228
}
369229

370230
/**
371-
* Sets the version of the policy. The default version is 0.
231+
* Sets the version of the policy. The default version is 0, meaning roles that are in alpha
232+
* (non-legacy) roles are not permitted. If the version is 1, you may use roles other than
233+
* "owner", "editor", and "viewer".
372234
*/
373-
public Builder version(int version) {
235+
protected B version(int version) {
374236
this.version = version;
375-
return this;
237+
return self();
376238
}
377239

378-
public IamPolicy build() {
379-
return new IamPolicy(this);
240+
@SuppressWarnings("unchecked")
241+
private B self() {
242+
return (B) this;
380243
}
244+
245+
public abstract BaseIamPolicy<R> build();
381246
}
382247

383-
IamPolicy(Builder builder) {
384-
acls = ImmutableList.copyOf(builder.acls);
385-
etag = builder.etag;
386-
version = builder.version;
248+
protected BaseIamPolicy(BaseBuilder<R, ? extends BaseBuilder<R, ?>> builder) {
249+
this.bindings = builder.bindings;
250+
this.etag = builder.etag;
251+
this.version = builder.version;
387252
}
388253

389254
/**
390255
* The list of ACLs specified in the policy.
391256
*/
392-
public List<Acl> acls() {
393-
return acls;
257+
public Map<R, List<Identity>> bindings() {
258+
return bindings;
394259
}
395260

396261
/**
@@ -415,26 +280,18 @@ public int version() {
415280
return version;
416281
}
417282

418-
@Override
419-
public int hashCode() {
420-
return Objects.hash(acls, etag, version);
283+
public int baseHashCode() {
284+
return Objects.hash(bindings, etag, version);
421285
}
422286

423-
@Override
424-
public boolean equals(Object obj) {
425-
if (!(obj instanceof IamPolicy)) {
287+
public boolean baseEquals(Object obj) {
288+
if (!(obj instanceof BaseIamPolicy)) {
426289
return false;
427290
}
428-
IamPolicy other = (IamPolicy) obj;
429-
return Objects.equals(acls, other.acls()) && Objects.equals(etag, other.etag())
291+
@SuppressWarnings("rawtypes")
292+
BaseIamPolicy other = (BaseIamPolicy) obj;
293+
return Objects.equals(bindings, other.bindings())
294+
&& Objects.equals(etag, other.etag())
430295
&& Objects.equals(version, other.version());
431296
}
432-
433-
public static Builder builder() {
434-
return new Builder();
435-
}
436-
437-
public Builder toBuilder() {
438-
return new Builder().acls(acls).etag(etag).version(version);
439-
}
440297
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright 2015 Google Inc. All Rights Reserved.
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.google.gcloud;
18+
19+
import static org.junit.Assert.assertEquals;
20+
21+
import com.google.gcloud.BaseIamPolicy.Identity;
22+
23+
import org.junit.Test;
24+
25+
public class BaseIamPolicyTest {
26+
27+
private static final Identity ALL_USERS = Identity.allUsers();
28+
private static final Identity ALL_AUTH_USERS = Identity.allAuthenticatedUsers();
29+
private static final Identity USER = Identity.user("[email protected]");
30+
private static final Identity SERVICE_ACCOUNT =
31+
Identity.serviceAccount("[email protected]");
32+
private static final Identity GROUP = Identity.group("[email protected]");
33+
private static final Identity DOMAIN = Identity.domain("google.com");
34+
35+
@Test
36+
public void testIdentityOf() {
37+
assertEquals(Identity.Type.ALL_USERS, ALL_USERS.type());
38+
assertEquals(null, ALL_USERS.id());
39+
assertEquals(Identity.Type.ALL_AUTHENTICATED_USERS, ALL_AUTH_USERS.type());
40+
assertEquals(null, ALL_AUTH_USERS.id());
41+
assertEquals(Identity.Type.USER, USER.type());
42+
assertEquals("[email protected]", USER.id());
43+
assertEquals(Identity.Type.SERVICE_ACCOUNT, SERVICE_ACCOUNT.type());
44+
assertEquals("[email protected]", SERVICE_ACCOUNT.id());
45+
assertEquals(Identity.Type.GROUP, GROUP.type());
46+
assertEquals("[email protected]", GROUP.id());
47+
assertEquals(Identity.Type.DOMAIN, DOMAIN.type());
48+
assertEquals("google.com", DOMAIN.id());
49+
}
50+
}

0 commit comments

Comments
 (0)