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(pubsublite): Pub/Sub Lite admin client #3036
Merged
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
48f09b3
feat(pubsublite): Admin client and integration tests
tmdiep c26521f
Do not wrap errors in the admin client
tmdiep 3ee7e6f
Add Pub/Sub Lite API to contributing.md and use token source from tes…
tmdiep 343b0c3
Fixed kokoro build failure
tmdiep 12b8608
Merge branch 'master' into admin_client
hongalex a71385e
Address additional review comments in https://github.com/googleapis/g…
tmdiep 0e06a8e
Address review comments from https://github.com/googleapis/google-clo…
tmdiep 15a0e60
Merge branch 'refs/heads/master' into admin_client
tmdiep 990613c
Merge branch 'admin_client' of github.com:tmdiep/google-cloud-go into…
tmdiep 66379c5
Rename Client to AdminClient
tmdiep d4a5ecb
Changed TopicConfig.RetentionDuration to denote no retention as Infin…
tmdiep d131d89
Merge branch 'refs/heads/master' into admin_client
tmdiep fb8200d
Merge branch 'refs/heads/master' into admin_client
tmdiep 04abefd
Comments for valid values of PerPartitionBytes and RetentionDuration
tmdiep 381a3ec
Merge branch 'master' into admin_client
tmdiep 6d92608
Merge branch 'master' into admin_client
tmdiep File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,208 @@ | ||
// Copyright 2020 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// https://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
|
||
package pubsublite | ||
|
||
import ( | ||
"context" | ||
|
||
"google.golang.org/api/option" | ||
"google.golang.org/api/option/internaloption" | ||
|
||
vkit "cloud.google.com/go/pubsublite/apiv1" | ||
pb "google.golang.org/genproto/googleapis/cloud/pubsublite/v1" | ||
) | ||
|
||
// AdminClient provides admin operations for Google Pub/Sub Lite resources | ||
// within a Google Cloud region. An AdminClient may be shared by multiple | ||
// goroutines. | ||
type AdminClient struct { | ||
admin *vkit.AdminClient | ||
} | ||
|
||
// NewAdminClient creates a new Cloud Pub/Sub Lite client to perform admin | ||
// operations for resources within a given region. | ||
// See https://cloud.google.com/pubsub/lite/docs/locations for the list of | ||
// regions and zones where Google Pub/Sub Lite is available. | ||
func NewAdminClient(ctx context.Context, region string, opts ...option.ClientOption) (*AdminClient, error) { | ||
if err := validateRegion(region); err != nil { | ||
return nil, err | ||
} | ||
options := []option.ClientOption{internaloption.WithDefaultEndpoint(region + "-pubsublite.googleapis.com:443")} | ||
options = append(options, opts...) | ||
admin, err := vkit.NewAdminClient(ctx, options...) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &AdminClient{admin: admin}, nil | ||
} | ||
|
||
// CreateTopic creates a new topic from the given config. | ||
func (ac *AdminClient) CreateTopic(ctx context.Context, config TopicConfig) (*TopicConfig, error) { | ||
req := &pb.CreateTopicRequest{ | ||
Parent: config.Name.location().String(), | ||
Topic: config.toProto(), | ||
TopicId: config.Name.TopicID, | ||
} | ||
topicpb, err := ac.admin.CreateTopic(ctx, req) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return protoToTopicConfig(topicpb) | ||
} | ||
|
||
// UpdateTopic updates an existing topic from the given config and returns the | ||
// new topic config. | ||
func (ac *AdminClient) UpdateTopic(ctx context.Context, config TopicConfigToUpdate) (*TopicConfig, error) { | ||
topicpb, err := ac.admin.UpdateTopic(ctx, config.toUpdateRequest()) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return protoToTopicConfig(topicpb) | ||
} | ||
|
||
// DeleteTopic deletes a topic. | ||
func (ac *AdminClient) DeleteTopic(ctx context.Context, topic TopicPath) error { | ||
return ac.admin.DeleteTopic(ctx, &pb.DeleteTopicRequest{Name: topic.String()}) | ||
} | ||
|
||
// Topic retrieves the configuration of a topic. | ||
func (ac *AdminClient) Topic(ctx context.Context, topic TopicPath) (*TopicConfig, error) { | ||
topicpb, err := ac.admin.GetTopic(ctx, &pb.GetTopicRequest{Name: topic.String()}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return protoToTopicConfig(topicpb) | ||
} | ||
|
||
// TopicPartitions returns the number of partitions for a topic. | ||
func (ac *AdminClient) TopicPartitions(ctx context.Context, topic TopicPath) (int, error) { | ||
partitions, err := ac.admin.GetTopicPartitions(ctx, &pb.GetTopicPartitionsRequest{Name: topic.String()}) | ||
if err != nil { | ||
return 0, err | ||
} | ||
return int(partitions.GetPartitionCount()), nil | ||
} | ||
|
||
// TopicSubscriptions retrieves the list of subscription paths for a topic. | ||
func (ac *AdminClient) TopicSubscriptions(ctx context.Context, topic TopicPath) (*SubscriptionPathIterator, error) { | ||
subsPathIt := ac.admin.ListTopicSubscriptions(ctx, &pb.ListTopicSubscriptionsRequest{Name: topic.String()}) | ||
return &SubscriptionPathIterator{it: subsPathIt}, nil | ||
} | ||
|
||
// Topics retrieves the list of topic configs for a given project and zone. | ||
func (ac *AdminClient) Topics(ctx context.Context, location LocationPath) *TopicIterator { | ||
return &TopicIterator{ | ||
it: ac.admin.ListTopics(ctx, &pb.ListTopicsRequest{Parent: location.String()}), | ||
} | ||
} | ||
|
||
// CreateSubscription creates a new subscription from the given config. | ||
func (ac *AdminClient) CreateSubscription(ctx context.Context, config SubscriptionConfig) (*SubscriptionConfig, error) { | ||
req := &pb.CreateSubscriptionRequest{ | ||
Parent: config.Name.location().String(), | ||
Subscription: config.toProto(), | ||
SubscriptionId: config.Name.SubscriptionID, | ||
} | ||
subspb, err := ac.admin.CreateSubscription(ctx, req) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return protoToSubscriptionConfig(subspb) | ||
} | ||
|
||
// UpdateSubscription updates an existing subscription from the given config and | ||
// returns the new subscription config. | ||
func (ac *AdminClient) UpdateSubscription(ctx context.Context, config SubscriptionConfigToUpdate) (*SubscriptionConfig, error) { | ||
subspb, err := ac.admin.UpdateSubscription(ctx, config.toUpdateRequest()) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return protoToSubscriptionConfig(subspb) | ||
} | ||
|
||
// DeleteSubscription deletes a subscription. | ||
func (ac *AdminClient) DeleteSubscription(ctx context.Context, subscription SubscriptionPath) error { | ||
return ac.admin.DeleteSubscription(ctx, &pb.DeleteSubscriptionRequest{Name: subscription.String()}) | ||
} | ||
|
||
// Subscription retrieves the configuration of a subscription. | ||
func (ac *AdminClient) Subscription(ctx context.Context, subscription SubscriptionPath) (*SubscriptionConfig, error) { | ||
subspb, err := ac.admin.GetSubscription(ctx, &pb.GetSubscriptionRequest{Name: subscription.String()}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return protoToSubscriptionConfig(subspb) | ||
} | ||
|
||
// Subscriptions retrieves the list of subscription configs for a given project | ||
// and zone. | ||
func (ac *AdminClient) Subscriptions(ctx context.Context, location LocationPath) *SubscriptionIterator { | ||
return &SubscriptionIterator{ | ||
it: ac.admin.ListSubscriptions(ctx, &pb.ListSubscriptionsRequest{Parent: location.String()}), | ||
} | ||
} | ||
|
||
// Close releases any resources held by the client when it is no longer | ||
// required. If the client is available for the lifetime of the program, then | ||
// Close need not be called at exit. | ||
func (ac *AdminClient) Close() error { | ||
return ac.admin.Close() | ||
} | ||
|
||
// TopicIterator is an iterator that returns a list of topic configs. | ||
type TopicIterator struct { | ||
it *vkit.TopicIterator | ||
} | ||
|
||
// Next returns the next topic config. The second return value will be | ||
// iterator.Done if there are no more topic configs. | ||
func (t *TopicIterator) Next() (*TopicConfig, error) { | ||
topicpb, err := t.it.Next() | ||
if err != nil { | ||
return nil, err | ||
} | ||
return protoToTopicConfig(topicpb) | ||
} | ||
|
||
// SubscriptionIterator is an iterator that returns a list of subscription | ||
// configs. | ||
type SubscriptionIterator struct { | ||
it *vkit.SubscriptionIterator | ||
} | ||
|
||
// Next returns the next subscription config. The second return value will be | ||
// iterator.Done if there are no more subscription configs. | ||
func (s *SubscriptionIterator) Next() (*SubscriptionConfig, error) { | ||
subspb, err := s.it.Next() | ||
if err != nil { | ||
return nil, err | ||
} | ||
return protoToSubscriptionConfig(subspb) | ||
} | ||
|
||
// SubscriptionPathIterator is an iterator that returns a list of subscription | ||
// paths. | ||
type SubscriptionPathIterator struct { | ||
it *vkit.StringIterator | ||
} | ||
|
||
// Next returns the next subscription path. The second return value will be | ||
// iterator.Done if there are no more subscription paths. | ||
func (sp *SubscriptionPathIterator) Next() (SubscriptionPath, error) { | ||
subsPath, err := sp.it.Next() | ||
if err != nil { | ||
return SubscriptionPath{}, err | ||
} | ||
return parseSubscriptionPath(subsPath) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here, and with other methods, XXXPath does not seems like a meaningful name. It makes sense if you know that it is used to build a resource name, but if you don't know that the name is confusing in my opinion. I wonder if there is a way to refactor this so that all you need to pass to the method is a context and
TopicID
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The topic is uniquely identified by its {project, zone, topic id}. I didn't store the project in the client because we can have cross-project subscriptions, where the topic and subscription resources are not necessarily owned by the same project. So having a default project set for the client could cause some confusion for, e.g. Create/Update Subscription (i.e. is the default project used for the topic or the subscription?).
I also saw that pubsub.Client had evolved to support multiple variants Topic/TopicInProject, Subscription/SubscriptionInProject, so wanted to avoid this, as it seems multi-project admin does occur.
Similar remarks for zone, as the admin server is regional. Users can use the same client to manage all resources within the region.
For these reasons, I thought it would be best to give the client more flexibility with projects and zones; minimizing the number of methods.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I forgot to mention: we have planned regional replication for topics (b/171412217), so the admin client could end up dealing with multiple zones within the same region, so storing a default zone could cause confusion and/or proliferation of method variants.