github.com/SaurabhDubey-Groww/go-cloud@v0.0.0-20221124105541-b26c29285fd8/pubsub/awssnssqs/example_test.go (about)

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