forked from awsdocs/aws-doc-sdk-examples
/
MakeTopic.go
64 lines (54 loc) · 1.79 KB
/
MakeTopic.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
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: MIT-0
// snippet-start:[sns.go.make_topic]
package main
// snippet-start:[sns.go.make_topic.imports]
import (
"flag"
"fmt"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/sns"
"github.com/aws/aws-sdk-go/service/sns/snsiface"
)
// snippet-end:[sns.go.make_topic.imports]
// MakeTopic creates an Amazon SNS topic
// Inputs:
// svc is an Amazon SNS service client
// topic is the name of the topic
// Output:
// If success, information about the topics and nil
// Otherwise, nil and an error from the call to CreateTopic
func MakeTopic(svc snsiface.SNSAPI, topic *string) (*sns.CreateTopicOutput, error) {
// snippet-start:[sns.go.make_topic.call]
results, err := svc.CreateTopic(&sns.CreateTopicInput{
Name: topic,
})
// snippet-end:[sns.go.make_topic.call]
return results, err
}
func main() {
// snippet-start:[sns.go.make_topic.args]
topic := flag.String("t", "", "The name of the topic")
flag.Parse()
if *topic == "" {
fmt.Println("You must supply the name of the topic")
fmt.Println("-t TOPIC")
return
}
// snippet-end:[sns.go.make_topic.args]
// snippet-start:[sns.go.make_topic.session]
sess := session.Must(session.NewSessionWithOptions(session.Options{
SharedConfigState: session.SharedConfigEnable,
}))
svc := sns.New(sess)
// snippet-end:[sns.go.make_topic.session]
results, err := MakeTopic(svc, topic)
if err != nil {
fmt.Println(err.Error())
return
}
// snippet-start:[sns.go.make_topic.display]
fmt.Println(*results.TopicArn)
// snippet-end:[sns.go.make_topic.display]
}
// snippet-end:[sns.go.make_topic]