/
flowpipe_pipeline_step_error_config.go
97 lines (77 loc) · 2.31 KB
/
flowpipe_pipeline_step_error_config.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
package modconfig
import (
"fmt"
"github.com/hashicorp/hcl/v2"
"github.com/turbot/pipe-fittings/hclhelpers"
"github.com/turbot/pipe-fittings/schema"
"github.com/turbot/pipe-fittings/utils"
)
type ErrorConfig struct {
// circular link to its "parent"
PipelineStepBase *PipelineStepBase `json:"-"`
UnresolvedAttributes map[string]hcl.Expression `json:"-"`
If *bool `json:"if"`
Ignore *bool `json:"ignore,omitempty" hcl:"ignore,optional" cty:"ignore"`
}
func NewErrorConfig(p *PipelineStepBase) *ErrorConfig {
return &ErrorConfig{
PipelineStepBase: p,
UnresolvedAttributes: make(map[string]hcl.Expression),
}
}
func (e *ErrorConfig) Equals(other *ErrorConfig) bool {
if e == nil || other == nil {
return false
}
if e == nil && other != nil || e != nil && other == nil {
return false
}
for key, expr := range e.UnresolvedAttributes {
otherExpr, ok := other.UnresolvedAttributes[key]
if !ok || !hclhelpers.ExpressionsEqual(expr, otherExpr) {
return false
}
}
// reverse
for key := range other.UnresolvedAttributes {
if _, ok := e.UnresolvedAttributes[key]; !ok {
return false
}
}
return utils.BoolPtrEqual(e.Ignore, other.Ignore)
}
func (e *ErrorConfig) AppendDependsOn(dependsOn ...string) {
e.PipelineStepBase.AppendDependsOn(dependsOn...)
}
func (e *ErrorConfig) AppendCredentialDependsOn(...string) {
// not implemented
}
func (e *ErrorConfig) AddUnresolvedAttribute(name string, expr hcl.Expression) {
e.UnresolvedAttributes[name] = expr
}
func (e *ErrorConfig) SetAttributes(hclAttributes hcl.Attributes, evalContext *hcl.EvalContext) hcl.Diagnostics {
diags := hcl.Diagnostics{}
for name, attr := range hclAttributes {
switch name {
case schema.AttributeTypeIf:
e.AddUnresolvedAttribute(name, attr.Expr)
case schema.AttributeTypeIgnore:
stepDiags := setBoolAttribute(attr, evalContext, e, "Ignore", true)
if stepDiags.HasErrors() {
diags = append(diags, stepDiags...)
continue
}
default:
diags = append(diags, &hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: "Invalid attribute",
Detail: fmt.Sprintf("Unsupported attribute '%s' in error block", attr.Name),
Subject: &attr.NameRange,
})
}
}
return diags
}
func (e *ErrorConfig) Validate() bool {
return true
}