2121import com .google .common .collect .ImmutableList ;
2222
2323import java .io .Serializable ;
24- import java .util .Arrays ;
25- import java .util .LinkedList ;
24+ import java .util .HashMap ;
2625import java .util .List ;
26+ import java .util .Map ;
2727import 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}
0 commit comments