Skip to content

Commit bebf1c6

Browse files
authored
feat(bigquery): Support Fine Grained ACLs for Datasets (#3803)
* feat(bigquery): Support Fine Grained ACLs for Datasets * fix style * Change enum strings to uppercase * add unspecified enum option
1 parent a21cde8 commit bebf1c6

File tree

4 files changed

+75
-13
lines changed

4 files changed

+75
-13
lines changed

google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQuery.java

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,42 @@ public String getSelector() {
7777
}
7878
}
7979

80+
enum DatasetView {
81+
DATASET_VIEW_UNSPECIFIED("DATASET_VIEW_UNSPECIFIED"),
82+
FULL("FULL"),
83+
METADATA("METADATA"),
84+
ACL("ACL");
85+
86+
private final String view;
87+
88+
DatasetView(String view) {
89+
this.view = view;
90+
}
91+
92+
@Override
93+
public String toString() {
94+
return view;
95+
}
96+
}
97+
98+
enum DatasetUpdateMode {
99+
UPDATE_MODE_UNSPECIFIED("UPDATE_MODE_UNSPECIFIED"),
100+
UPDATE_FULL("UPDATE_FULL"),
101+
UPDATE_METADATA("UPDATE_METADATA"),
102+
UPDATE_ACL("UPDATE_ACL");
103+
104+
private final String updateMode;
105+
106+
DatasetUpdateMode(String updateMode) {
107+
this.updateMode = updateMode;
108+
}
109+
110+
@Override
111+
public String toString() {
112+
return updateMode;
113+
}
114+
}
115+
80116
/**
81117
* Fields of a BigQuery Table resource.
82118
*
@@ -307,6 +343,22 @@ public static DatasetOption fields(DatasetField... fields) {
307343
public static DatasetOption accessPolicyVersion(Integer accessPolicyVersion) {
308344
return new DatasetOption(BigQueryRpc.Option.ACCESS_POLICY_VERSION, accessPolicyVersion);
309345
}
346+
347+
/**
348+
* Returns an option to specify the view that determines which dataset information is returned.
349+
* By default, metadata and ACL information are returned.
350+
*/
351+
public static DatasetOption datasetView(DatasetView datasetView) {
352+
return new DatasetOption(BigQueryRpc.Option.DATASET_VIEW, datasetView);
353+
}
354+
355+
/**
356+
* Returns an option to specify the fields of dataset that update/patch operation is targeting.
357+
* By default, both metadata and ACL fields are updated.
358+
*/
359+
public static DatasetOption updateMode(DatasetUpdateMode updateMode) {
360+
return new DatasetOption(BigQueryRpc.Option.DATASET_UPDATE_MODE, updateMode);
361+
}
310362
}
311363

312364
/** Class for specifying dataset delete options. */

google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/spi/v2/BigQueryRpc.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,9 @@ enum Option {
6060
TABLE_METADATA_VIEW("view"),
6161
RETRY_OPTIONS("retryOptions"),
6262
BIGQUERY_RETRY_CONFIG("bigQueryRetryConfig"),
63-
ACCESS_POLICY_VERSION("accessPolicyVersion");
63+
ACCESS_POLICY_VERSION("accessPolicyVersion"),
64+
DATASET_VIEW("datasetView"),
65+
DATASET_UPDATE_MODE("datasetUpdateMode");
6466

6567
private final String value;
6668

google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/spi/v2/HttpBigQueryRpc.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -149,10 +149,11 @@ public Dataset getDatasetSkipExceptionTranslation(
149149
.get(projectId, datasetId)
150150
.setFields(Option.FIELDS.getString(options))
151151
.setPrettyPrint(false);
152-
for (Map.Entry<Option, ?> entry : options.entrySet()) {
153-
if (entry.getKey() == Option.ACCESS_POLICY_VERSION && entry.getValue() != null) {
154-
bqGetRequest.setAccessPolicyVersion((Integer) entry.getValue());
155-
}
152+
if (options.containsKey(Option.ACCESS_POLICY_VERSION)) {
153+
bqGetRequest.setAccessPolicyVersion((Integer) options.get(Option.ACCESS_POLICY_VERSION));
154+
}
155+
if (options.containsKey(Option.DATASET_VIEW)) {
156+
bqGetRequest.setDatasetView(options.get(Option.DATASET_VIEW).toString());
156157
}
157158
return bqGetRequest.execute();
158159
}
@@ -350,10 +351,11 @@ public Dataset patchSkipExceptionTranslation(Dataset dataset, Map<Option, ?> opt
350351
.patch(reference.getProjectId(), reference.getDatasetId(), dataset)
351352
.setPrettyPrint(false)
352353
.setFields(Option.FIELDS.getString(options));
353-
for (Map.Entry<Option, ?> entry : options.entrySet()) {
354-
if (entry.getKey() == Option.ACCESS_POLICY_VERSION && entry.getValue() != null) {
355-
bqPatchRequest.setAccessPolicyVersion((Integer) entry.getValue());
356-
}
354+
if (options.containsKey(Option.ACCESS_POLICY_VERSION)) {
355+
bqPatchRequest.setAccessPolicyVersion((Integer) options.get(Option.ACCESS_POLICY_VERSION));
356+
}
357+
if (options.containsKey(Option.DATASET_UPDATE_MODE)) {
358+
bqPatchRequest.setUpdateMode(options.get(Option.DATASET_UPDATE_MODE).toString());
357359
}
358360
return bqPatchRequest.execute();
359361
}

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

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@
4848
import com.google.cloud.bigquery.BigQuery.DatasetField;
4949
import com.google.cloud.bigquery.BigQuery.DatasetListOption;
5050
import com.google.cloud.bigquery.BigQuery.DatasetOption;
51+
import com.google.cloud.bigquery.BigQuery.DatasetUpdateMode;
52+
import com.google.cloud.bigquery.BigQuery.DatasetView;
5153
import com.google.cloud.bigquery.BigQuery.JobField;
5254
import com.google.cloud.bigquery.BigQuery.JobListOption;
5355
import com.google.cloud.bigquery.BigQuery.JobOption;
@@ -1236,18 +1238,20 @@ public void testGetDatasetWithAccessPolicyVersion() throws IOException {
12361238
"requests after the year 2024",
12371239
"location");
12381240
Acl acl = Acl.of(user, role, condition);
1239-
DatasetOption datasetOption = DatasetOption.accessPolicyVersion(3);
1241+
DatasetOption accessPolicyOption = DatasetOption.accessPolicyVersion(3);
1242+
DatasetOption viewOption = DatasetOption.datasetView(DatasetView.FULL);
12401243

12411244
Dataset dataset =
12421245
bigquery.create(
12431246
DatasetInfo.newBuilder(accessPolicyDataset)
12441247
.setDescription("Some Description")
12451248
.setAcl(ImmutableList.of(acl))
12461249
.build(),
1247-
datasetOption);
1250+
accessPolicyOption);
12481251
assertThat(dataset).isNotNull();
12491252

1250-
Dataset remoteDataset = bigquery.getDataset(accessPolicyDataset, datasetOption);
1253+
Dataset remoteDataset =
1254+
bigquery.getDataset(accessPolicyDataset, accessPolicyOption, viewOption);
12511255
assertNotNull(remoteDataset);
12521256
assertEquals(dataset.getDescription(), remoteDataset.getDescription());
12531257
assertNotNull(remoteDataset.getCreationTime());
@@ -1356,14 +1360,16 @@ public void testUpdateDatabaseWithAccessPolicyVersion() throws IOException {
13561360
acls.add(acl);
13571361

13581362
DatasetOption datasetOption = DatasetOption.accessPolicyVersion(3);
1363+
DatasetOption updateModeOption = DatasetOption.updateMode(DatasetUpdateMode.UPDATE_FULL);
13591364
Dataset updatedDataset =
13601365
bigquery.update(
13611366
dataset.toBuilder()
13621367
.setDescription("Updated Description")
13631368
.setLabels(null)
13641369
.setAcl(acls)
13651370
.build(),
1366-
datasetOption);
1371+
datasetOption,
1372+
updateModeOption);
13671373
assertNotNull(updatedDataset);
13681374
assertEquals(updatedDataset.getDescription(), "Updated Description");
13691375
assertThat(updatedDataset.getLabels().isEmpty());

0 commit comments

Comments
 (0)