/
flowpipe_pipeline_step_function.go
223 lines (190 loc) · 6.05 KB
/
flowpipe_pipeline_step_function.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
package modconfig
import (
"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/gohcl"
"github.com/turbot/go-kit/helpers"
"github.com/turbot/pipe-fittings/error_helpers"
"github.com/turbot/pipe-fittings/hclhelpers"
"github.com/turbot/pipe-fittings/perr"
"github.com/turbot/pipe-fittings/schema"
"github.com/zclconf/go-cty/cty"
)
type PipelineStepFunction struct {
PipelineStepBase
Runtime string `json:"runtime" cty:"runtime"`
Source string `json:"source" cty:"source"`
Handler string `json:"handler" cty:"handler"`
Event map[string]interface{} `json:"event"`
Env map[string]string `json:"env"`
}
func (p *PipelineStepFunction) Equals(iOther PipelineStep) bool {
// If both pointers are nil, they are considered equal
if p == nil && helpers.IsNil(iOther) {
return true
}
if p == nil && !helpers.IsNil(iOther) || p != nil && helpers.IsNil(iOther) {
return false
}
other, ok := iOther.(*PipelineStepFunction)
if !ok {
return false
}
if !p.PipelineStepBase.Equals(&other.PipelineStepBase) {
return false
}
return p.Name == other.Name &&
p.Runtime == other.Runtime &&
p.Handler == other.Handler &&
p.Source == other.Source
}
func (p *PipelineStepFunction) GetInputs(evalContext *hcl.EvalContext) (map[string]interface{}, error) {
results, err := p.GetBaseInputs(evalContext)
if err != nil {
return nil, err
}
var env map[string]string
if p.UnresolvedAttributes[schema.AttributeTypeEnv] == nil {
env = p.Env
} else {
var args cty.Value
diags := gohcl.DecodeExpression(p.UnresolvedAttributes[schema.AttributeTypeEnv], evalContext, &args)
if diags.HasErrors() {
return nil, error_helpers.BetterHclDiagsToError(p.Name, diags)
}
var err error
env, err = hclhelpers.CtyToGoMapString(args)
if err != nil {
return nil, perr.BadRequestWithMessage(p.Name + ": unable to parse env attribute to map[string]string: " + err.Error())
}
}
var event map[string]interface{}
if p.UnresolvedAttributes[schema.AttributeTypeEvent] == nil {
event = p.Event
} else {
val, diags := p.UnresolvedAttributes[schema.AttributeTypeEvent].Value(evalContext)
if diags.HasErrors() {
return nil, error_helpers.BetterHclDiagsToError(p.Name, diags)
}
var err error
event, err = hclhelpers.CtyToGoMapInterface(val)
if err != nil {
return nil, perr.BadRequestWithMessage(p.Name + ": unable to parse event attribute to map[string]interface{}: " + err.Error())
}
}
var src string
if p.UnresolvedAttributes[schema.AttributeTypeSource] == nil {
src = p.Source
} else {
diags := gohcl.DecodeExpression(p.UnresolvedAttributes[schema.AttributeTypeSource], evalContext, &src)
if diags.HasErrors() {
return nil, error_helpers.BetterHclDiagsToError(p.Name, diags)
}
}
var runtime string
if p.UnresolvedAttributes[schema.AttributeTypeRuntime] == nil {
runtime = p.Runtime
} else {
diags := gohcl.DecodeExpression(p.UnresolvedAttributes[schema.AttributeTypeRuntime], evalContext, &runtime)
if diags.HasErrors() {
return nil, error_helpers.BetterHclDiagsToError(p.Name, diags)
}
}
var handler string
if p.UnresolvedAttributes[schema.AttributeTypeHandler] == nil {
handler = p.Handler
} else {
diags := gohcl.DecodeExpression(p.UnresolvedAttributes[schema.AttributeTypeHandler], evalContext, &handler)
if diags.HasErrors() {
return nil, error_helpers.BetterHclDiagsToError(p.Name, diags)
}
}
results[schema.LabelName] = p.PipelineName + "." + p.GetFullyQualifiedName()
results[schema.AttributeTypeSource] = src
results[schema.AttributeTypeRuntime] = runtime
results[schema.AttributeTypeHandler] = handler
results[schema.AttributeTypeEvent] = event
results[schema.AttributeTypeEnv] = env
return results, nil
}
func (p *PipelineStepFunction) SetAttributes(hclAttributes hcl.Attributes, evalContext *hcl.EvalContext) hcl.Diagnostics {
diags := p.SetBaseAttributes(hclAttributes, evalContext)
for name, attr := range hclAttributes {
switch name {
case schema.AttributeTypeSource:
val, stepDiags := dependsOnFromExpressions(attr, evalContext, p)
if stepDiags.HasErrors() {
diags = append(diags, stepDiags...)
continue
}
if val != cty.NilVal {
p.Source = val.AsString()
}
case schema.AttributeTypeHandler:
val, stepDiags := dependsOnFromExpressions(attr, evalContext, p)
if stepDiags.HasErrors() {
diags = append(diags, stepDiags...)
continue
}
if val != cty.NilVal {
p.Handler = val.AsString()
}
case schema.AttributeTypeRuntime:
val, stepDiags := dependsOnFromExpressions(attr, evalContext, p)
if stepDiags.HasErrors() {
diags = append(diags, stepDiags...)
}
if val != cty.NilVal {
p.Runtime = val.AsString()
}
case schema.AttributeTypeEnv:
val, stepDiags := dependsOnFromExpressions(attr, evalContext, p)
if stepDiags.HasErrors() {
diags = append(diags, stepDiags...)
continue
}
if val != cty.NilVal {
env, moreErr := hclhelpers.CtyToGoMapString(val)
if moreErr != nil {
diags = append(diags, &hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: "Unable to parse '" + schema.AttributeTypeEnv + "' attribute to string map",
Subject: &attr.Range,
})
continue
}
p.Env = env
}
case schema.AttributeTypeEvent:
val, stepDiags := dependsOnFromExpressions(attr, evalContext, p)
if stepDiags.HasErrors() {
diags = append(diags, stepDiags...)
}
if val != cty.NilVal {
events, err := hclhelpers.CtyToGoMapInterface(val)
if err != nil {
diags = append(diags, &hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: "Unable to parse '" + schema.AttributeTypeEvent + "' attribute to string map",
Subject: &attr.Range,
})
continue
}
p.Event = events
}
default:
if !p.IsBaseAttribute(name) {
diags = append(diags, &hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: "Unsupported attribute for Function Step: " + attr.Name,
Subject: &attr.Range,
})
}
}
}
return diags
}
func (p *PipelineStepFunction) Validate() hcl.Diagnostics {
// validate the base attributes
diags := p.ValidateBaseAttributes()
return diags
}