forked from vijaynalawade/flogo-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
activity.go
99 lines (85 loc) · 3.05 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
package awssns
import (
"fmt"
"github.com/TIBCOSoftware/flogo-lib/core/activity"
"github.com/TIBCOSoftware/flogo-lib/logger"
"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/sns"
)
// List of input and output variables names
const (
ConfAWSAccessKeyID = "accessKey"
ConfAWSSecretAccessKey = "secretKey"
ConfAWSDefaultRegion = "region"
ConfSMSType = "smsType"
ConfSMSFrom = "from"
ConfSMSTo = "to"
ConfSMSMessage = "message"
OUTMessageID = "messageId"
)
// log is the default package logger
var log = logger.GetLogger("activity-tibco-awssns")
// AWSSNS Structure for the AWSNS activity
type AWSSNS struct {
metadata *activity.Metadata
}
// NewActivity creates a new activity
func NewActivity(metadata *activity.Metadata) activity.Activity {
return &AWSSNS{metadata: metadata}
}
// Metadata implements activity.Activity.Metadata
func (a *AWSSNS) Metadata() *activity.Metadata {
return a.metadata
}
// Eval implements activity.Activity.Eval
func (a *AWSSNS) Eval(context activity.Context) (done bool, err error) {
if context.GetInput(ConfAWSAccessKeyID) == nil || context.GetInput(ConfAWSSecretAccessKey) == nil || context.GetInput(ConfAWSDefaultRegion) == nil || context.GetInput(ConfSMSFrom) == nil || context.GetInput(ConfSMSTo) == nil || context.GetInput(ConfSMSMessage) == nil {
log.Error("Required variables have not been set !")
return false, fmt.Errorf("required variables have not been set")
}
AWSAccessKeyID := context.GetInput(ConfAWSAccessKeyID).(string)
AWSSecretAccessKey := context.GetInput(ConfAWSSecretAccessKey).(string)
AWSDefaultRegion := context.GetInput(ConfAWSDefaultRegion).(string)
SMSFrom := context.GetInput(ConfSMSFrom).(string)
SMSTo := context.GetInput(ConfSMSTo).(string)
SMSMessage := context.GetInput(ConfSMSMessage).(string)
SMSType := context.GetInput(ConfSMSType).(string)
log.Debug("Setting credentials")
var snsCreds = credentials.NewStaticCredentials(AWSAccessKeyID, AWSSecretAccessKey, "")
log.Debug("Creating session")
sess := session.Must(session.NewSession(&aws.Config{
Credentials: snsCreds,
Region: aws.String(AWSDefaultRegion),
}))
log.Debug("Session created")
log.Debug("Creating service")
svc := sns.New(sess)
log.Debug("Service created")
log.Debug("Setting SMS parameters")
params := &sns.PublishInput{
Message: aws.String(SMSMessage),
PhoneNumber: aws.String(SMSTo),
MessageAttributes: map[string]*sns.MessageAttributeValue{
"AWS.SNS.SMS.SenderID": {
DataType: aws.String("String"),
StringValue: aws.String(SMSFrom),
},
"AWS.SNS.SMS.SMSType": {
DataType: aws.String("String"),
StringValue: aws.String(SMSType),
},
},
}
log.Debug("SMS parameters set.")
log.Debug("Publishing SMS")
resp, err := svc.Publish(params)
if err != nil {
log.Errorf(err.Error())
return false, err
}
context.SetOutput(OUTMessageID, *resp.MessageId)
log.Infof("Message sent [%s]", *resp.MessageId)
return true, nil
}