Debian Codegraph @

pubsub/awssnssqs/example_test.go

raw
1 // Copyright 2019 The Go Cloud Development Kit Authors
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 // limitations under the License.
14
15 package awssnssqs_test
16
17 import (
18 "context"
19 "log"
20
21 "github.com/aws/aws-sdk-go-v2/config"
22 "github.com/aws/aws-sdk-go-v2/service/sns"
23 "github.com/aws/aws-sdk-go-v2/service/sqs"
24 "gocloud.dev/pubsub"
25 "gocloud.dev/pubsub/awssnssqs"
26 )
27
28 func ExampleOpenSNSTopic() {
29 // PRAGMA: This example is used on gocloud.dev; PRAGMA comments adjust how it is shown and can be ignored.
30 // PRAGMA: On gocloud.dev, hide lines until the next blank line.
31 ctx := context.Background()
32
33 // Establish a AWS V2 Config.
34 // See https://aws.github.io/aws-sdk-go-v2/docs/configuring-sdk/ for more info.
35 cfg, err := config.LoadDefaultConfig(ctx)
36 if err != nil {
37 log.Fatal(err)
38 }
39
40 // Create a *pubsub.Topic.
41 const topicARN = "arn:aws:sns:us-east-2:123456789012:mytopic"
42 client := sns.NewFromConfig(cfg)
43 topic := awssnssqs.OpenSNSTopic(ctx, client, topicARN, nil)
44 defer topic.Shutdown(ctx)
45 }
46
47 func ExampleOpenSQSTopic() {
48 // PRAGMA: This example is used on gocloud.dev; PRAGMA comments adjust how it is shown and can be ignored.
49 // PRAGMA: On gocloud.dev, hide lines until the next blank line.
50 ctx := context.Background()
51
52 // Establish a AWS V2 Config.
53 // See https://aws.github.io/aws-sdk-go-v2/docs/configuring-sdk/ for more info.
54 cfg, err := config.LoadDefaultConfig(ctx)
55 if err != nil {
56 log.Fatal(err)
57 }
58
59 // Create a *pubsub.Topic.
60 const queueURL = "https://sqs.us-east-2.amazonaws.com/123456789012/myqueue"
61 client := sqs.NewFromConfig(cfg)
62 topic := awssnssqs.OpenSQSTopic(ctx, client, queueURL, nil)
63 defer topic.Shutdown(ctx)
64 }
65
66 func Example_openSNSTopicFromURL() {
67 // PRAGMA: This example is used on gocloud.dev; PRAGMA comments adjust how it is shown and can be ignored.
68 // PRAGMA: On gocloud.dev, add a blank import: _ "gocloud.dev/pubsub/awssnssqs"
69 // PRAGMA: On gocloud.dev, hide lines until the next blank line.
70 ctx := context.Background()
71
72 const topicARN = "arn:aws:sns:us-east-2:123456789012:mytopic"
73 // Note the 3 slashes; ARNs have multiple colons and therefore aren't valid
74 // as hostnames in the URL.
75 topic, err := pubsub.OpenTopic(ctx, "awssns:///"+topicARN+"?region=us-east-2")
76 if err != nil {
77 log.Fatal(err)
78 }
79 defer topic.Shutdown(ctx)
80 }
81
82 func Example_openSQSTopicFromURL() {
83 // PRAGMA: This example is used on gocloud.dev; PRAGMA comments adjust how it is shown and can be ignored.
84 // PRAGMA: On gocloud.dev, add a blank import: _ "gocloud.dev/pubsub/awssnssqs"
85 // PRAGMA: On gocloud.dev, hide lines until the next blank line.
86 ctx := context.Background()
87
88 // https://docs.aws.amazon.com/sdk-for-net/v2/developer-guide/QueueURL.html
89 const queueURL = "sqs.us-east-2.amazonaws.com/123456789012/myqueue"
90 topic, err := pubsub.OpenTopic(ctx, "awssqs://"+queueURL+"?region=us-east-2")
91 if err != nil {
92 log.Fatal(err)
93 }
94 defer topic.Shutdown(ctx)
95 }
96
97 func ExampleOpenSubscription() {
98 // PRAGMA: This example is used on gocloud.dev; PRAGMA comments adjust how it is shown and can be ignored.
99 // PRAGMA: On gocloud.dev, hide lines until the next blank line.
100 ctx := context.Background()
101
102 // Establish a AWS V2 Config.
103 // See https://aws.github.io/aws-sdk-go-v2/docs/configuring-sdk/ for more info.
104 cfg, err := config.LoadDefaultConfig(ctx)
105 if err != nil {
106 log.Fatal(err)
107 }
108
109 // Construct a *pubsub.Subscription.
110 // https://docs.aws.amazon.com/sdk-for-net/v2/developer-guide/QueueURL.html
111 const queueURL = "https://sqs.us-east-2.amazonaws.com/123456789012/MyQueue"
112 client := sqs.NewFromConfig(cfg)
113 subscription := awssnssqs.OpenSubscription(ctx, client, queueURL, nil)
114 defer subscription.Shutdown(ctx)
115 }
116
117 func Example_openSubscriptionFromURL() {
118 // PRAGMA: This example is used on gocloud.dev; PRAGMA comments adjust how it is shown and can be ignored.
119 // PRAGMA: On gocloud.dev, add a blank import: _ "gocloud.dev/pubsub/awssnssqs"
120 // PRAGMA: On gocloud.dev, hide lines until the next blank line.
121 ctx := context.Background()
122
123 // pubsub.OpenSubscription creates a *pubsub.Subscription from a URL.
124 // This URL will open the subscription with the URL
125 // "https://sqs.us-east-2.amazonaws.com/123456789012/myqueue".
126 subscription, err := pubsub.OpenSubscription(ctx,
127 "awssqs://sqs.us-east-2.amazonaws.com/123456789012/"+
128 "myqueue?region=us-east-2")
129 if err != nil {
130 log.Fatal(err)
131 }
132 defer subscription.Shutdown(ctx)
133 }