From 7d61111d23282c7e2478ac31ba0d4e423330ec92 Mon Sep 17 00:00:00 2001 From: PhongChuong <147636638+PhongChuong@users.noreply.github.com> Date: Fri, 12 Jan 2024 07:50:18 -0500 Subject: [PATCH] feat: Add support for Table resource tags (#3046) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 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 --- .../clirr-ignored-differences.xml | 5 +++ .../{Labels.java => Annotations.java} | 12 +++---- .../google/cloud/bigquery/DatasetInfo.java | 8 ++--- .../com/google/cloud/bigquery/ModelInfo.java | 8 ++--- .../java/com/google/cloud/bigquery/Table.java | 6 ++++ .../com/google/cloud/bigquery/TableInfo.java | 31 ++++++++++++++++--- .../{LabelsTest.java => AnnotationsTest.java} | 28 ++++++++--------- .../google/cloud/bigquery/TableInfoTest.java | 2 ++ 8 files changed, 68 insertions(+), 32 deletions(-) rename google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/{Labels.java => Annotations.java} (87%) rename google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/{LabelsTest.java => AnnotationsTest.java} (62%) diff --git a/google-cloud-bigquery/clirr-ignored-differences.xml b/google-cloud-bigquery/clirr-ignored-differences.xml index 7513b5324..9c69fd6a6 100644 --- a/google-cloud-bigquery/clirr-ignored-differences.xml +++ b/google-cloud-bigquery/clirr-ignored-differences.xml @@ -29,6 +29,11 @@ com/google/cloud/bigquery/RoutineInfo* *RemoteFunctionOptions(*) + + 7013 + com/google/cloud/bigquery/TableInfo* + *ResourceTags(*) + 7012 com/google/cloud/bigquery/Connection diff --git a/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/Labels.java b/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/Annotations.java similarity index 87% rename from google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/Labels.java rename to google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/Annotations.java index 8f91aa37a..5d8d1e7e0 100644 --- a/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/Labels.java +++ b/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/Annotations.java @@ -28,9 +28,9 @@ import javax.annotation.Nullable; @AutoValue -abstract class Labels implements Serializable { +abstract class Annotations implements Serializable { private static final long serialVersionUID = 1L; - static final Labels ZERO = of(Collections.emptyMap()); + static final Annotations ZERO = of(Collections.emptyMap()); @Nullable abstract Map userMap(); @@ -59,13 +59,13 @@ Map toPb() { return Collections.unmodifiableMap(pbMap); } - private static Labels of(Map userMap) { + private static Annotations of(Map userMap) { Preconditions.checkArgument( userMap == null || !userMap.containsKey(null), "null keys are not supported"); - return new AutoValue_Labels(userMap); + return new AutoValue_Annotations(userMap); } - static Labels fromUser(Map map) { + static Annotations fromUser(Map map) { if (map == null || map instanceof ImmutableMap) { return of(map); } @@ -73,7 +73,7 @@ static Labels fromUser(Map map) { return of(Collections.unmodifiableMap(new HashMap<>(map))); } - static Labels fromPb(Map pb) { + static Annotations fromPb(Map pb) { if (Data.isNull(pb)) { return of(null); } diff --git a/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/DatasetInfo.java b/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/DatasetInfo.java index 0dddc0e86..b5fabe97d 100644 --- a/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/DatasetInfo.java +++ b/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/DatasetInfo.java @@ -69,7 +69,7 @@ public Dataset apply(DatasetInfo datasetInfo) { private final Long lastModified; private final String location; private final String selfLink; - private final Labels labels; + private final Annotations labels; private final EncryptionConfiguration defaultEncryptionConfiguration; private final Long defaultPartitionExpirationMs; private final String defaultCollation; @@ -194,7 +194,7 @@ static final class BuilderImpl extends Builder { private Long lastModified; private String location; private String selfLink; - private Labels labels = Labels.ZERO; + private Annotations labels = Annotations.ZERO; private EncryptionConfiguration defaultEncryptionConfiguration; private Long defaultPartitionExpirationMs; private String defaultCollation; @@ -247,7 +247,7 @@ public Acl apply(Dataset.Access accessPb) { this.lastModified = datasetPb.getLastModifiedTime(); this.location = datasetPb.getLocation(); this.selfLink = datasetPb.getSelfLink(); - this.labels = Labels.fromPb(datasetPb.getLabels()); + this.labels = Annotations.fromPb(datasetPb.getLabels()); if (datasetPb.getDefaultEncryptionConfiguration() != null) { this.defaultEncryptionConfiguration = new EncryptionConfiguration.Builder(datasetPb.getDefaultEncryptionConfiguration()) @@ -337,7 +337,7 @@ Builder setSelfLink(String selfLink) { */ @Override public Builder setLabels(Map labels) { - this.labels = Labels.fromUser(labels); + this.labels = Annotations.fromUser(labels); return this; } diff --git a/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/ModelInfo.java b/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/ModelInfo.java index 83603cbd2..922ed441c 100644 --- a/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/ModelInfo.java +++ b/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/ModelInfo.java @@ -67,7 +67,7 @@ public Model apply(ModelInfo ModelInfo) { private final Long creationTime; private final Long lastModifiedTime; private final Long expirationTime; - private final Labels labels; + private final Annotations labels; private final String location; private final ImmutableList trainingRunList; private final ImmutableList featureColumnList; @@ -132,7 +132,7 @@ static class BuilderImpl extends Builder { private Long creationTime; private Long lastModifiedTime; private Long expirationTime; - private Labels labels = Labels.ZERO; + private Annotations labels = Annotations.ZERO; private String location; private List trainingRunList = Collections.emptyList(); private List labelColumnList = Collections.emptyList(); @@ -169,7 +169,7 @@ static class BuilderImpl extends Builder { this.creationTime = modelPb.getCreationTime(); this.lastModifiedTime = modelPb.getLastModifiedTime(); this.expirationTime = modelPb.getExpirationTime(); - this.labels = Labels.fromPb(modelPb.getLabels()); + this.labels = Annotations.fromPb(modelPb.getLabels()); this.location = modelPb.getLocation(); if (modelPb.getTrainingRuns() != null) { this.trainingRunList = modelPb.getTrainingRuns(); @@ -238,7 +238,7 @@ public Builder setModelId(ModelId modelId) { @Override public Builder setLabels(Map labels) { - this.labels = Labels.fromUser(labels); + this.labels = Annotations.fromUser(labels); return this; } diff --git a/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/Table.java b/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/Table.java index fdf0bad66..56c655855 100644 --- a/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/Table.java +++ b/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/Table.java @@ -192,6 +192,12 @@ public Builder setLabels(Map labels) { return this; } + @Override + public Builder setResourceTags(Map resourceTags) { + infoBuilder.setResourceTags(resourceTags); + return this; + } + @Override public Builder setRequirePartitionFilter(Boolean requirePartitionFilter) { infoBuilder.setRequirePartitionFilter(requirePartitionFilter); diff --git a/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/TableInfo.java b/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/TableInfo.java index c00ba8441..6e050d064 100644 --- a/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/TableInfo.java +++ b/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/TableInfo.java @@ -77,7 +77,9 @@ public Table apply(TableInfo tableInfo) { private final BigInteger numRows; private final TableDefinition definition; private final EncryptionConfiguration encryptionConfiguration; - private final Labels labels; + private final Annotations labels; + + private final Annotations resourceTags; private final Boolean requirePartitionFilter; private final String defaultCollation; @@ -152,6 +154,9 @@ public abstract static class Builder { @BetaApi public abstract Builder setLabels(Map labels); + /** Sets the resource tags applied to this table. */ + public abstract Builder setResourceTags(Map resourceTags); + /** Creates a {@code TableInfo} object. */ public abstract TableInfo build(); @@ -191,7 +196,9 @@ static class BuilderImpl extends Builder { private BigInteger numRows; private TableDefinition definition; private EncryptionConfiguration encryptionConfiguration; - private Labels labels = Labels.ZERO; + private Annotations labels = Annotations.ZERO; + + private Annotations resourceTags = Annotations.ZERO; private Boolean requirePartitionFilter; private String defaultCollation; private CloneDefinition cloneDefinition; @@ -222,6 +229,7 @@ static class BuilderImpl extends Builder { this.definition = tableInfo.definition; this.encryptionConfiguration = tableInfo.encryptionConfiguration; this.labels = tableInfo.labels; + this.resourceTags = tableInfo.resourceTags; this.requirePartitionFilter = tableInfo.requirePartitionFilter; this.defaultCollation = tableInfo.defaultCollation; this.cloneDefinition = tableInfo.cloneDefinition; @@ -255,7 +263,8 @@ static class BuilderImpl extends Builder { this.encryptionConfiguration = new EncryptionConfiguration.Builder(tablePb.getEncryptionConfiguration()).build(); } - this.labels = Labels.fromPb(tablePb.getLabels()); + this.labels = Annotations.fromPb(tablePb.getLabels()); + this.resourceTags = Annotations.fromPb(tablePb.getResourceTags()); this.requirePartitionFilter = tablePb.getRequirePartitionFilter(); this.defaultCollation = tablePb.getDefaultCollation(); if (tablePb.getCloneDefinition() != null) { @@ -394,7 +403,13 @@ public Builder setEncryptionConfiguration(EncryptionConfiguration configuration) @Override public Builder setLabels(Map labels) { - this.labels = Labels.fromUser(labels); + this.labels = Annotations.fromUser(labels); + return this; + } + + @Override + public Builder setResourceTags(Map resourceTags) { + this.resourceTags = Annotations.fromUser(resourceTags); return this; } @@ -449,6 +464,7 @@ public TableInfo build() { this.definition = builder.definition; this.encryptionConfiguration = builder.encryptionConfiguration; this.labels = builder.labels; + this.resourceTags = builder.resourceTags; this.requirePartitionFilter = builder.requirePartitionFilter; this.defaultCollation = builder.defaultCollation; this.cloneDefinition = builder.cloneDefinition; @@ -610,6 +626,11 @@ public Map getLabels() { return labels.userMap(); } + /** Return a map for resource tags applied to the table. */ + public Map getResourceTags() { + return resourceTags.userMap(); + } + /** * Returns true if a partition filter (that can be used for partition elimination) is required for * queries over this table. @@ -660,6 +681,7 @@ public String toString() { .add("definition", definition) .add("encryptionConfiguration", encryptionConfiguration) .add("labels", labels) + .add("resourceTags", resourceTags) .add("requirePartitionFilter", requirePartitionFilter) .add("defaultCollation", defaultCollation) .add("cloneDefinition", cloneDefinition) @@ -724,6 +746,7 @@ Table toPb() { tablePb.setEncryptionConfiguration(encryptionConfiguration.toPb()); } tablePb.setLabels(labels.toPb()); + tablePb.setResourceTags(resourceTags.toPb()); tablePb.setRequirePartitionFilter(requirePartitionFilter); if (defaultCollation != null) { tablePb.setDefaultCollation(defaultCollation); diff --git a/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/LabelsTest.java b/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/AnnotationsTest.java similarity index 62% rename from google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/LabelsTest.java rename to google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/AnnotationsTest.java index 787dd77df..aa3dd9fde 100644 --- a/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/LabelsTest.java +++ b/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/AnnotationsTest.java @@ -25,50 +25,50 @@ import java.util.Map; import org.junit.Test; -public class LabelsTest { +public class AnnotationsTest { @Test public void testFromUser() { - assertThat(Labels.fromUser(null).userMap()).isNull(); + assertThat(Annotations.fromUser(null).userMap()).isNull(); HashMap user = new HashMap<>(); - assertThat(Labels.fromUser(user).userMap()).isEmpty(); + assertThat(Annotations.fromUser(user).userMap()).isEmpty(); user.put("a", "b"); - Labels labels = Labels.fromUser(user); - assertThat(labels.userMap()).containsExactly("a", "b"); + Annotations annotations = Annotations.fromUser(user); + assertThat(annotations.userMap()).containsExactly("a", "b"); - // Changing map afterwards does not change the labels. + // Changing map afterwards does not change the annotation. user.put("c", "d"); - assertThat(labels.userMap()).containsExactly("a", "b"); + assertThat(annotations.userMap()).containsExactly("a", "b"); } @Test public void testFromToPb() { - assertThat(Labels.fromPb(null).toPb()).isNull(); + assertThat(Annotations.fromPb(null).toPb()).isNull(); HashMap pb = new HashMap<>(); - assertThat(Labels.fromPb(pb).toPb()).isNull(); + assertThat(Annotations.fromPb(pb).toPb()).isNull(); pb.put("a", "b"); - assertThat(Labels.fromPb(pb).toPb()).isEqualTo(pb); + assertThat(Annotations.fromPb(pb).toPb()).isEqualTo(pb); pb.put("c", Data.NULL_STRING); - assertThat(Labels.fromPb(pb).toPb()).isEqualTo(pb); + assertThat(Annotations.fromPb(pb).toPb()).isEqualTo(pb); Map jsonNullMap = Data.nullOf(HashMap.class); - assertThat(Data.isNull(Labels.fromPb(jsonNullMap).toPb())).isTrue(); + assertThat(Data.isNull(Annotations.fromPb(jsonNullMap).toPb())).isTrue(); } @Test public void testNullKey() { try { - Labels.fromUser(Collections.singletonMap((String) null, "foo")); + Annotations.fromUser(Collections.singletonMap((String) null, "foo")); fail("null key shouldn't work"); } catch (IllegalArgumentException e) { } try { - Labels.fromPb(Collections.singletonMap((String) null, "foo")); + Annotations.fromPb(Collections.singletonMap((String) null, "foo")); fail("null key shouldn't work"); } catch (IllegalArgumentException e) { } diff --git a/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/TableInfoTest.java b/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/TableInfoTest.java index 6283f71c2..a4ce6fbb4 100644 --- a/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/TableInfoTest.java +++ b/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/TableInfoTest.java @@ -126,6 +126,7 @@ public class TableInfoTest { .setNumRows(BigInteger.valueOf(NUM_ROWS)) .setSelfLink(SELF_LINK) .setLabels(Collections.singletonMap("a", "b")) + .setResourceTags(Collections.singletonMap("resourceTagA", "resourceTagB")) .setRequirePartitionFilter(REQUIRE_PARTITION_FILTER) .build(); private static final TableInfo VIEW_INFO = @@ -309,6 +310,7 @@ private void compareTableInfo(TableInfo expected, TableInfo value) { assertEquals(expected.getNumRows(), value.getNumRows()); assertEquals(expected.getSelfLink(), value.getSelfLink()); assertEquals(expected.getLabels(), value.getLabels()); + assertEquals(expected.getResourceTags(), value.getResourceTags()); assertEquals(expected.getRequirePartitionFilter(), value.getRequirePartitionFilter()); assertEquals(expected.toString(), value.toString()); assertEquals(expected.hashCode(), value.hashCode());