From c339137f43410e82b6ed8e316f91d2df139329e4 Mon Sep 17 00:00:00 2001 From: Alex Hong <9397363+hongalex@users.noreply.github.com> Date: Mon, 10 Oct 2022 17:46:04 -0700 Subject: [PATCH 1/3] feat(pubsub): add support for snapshot labels --- pubsub/integration_test.go | 9 +++++++++ pubsub/snapshot.go | 23 +++++++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/pubsub/integration_test.go b/pubsub/integration_test.go index 121cc2518ec..ae3356873e2 100644 --- a/pubsub/integration_test.go +++ b/pubsub/integration_test.go @@ -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) { diff --git a/pubsub/snapshot.go b/pubsub/snapshot.go index 8a223a99f9a..a52046f2c10 100644 --- a/pubsub/snapshot.go +++ b/pubsub/snapshot.go @@ -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" ) @@ -42,11 +43,32 @@ func (s *Snapshot) ID() string { return s.name[slash+1:] } +// SetLabels replaces the current set of labels completely with the new set. +func (s *Snapshot) SetLabels(ctx context.Context, label map[string]string) (*SnapshotConfig, error) { + var fields []string + fields = append(fields, "labels") + sc, err := s.c.subc.UpdateSnapshot(ctx, &pb.UpdateSnapshotRequest{ + Snapshot: &pb.Snapshot{ + Name: s.name, + Labels: label, + }, + UpdateMask: &fmpb.FieldMask{ + Paths: fields, + }, + }) + 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. @@ -151,5 +173,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 } From dc9599427ec40504942028ca00391c8e1fe58d1b Mon Sep 17 00:00:00 2001 From: Alex Hong <9397363+hongalex@users.noreply.github.com> Date: Tue, 11 Oct 2022 09:08:29 -0700 Subject: [PATCH 2/3] update comments and field construction --- pubsub/snapshot.go | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/pubsub/snapshot.go b/pubsub/snapshot.go index a52046f2c10..f6e9843687a 100644 --- a/pubsub/snapshot.go +++ b/pubsub/snapshot.go @@ -43,17 +43,15 @@ func (s *Snapshot) ID() string { return s.name[slash+1:] } -// SetLabels replaces the current set of labels completely with the new set. +// SetLabels set or replaces the labels on a given snapshot. func (s *Snapshot) SetLabels(ctx context.Context, label map[string]string) (*SnapshotConfig, error) { - var fields []string - fields = append(fields, "labels") sc, err := s.c.subc.UpdateSnapshot(ctx, &pb.UpdateSnapshotRequest{ Snapshot: &pb.Snapshot{ Name: s.name, Labels: label, }, UpdateMask: &fmpb.FieldMask{ - Paths: fields, + Paths: []string{"labels"}, }, }) if err != nil { From 9e576ac337e9709307ef04b93401d18f049377e2 Mon Sep 17 00:00:00 2001 From: Alex Hong <9397363+hongalex@users.noreply.github.com> Date: Tue, 11 Oct 2022 10:24:09 -0700 Subject: [PATCH 3/3] fix typo in comment --- pubsub/snapshot.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pubsub/snapshot.go b/pubsub/snapshot.go index f6e9843687a..78797f36475 100644 --- a/pubsub/snapshot.go +++ b/pubsub/snapshot.go @@ -43,7 +43,7 @@ func (s *Snapshot) ID() string { return s.name[slash+1:] } -// SetLabels set or replaces the labels on a given snapshot. +// 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{