Skip to content

Commit 7d61111

Browse files
feat: Add support for Table resource tags (#3046)
* Refactor labels into annotations to be used for resource tags * Fix issue where changelog was updated during a refactoring * Fix issue where changelog was updated during a refactoring * Add resource tags to Tables * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * fix: clirr * Update TableInfoTest to include ResourceTags --------- Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
1 parent a7ffacf commit 7d61111

File tree

8 files changed

+68
-32
lines changed

8 files changed

+68
-32
lines changed

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

+5
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@
2929
<className>com/google/cloud/bigquery/RoutineInfo*</className>
3030
<method>*RemoteFunctionOptions(*)</method>
3131
</difference>
32+
<difference>
33+
<differenceType>7013</differenceType>
34+
<className>com/google/cloud/bigquery/TableInfo*</className>
35+
<method>*ResourceTags(*)</method>
36+
</difference>
3237
<difference>
3338
<differenceType>7012</differenceType>
3439
<className>com/google/cloud/bigquery/Connection</className>

google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/Labels.java renamed to google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/Annotations.java

+6-6
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@
2828
import javax.annotation.Nullable;
2929

3030
@AutoValue
31-
abstract class Labels implements Serializable {
31+
abstract class Annotations implements Serializable {
3232
private static final long serialVersionUID = 1L;
33-
static final Labels ZERO = of(Collections.<String, String>emptyMap());
33+
static final Annotations ZERO = of(Collections.<String, String>emptyMap());
3434

3535
@Nullable
3636
abstract Map<String, String> userMap();
@@ -59,21 +59,21 @@ Map<String, String> toPb() {
5959
return Collections.unmodifiableMap(pbMap);
6060
}
6161

62-
private static Labels of(Map<String, String> userMap) {
62+
private static Annotations of(Map<String, String> userMap) {
6363
Preconditions.checkArgument(
6464
userMap == null || !userMap.containsKey(null), "null keys are not supported");
65-
return new AutoValue_Labels(userMap);
65+
return new AutoValue_Annotations(userMap);
6666
}
6767

68-
static Labels fromUser(Map<String, String> map) {
68+
static Annotations fromUser(Map<String, String> map) {
6969
if (map == null || map instanceof ImmutableMap) {
7070
return of(map);
7171
}
7272
// Not ImmutableMap; we support null values!
7373
return of(Collections.unmodifiableMap(new HashMap<>(map)));
7474
}
7575

76-
static Labels fromPb(Map<String, String> pb) {
76+
static Annotations fromPb(Map<String, String> pb) {
7777
if (Data.isNull(pb)) {
7878
return of(null);
7979
}

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public Dataset apply(DatasetInfo datasetInfo) {
6969
private final Long lastModified;
7070
private final String location;
7171
private final String selfLink;
72-
private final Labels labels;
72+
private final Annotations labels;
7373
private final EncryptionConfiguration defaultEncryptionConfiguration;
7474
private final Long defaultPartitionExpirationMs;
7575
private final String defaultCollation;
@@ -194,7 +194,7 @@ static final class BuilderImpl extends Builder {
194194
private Long lastModified;
195195
private String location;
196196
private String selfLink;
197-
private Labels labels = Labels.ZERO;
197+
private Annotations labels = Annotations.ZERO;
198198
private EncryptionConfiguration defaultEncryptionConfiguration;
199199
private Long defaultPartitionExpirationMs;
200200
private String defaultCollation;
@@ -247,7 +247,7 @@ public Acl apply(Dataset.Access accessPb) {
247247
this.lastModified = datasetPb.getLastModifiedTime();
248248
this.location = datasetPb.getLocation();
249249
this.selfLink = datasetPb.getSelfLink();
250-
this.labels = Labels.fromPb(datasetPb.getLabels());
250+
this.labels = Annotations.fromPb(datasetPb.getLabels());
251251
if (datasetPb.getDefaultEncryptionConfiguration() != null) {
252252
this.defaultEncryptionConfiguration =
253253
new EncryptionConfiguration.Builder(datasetPb.getDefaultEncryptionConfiguration())
@@ -337,7 +337,7 @@ Builder setSelfLink(String selfLink) {
337337
*/
338338
@Override
339339
public Builder setLabels(Map<String, String> labels) {
340-
this.labels = Labels.fromUser(labels);
340+
this.labels = Annotations.fromUser(labels);
341341
return this;
342342
}
343343

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public Model apply(ModelInfo ModelInfo) {
6767
private final Long creationTime;
6868
private final Long lastModifiedTime;
6969
private final Long expirationTime;
70-
private final Labels labels;
70+
private final Annotations labels;
7171
private final String location;
7272
private final ImmutableList<TrainingRun> trainingRunList;
7373
private final ImmutableList<StandardSQLField> featureColumnList;
@@ -132,7 +132,7 @@ static class BuilderImpl extends Builder {
132132
private Long creationTime;
133133
private Long lastModifiedTime;
134134
private Long expirationTime;
135-
private Labels labels = Labels.ZERO;
135+
private Annotations labels = Annotations.ZERO;
136136
private String location;
137137
private List<TrainingRun> trainingRunList = Collections.emptyList();
138138
private List<StandardSQLField> labelColumnList = Collections.emptyList();
@@ -169,7 +169,7 @@ static class BuilderImpl extends Builder {
169169
this.creationTime = modelPb.getCreationTime();
170170
this.lastModifiedTime = modelPb.getLastModifiedTime();
171171
this.expirationTime = modelPb.getExpirationTime();
172-
this.labels = Labels.fromPb(modelPb.getLabels());
172+
this.labels = Annotations.fromPb(modelPb.getLabels());
173173
this.location = modelPb.getLocation();
174174
if (modelPb.getTrainingRuns() != null) {
175175
this.trainingRunList = modelPb.getTrainingRuns();
@@ -238,7 +238,7 @@ public Builder setModelId(ModelId modelId) {
238238

239239
@Override
240240
public Builder setLabels(Map<String, String> labels) {
241-
this.labels = Labels.fromUser(labels);
241+
this.labels = Annotations.fromUser(labels);
242242
return this;
243243
}
244244

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

+6
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,12 @@ public Builder setLabels(Map<String, String> labels) {
192192
return this;
193193
}
194194

195+
@Override
196+
public Builder setResourceTags(Map<String, String> resourceTags) {
197+
infoBuilder.setResourceTags(resourceTags);
198+
return this;
199+
}
200+
195201
@Override
196202
public Builder setRequirePartitionFilter(Boolean requirePartitionFilter) {
197203
infoBuilder.setRequirePartitionFilter(requirePartitionFilter);

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

+27-4
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,9 @@ public Table apply(TableInfo tableInfo) {
7777
private final BigInteger numRows;
7878
private final TableDefinition definition;
7979
private final EncryptionConfiguration encryptionConfiguration;
80-
private final Labels labels;
80+
private final Annotations labels;
81+
82+
private final Annotations resourceTags;
8183
private final Boolean requirePartitionFilter;
8284
private final String defaultCollation;
8385

@@ -152,6 +154,9 @@ public abstract static class Builder {
152154
@BetaApi
153155
public abstract Builder setLabels(Map<String, String> labels);
154156

157+
/** Sets the resource tags applied to this table. */
158+
public abstract Builder setResourceTags(Map<String, String> resourceTags);
159+
155160
/** Creates a {@code TableInfo} object. */
156161
public abstract TableInfo build();
157162

@@ -191,7 +196,9 @@ static class BuilderImpl extends Builder {
191196
private BigInteger numRows;
192197
private TableDefinition definition;
193198
private EncryptionConfiguration encryptionConfiguration;
194-
private Labels labels = Labels.ZERO;
199+
private Annotations labels = Annotations.ZERO;
200+
201+
private Annotations resourceTags = Annotations.ZERO;
195202
private Boolean requirePartitionFilter;
196203
private String defaultCollation;
197204
private CloneDefinition cloneDefinition;
@@ -222,6 +229,7 @@ static class BuilderImpl extends Builder {
222229
this.definition = tableInfo.definition;
223230
this.encryptionConfiguration = tableInfo.encryptionConfiguration;
224231
this.labels = tableInfo.labels;
232+
this.resourceTags = tableInfo.resourceTags;
225233
this.requirePartitionFilter = tableInfo.requirePartitionFilter;
226234
this.defaultCollation = tableInfo.defaultCollation;
227235
this.cloneDefinition = tableInfo.cloneDefinition;
@@ -255,7 +263,8 @@ static class BuilderImpl extends Builder {
255263
this.encryptionConfiguration =
256264
new EncryptionConfiguration.Builder(tablePb.getEncryptionConfiguration()).build();
257265
}
258-
this.labels = Labels.fromPb(tablePb.getLabels());
266+
this.labels = Annotations.fromPb(tablePb.getLabels());
267+
this.resourceTags = Annotations.fromPb(tablePb.getResourceTags());
259268
this.requirePartitionFilter = tablePb.getRequirePartitionFilter();
260269
this.defaultCollation = tablePb.getDefaultCollation();
261270
if (tablePb.getCloneDefinition() != null) {
@@ -394,7 +403,13 @@ public Builder setEncryptionConfiguration(EncryptionConfiguration configuration)
394403

395404
@Override
396405
public Builder setLabels(Map<String, String> labels) {
397-
this.labels = Labels.fromUser(labels);
406+
this.labels = Annotations.fromUser(labels);
407+
return this;
408+
}
409+
410+
@Override
411+
public Builder setResourceTags(Map<String, String> resourceTags) {
412+
this.resourceTags = Annotations.fromUser(resourceTags);
398413
return this;
399414
}
400415

@@ -449,6 +464,7 @@ public TableInfo build() {
449464
this.definition = builder.definition;
450465
this.encryptionConfiguration = builder.encryptionConfiguration;
451466
this.labels = builder.labels;
467+
this.resourceTags = builder.resourceTags;
452468
this.requirePartitionFilter = builder.requirePartitionFilter;
453469
this.defaultCollation = builder.defaultCollation;
454470
this.cloneDefinition = builder.cloneDefinition;
@@ -610,6 +626,11 @@ public Map<String, String> getLabels() {
610626
return labels.userMap();
611627
}
612628

629+
/** Return a map for resource tags applied to the table. */
630+
public Map<String, String> getResourceTags() {
631+
return resourceTags.userMap();
632+
}
633+
613634
/**
614635
* Returns true if a partition filter (that can be used for partition elimination) is required for
615636
* queries over this table.
@@ -660,6 +681,7 @@ public String toString() {
660681
.add("definition", definition)
661682
.add("encryptionConfiguration", encryptionConfiguration)
662683
.add("labels", labels)
684+
.add("resourceTags", resourceTags)
663685
.add("requirePartitionFilter", requirePartitionFilter)
664686
.add("defaultCollation", defaultCollation)
665687
.add("cloneDefinition", cloneDefinition)
@@ -724,6 +746,7 @@ Table toPb() {
724746
tablePb.setEncryptionConfiguration(encryptionConfiguration.toPb());
725747
}
726748
tablePb.setLabels(labels.toPb());
749+
tablePb.setResourceTags(resourceTags.toPb());
727750
tablePb.setRequirePartitionFilter(requirePartitionFilter);
728751
if (defaultCollation != null) {
729752
tablePb.setDefaultCollation(defaultCollation);

google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/LabelsTest.java renamed to google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/AnnotationsTest.java

+14-14
Original file line numberDiff line numberDiff line change
@@ -25,50 +25,50 @@
2525
import java.util.Map;
2626
import org.junit.Test;
2727

28-
public class LabelsTest {
28+
public class AnnotationsTest {
2929
@Test
3030
public void testFromUser() {
31-
assertThat(Labels.fromUser(null).userMap()).isNull();
31+
assertThat(Annotations.fromUser(null).userMap()).isNull();
3232

3333
HashMap<String, String> user = new HashMap<>();
34-
assertThat(Labels.fromUser(user).userMap()).isEmpty();
34+
assertThat(Annotations.fromUser(user).userMap()).isEmpty();
3535

3636
user.put("a", "b");
37-
Labels labels = Labels.fromUser(user);
38-
assertThat(labels.userMap()).containsExactly("a", "b");
37+
Annotations annotations = Annotations.fromUser(user);
38+
assertThat(annotations.userMap()).containsExactly("a", "b");
3939

40-
// Changing map afterwards does not change the labels.
40+
// Changing map afterwards does not change the annotation.
4141
user.put("c", "d");
42-
assertThat(labels.userMap()).containsExactly("a", "b");
42+
assertThat(annotations.userMap()).containsExactly("a", "b");
4343
}
4444

4545
@Test
4646
public void testFromToPb() {
47-
assertThat(Labels.fromPb(null).toPb()).isNull();
47+
assertThat(Annotations.fromPb(null).toPb()).isNull();
4848

4949
HashMap<String, String> pb = new HashMap<>();
50-
assertThat(Labels.fromPb(pb).toPb()).isNull();
50+
assertThat(Annotations.fromPb(pb).toPb()).isNull();
5151

5252
pb.put("a", "b");
53-
assertThat(Labels.fromPb(pb).toPb()).isEqualTo(pb);
53+
assertThat(Annotations.fromPb(pb).toPb()).isEqualTo(pb);
5454

5555
pb.put("c", Data.NULL_STRING);
56-
assertThat(Labels.fromPb(pb).toPb()).isEqualTo(pb);
56+
assertThat(Annotations.fromPb(pb).toPb()).isEqualTo(pb);
5757

5858
Map<String, String> jsonNullMap = Data.nullOf(HashMap.class);
59-
assertThat(Data.isNull(Labels.fromPb(jsonNullMap).toPb())).isTrue();
59+
assertThat(Data.isNull(Annotations.fromPb(jsonNullMap).toPb())).isTrue();
6060
}
6161

6262
@Test
6363
public void testNullKey() {
6464
try {
65-
Labels.fromUser(Collections.singletonMap((String) null, "foo"));
65+
Annotations.fromUser(Collections.singletonMap((String) null, "foo"));
6666
fail("null key shouldn't work");
6767
} catch (IllegalArgumentException e) {
6868
}
6969

7070
try {
71-
Labels.fromPb(Collections.singletonMap((String) null, "foo"));
71+
Annotations.fromPb(Collections.singletonMap((String) null, "foo"));
7272
fail("null key shouldn't work");
7373
} catch (IllegalArgumentException e) {
7474
}

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

+2
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ public class TableInfoTest {
126126
.setNumRows(BigInteger.valueOf(NUM_ROWS))
127127
.setSelfLink(SELF_LINK)
128128
.setLabels(Collections.singletonMap("a", "b"))
129+
.setResourceTags(Collections.singletonMap("resourceTagA", "resourceTagB"))
129130
.setRequirePartitionFilter(REQUIRE_PARTITION_FILTER)
130131
.build();
131132
private static final TableInfo VIEW_INFO =
@@ -309,6 +310,7 @@ private void compareTableInfo(TableInfo expected, TableInfo value) {
309310
assertEquals(expected.getNumRows(), value.getNumRows());
310311
assertEquals(expected.getSelfLink(), value.getSelfLink());
311312
assertEquals(expected.getLabels(), value.getLabels());
313+
assertEquals(expected.getResourceTags(), value.getResourceTags());
312314
assertEquals(expected.getRequirePartitionFilter(), value.getRequirePartitionFilter());
313315
assertEquals(expected.toString(), value.toString());
314316
assertEquals(expected.hashCode(), value.hashCode());

0 commit comments

Comments
 (0)