Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 7464e23

Browse files
author
Praful Makani
authoredFeb 19, 2020
fix: allUsers access (#178)
1 parent 646c2b4 commit 7464e23

File tree

3 files changed

+77
-1
lines changed

3 files changed

+77
-1
lines changed
 

‎google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/Acl.java

+52-1
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,8 @@ public enum Type {
103103
DOMAIN,
104104
GROUP,
105105
USER,
106-
VIEW
106+
VIEW,
107+
IAM_MEMBER
107108
}
108109

109110
Entity(Type type) {
@@ -132,6 +133,9 @@ static Entity fromPb(Access access) {
132133
if (access.getView() != null) {
133134
return new View(TableId.fromPb(access.getView()));
134135
}
136+
if (access.getIamMember() != null) {
137+
return new IamMember(access.getIamMember());
138+
}
135139
// Unreachable
136140
throw new BigQueryException(
137141
BigQueryException.UNKNOWN_CODE, "Unrecognized access configuration");
@@ -383,6 +387,53 @@ Access toPb() {
383387
}
384388
}
385389

390+
/**
391+
* Class for a BigQuery IamMember entity. Objects of this class represent a iamMember to grant
392+
* access to given the IAM Policy.
393+
*/
394+
public static final class IamMember extends Entity {
395+
396+
private final String iamMember;
397+
398+
/** Creates a iamMember entity given the iamMember. */
399+
public IamMember(String iamMember) {
400+
super(Type.IAM_MEMBER);
401+
this.iamMember = iamMember;
402+
}
403+
404+
/** Returns iamMember. */
405+
public String getIamMember() {
406+
return iamMember;
407+
}
408+
409+
@Override
410+
public boolean equals(Object obj) {
411+
if (this == obj) {
412+
return true;
413+
}
414+
if (obj == null || getClass() != obj.getClass()) {
415+
return false;
416+
}
417+
IamMember iam = (IamMember) obj;
418+
return Objects.equals(getType(), iam.getType()) && Objects.equals(iamMember, iam.iamMember);
419+
}
420+
421+
@Override
422+
public int hashCode() {
423+
return Objects.hash(getType(), iamMember);
424+
}
425+
426+
@Override
427+
public String toString() {
428+
return toPb().toString();
429+
}
430+
431+
@Override
432+
Access toPb() {
433+
return new Access().setIamMember(iamMember);
434+
}
435+
}
436+
386437
private Acl(Entity entity, Role role) {
387438
this.entity = checkNotNull(entity);
388439
this.role = role;

‎google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/DatasetInfoTest.java

+7
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ public class DatasetInfoTest {
3636
ImmutableList.of(
3737
Acl.of(Acl.Group.ofAllAuthenticatedUsers(), Acl.Role.READER),
3838
Acl.of(new Acl.View(TableId.of("project", "dataset", "table"))));
39+
private static final List<Acl> ACCESS_RULES_IAM_MEMBER =
40+
ImmutableList.of(Acl.of(new Acl.IamMember("allUsers"), Acl.Role.READER));
3941
private static final Map<String, String> LABELS =
4042
ImmutableMap.of(
4143
"example-label1", "example-value1",
@@ -76,10 +78,15 @@ public class DatasetInfoTest {
7678
.setDatasetId(DATASET_ID_COMPLETE)
7779
.setAcl(ACCESS_RULES_COMPLETE)
7880
.build();
81+
private static final DatasetInfo DATASET_INFO_COMPLETE_WITH_IAM_MEMBER =
82+
DATASET_INFO.toBuilder().setAcl(ACCESS_RULES_IAM_MEMBER).build();
7983

8084
@Test
8185
public void testToBuilder() {
8286
compareDatasets(DATASET_INFO, DATASET_INFO.toBuilder().build());
87+
compareDatasets(
88+
DATASET_INFO_COMPLETE_WITH_IAM_MEMBER,
89+
DATASET_INFO_COMPLETE_WITH_IAM_MEMBER.toBuilder().build());
8390
DatasetInfo datasetInfo =
8491
DATASET_INFO
8592
.toBuilder()

‎google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/it/ITBigQueryTest.java

+18
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,11 @@
2828
import static org.junit.Assert.fail;
2929

3030
import com.google.api.gax.paging.Page;
31+
import com.google.auth.oauth2.GoogleCredentials;
32+
import com.google.auth.oauth2.ServiceAccountCredentials;
3133
import com.google.cloud.Date;
3234
import com.google.cloud.RetryOption;
35+
import com.google.cloud.bigquery.Acl;
3336
import com.google.cloud.bigquery.BigQuery;
3437
import com.google.cloud.bigquery.BigQuery.DatasetDeleteOption;
3538
import com.google.cloud.bigquery.BigQuery.DatasetField;
@@ -375,6 +378,21 @@ public void testGetDataset() {
375378
assertNotNull(dataset.getSelfLink());
376379
}
377380

381+
@Test
382+
public void testDatasetUpdateAccess() throws IOException {
383+
Dataset dataset = bigquery.getDataset(DATASET);
384+
ServiceAccountCredentials credentials =
385+
(ServiceAccountCredentials) GoogleCredentials.getApplicationDefault();
386+
List<Acl> acl =
387+
ImmutableList.of(
388+
Acl.of(new Acl.Group("projectOwners"), Acl.Role.OWNER),
389+
Acl.of(new Acl.User(credentials.getClientEmail()), Acl.Role.OWNER),
390+
Acl.of(new Acl.IamMember("allUsers"), Acl.Role.READER));
391+
Dataset remoteDataset = dataset.toBuilder().setAcl(acl).build().update();
392+
assertNotNull(remoteDataset);
393+
assertEquals(3, remoteDataset.getAcl().size());
394+
}
395+
378396
@Test
379397
public void testGetDatasetWithSelectedFields() {
380398
Dataset dataset =

0 commit comments

Comments
 (0)
Failed to load comments.