Skip to content

Commit 01e0b74

Browse files
authored
feat(bigquery): Support resource tags for datasets in java client (#3647)
* feat(bigquery): Support resource tags for datasets in java client * add method to clirr-ignored-diff file * Try Acl permissions to grant tag permissions * Add exception to func signature * Remove IT tests
1 parent 1a14342 commit 01e0b74

File tree

5 files changed

+67
-0
lines changed

5 files changed

+67
-0
lines changed

google-cloud-bigquery/clirr-ignored-differences.xml

+5
Original file line numberDiff line numberDiff line change
@@ -134,4 +134,9 @@
134134
<className>com/google/cloud/bigquery/DatasetInfo*</className>
135135
<method>*setMaxTimeTravelHours(*)</method>
136136
</difference>
137+
<difference>
138+
<differenceType>7013</differenceType>
139+
<className>com/google/cloud/bigquery/DatasetInfo*</className>
140+
<method>*setResourceTags(*)</method>
141+
</difference>
137142
</differences>

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

+6
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,12 @@ public Builder setMaxTimeTravelHours(Long maxTimeTravelHours) {
170170
return this;
171171
}
172172

173+
@Override
174+
public Builder setResourceTags(Map<String, String> resourceTags) {
175+
infoBuilder.setResourceTags(resourceTags);
176+
return this;
177+
}
178+
173179
@Override
174180
public Dataset build() {
175181
return new Dataset(bigquery, infoBuilder);

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

+41
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ public Dataset apply(DatasetInfo datasetInfo) {
7676
private final ExternalDatasetReference externalDatasetReference;
7777
private final String storageBillingModel;
7878
private final Long maxTimeTravelHours;
79+
private final Annotations resourceTags;
7980

8081
/** A builder for {@code DatasetInfo} objects. */
8182
public abstract static class Builder {
@@ -184,6 +185,19 @@ public abstract Builder setDefaultEncryptionConfiguration(
184185
*/
185186
public abstract Builder setDefaultCollation(String defaultCollation);
186187

188+
/**
189+
* Optional. The <a href="https://cloud.google.com/bigquery/docs/tags">tags</a> attached to this
190+
* dataset. Tag keys are globally unique. Tag key is expected to be in the namespaced format,
191+
* for example "123456789012/environment" where 123456789012 is the ID of the parent
192+
* organization or project resource for this tag key. Tag value is expected to be the short
193+
* name, for example "Production".
194+
*
195+
* @see <a href="https://cloud.google.com/iam/docs/tags-access-control#definitions">Tag
196+
* definitions</a> for more details.
197+
* @param resourceTags resourceTags or {@code null} for none
198+
*/
199+
public abstract Builder setResourceTags(Map<String, String> resourceTags);
200+
187201
/** Creates a {@code DatasetInfo} object. */
188202
public abstract DatasetInfo build();
189203
}
@@ -208,6 +222,7 @@ static final class BuilderImpl extends Builder {
208222
private ExternalDatasetReference externalDatasetReference;
209223
private String storageBillingModel;
210224
private Long maxTimeTravelHours;
225+
private Annotations resourceTags = Annotations.ZERO;
211226

212227
BuilderImpl() {}
213228

@@ -230,6 +245,7 @@ static final class BuilderImpl extends Builder {
230245
this.externalDatasetReference = datasetInfo.externalDatasetReference;
231246
this.storageBillingModel = datasetInfo.storageBillingModel;
232247
this.maxTimeTravelHours = datasetInfo.maxTimeTravelHours;
248+
this.resourceTags = datasetInfo.resourceTags;
233249
}
234250

235251
BuilderImpl(com.google.api.services.bigquery.model.Dataset datasetPb) {
@@ -270,6 +286,7 @@ public Acl apply(Dataset.Access accessPb) {
270286
}
271287
this.storageBillingModel = datasetPb.getStorageBillingModel();
272288
this.maxTimeTravelHours = datasetPb.getMaxTimeTravelHours();
289+
this.resourceTags = Annotations.fromPb(datasetPb.getResourceTags());
273290
}
274291

275292
@Override
@@ -388,6 +405,12 @@ public Builder setMaxTimeTravelHours(Long maxTimeTravelHours) {
388405
return this;
389406
}
390407

408+
@Override
409+
public Builder setResourceTags(Map<String, String> resourceTags) {
410+
this.resourceTags = Annotations.fromUser(resourceTags);
411+
return this;
412+
}
413+
391414
@Override
392415
public DatasetInfo build() {
393416
return new DatasetInfo(this);
@@ -413,6 +436,7 @@ public DatasetInfo build() {
413436
externalDatasetReference = builder.externalDatasetReference;
414437
storageBillingModel = builder.storageBillingModel;
415438
maxTimeTravelHours = builder.maxTimeTravelHours;
439+
resourceTags = builder.resourceTags;
416440
}
417441

418442
/** Returns the dataset identity. */
@@ -554,6 +578,21 @@ public Long getMaxTimeTravelHours() {
554578
return maxTimeTravelHours;
555579
}
556580

581+
/**
582+
* Optional. The <a href="https://cloud.google.com/bigquery/docs/tags">tags</a> attached to this
583+
* dataset. Tag keys are globally unique. Tag key is expected to be in the namespaced format, for
584+
* example "123456789012/environment" where 123456789012 is the ID of the parent organization or
585+
* project resource for this tag key. Tag value is expected to be the short name, for example
586+
* "Production".
587+
*
588+
* @see <a href="https://cloud.google.com/iam/docs/tags-access-control#definitions">Tag
589+
* definitions</a> for more details.
590+
* @return value or {@code null} for none
591+
*/
592+
public Map<String, String> getResourceTags() {
593+
return resourceTags.userMap();
594+
}
595+
557596
/**
558597
* Returns information about the external metadata storage where the dataset is defined. Filled
559598
* out when the dataset type is EXTERNAL.
@@ -588,6 +627,7 @@ public String toString() {
588627
.add("externalDatasetReference", externalDatasetReference)
589628
.add("storageBillingModel", storageBillingModel)
590629
.add("maxTimeTravelHours", maxTimeTravelHours)
630+
.add("resourceTags", resourceTags)
591631
.toString();
592632
}
593633

@@ -675,6 +715,7 @@ public Dataset.Access apply(Acl acl) {
675715
if (maxTimeTravelHours != null) {
676716
datasetPb.setMaxTimeTravelHours(maxTimeTravelHours);
677717
}
718+
datasetPb.setResourceTags(resourceTags.toPb());
678719
return datasetPb;
679720
}
680721

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

+7
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ public class DatasetInfoTest {
6262
private static final String STORAGE_BILLING_MODEL = "LOGICAL";
6363
private static final Long MAX_TIME_TRAVEL_HOURS_5_DAYS = 120L;
6464
private static final Long MAX_TIME_TRAVEL_HOURS_7_DAYS = 168L;
65+
private static final Map<String, String> RESOURCE_TAGS =
66+
ImmutableMap.of(
67+
"example-key1", "example-value1",
68+
"example-key2", "example-value2");
6569

6670
private static final ExternalDatasetReference EXTERNAL_DATASET_REFERENCE =
6771
ExternalDatasetReference.newBuilder()
@@ -85,6 +89,7 @@ public class DatasetInfoTest {
8589
.setDefaultPartitionExpirationMs(DEFAULT_PARTITION__EXPIRATION)
8690
.setStorageBillingModel(STORAGE_BILLING_MODEL)
8791
.setMaxTimeTravelHours(MAX_TIME_TRAVEL_HOURS_7_DAYS)
92+
.setResourceTags(RESOURCE_TAGS)
8893
.build();
8994
private static final DatasetInfo DATASET_INFO_COMPLETE =
9095
DATASET_INFO
@@ -183,6 +188,7 @@ public void testBuilder() {
183188
assertEquals(
184189
MAX_TIME_TRAVEL_HOURS_5_DAYS,
185190
DATASET_INFO_WITH_MAX_TIME_TRAVEL_5_DAYS.getMaxTimeTravelHours());
191+
assertEquals(RESOURCE_TAGS, DATASET_INFO.getResourceTags());
186192
}
187193

188194
@Test
@@ -272,5 +278,6 @@ private void compareDatasets(DatasetInfo expected, DatasetInfo value) {
272278
assertEquals(expected.getExternalDatasetReference(), value.getExternalDatasetReference());
273279
assertEquals(expected.getStorageBillingModel(), value.getStorageBillingModel());
274280
assertEquals(expected.getMaxTimeTravelHours(), value.getMaxTimeTravelHours());
281+
assertEquals(expected.getResourceTags(), value.getResourceTags());
275282
}
276283
}

google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/DatasetTest.java

+8
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ public class DatasetTest {
6868
private static final Field FIELD = Field.of("FieldName", LegacySQLTypeName.INTEGER);
6969
private static final String STORAGE_BILLING_MODEL = "LOGICAL";
7070
private static final Long MAX_TIME_TRAVEL_HOURS = 168L;
71+
private static final Map<String, String> RESOURCE_TAGS =
72+
ImmutableMap.of(
73+
"example-key1", "example-value1",
74+
"example-key2", "example-value2");
7175
private static final StandardTableDefinition TABLE_DEFINITION =
7276
StandardTableDefinition.of(Schema.of(FIELD));
7377
private static final ViewDefinition VIEW_DEFINITION = ViewDefinition.of("QUERY");
@@ -124,6 +128,7 @@ public void testBuilder() {
124128
.setLabels(LABELS)
125129
.setStorageBillingModel(STORAGE_BILLING_MODEL)
126130
.setMaxTimeTravelHours(MAX_TIME_TRAVEL_HOURS)
131+
.setResourceTags(RESOURCE_TAGS)
127132
.build();
128133
assertEquals(DATASET_ID, builtDataset.getDatasetId());
129134
assertEquals(ACCESS_RULES, builtDataset.getAcl());
@@ -139,6 +144,7 @@ public void testBuilder() {
139144
assertEquals(LABELS, builtDataset.getLabels());
140145
assertEquals(STORAGE_BILLING_MODEL, builtDataset.getStorageBillingModel());
141146
assertEquals(MAX_TIME_TRAVEL_HOURS, builtDataset.getMaxTimeTravelHours());
147+
assertEquals(RESOURCE_TAGS, builtDataset.getResourceTags());
142148
}
143149

144150
@Test
@@ -348,6 +354,7 @@ public void testExternalDatasetReference() {
348354
.setExternalDatasetReference(EXTERNAL_DATASET_REFERENCE)
349355
.setStorageBillingModel(STORAGE_BILLING_MODEL)
350356
.setMaxTimeTravelHours(MAX_TIME_TRAVEL_HOURS)
357+
.setResourceTags(RESOURCE_TAGS)
351358
.build();
352359
assertEquals(
353360
EXTERNAL_DATASET_REFERENCE,
@@ -379,5 +386,6 @@ private void compareDatasetInfo(DatasetInfo expected, DatasetInfo value) {
379386
assertEquals(expected.getExternalDatasetReference(), value.getExternalDatasetReference());
380387
assertEquals(expected.getStorageBillingModel(), value.getStorageBillingModel());
381388
assertEquals(expected.getMaxTimeTravelHours(), value.getMaxTimeTravelHours());
389+
assertEquals(expected.getResourceTags(), value.getResourceTags());
382390
}
383391
}

0 commit comments

Comments
 (0)