forked from retgits/flogo-components
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathactivity.go
115 lines (94 loc) · 3.58 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// Package dynamodbinsert inserts an object into Amazon DynamoDB
package dynamodbinsert
import (
"encoding/json"
"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/dynamodb"
"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"
ivDynamoDBTableName = "DynamoDBTableName"
ivDynamoDBRecord = "DynamoDBRecord"
ovResult = "result"
)
// log is the default package logger
var log = logger.GetLogger("activity-dynamodbinsert")
// 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
}
// RecordAttribute is a structure representing the JSON payload for the record syntax
type RecordAttribute 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)
dynamoDBTableName := context.GetInput(ivDynamoDBTableName).(string)
dynamoDBRecord := context.GetInput(ivDynamoDBRecord)
// 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 DynamoDB service
dynamoService := dynamodb.New(awsSession)
// Construct the expression attributes from the JSON payload
var recordAttributes []RecordAttribute
json.Unmarshal([]byte(dynamoDBRecord.(string)), &recordAttributes)
recordAttributeMap := make(map[string]*dynamodb.AttributeValue)
for _, attribute := range recordAttributes {
recordAttributeMap[attribute.Name] = &dynamodb.AttributeValue{S: aws.String(attribute.Value)}
}
// Construct the DynamoDB Input
input := &dynamodb.PutItemInput{
TableName: aws.String(dynamoDBTableName),
Item: recordAttributeMap,
}
// Put the item in DynamoDB
_, err1 := dynamoService.PutItem(input)
if err1 != nil {
log.Errorf("Error while executing query [%s]", err1)
context.SetOutput(ovResult, "ERROR")
} else {
context.SetOutput(ovResult, "Added record to DynamoDB")
}
// Complete the activity
return true, nil
}