-
Notifications
You must be signed in to change notification settings - Fork 263
/
connection.go
233 lines (206 loc) · 7.54 KB
/
connection.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
224
225
226
227
228
229
230
231
232
233
package modconfig
import (
"fmt"
"log"
"path"
"reflect"
"strings"
"github.com/hashicorp/hcl/v2"
"github.com/turbot/go-kit/helpers"
"github.com/turbot/steampipe/pkg/steampipeconfig/options"
"golang.org/x/exp/maps"
)
const (
ConnectionTypeAggregator = "aggregator"
)
// Connection is a struct representing the partially parsed connection
//
// (Partial as the connection config, which is plugin specific, is stored as raw HCL.
// This will be parsed by the plugin)
// json tags needed as this is stored in the connection state file
type Connection struct {
// connection name
Name string `json:"name,omitempty"`
// The name of plugin as mentioned in config
PluginShortName string `json:"plugin_short_name,omitempty"`
// The fully qualified name of the plugin. derived from the short name
Plugin string `json:"plugin,omitempty"`
// Type - supported values: "aggregator"
Type string `json:"type,omitempty"`
// this is a list of names or wildcards which are resolved to connections
// (only valid for "aggregator" type)
ConnectionNames []string `json:"connections,omitempty"`
// a map of the resolved child connections
// (only valid for "aggregator" type)
Connections map[string]*Connection `json:"-"`
// a list of the names resolved child connections
// (only valid for "aggregator" type)
ResolvedConnectionNames []string `json:"resolved_connections,omitempty"`
// unparsed HCL of plugin specific connection config
Config string `json:"config,omitempty"`
// options
Options *options.Connection `json:"options,omitempty"`
DeclRange Range `json:"decl_range,omitempty"`
}
// Range represents a span of characters between two positions in a source file.
// This is a direct re-implementation of hcl.Range, allowing us to control JSON serialization
type Range struct {
// Filename is the name of the file into which this range's positions point.
Filename string `json:"filename,omitempty"`
// Start and End represent the bounds of this range. Start is inclusive and End is exclusive.
Start Pos `json:"start,omitempty"`
End Pos `json:"end,omitempty"`
}
func (r Range) GetLegacy() hcl.Range {
return hcl.Range{
Filename: r.Filename,
Start: r.Start.GetLegacy(),
End: r.End.GetLegacy(),
}
}
func NewRange(sourceRange hcl.Range) Range {
return Range{
Filename: sourceRange.Filename,
Start: NewPos(sourceRange.Start),
End: NewPos(sourceRange.End),
}
}
// Pos represents a single position in a source file
// This is a direct re-implementation of hcl.Pos, allowing us to control JSON serialization
type Pos struct {
Line int `json:"line"`
Column int `json:"column"`
Byte int `json:"byte"`
}
func (r Pos) GetLegacy() hcl.Pos {
return hcl.Pos{
Line: r.Line,
Column: r.Column,
Byte: r.Byte,
}
}
func NewPos(sourcePos hcl.Pos) Pos {
return Pos{
Line: sourcePos.Line,
Column: sourcePos.Column,
Byte: sourcePos.Byte,
}
}
func NewConnection(block *hcl.Block) *Connection {
return &Connection{
Name: block.Labels[0],
DeclRange: NewRange(block.TypeRange),
}
}
func (c *Connection) Equals(other *Connection) bool {
connectionOptionsEqual := (c.Options == nil) == (other.Options == nil)
if c.Options != nil {
connectionOptionsEqual = c.Options.Equals(other.Options)
}
return c.Name == other.Name &&
c.Plugin == other.Plugin &&
c.Type == other.Type &&
strings.Join(c.ConnectionNames, ",") == strings.Join(other.ConnectionNames, ",") &&
connectionOptionsEqual &&
c.Config == other.Config
}
// SetOptions sets the options on the connection
// verify the options object is a valid options type (only options.Connection currently supported)
func (c *Connection) SetOptions(opts options.Options, block *hcl.Block) hcl.Diagnostics {
var diags hcl.Diagnostics
switch o := opts.(type) {
case *options.Connection:
c.Options = o
default:
diags = append(diags, &hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: fmt.Sprintf("invalid nested option type %s - only 'connection' options blocks are supported for Connections", reflect.TypeOf(o).Name()),
Subject: &block.DefRange,
})
}
return diags
}
func (c *Connection) String() string {
return fmt.Sprintf("\n----\nName: %s\nPlugin: %s\nConfig:\n%s\nOptions:\n%s\n", c.Name, c.Plugin, c.Config, c.Options.String())
}
// Validate verifies the Type property is valid,
// if this is an aggregator connection, there must be at least one child, and no duplicates
// if this is NOT an aggregator, there must be no children
func (c *Connection) Validate(map[string]*Connection) (warnings []string, errors []string) {
validConnectionTypes := []string{"", ConnectionTypeAggregator}
if !helpers.StringSliceContains(validConnectionTypes, c.Type) {
return nil, []string{fmt.Sprintf("connection '%s' has invalid connection type '%s'", c.Name, c.Type)}
}
if c.Type == ConnectionTypeAggregator {
return c.ValidateAggregatorConnection()
}
// this is NOT an aggregator group - there should be no children
var validationErrors []string
if len(c.ConnectionNames) != 0 {
validationErrors = append(validationErrors, fmt.Sprintf("connection '%s' has %d children, but is not of type 'aggregator'", c.Name, len(c.ConnectionNames)))
}
return nil, validationErrors
}
func (c *Connection) ValidateAggregatorConnection() (warnings, errors []string) {
if len(c.Connections) == 0 {
/// there should be at least one connection - raise as warning
return []string{fmt.Sprintf("aggregator connection '%s' has no children", c.Name)}, nil
}
var validationErrors []string
// now ensure all child connections are loaded and use the same plugin as the parent connection
for _, childConnection := range c.Connections {
if childConnection.Plugin != c.Plugin {
validationErrors = append(validationErrors,
fmt.Sprintf("aggregator connection '%s' uses plugin %s but child connection '%s' uses plugin '%s'",
c.Name,
c.Plugin,
childConnection.Name,
childConnection.Plugin,
))
}
}
return nil, validationErrors
}
func (c *Connection) PopulateChildren(connectionMap map[string]*Connection) {
log.Printf("[TRACE] Connection.PopulateChildren for aggregator connection %s", c.Name)
c.Connections = make(map[string]*Connection)
for _, childName := range c.ConnectionNames {
// if this resolves as an existing connection, populate it
if childConnection, ok := connectionMap[childName]; ok {
log.Printf("[TRACE] Connection.PopulateChildren found matching connection %s", childName)
c.Connections[childName] = childConnection
continue
}
log.Printf("[TRACE] Connection.PopulateChildren no connection matches %s - treating as a wildcard", childName)
// otherwise treat the connection name as a wildcard and see what matches
for name, connection := range connectionMap {
// if this is an aggregator connection, skip (this will also avoid us adding ourselves)
if connection.Type == ConnectionTypeAggregator {
continue
}
// have we already added this connection
if _, ok := c.Connections[name]; ok {
continue
}
if match, _ := path.Match(childName, name); match {
// verify that this connection is of a compatible type
if connection.Plugin == c.Plugin {
c.Connections[name] = connection
log.Printf("[TRACE] connection '%s' matches pattern '%s'", name, childName)
}
}
}
}
c.ResolvedConnectionNames = maps.Keys(c.Connections)
}
// GetResolveConnectionNames return the names of all child connections
// (will only be non-empty for aggregator connections)
func (c *Connection) GetResolveConnectionNames() []string {
res := make([]string, len(c.Connections))
idx := 0
for k := range c.Connections {
res[idx] = k
idx++
}
return res
}