From a07fd035bdbc533b3f064db0ba6d17000c0adc36 Mon Sep 17 00:00:00 2001 From: Alex Hong <9397363+hongalex@users.noreply.github.com> Date: Thu, 12 Aug 2021 22:05:00 -0700 Subject: [PATCH 1/4] feat(pubsub): add list configs for topic & sub --- pubsub/subscription.go | 19 ++++++++++++++++++- pubsub/subscription_test.go | 16 ++++++++++++++++ pubsub/topic.go | 16 +++++++++++++++- pubsub/topic_test.go | 21 ++++++++++++++++++++- 4 files changed, 69 insertions(+), 3 deletions(-) diff --git a/pubsub/subscription.go b/pubsub/subscription.go index d228ed12c33d..497d212bebe3 100644 --- a/pubsub/subscription.go +++ b/pubsub/subscription.go @@ -33,6 +33,8 @@ import ( "google.golang.org/grpc/codes" "google.golang.org/grpc/status" durpb "google.golang.org/protobuf/types/known/durationpb" + + vkit "cloud.google.com/go/pubsub/apiv1" ) // Subscription is a reference to a PubSub subscription. @@ -86,7 +88,8 @@ func (c *Client) Subscriptions(ctx context.Context) *SubscriptionIterator { Project: c.fullyQualifiedProjectName(), }) return &SubscriptionIterator{ - c: c, + c: c, + it: it, next: func() (string, error) { sub, err := it.Next() if err != nil { @@ -100,6 +103,7 @@ func (c *Client) Subscriptions(ctx context.Context) *SubscriptionIterator { // SubscriptionIterator is an iterator that returns a series of subscriptions. type SubscriptionIterator struct { c *Client + it *vkit.SubscriptionIterator next func() (string, error) } @@ -112,6 +116,19 @@ func (subs *SubscriptionIterator) Next() (*Subscription, error) { return &Subscription{c: subs.c, name: subName}, nil } +// Next returns the next subscription config. If there are no more subscriptions, iterator.Done will be returned. +func (subs *SubscriptionIterator) NextConfig() (*SubscriptionConfig, error) { + spb, err := subs.it.Next() + if err != nil { + return nil, err + } + cfg, err := protoToSubscriptionConfig(spb, subs.c) + if err != nil { + return nil, err + } + return &cfg, nil +} + // PushConfig contains configuration for subscriptions that operate in push mode. type PushConfig struct { // A URL locating the endpoint to which messages should be pushed. diff --git a/pubsub/subscription_test.go b/pubsub/subscription_test.go index 186f0723a752..832f2b16c6ba 100644 --- a/pubsub/subscription_test.go +++ b/pubsub/subscription_test.go @@ -81,6 +81,22 @@ func TestListProjectSubscriptions(t *testing.T) { if !testutil.Equal(got, want) { t.Errorf("got %v, want %v", got, want) } + + // Call list again, but check the config this time. + it := c.Subscriptions(ctx) + for { + sub, err := it.NextConfig() + if err == iterator.Done { + break + } + if err != nil { + t.Errorf("SubscriptionIterator.NextConfig() got err: %v", err) + } else { + if got := sub.Topic.ID(); got != topic.ID() { + t.Errorf("subConfig.Topic mismatch, got: %v, want: %v", got, topic.ID()) + } + } + } } func getSubIDs(subs []*Subscription) []string { diff --git a/pubsub/topic.go b/pubsub/topic.go index c991617e3030..a8c737e3e782 100644 --- a/pubsub/topic.go +++ b/pubsub/topic.go @@ -37,6 +37,8 @@ import ( "google.golang.org/grpc/codes" "google.golang.org/grpc/status" "google.golang.org/protobuf/proto" + + vkit "cloud.google.com/go/pubsub/apiv1" ) const ( @@ -326,7 +328,8 @@ func (t *Topic) updateRequest(cfg TopicConfigToUpdate) *pb.UpdateTopicRequest { func (c *Client) Topics(ctx context.Context) *TopicIterator { it := c.pubc.ListTopics(ctx, &pb.ListTopicsRequest{Project: c.fullyQualifiedProjectName()}) return &TopicIterator{ - c: c, + c: c, + it: it, next: func() (string, error) { topic, err := it.Next() if err != nil { @@ -340,6 +343,7 @@ func (c *Client) Topics(ctx context.Context) *TopicIterator { // TopicIterator is an iterator that returns a series of topics. type TopicIterator struct { c *Client + it *vkit.TopicIterator next func() (string, error) } @@ -352,6 +356,16 @@ func (tps *TopicIterator) Next() (*Topic, error) { return newTopic(tps.c, topicName), nil } +// NextConfig returns the next topic config. If there are no more topics, iterator.Done will be returned. +func (t *TopicIterator) NextConfig() (*TopicConfig, error) { + tpb, err := t.it.Next() + if err != nil { + return nil, err + } + cfg := protoToTopicConfig(tpb) + return &cfg, nil +} + // ID returns the unique identifier of the topic within its project. func (t *Topic) ID() string { slash := strings.LastIndex(t.name, "/") diff --git a/pubsub/topic_test.go b/pubsub/topic_test.go index d5d7299aa951..799ef2d04914 100644 --- a/pubsub/topic_test.go +++ b/pubsub/topic_test.go @@ -102,17 +102,36 @@ func TestCreateTopicWithConfig(t *testing.T) { } func TestListTopics(t *testing.T) { + ctx := context.Background() c, srv := newFake(t) defer c.Close() defer srv.Close() var ids []string - for i := 1; i <= 4; i++ { + numTopics := 4 + for i := 1; i <= numTopics; i++ { id := fmt.Sprintf("t%d", i) ids = append(ids, id) mustCreateTopic(t, c, id) } checkTopicListing(t, c, ids) + + var tt []*TopicConfig + it := c.Topics(ctx) + for { + topic, err := it.NextConfig() + if err == iterator.Done { + break + } + if err != nil { + t.Errorf("TopicIterator.NextConfig() got err: %v", err) + } else { + tt = append(tt, topic) + } + } + if got := len(tt); got != numTopics { + t.Errorf("c.Topics(ctx) returned %d topics, expected %d", got, numTopics) + } } func TestListCompletelyEmptyTopics(t *testing.T) { From 9270dfa8a3fb78f8e3b03248f2ad5d851f981843 Mon Sep 17 00:00:00 2001 From: Alex Hong <9397363+hongalex@users.noreply.github.com> Date: Fri, 13 Aug 2021 11:19:26 -0700 Subject: [PATCH 2/4] fix export comment --- pubsub/subscription.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pubsub/subscription.go b/pubsub/subscription.go index 497d212bebe3..a4afa48d5575 100644 --- a/pubsub/subscription.go +++ b/pubsub/subscription.go @@ -116,7 +116,7 @@ func (subs *SubscriptionIterator) Next() (*Subscription, error) { return &Subscription{c: subs.c, name: subName}, nil } -// Next returns the next subscription config. If there are no more subscriptions, iterator.Done will be returned. +// NextConfig returns the next subscription config. If there are no more subscriptions, iterator.Done will be returned. func (subs *SubscriptionIterator) NextConfig() (*SubscriptionConfig, error) { spb, err := subs.it.Next() if err != nil { From 75beb4b8f0bd58848230e014863c15eea3e4905a Mon Sep 17 00:00:00 2001 From: Alex Hong <9397363+hongalex@users.noreply.github.com> Date: Tue, 24 Aug 2021 12:03:59 -0700 Subject: [PATCH 3/4] clarify comments and style code --- pubsub/subscription.go | 5 ++++- pubsub/subscription_test.go | 7 +++---- pubsub/topic.go | 5 ++++- 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/pubsub/subscription.go b/pubsub/subscription.go index 3000aedcd173..98c041dfbd26 100644 --- a/pubsub/subscription.go +++ b/pubsub/subscription.go @@ -115,7 +115,10 @@ func (subs *SubscriptionIterator) Next() (*Subscription, error) { return &Subscription{c: subs.c, name: subName}, nil } -// NextConfig returns the next subscription config. If there are no more subscriptions, iterator.Done will be returned. +// NextConfig returns the next subscription config. If there are no more subscriptions, +// iterator.Done will be returned. +// This call shares the underlying iterator with calls to `SubscriptionIterator.Next`. +// If you wish to use mix calls, create separate iterator instances for both. func (subs *SubscriptionIterator) NextConfig() (*SubscriptionConfig, error) { spb, err := subs.it.Next() if err != nil { diff --git a/pubsub/subscription_test.go b/pubsub/subscription_test.go index 832f2b16c6ba..ca428777e306 100644 --- a/pubsub/subscription_test.go +++ b/pubsub/subscription_test.go @@ -91,10 +91,9 @@ func TestListProjectSubscriptions(t *testing.T) { } if err != nil { t.Errorf("SubscriptionIterator.NextConfig() got err: %v", err) - } else { - if got := sub.Topic.ID(); got != topic.ID() { - t.Errorf("subConfig.Topic mismatch, got: %v, want: %v", got, topic.ID()) - } + } + if got := sub.Topic.ID(); got != topic.ID() { + t.Errorf("subConfig.Topic mismatch, got: %v, want: %v", got, topic.ID()) } } } diff --git a/pubsub/topic.go b/pubsub/topic.go index a8c737e3e782..b0ca35419809 100644 --- a/pubsub/topic.go +++ b/pubsub/topic.go @@ -356,7 +356,10 @@ func (tps *TopicIterator) Next() (*Topic, error) { return newTopic(tps.c, topicName), nil } -// NextConfig returns the next topic config. If there are no more topics, iterator.Done will be returned. +// NextConfig returns the next topic config. If there are no more topics, +// iterator.Done will be returned. +// This call shares the underlying iterator with calls to `TopicIterator.Next`. +// If you wish to use mix calls, create separate iterator instances for both. func (t *TopicIterator) NextConfig() (*TopicConfig, error) { tpb, err := t.it.Next() if err != nil { From 4266c745e3b129ca2ee7bad3819f9c3691e52610 Mon Sep 17 00:00:00 2001 From: Alex Hong <9397363+hongalex@users.noreply.github.com> Date: Wed, 1 Sep 2021 15:39:52 -0700 Subject: [PATCH 4/4] run gofmt --- pubsub/topic.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pubsub/topic.go b/pubsub/topic.go index 3d57a2eeab40..f3bd9321cafe 100644 --- a/pubsub/topic.go +++ b/pubsub/topic.go @@ -27,6 +27,7 @@ import ( "cloud.google.com/go/iam" "cloud.google.com/go/internal/optional" ipubsub "cloud.google.com/go/internal/pubsub" + vkit "cloud.google.com/go/pubsub/apiv1" "cloud.google.com/go/pubsub/internal/scheduler" gax "github.com/googleapis/gax-go/v2" "go.opencensus.io/stats" @@ -38,7 +39,6 @@ import ( "google.golang.org/grpc/codes" "google.golang.org/grpc/status" "google.golang.org/protobuf/proto" - vkit "cloud.google.com/go/pubsub/apiv1" "google.golang.org/protobuf/types/known/durationpb" )