forked from xinyunh0929/golang-samples
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
243 lines (216 loc) · 6.17 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
// Copyright 2016 Google Inc. All rights reserved.
// Use of this source code is governed by the Apache 2.0
// license that can be found in the LICENSE file.
// 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 (
"fmt"
"log"
"os"
"time"
"golang.org/x/net/context"
"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 = "example-topic"
// Create a new topic called example-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)
}
// 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 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:[email protected]", 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
}