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(pubsub): add support for snapshot labels #6835

Merged
merged 4 commits into from
Oct 11, 2022
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
9 changes: 9 additions & 0 deletions pubsub/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,15 @@ func TestIntegration_All(t *testing.T) {
t.Fatalf("CreateSnapshot error: %v", err)
}

labels := map[string]string{"foo": "bar"}
sc, err := snap.SetLabels(ctx, labels)
if err != nil {
t.Fatalf("Snapshot.SetLabels error: %v", err)
}
if diff := testutil.Diff(sc.Labels, labels); diff != "" {
t.Fatalf("\ngot: - want: +\n%s", diff)
}

timeoutCtx, cancel := context.WithTimeout(ctx, time.Minute)
defer cancel()
err = internal.Retry(timeoutCtx, gax.Backoff{}, func() (bool, error) {
Expand Down
21 changes: 21 additions & 0 deletions pubsub/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"time"

pb "google.golang.org/genproto/googleapis/pubsub/v1"
fmpb "google.golang.org/genproto/protobuf/field_mask"
"google.golang.org/protobuf/types/known/timestamppb"
)

Expand All @@ -42,11 +43,30 @@ func (s *Snapshot) ID() string {
return s.name[slash+1:]
}

// SetLabels sets or replaces the labels on a given snapshot.
func (s *Snapshot) SetLabels(ctx context.Context, label map[string]string) (*SnapshotConfig, error) {
sc, err := s.c.subc.UpdateSnapshot(ctx, &pb.UpdateSnapshotRequest{
Snapshot: &pb.Snapshot{
Name: s.name,
Labels: label,
},
UpdateMask: &fmpb.FieldMask{
Paths: []string{"labels"},
},
})
if err != nil {
return nil, err
}
return toSnapshotConfig(sc, s.c)
}

// SnapshotConfig contains the details of a Snapshot.
type SnapshotConfig struct {
*Snapshot
Topic *Topic
Expiration time.Time
// The set of labels for the snapshot.
Labels map[string]string
}

// Snapshot creates a reference to a snapshot.
Expand Down Expand Up @@ -151,5 +171,6 @@ func toSnapshotConfig(snap *pb.Snapshot, c *Client) (*SnapshotConfig, error) {
Snapshot: &Snapshot{c: c, name: snap.Name},
Topic: newTopic(c, snap.Topic),
Expiration: exp,
Labels: snap.Labels,
}, nil
}