-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathactivity.go
174 lines (138 loc) · 3.99 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package mongodb
import (
"context"
"strings"
"time"
"github.com/project-flogo/core/activity"
"github.com/project-flogo/core/data/metadata"
"github.com/project-flogo/core/support/log"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
const (
methodUnknown int8 = iota
methodGet
methodInsert
methodDelete
methodUpdate
)
func init() {
_ = activity.Register(&Activity{}, New)
}
func New(ctx activity.InitContext) (activity.Activity, error) {
s := &Settings{}
err := metadata.MapToStruct(ctx.Settings(), s, true)
if err != nil {
return nil, err
}
mCtx, _ := context.WithTimeout(context.Background(), 10*time.Second)
opts := options.Client()
if s.Username != "" && s.Password != "" {
opts = opts.SetAuth(options.Credential{
Username: s.Username,
Password: s.Password,
})
}
client, err := mongo.Connect(mCtx, opts.ApplyURI(s.URI))
if err != nil {
return nil, err
}
db := client.Database(s.DbName)
collection := db.Collection(s.Collection)
act := &Activity{client: client, collection: collection}
switch strings.ToLower(s.Method) {
case "get":
act.method = methodGet
case "insert":
act.method = methodInsert
case "delete":
act.method = methodDelete
case "update":
act.method = methodUpdate
}
return act, nil
}
var activityMd = activity.ToMetadata(&Settings{}, &Input{}, &Output{})
type Activity struct {
client *mongo.Client
collection *mongo.Collection
method int8
}
// Metadata returns the activity's metadata
func (a *Activity) Metadata() *activity.Metadata {
return activityMd
}
func (a *Activity) Cleanup() error {
log.RootLogger().Tracef("cleaning up MongoDB activity")
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
return a.client.Disconnect(ctx)
}
// Eval implements api.Activity.Eval - Logs the Message
func (a *Activity) Eval(ctx activity.Context) (done bool, err error) {
logger := ctx.Logger()
input := &Input{}
err = ctx.GetInputObject(input)
if err != nil {
return true, nil
}
output := &Output{}
//todo consider making timeout a setting
bCtx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
switch a.method {
case methodGet:
result := a.collection.FindOne(bCtx, bson.M{input.KeyName: input.KeyValue})
err = result.Decode(&output.Data)
if err != nil {
return true, err
}
logger.Tracef("Get Result: %v", output.Data)
case methodInsert:
if input.Data == nil && input.KeyValue == "" {
// should we throw an error or warn?
ctx.Logger().Warnf("Nothing to insert")
return true, nil
}
var result *mongo.InsertOneResult
if input.Data != nil {
result, err = a.collection.InsertOne(bCtx, bson.D{input.Data.(bson.E)})
if err != nil {
logger.Debugf("Error performing Insert: %v", err)
return true, err
}
} else {
result, err = a.collection.InsertOne(bCtx, bson.M{input.KeyName: input.KeyValue})
if err != nil {
logger.Debugf("Error performing Insert: %v", err)
return true, err
}
}
logger.Tracef("Inserted ID: %v", result.InsertedID)
output.Data = result.InsertedID
case methodDelete:
result, err := a.collection.DeleteOne(bCtx, bson.M{input.KeyName: input.KeyValue}, nil)
if err != nil {
logger.Debugf("Error performing Delete: %v", err)
return true, err
}
logger.Tracef("Delete Count: %d", result.DeletedCount)
output.Data = result.DeletedCount
case methodUpdate:
result, err := a.collection.UpdateOne(bCtx, bson.M{input.KeyName: input.KeyValue}, bson.M{"$set": input.Data})
if err != nil {
logger.Debugf("Error performing Update: %v", err)
return true, err
}
resultObj := map[string]interface{}{"MatchedCount": result.MatchedCount, "ModifiedCount": result.ModifiedCount,
"UpsertedCount": result.UpsertedCount, "UpsertedID": result.UpsertedID}
logger.Tracef("Update Result: %v", resultObj)
output.Data = resultObj
}
err = ctx.SetOutputObject(output)
if err != nil {
return false, err
}
return true, nil
}