Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(bigquery): Support resource tags for datasets in java client #3647

Merged
merged 5 commits into from
Jan 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions google-cloud-bigquery/clirr-ignored-differences.xml
Original file line number Diff line number Diff line change
Expand Up @@ -134,4 +134,9 @@
<className>com/google/cloud/bigquery/DatasetInfo*</className>
<method>*setMaxTimeTravelHours(*)</method>
</difference>
<difference>
<differenceType>7013</differenceType>
<className>com/google/cloud/bigquery/DatasetInfo*</className>
<method>*setResourceTags(*)</method>
</difference>
</differences>
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,12 @@ public Builder setMaxTimeTravelHours(Long maxTimeTravelHours) {
return this;
}

@Override
public Builder setResourceTags(Map<String, String> resourceTags) {
infoBuilder.setResourceTags(resourceTags);
return this;
}

@Override
public Dataset build() {
return new Dataset(bigquery, infoBuilder);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ public Dataset apply(DatasetInfo datasetInfo) {
private final ExternalDatasetReference externalDatasetReference;
private final String storageBillingModel;
private final Long maxTimeTravelHours;
private final Annotations resourceTags;

/** A builder for {@code DatasetInfo} objects. */
public abstract static class Builder {
Expand Down Expand Up @@ -184,6 +185,19 @@ public abstract Builder setDefaultEncryptionConfiguration(
*/
public abstract Builder setDefaultCollation(String defaultCollation);

/**
* Optional. The <a href="https://cloud.google.com/bigquery/docs/tags">tags</a> attached to this
* dataset. Tag keys are globally unique. Tag key is expected to be in the namespaced format,
* for example "123456789012/environment" where 123456789012 is the ID of the parent
* organization or project resource for this tag key. Tag value is expected to be the short
* name, for example "Production".
*
* @see <a href="https://cloud.google.com/iam/docs/tags-access-control#definitions">Tag
* definitions</a> for more details.
* @param resourceTags resourceTags or {@code null} for none
*/
public abstract Builder setResourceTags(Map<String, String> resourceTags);

/** Creates a {@code DatasetInfo} object. */
public abstract DatasetInfo build();
}
Expand All @@ -208,6 +222,7 @@ static final class BuilderImpl extends Builder {
private ExternalDatasetReference externalDatasetReference;
private String storageBillingModel;
private Long maxTimeTravelHours;
private Annotations resourceTags = Annotations.ZERO;

BuilderImpl() {}

Expand All @@ -230,6 +245,7 @@ static final class BuilderImpl extends Builder {
this.externalDatasetReference = datasetInfo.externalDatasetReference;
this.storageBillingModel = datasetInfo.storageBillingModel;
this.maxTimeTravelHours = datasetInfo.maxTimeTravelHours;
this.resourceTags = datasetInfo.resourceTags;
}

BuilderImpl(com.google.api.services.bigquery.model.Dataset datasetPb) {
Expand Down Expand Up @@ -270,6 +286,7 @@ public Acl apply(Dataset.Access accessPb) {
}
this.storageBillingModel = datasetPb.getStorageBillingModel();
this.maxTimeTravelHours = datasetPb.getMaxTimeTravelHours();
this.resourceTags = Annotations.fromPb(datasetPb.getResourceTags());
}

@Override
Expand Down Expand Up @@ -388,6 +405,12 @@ public Builder setMaxTimeTravelHours(Long maxTimeTravelHours) {
return this;
}

@Override
public Builder setResourceTags(Map<String, String> resourceTags) {
this.resourceTags = Annotations.fromUser(resourceTags);
return this;
}

@Override
public DatasetInfo build() {
return new DatasetInfo(this);
Expand All @@ -413,6 +436,7 @@ public DatasetInfo build() {
externalDatasetReference = builder.externalDatasetReference;
storageBillingModel = builder.storageBillingModel;
maxTimeTravelHours = builder.maxTimeTravelHours;
resourceTags = builder.resourceTags;
}

/** Returns the dataset identity. */
Expand Down Expand Up @@ -554,6 +578,21 @@ public Long getMaxTimeTravelHours() {
return maxTimeTravelHours;
}

/**
* Optional. The <a href="https://cloud.google.com/bigquery/docs/tags">tags</a> attached to this
* dataset. Tag keys are globally unique. Tag key is expected to be in the namespaced format, for
* example "123456789012/environment" where 123456789012 is the ID of the parent organization or
* project resource for this tag key. Tag value is expected to be the short name, for example
* "Production".
*
* @see <a href="https://cloud.google.com/iam/docs/tags-access-control#definitions">Tag
* definitions</a> for more details.
* @return value or {@code null} for none
*/
public Map<String, String> getResourceTags() {
return resourceTags.userMap();
}

/**
* Returns information about the external metadata storage where the dataset is defined. Filled
* out when the dataset type is EXTERNAL.
Expand Down Expand Up @@ -588,6 +627,7 @@ public String toString() {
.add("externalDatasetReference", externalDatasetReference)
.add("storageBillingModel", storageBillingModel)
.add("maxTimeTravelHours", maxTimeTravelHours)
.add("resourceTags", resourceTags)
.toString();
}

Expand Down Expand Up @@ -675,6 +715,7 @@ public Dataset.Access apply(Acl acl) {
if (maxTimeTravelHours != null) {
datasetPb.setMaxTimeTravelHours(maxTimeTravelHours);
}
datasetPb.setResourceTags(resourceTags.toPb());
return datasetPb;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ public class DatasetInfoTest {
private static final String STORAGE_BILLING_MODEL = "LOGICAL";
private static final Long MAX_TIME_TRAVEL_HOURS_5_DAYS = 120L;
private static final Long MAX_TIME_TRAVEL_HOURS_7_DAYS = 168L;
private static final Map<String, String> RESOURCE_TAGS =
ImmutableMap.of(
"example-key1", "example-value1",
"example-key2", "example-value2");

private static final ExternalDatasetReference EXTERNAL_DATASET_REFERENCE =
ExternalDatasetReference.newBuilder()
Expand All @@ -85,6 +89,7 @@ public class DatasetInfoTest {
.setDefaultPartitionExpirationMs(DEFAULT_PARTITION__EXPIRATION)
.setStorageBillingModel(STORAGE_BILLING_MODEL)
.setMaxTimeTravelHours(MAX_TIME_TRAVEL_HOURS_7_DAYS)
.setResourceTags(RESOURCE_TAGS)
.build();
private static final DatasetInfo DATASET_INFO_COMPLETE =
DATASET_INFO
Expand Down Expand Up @@ -183,6 +188,7 @@ public void testBuilder() {
assertEquals(
MAX_TIME_TRAVEL_HOURS_5_DAYS,
DATASET_INFO_WITH_MAX_TIME_TRAVEL_5_DAYS.getMaxTimeTravelHours());
assertEquals(RESOURCE_TAGS, DATASET_INFO.getResourceTags());
}

@Test
Expand Down Expand Up @@ -272,5 +278,6 @@ private void compareDatasets(DatasetInfo expected, DatasetInfo value) {
assertEquals(expected.getExternalDatasetReference(), value.getExternalDatasetReference());
assertEquals(expected.getStorageBillingModel(), value.getStorageBillingModel());
assertEquals(expected.getMaxTimeTravelHours(), value.getMaxTimeTravelHours());
assertEquals(expected.getResourceTags(), value.getResourceTags());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ public class DatasetTest {
private static final Field FIELD = Field.of("FieldName", LegacySQLTypeName.INTEGER);
private static final String STORAGE_BILLING_MODEL = "LOGICAL";
private static final Long MAX_TIME_TRAVEL_HOURS = 168L;
private static final Map<String, String> RESOURCE_TAGS =
ImmutableMap.of(
"example-key1", "example-value1",
"example-key2", "example-value2");
private static final StandardTableDefinition TABLE_DEFINITION =
StandardTableDefinition.of(Schema.of(FIELD));
private static final ViewDefinition VIEW_DEFINITION = ViewDefinition.of("QUERY");
Expand Down Expand Up @@ -124,6 +128,7 @@ public void testBuilder() {
.setLabels(LABELS)
.setStorageBillingModel(STORAGE_BILLING_MODEL)
.setMaxTimeTravelHours(MAX_TIME_TRAVEL_HOURS)
.setResourceTags(RESOURCE_TAGS)
.build();
assertEquals(DATASET_ID, builtDataset.getDatasetId());
assertEquals(ACCESS_RULES, builtDataset.getAcl());
Expand All @@ -139,6 +144,7 @@ public void testBuilder() {
assertEquals(LABELS, builtDataset.getLabels());
assertEquals(STORAGE_BILLING_MODEL, builtDataset.getStorageBillingModel());
assertEquals(MAX_TIME_TRAVEL_HOURS, builtDataset.getMaxTimeTravelHours());
assertEquals(RESOURCE_TAGS, builtDataset.getResourceTags());
}

@Test
Expand Down Expand Up @@ -348,6 +354,7 @@ public void testExternalDatasetReference() {
.setExternalDatasetReference(EXTERNAL_DATASET_REFERENCE)
.setStorageBillingModel(STORAGE_BILLING_MODEL)
.setMaxTimeTravelHours(MAX_TIME_TRAVEL_HOURS)
.setResourceTags(RESOURCE_TAGS)
.build();
assertEquals(
EXTERNAL_DATASET_REFERENCE,
Expand Down Expand Up @@ -379,5 +386,6 @@ private void compareDatasetInfo(DatasetInfo expected, DatasetInfo value) {
assertEquals(expected.getExternalDatasetReference(), value.getExternalDatasetReference());
assertEquals(expected.getStorageBillingModel(), value.getStorageBillingModel());
assertEquals(expected.getMaxTimeTravelHours(), value.getMaxTimeTravelHours());
assertEquals(expected.getResourceTags(), value.getResourceTags());
}
}
Loading