forked from GoogleCloudPlatform/golang-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
322 lines (289 loc) · 8.42 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
// Copyright 2019 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
// limitations under the License.
// Command topics is a tool to manage Google Cloud Pub/Sub topics by using the Pub/Sub API.
// See more about Google Cloud Pub/Sub at https://cloud.google.com/pubsub/docs/overview.
package main
import (
"context"
"errors"
"fmt"
"log"
"os"
"strconv"
"sync"
"sync/atomic"
"time"
"cloud.google.com/go/iam"
"cloud.google.com/go/pubsub"
"google.golang.org/api/iterator"
)
func main() {
ctx := context.Background()
proj := os.Getenv("GOOGLE_CLOUD_PROJECT")
if proj == "" {
fmt.Fprintf(os.Stderr, "GOOGLE_CLOUD_PROJECT environment variable must be set.\n")
os.Exit(1)
}
client, err := pubsub.NewClient(ctx, proj)
if err != nil {
log.Fatalf("Could not create pubsub Client: %v", err)
}
// List all the topics from the project.
fmt.Println("Listing all topics from the project:")
topics, err := list(client)
if err != nil {
log.Fatalf("Failed to list topics: %v", err)
}
for _, t := range topics {
fmt.Println(t)
}
const topic = "my-topic"
// Create a new topic called my-topic.
if err := create(client, topic); err != nil {
log.Fatalf("Failed to create a topic: %v", err)
}
// Publish a text message on the created topic.
if err := publish(client, topic, "hello world!"); err != nil {
log.Fatalf("Failed to publish: %v", err)
}
// Publish 10 messages with asynchronous error handling.
if err := publishThatScales(client, topic, 10); err != nil {
log.Fatalf("Failed to publish: %v", err)
}
// Delete the topic.
if err := delete(client, topic); err != nil {
log.Fatalf("Failed to delete the topic: %v", err)
}
}
func create(client *pubsub.Client, topic string) error {
ctx := context.Background()
// [START pubsub_create_topic]
t, err := client.CreateTopic(ctx, topic)
if err != nil {
return err
}
fmt.Printf("Topic created: %v\n", t)
// [END pubsub_create_topic]
return nil
}
func list(client *pubsub.Client) ([]*pubsub.Topic, error) {
ctx := context.Background()
// [START pubsub_list_topics]
var topics []*pubsub.Topic
it := client.Topics(ctx)
for {
topic, err := it.Next()
if err == iterator.Done {
break
}
if err != nil {
return nil, err
}
topics = append(topics, topic)
}
return topics, nil
// [END pubsub_list_topics]
}
func listSubscriptions(client *pubsub.Client, topicID string) ([]*pubsub.Subscription, error) {
ctx := context.Background()
// [START pubsub_list_topic_subscriptions]
var subs []*pubsub.Subscription
it := client.Topic(topicID).Subscriptions(ctx)
for {
sub, err := it.Next()
if err == iterator.Done {
break
}
if err != nil {
return nil, err
}
subs = append(subs, sub)
}
// [END pubsub_list_topic_subscriptions]
return subs, nil
}
func delete(client *pubsub.Client, topic string) error {
ctx := context.Background()
// [START pubsub_delete_topic]
t := client.Topic(topic)
if err := t.Delete(ctx); err != nil {
return err
}
fmt.Printf("Deleted topic: %v\n", t)
// [END pubsub_delete_topic]
return nil
}
func publish(client *pubsub.Client, topic, msg string) error {
ctx := context.Background()
// [START pubsub_publish]
// [START pubsub_quickstart_publisher]
t := client.Topic(topic)
result := t.Publish(ctx, &pubsub.Message{
Data: []byte(msg),
})
// Block until the result is returned and a server-generated
// ID is returned for the published message.
id, err := result.Get(ctx)
if err != nil {
return err
}
fmt.Printf("Published a message; msg ID: %v\n", id)
// [END pubsub_publish]
// [END pubsub_quickstart_publisher]
return nil
}
func publishThatScales(client *pubsub.Client, topic string, n int) error {
ctx := context.Background()
// [START pubsub_publish_with_error_handling_that_scales]
var wg sync.WaitGroup
var totalErrors uint64
t := client.Topic(topic)
for i := 0; i < n; i++ {
result := t.Publish(ctx, &pubsub.Message{
Data: []byte("Message " + strconv.Itoa(i)),
})
wg.Add(1)
go func(i int, res *pubsub.PublishResult) {
defer wg.Done()
// The Get method blocks until a server-generated ID or
// an error is returned for the published message.
id, err := res.Get(ctx)
if err != nil {
// Error handling code can be added here.
log.Output(1, fmt.Sprintf("Failed to publish: %v", err))
atomic.AddUint64(&totalErrors, 1)
return
}
fmt.Printf("Published message %d; msg ID: %v\n", i, id)
}(i, result)
}
wg.Wait()
if totalErrors > 0 {
return errors.New(
fmt.Sprintf("%d of %d messages did not publish successfully",
totalErrors, n))
}
return nil
// [END pubsub_publish_with_error_handling_that_scales]
}
func publishCustomAttributes(client *pubsub.Client, topic string) error {
ctx := context.Background()
// [START pubsub_publish_custom_attributes]
t := client.Topic(topic)
result := t.Publish(ctx, &pubsub.Message{
Data: []byte("Hello world!"),
Attributes: map[string]string{
"origin": "golang",
"username": "gcp",
},
})
// Block until the result is returned and a server-generated
// ID is returned for the published message.
id, err := result.Get(ctx)
if err != nil {
return err
}
fmt.Printf("Published message with custom attributes; msg ID: %v\n", id)
// [END pubsub_publish_custom_attributes]
return nil
}
func publishWithSettings(client *pubsub.Client, topic string, msg []byte) error {
ctx := context.Background()
// [START pubsub_publisher_batch_settings]
t := client.Topic(topic)
t.PublishSettings = pubsub.PublishSettings{
ByteThreshold: 5000,
CountThreshold: 10,
DelayThreshold: 100 * time.Millisecond,
}
result := t.Publish(ctx, &pubsub.Message{Data: msg})
// Block until the result is returned and a server-generated
// ID is returned for the published message.
id, err := result.Get(ctx)
if err != nil {
return err
}
fmt.Printf("Published a message; msg ID: %v\n", id)
// [END pubsub_publisher_batch_settings]
return nil
}
func publishSingleGoroutine(client *pubsub.Client, topic string, msg []byte) error {
ctx := context.Background()
// [START pubsub_publisher_concurrency_control]
t := client.Topic(topic)
t.PublishSettings = pubsub.PublishSettings{
NumGoroutines: 1,
}
result := t.Publish(ctx, &pubsub.Message{Data: msg})
// Block until the result is returned and a server-generated
// ID is returned for the published message.
id, err := result.Get(ctx)
if err != nil {
return err
}
fmt.Printf("Published a message; msg ID: %v\n", id)
// [END pubsub_publisher_concurrency_control]
return nil
}
func getPolicy(c *pubsub.Client, topicName string) (*iam.Policy, error) {
ctx := context.Background()
// [START pubsub_get_topic_policy]
policy, err := c.Topic(topicName).IAM().Policy(ctx)
if err != nil {
return nil, err
}
for _, role := range policy.Roles() {
log.Print(policy.Members(role))
}
// [END pubsub_get_topic_policy]
return policy, nil
}
func addUsers(c *pubsub.Client, topicName string) error {
ctx := context.Background()
// [START pubsub_set_topic_policy]
topic := c.Topic(topicName)
policy, err := topic.IAM().Policy(ctx)
if err != nil {
return err
}
// Other valid prefixes are "serviceAccount:", "user:"
// See the documentation for more values.
policy.Add(iam.AllUsers, iam.Viewer)
policy.Add("group:cloud-logs@google.com", iam.Editor)
if err := topic.IAM().SetPolicy(ctx, policy); err != nil {
log.Fatalf("SetPolicy: %v", err)
}
// NOTE: It may be necessary to retry this operation if IAM policies are
// being modified concurrently. SetPolicy will return an error if the policy
// was modified since it was retrieved.
// [END pubsub_set_topic_policy]
return nil
}
func testPermissions(c *pubsub.Client, topicName string) ([]string, error) {
ctx := context.Background()
// [START pubsub_test_topic_permissions]
topic := c.Topic(topicName)
perms, err := topic.IAM().TestPermissions(ctx, []string{
"pubsub.topics.publish",
"pubsub.topics.update",
})
if err != nil {
return nil, err
}
for _, perm := range perms {
log.Printf("Allowed: %v", perm)
}
// [END pubsub_test_topic_permissions]
return perms, nil
}