Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for create/get/update/delete/list ACLs for blob and bucket #1228

Merged
merged 4 commits into from
Sep 12, 2016
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Add support for create/get/update/delete/list ACLs for blob and bucket
mziccard committed Sep 7, 2016
commit 720a4789e6128b68fc648bd36833f7d0ea3c8953
Original file line number Diff line number Diff line change
@@ -17,6 +17,8 @@
package com.google.cloud.storage.contrib.nio;

import com.google.api.services.storage.model.Bucket;
import com.google.api.services.storage.model.BucketAccessControl;
import com.google.api.services.storage.model.ObjectAccessControl;
import com.google.api.services.storage.model.StorageObject;
import com.google.cloud.storage.Storage;
import com.google.cloud.storage.StorageException;
@@ -336,6 +338,81 @@ public RewriteResponse continueRewrite(RewriteResponse previousResponse) throws
throw new UnsupportedOperationException();
}

@Override
public BucketAccessControl getAcl(String bucket, String entity) {
throw new UnsupportedOperationException();
}

@Override
public boolean deleteAcl(String bucket, String entity) {
throw new UnsupportedOperationException();
}

@Override
public BucketAccessControl createAcl(BucketAccessControl acl) {
throw new UnsupportedOperationException();
}

@Override
public BucketAccessControl patchAcl(BucketAccessControl acl) {
throw new UnsupportedOperationException();
}

@Override
public List<BucketAccessControl> listAcls(String bucket) {
throw new UnsupportedOperationException();
}

@Override
public ObjectAccessControl getDefaultAcl(String bucket, String entity) {
throw new UnsupportedOperationException();
}

@Override
public boolean deleteDefaultAcl(String bucket, String entity) {
throw new UnsupportedOperationException();
}

@Override
public ObjectAccessControl createDefaultAcl(ObjectAccessControl acl) {
throw new UnsupportedOperationException();
}

@Override
public ObjectAccessControl patchDefaultAcl(ObjectAccessControl acl) {
throw new UnsupportedOperationException();
}

@Override
public List<ObjectAccessControl> listDefaultAcls(String bucket) {
throw new UnsupportedOperationException();
}

@Override
public ObjectAccessControl getAcl(String bucket, String object, Long generation, String entity) {
throw new UnsupportedOperationException();
}

@Override
public boolean deleteAcl(String bucket, String object, Long generation, String entity) {
throw new UnsupportedOperationException();
}

@Override
public ObjectAccessControl createAcl(ObjectAccessControl acl) {
throw new UnsupportedOperationException();
}

@Override
public ObjectAccessControl patchAcl(ObjectAccessControl acl) {
throw new UnsupportedOperationException();
}

@Override
public List<ObjectAccessControl> listAcls(String bucket, String object, Long generation) {
throw new UnsupportedOperationException();
}

private String fullname(StorageObject so) {
return (so.getBucket() + "/" + so.getName());
}
2 changes: 1 addition & 1 deletion google-cloud-storage/pom.xml
Original file line number Diff line number Diff line change
@@ -25,7 +25,7 @@
<dependency>
<groupId>com.google.apis</groupId>
<artifactId>google-api-services-storage</artifactId>
<version>v1-rev62-1.21.0</version>
<version>v1-rev82-1.22.0</version>
<scope>compile</scope>
<exclusions>
<exclusion>
152 changes: 144 additions & 8 deletions google-cloud-storage/src/main/java/com/google/cloud/storage/Acl.java
Original file line number Diff line number Diff line change
@@ -16,8 +16,12 @@

package com.google.cloud.storage;

import static com.google.common.base.Preconditions.checkNotNull;

import com.google.api.services.storage.model.BucketAccessControl;
import com.google.api.services.storage.model.ObjectAccessControl;
import com.google.common.base.Function;
import com.google.common.base.MoreObjects;

import java.io.Serializable;
import java.util.Objects;
@@ -32,13 +36,86 @@ public final class Acl implements Serializable {

private static final long serialVersionUID = 6435575339887912222L;

static final Function<ObjectAccessControl, Acl> FROM_OBJECT_PB_FUNCTION =
new Function<ObjectAccessControl, Acl>() {
@Override
public Acl apply(ObjectAccessControl aclPb) {
return Acl.fromPb(aclPb);
}
};
static final Function<BucketAccessControl, Acl> FROM_BUCKET_PB_FUNCTION =
new Function<BucketAccessControl, Acl>() {
@Override
public Acl apply(BucketAccessControl aclPb) {
return Acl.fromPb(aclPb);
}
};

private final String id;
private final String etag;
private final Entity entity;
private final Role role;

public enum Role {
OWNER, READER, WRITER
}

/**
* Builder for {@code Acl} objects.
*/
public static class Builder {

private Entity entity;
private Role role;
private String id;
private String etag;

private Builder(Entity entity, Role role) {
this.entity = entity;
this.role = role;
}

private Builder(Acl acl) {
this.entity = acl.entity;
this.role = acl.role;
this.id = acl.id;
this.etag = acl.etag;
}

/**
* Sets the entity for the ACL object.
*/
public Builder entity(Entity entity) {
this.entity = entity;
return this;
}

/**
* Sets the role to associate to the {@code entity} object.
*/
public Builder role(Role role) {
this.role = role;
return this;
}

Builder id(String id) {
this.id = id;
return this;
}

Builder etag(String etag) {
this.etag = etag;
return this;
}

/**
* Creates an {@code Acl} object from this builder.
*/
public Acl build() {
return new Acl(this);
}
}

/**
* Base class for Access Control List entities.
*/
@@ -274,9 +351,27 @@ String toPb() {
}
}

private Acl(Entity entity, Role role) {
this.entity = entity;
this.role = role;
private Acl(Builder builder) {
this.entity = checkNotNull(builder.entity);
this.role = checkNotNull(builder.role);
this.id = builder.id;
this.etag = builder.etag;
}

/**
* Returns the ID of the ACL entry.
*/
public String id() {
return id;
}

/**
* Returns HTTP 1.1 Entity tag for the ACL entry.
*
* @see <a href="http://tools.ietf.org/html/rfc2616#section-3.11">Entity Tags</a>
*/
public String etag() {
return etag;
}

/**
@@ -294,13 +389,40 @@ public Role role() {
}

/**
* Returns an Acl object.
* Returns a builder for this {@code Acl} object.
*/
public Builder toBuilder() {
return new Builder(this);
}

/**
* Returns an {@code Acl} object.
*
* @param entity the entity for this ACL object
* @param role the role to associate to the {@code entity} object
*/
public static Acl of(Entity entity, Role role) {
return new Acl(entity, role);
return builder(entity, role).build();
}

/**
* Returns a builder for {@code Acl} objects.
*
* @param entity the entity for this ACL object
* @param role the role to associate to the {@code entity} object
*/
public static Builder builder(Entity entity, Role role) {
return new Builder(entity, role);
}

@Override
public String toString() {
return MoreObjects.toStringHelper(this)

This comment was marked as spam.

.add("entity", entity)
.add("role", role)
.add("etag", etag)
.add("id", id)
.toString();
}

@Override
@@ -318,30 +440,44 @@ public boolean equals(Object obj) {
}
final Acl other = (Acl) obj;
return Objects.equals(this.entity, other.entity)
&& Objects.equals(this.role, other.role);
&& Objects.equals(this.role, other.role)
&& Objects.equals(this.etag, other.etag)
&& Objects.equals(this.id, other.id);
}

BucketAccessControl toBucketPb() {
BucketAccessControl bucketPb = new BucketAccessControl();
bucketPb.setRole(role().toString());
bucketPb.setEntity(entity().toString());
bucketPb.setId(id());
bucketPb.setEtag(etag());
return bucketPb;
}

ObjectAccessControl toObjectPb() {
ObjectAccessControl objectPb = new ObjectAccessControl();
objectPb.setRole(role().name());
objectPb.setEntity(entity().toPb());
objectPb.setId(id());
objectPb.setEtag(etag());
return objectPb;
}

static Acl fromPb(ObjectAccessControl objectAccessControl) {
Role role = Role.valueOf(objectAccessControl.getRole());
return Acl.of(Entity.fromPb(objectAccessControl.getEntity()), role);
Entity entity = Entity.fromPb(objectAccessControl.getEntity());
return builder(entity, role)
.etag(objectAccessControl.getEtag())
.id(objectAccessControl.getId())
.build();
}

static Acl fromPb(BucketAccessControl bucketAccessControl) {
Role role = Role.valueOf(bucketAccessControl.getRole());
return Acl.of(Entity.fromPb(bucketAccessControl.getEntity()), role);
Entity entity = Entity.fromPb(bucketAccessControl.getEntity());
return builder(entity, role)
.etag(bucketAccessControl.getEtag())
.id(bucketAccessControl.getId())
.build();
}
}
Original file line number Diff line number Diff line change
@@ -28,6 +28,7 @@
import com.google.cloud.ServiceAccountSigner;
import com.google.cloud.ServiceAccountSigner.SigningException;
import com.google.cloud.WriteChannel;
import com.google.cloud.storage.Acl.Entity;
import com.google.cloud.storage.Storage.BlobTargetOption;
import com.google.cloud.storage.Storage.BlobWriteOption;
import com.google.cloud.storage.Storage.CopyRequest;
@@ -230,7 +231,7 @@ public Builder acl(List<Acl> acl) {
}

@Override
Builder owner(Acl.Entity owner) {
Builder owner(Entity owner) {
infoBuilder.owner(owner);
return this;
}
@@ -602,6 +603,52 @@ public URL signUrl(long duration, TimeUnit unit, SignUrlOption... options) {
return storage.signUrl(this, duration, unit, options);
}

/**
* Returns the ACL entry for the specified entity on this blob or {@code null} if not found.
*
* @throws StorageException upon failure
*/
public Acl getAcl(Entity entity) {
return storage.getAcl(blobId(), entity);
}

/**
* Deletes the ACL entry for the specified entity on this blob.
*
* @return {@code true} if the ACL was deleted, {@code false} if it was not found
* @throws StorageException upon failure
*/
public boolean deleteAcl(Entity entity) {
return storage.deleteAcl(blobId(), entity);
}

/**
* Creates a new ACL entry on this blob.
*
* @throws StorageException upon failure
*/
public Acl createAcl(Acl acl) {
return storage.createAcl(blobId(), acl);
}

/**
* Updates an ACL entry on this blob.
*
* @throws StorageException upon failure
*/
public Acl updateAcl(Acl acl) {
return storage.updateAcl(blobId(), acl);
}

/**
* Lists the ACL entries for this blob.
*
* @throws StorageException upon failure
*/
public List<Acl> listAcls() {
return storage.listAcls(blobId());
}

/**
* Returns the blob's {@code Storage} object used to issue requests.
*/
Loading