Skip to content

Commit c17851b

Browse files
authored
feat(pubsub): add support for snapshot labels (#6835)
* feat(pubsub): add support for snapshot labels * update comments and field construction * fix typo in comment
1 parent 1d0116a commit c17851b

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

pubsub/integration_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,15 @@ func TestIntegration_All(t *testing.T) {
164164
t.Fatalf("CreateSnapshot error: %v", err)
165165
}
166166

167+
labels := map[string]string{"foo": "bar"}
168+
sc, err := snap.SetLabels(ctx, labels)
169+
if err != nil {
170+
t.Fatalf("Snapshot.SetLabels error: %v", err)
171+
}
172+
if diff := testutil.Diff(sc.Labels, labels); diff != "" {
173+
t.Fatalf("\ngot: - want: +\n%s", diff)
174+
}
175+
167176
timeoutCtx, cancel := context.WithTimeout(ctx, time.Minute)
168177
defer cancel()
169178
err = internal.Retry(timeoutCtx, gax.Backoff{}, func() (bool, error) {

pubsub/snapshot.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"time"
2222

2323
pb "google.golang.org/genproto/googleapis/pubsub/v1"
24+
fmpb "google.golang.org/genproto/protobuf/field_mask"
2425
"google.golang.org/protobuf/types/known/timestamppb"
2526
)
2627

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

46+
// SetLabels sets or replaces the labels on a given snapshot.
47+
func (s *Snapshot) SetLabels(ctx context.Context, label map[string]string) (*SnapshotConfig, error) {
48+
sc, err := s.c.subc.UpdateSnapshot(ctx, &pb.UpdateSnapshotRequest{
49+
Snapshot: &pb.Snapshot{
50+
Name: s.name,
51+
Labels: label,
52+
},
53+
UpdateMask: &fmpb.FieldMask{
54+
Paths: []string{"labels"},
55+
},
56+
})
57+
if err != nil {
58+
return nil, err
59+
}
60+
return toSnapshotConfig(sc, s.c)
61+
}
62+
4563
// SnapshotConfig contains the details of a Snapshot.
4664
type SnapshotConfig struct {
4765
*Snapshot
4866
Topic *Topic
4967
Expiration time.Time
68+
// The set of labels for the snapshot.
69+
Labels map[string]string
5070
}
5171

5272
// Snapshot creates a reference to a snapshot.
@@ -151,5 +171,6 @@ func toSnapshotConfig(snap *pb.Snapshot, c *Client) (*SnapshotConfig, error) {
151171
Snapshot: &Snapshot{c: c, name: snap.Name},
152172
Topic: newTopic(c, snap.Topic),
153173
Expiration: exp,
174+
Labels: snap.Labels,
154175
}, nil
155176
}

0 commit comments

Comments
 (0)