-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathactivity.go
101 lines (84 loc) · 2.98 KB
/
activity.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
// Package amazonsqssend sends a message using Amazon SQS
package amazonsqssend
import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/sqs"
"github.com/TIBCOSoftware/flogo-lib/core/activity"
"github.com/TIBCOSoftware/flogo-lib/logger"
)
// Constants used by the code to represent the input and outputs of the JSON structure
const (
ivAwsAccessKeyID = "awsAccessKeyID"
ivAwsSecretAccessKey = "awsSecretAccessKey"
ivAwsRegion = "awsRegion"
ivQueueURL = "queueUrl"
ivMessageBody = "messageBody"
ovResult = "result"
)
// log is the default package logger
var log = logger.GetLogger("activity-amazonsqssend")
// MyActivity is a stub for your Activity implementation
type MyActivity struct {
metadata *activity.Metadata
}
// NewActivity creates a new activity
func NewActivity(metadata *activity.Metadata) activity.Activity {
return &MyActivity{metadata: metadata}
}
// Metadata implements activity.Activity.Metadata
func (a *MyActivity) Metadata() *activity.Metadata {
return a.metadata
}
// ExpressionAttribute is a structure representing the JSON payload for the expression syntax
type ExpressionAttribute struct {
Name string
Value string
}
// Eval implements activity.Activity.Eval
func (a *MyActivity) Eval(context activity.Context) (done bool, err error) {
// Get the inputs
awsRegion := context.GetInput(ivAwsRegion).(string)
queueURL := context.GetInput(ivQueueURL).(string)
messageBody := context.GetInput(ivMessageBody).(string)
// AWS Credentials, only if needed
var awsAccessKeyID, awsSecretAccessKey = "", ""
if context.GetInput(ivAwsAccessKeyID) != nil {
awsAccessKeyID = context.GetInput(ivAwsAccessKeyID).(string)
}
if context.GetInput(ivAwsSecretAccessKey) != nil {
awsSecretAccessKey = context.GetInput(ivAwsSecretAccessKey).(string)
}
// Create a session with Credentials only if they are set
var awsSession *session.Session
if awsAccessKeyID != "" && awsSecretAccessKey != "" {
// Create new credentials using the accessKey and secretKey
awsCredentials := credentials.NewStaticCredentials(awsAccessKeyID, awsSecretAccessKey, "")
// Create a new session with AWS credentials
awsSession = session.Must(session.NewSession(&aws.Config{
Credentials: awsCredentials,
Region: aws.String(awsRegion),
}))
} else {
// Create a new session without AWS credentials
awsSession = session.Must(session.NewSession(&aws.Config{
Region: aws.String(awsRegion),
}))
}
// Create a new login to the SQS service
sqsService := sqs.New(awsSession)
// Create a new SQS message
sendMessageInput := &sqs.SendMessageInput{
QueueUrl: aws.String(queueURL),
MessageBody: aws.String(messageBody),
}
//Send message to SQS
response, err := sqsService.SendMessage(sendMessageInput)
if err != nil {
return true, err
}
//Set Message ID in the output
context.SetOutput(ovResult, *response.MessageId)
return true, nil
}