/
seederstrategies.go
192 lines (169 loc) · 5.84 KB
/
seederstrategies.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package seeder
import (
"context"
"errors"
)
// GetPost strategy calls a GET endpoint and if item ***FOUND it does NOT do a POST***
// this strategy should be used sparingly and only in cases where the service REST implementation
// does not support an update of existing item.
func (r *SeederImpl) GetPost(ctx context.Context, action *Action) error {
action.templatedPayload = r.TemplatePayload(action.PayloadTemplate, action.Variables)
resp, err := r.get(ctx, action)
if err != nil {
return err
}
if string(resp) == "" {
r.log.Infof("get endpoint returned no response, item can be created")
return r.post(ctx, action)
}
r.log.Infof("get endpoint returned a response, item cannot be updated continuing")
return nil
}
// FindPost strategy calls a GET endpoint and if item ***FOUND it does NOT do a POST***
// this strategy should be used sparingly and only in cases where the service REST implementation
// does not support an update of existing item.
func (r *SeederImpl) FindPost(ctx context.Context, action *Action) error {
action.templatedPayload = r.TemplatePayload(action.PayloadTemplate, action.Variables)
resp, err := r.get(ctx, action)
if err != nil {
return err
}
found, err := r.FindPathByExpression(resp, action.FindByJsonPathExpr)
if err != nil {
return err
}
if found == "" {
return r.post(ctx, action)
}
r.log.Infof("item: %s,found by expression: %s and cannot be updated continuing", found, action.FindByJsonPathExpr)
return nil
}
// FindPutPost strategy gets an item either by specifying a known ID in the endpoint suffix
// or by pathExpression. Get can look for a response in an array or in a single response object.
// once a single item that matches is found and the relevant ID is extracted it will do a PUT
// else it will do a POST as the item can be created
func (r *SeederImpl) FindPutPost(ctx context.Context, action *Action) error {
action.templatedPayload = r.TemplatePayload(action.PayloadTemplate, action.Variables)
resp, err := r.get(ctx, action)
if err != nil {
return err
}
found, err := r.FindPathByExpression(resp, action.FindByJsonPathExpr)
if err != nil {
return err
}
if found == "" {
r.log.Info("item not found by expression running POST")
return r.post(ctx, action)
}
r.log.Infof("item: %s,found by expression: %s\nupdating in place", found, action.FindByJsonPathExpr)
action.foundId = found
return r.put(ctx, action)
}
// FindPatchPost is same as FindPutPost strategy but uses PATCH
func (r *SeederImpl) FindPatchPost(ctx context.Context, action *Action) error {
action.templatedPayload = r.TemplatePayload(action.PayloadTemplate, action.Variables)
resp, err := r.get(ctx, action)
if err != nil {
return err
}
found, err := r.FindPathByExpression(resp, action.FindByJsonPathExpr)
if err != nil {
return err
}
if found == "" {
r.log.Info("item not found by expression running POST")
return r.post(ctx, action)
}
r.log.Infof("item: %s,found by expression: %s\nupdating in place", found, action.FindByJsonPathExpr)
action.foundId = found
action.templatedPayload = r.TemplatePayload(action.PatchPayloadTemplate, action.Variables)
return r.patch(ctx, action)
}
// FindDeletePost
func (r *SeederImpl) FindDeletePost(ctx context.Context, action *Action) error {
action.templatedPayload = r.TemplatePayload(action.PayloadTemplate, action.Variables)
resp, err := r.get(ctx, action)
if err != nil {
var diag *Diagnostic
if errors.As(err, &diag) {
if !diag.ProceedFallback {
return err
}
// proceeding with request
} else {
r.log.Debug("")
return err
}
}
found, err := r.FindPathByExpression(resp, action.FindByJsonPathExpr)
if err != nil {
return err
}
if found != "" {
action.foundId = found
r.log.Info("item found by expression running DELETE")
if err := r.delete(ctx, action); err != nil {
return err
}
return r.post(ctx, action)
}
r.log.Infof("item not found by expression: %s, creating...", found, action.FindByJsonPathExpr)
return r.post(ctx, action)
}
// GetPutPost strategy gets an item by specifying a known ID in the endpoint suffix
// If a non error or non empty response is found it will do a PUT
// else it will do a POST as the item can be created
func (r *SeederImpl) GetPutPost(ctx context.Context, action *Action) error {
action.templatedPayload = r.TemplatePayload(action.PayloadTemplate, action.Variables)
resp, err := r.get(ctx, action)
if err != nil {
var diag *Diagnostic
if errors.As(err, &diag) {
if !diag.ProceedFallback {
return err
}
// proceeding with request
} else {
r.log.Debug("")
return err
}
}
// // if FindByJsonPathExpr is provided we will try to extract it from response
// // otherwise assume a known was supplied to the put endpoint
// if action.FindByJsonPathExpr != "" && string(resp) == "" {
// found, err := r.FindPathByExpression(resp, action.FindByJsonPathExpr)
// if err != nil {
// return err
// }
// action.foundId = found
// }
if string(resp) == "" {
r.log.Info("item not found. posting")
return r.post(ctx, action)
}
r.log.Infof("found item: %v", string(resp))
return r.put(ctx, action)
}
// Put strategy calls a PUT endpoint
// if standards compliant this should be an idempotent operation
func (r *SeederImpl) Put(ctx context.Context, action *Action) error {
action.templatedPayload = r.TemplatePayload(action.PayloadTemplate, action.Variables)
return r.put(ctx, action)
}
// Put strategy calls a PUT endpoint
// if standards compliant this should be an idempotent operation
func (r *SeederImpl) PutPost(ctx context.Context, action *Action) error {
action.templatedPayload = r.TemplatePayload(action.PayloadTemplate, action.Variables)
if err := r.put(ctx, action); err != nil {
var diag *Diagnostic
if errors.As(err, &diag) {
if diag.IsFatal || !diag.ProceedFallback {
return err
}
r.log.Debug("falling back on POST")
return r.post(ctx, action)
}
}
return nil
}