|
| 1 | +// Copyright 2020 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | + |
| 14 | +package pubsublite |
| 15 | + |
| 16 | +import ( |
| 17 | + "context" |
| 18 | + |
| 19 | + "google.golang.org/api/option" |
| 20 | + "google.golang.org/api/option/internaloption" |
| 21 | + |
| 22 | + vkit "cloud.google.com/go/pubsublite/apiv1" |
| 23 | + pb "google.golang.org/genproto/googleapis/cloud/pubsublite/v1" |
| 24 | +) |
| 25 | + |
| 26 | +// AdminClient provides admin operations for Google Pub/Sub Lite resources |
| 27 | +// within a Google Cloud region. An AdminClient may be shared by multiple |
| 28 | +// goroutines. |
| 29 | +type AdminClient struct { |
| 30 | + admin *vkit.AdminClient |
| 31 | +} |
| 32 | + |
| 33 | +// NewAdminClient creates a new Cloud Pub/Sub Lite client to perform admin |
| 34 | +// operations for resources within a given region. |
| 35 | +// See https://cloud.google.com/pubsub/lite/docs/locations for the list of |
| 36 | +// regions and zones where Google Pub/Sub Lite is available. |
| 37 | +func NewAdminClient(ctx context.Context, region string, opts ...option.ClientOption) (*AdminClient, error) { |
| 38 | + if err := validateRegion(region); err != nil { |
| 39 | + return nil, err |
| 40 | + } |
| 41 | + options := []option.ClientOption{internaloption.WithDefaultEndpoint(region + "-pubsublite.googleapis.com:443")} |
| 42 | + options = append(options, opts...) |
| 43 | + admin, err := vkit.NewAdminClient(ctx, options...) |
| 44 | + if err != nil { |
| 45 | + return nil, err |
| 46 | + } |
| 47 | + return &AdminClient{admin: admin}, nil |
| 48 | +} |
| 49 | + |
| 50 | +// CreateTopic creates a new topic from the given config. |
| 51 | +func (ac *AdminClient) CreateTopic(ctx context.Context, config TopicConfig) (*TopicConfig, error) { |
| 52 | + req := &pb.CreateTopicRequest{ |
| 53 | + Parent: config.Name.location().String(), |
| 54 | + Topic: config.toProto(), |
| 55 | + TopicId: config.Name.TopicID, |
| 56 | + } |
| 57 | + topicpb, err := ac.admin.CreateTopic(ctx, req) |
| 58 | + if err != nil { |
| 59 | + return nil, err |
| 60 | + } |
| 61 | + return protoToTopicConfig(topicpb) |
| 62 | +} |
| 63 | + |
| 64 | +// UpdateTopic updates an existing topic from the given config and returns the |
| 65 | +// new topic config. |
| 66 | +func (ac *AdminClient) UpdateTopic(ctx context.Context, config TopicConfigToUpdate) (*TopicConfig, error) { |
| 67 | + topicpb, err := ac.admin.UpdateTopic(ctx, config.toUpdateRequest()) |
| 68 | + if err != nil { |
| 69 | + return nil, err |
| 70 | + } |
| 71 | + return protoToTopicConfig(topicpb) |
| 72 | +} |
| 73 | + |
| 74 | +// DeleteTopic deletes a topic. |
| 75 | +func (ac *AdminClient) DeleteTopic(ctx context.Context, topic TopicPath) error { |
| 76 | + return ac.admin.DeleteTopic(ctx, &pb.DeleteTopicRequest{Name: topic.String()}) |
| 77 | +} |
| 78 | + |
| 79 | +// Topic retrieves the configuration of a topic. |
| 80 | +func (ac *AdminClient) Topic(ctx context.Context, topic TopicPath) (*TopicConfig, error) { |
| 81 | + topicpb, err := ac.admin.GetTopic(ctx, &pb.GetTopicRequest{Name: topic.String()}) |
| 82 | + if err != nil { |
| 83 | + return nil, err |
| 84 | + } |
| 85 | + return protoToTopicConfig(topicpb) |
| 86 | +} |
| 87 | + |
| 88 | +// TopicPartitions returns the number of partitions for a topic. |
| 89 | +func (ac *AdminClient) TopicPartitions(ctx context.Context, topic TopicPath) (int, error) { |
| 90 | + partitions, err := ac.admin.GetTopicPartitions(ctx, &pb.GetTopicPartitionsRequest{Name: topic.String()}) |
| 91 | + if err != nil { |
| 92 | + return 0, err |
| 93 | + } |
| 94 | + return int(partitions.GetPartitionCount()), nil |
| 95 | +} |
| 96 | + |
| 97 | +// TopicSubscriptions retrieves the list of subscription paths for a topic. |
| 98 | +func (ac *AdminClient) TopicSubscriptions(ctx context.Context, topic TopicPath) (*SubscriptionPathIterator, error) { |
| 99 | + subsPathIt := ac.admin.ListTopicSubscriptions(ctx, &pb.ListTopicSubscriptionsRequest{Name: topic.String()}) |
| 100 | + return &SubscriptionPathIterator{it: subsPathIt}, nil |
| 101 | +} |
| 102 | + |
| 103 | +// Topics retrieves the list of topic configs for a given project and zone. |
| 104 | +func (ac *AdminClient) Topics(ctx context.Context, location LocationPath) *TopicIterator { |
| 105 | + return &TopicIterator{ |
| 106 | + it: ac.admin.ListTopics(ctx, &pb.ListTopicsRequest{Parent: location.String()}), |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +// CreateSubscription creates a new subscription from the given config. |
| 111 | +func (ac *AdminClient) CreateSubscription(ctx context.Context, config SubscriptionConfig) (*SubscriptionConfig, error) { |
| 112 | + req := &pb.CreateSubscriptionRequest{ |
| 113 | + Parent: config.Name.location().String(), |
| 114 | + Subscription: config.toProto(), |
| 115 | + SubscriptionId: config.Name.SubscriptionID, |
| 116 | + } |
| 117 | + subspb, err := ac.admin.CreateSubscription(ctx, req) |
| 118 | + if err != nil { |
| 119 | + return nil, err |
| 120 | + } |
| 121 | + return protoToSubscriptionConfig(subspb) |
| 122 | +} |
| 123 | + |
| 124 | +// UpdateSubscription updates an existing subscription from the given config and |
| 125 | +// returns the new subscription config. |
| 126 | +func (ac *AdminClient) UpdateSubscription(ctx context.Context, config SubscriptionConfigToUpdate) (*SubscriptionConfig, error) { |
| 127 | + subspb, err := ac.admin.UpdateSubscription(ctx, config.toUpdateRequest()) |
| 128 | + if err != nil { |
| 129 | + return nil, err |
| 130 | + } |
| 131 | + return protoToSubscriptionConfig(subspb) |
| 132 | +} |
| 133 | + |
| 134 | +// DeleteSubscription deletes a subscription. |
| 135 | +func (ac *AdminClient) DeleteSubscription(ctx context.Context, subscription SubscriptionPath) error { |
| 136 | + return ac.admin.DeleteSubscription(ctx, &pb.DeleteSubscriptionRequest{Name: subscription.String()}) |
| 137 | +} |
| 138 | + |
| 139 | +// Subscription retrieves the configuration of a subscription. |
| 140 | +func (ac *AdminClient) Subscription(ctx context.Context, subscription SubscriptionPath) (*SubscriptionConfig, error) { |
| 141 | + subspb, err := ac.admin.GetSubscription(ctx, &pb.GetSubscriptionRequest{Name: subscription.String()}) |
| 142 | + if err != nil { |
| 143 | + return nil, err |
| 144 | + } |
| 145 | + return protoToSubscriptionConfig(subspb) |
| 146 | +} |
| 147 | + |
| 148 | +// Subscriptions retrieves the list of subscription configs for a given project |
| 149 | +// and zone. |
| 150 | +func (ac *AdminClient) Subscriptions(ctx context.Context, location LocationPath) *SubscriptionIterator { |
| 151 | + return &SubscriptionIterator{ |
| 152 | + it: ac.admin.ListSubscriptions(ctx, &pb.ListSubscriptionsRequest{Parent: location.String()}), |
| 153 | + } |
| 154 | +} |
| 155 | + |
| 156 | +// Close releases any resources held by the client when it is no longer |
| 157 | +// required. If the client is available for the lifetime of the program, then |
| 158 | +// Close need not be called at exit. |
| 159 | +func (ac *AdminClient) Close() error { |
| 160 | + return ac.admin.Close() |
| 161 | +} |
| 162 | + |
| 163 | +// TopicIterator is an iterator that returns a list of topic configs. |
| 164 | +type TopicIterator struct { |
| 165 | + it *vkit.TopicIterator |
| 166 | +} |
| 167 | + |
| 168 | +// Next returns the next topic config. The second return value will be |
| 169 | +// iterator.Done if there are no more topic configs. |
| 170 | +func (t *TopicIterator) Next() (*TopicConfig, error) { |
| 171 | + topicpb, err := t.it.Next() |
| 172 | + if err != nil { |
| 173 | + return nil, err |
| 174 | + } |
| 175 | + return protoToTopicConfig(topicpb) |
| 176 | +} |
| 177 | + |
| 178 | +// SubscriptionIterator is an iterator that returns a list of subscription |
| 179 | +// configs. |
| 180 | +type SubscriptionIterator struct { |
| 181 | + it *vkit.SubscriptionIterator |
| 182 | +} |
| 183 | + |
| 184 | +// Next returns the next subscription config. The second return value will be |
| 185 | +// iterator.Done if there are no more subscription configs. |
| 186 | +func (s *SubscriptionIterator) Next() (*SubscriptionConfig, error) { |
| 187 | + subspb, err := s.it.Next() |
| 188 | + if err != nil { |
| 189 | + return nil, err |
| 190 | + } |
| 191 | + return protoToSubscriptionConfig(subspb) |
| 192 | +} |
| 193 | + |
| 194 | +// SubscriptionPathIterator is an iterator that returns a list of subscription |
| 195 | +// paths. |
| 196 | +type SubscriptionPathIterator struct { |
| 197 | + it *vkit.StringIterator |
| 198 | +} |
| 199 | + |
| 200 | +// Next returns the next subscription path. The second return value will be |
| 201 | +// iterator.Done if there are no more subscription paths. |
| 202 | +func (sp *SubscriptionPathIterator) Next() (SubscriptionPath, error) { |
| 203 | + subsPath, err := sp.it.Next() |
| 204 | + if err != nil { |
| 205 | + return SubscriptionPath{}, err |
| 206 | + } |
| 207 | + return parseSubscriptionPath(subsPath) |
| 208 | +} |
0 commit comments