Skip to content

Commit

Permalink
feat(bigquery): add support for dataset tags (#6114)
Browse files Browse the repository at this point in the history
  • Loading branch information
shollyman committed Jun 13, 2022
1 parent 3c0e4e0 commit 1f35044
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
32 changes: 32 additions & 0 deletions bigquery/dataset.go
Expand Up @@ -50,11 +50,37 @@ type DatasetMetadata struct {
LastModifiedTime time.Time // When the dataset or any of its tables were modified.
FullID string // The full dataset ID in the form projectID:datasetID.

// The tags associated with this dataset. Tag keys are
// globally unique, and managed via the resource manager API.
// More information: https://cloud.google.com/resource-manager/docs/tags/tags-overview
Tags []*DatasetTag

// ETag is the ETag obtained when reading metadata. Pass it to Dataset.Update to
// ensure that the metadata hasn't changed since it was read.
ETag string
}

// DatasetTag is a representation of a single tag key/value.
type DatasetTag struct {
// TagKey is the namespaced friendly name of the tag key, e.g.
// "12345/environment" where 12345 is org id.
TagKey string

// TagValue is the friendly short name of the tag value, e.g.
// "production".
TagValue string
}

func bqToDatasetTag(in *bq.DatasetTags) *DatasetTag {
if in == nil {
return nil
}
return &DatasetTag{
TagKey: in.TagKey,
TagValue: in.TagValue,
}
}

// DatasetMetadataToUpdate is used when updating a dataset's metadata.
// Only non-nil fields will be updated.
type DatasetMetadataToUpdate struct {
Expand Down Expand Up @@ -230,6 +256,12 @@ func bqToDatasetMetadata(d *bq.Dataset, c *Client) (*DatasetMetadata, error) {
}
dm.Access = append(dm.Access, e)
}
for _, bqTag := range d.Tags {
tag := bqToDatasetTag(bqTag)
if tag != nil {
dm.Tags = append(dm.Tags, tag)
}
}
return dm, nil
}

Expand Down
8 changes: 8 additions & 0 deletions bigquery/dataset_test.go
Expand Up @@ -413,6 +413,10 @@ func TestBQToDatasetMetadata(t *testing.T) {
},
},
},
Tags: []*bq.DatasetTags{
{TagKey: "tag1", TagValue: "value1"},
{TagKey: "tag2", TagValue: "value2"},
},
Etag: "etag",
}
want := &DatasetMetadata{
Expand All @@ -437,6 +441,10 @@ func TestBQToDatasetMetadata(t *testing.T) {
},
},
},
Tags: []*DatasetTag{
{TagKey: "tag1", TagValue: "value1"},
{TagKey: "tag2", TagValue: "value2"},
},
ETag: "etag",
}
got, err := bqToDatasetMetadata(q, client)
Expand Down

0 comments on commit 1f35044

Please sign in to comment.