-
Notifications
You must be signed in to change notification settings - Fork 261
/
config_map.go
64 lines (54 loc) · 2.38 KB
/
config_map.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
package connection
import (
typehelpers "github.com/turbot/go-kit/types"
sdkproto "github.com/turbot/steampipe-plugin-sdk/v5/grpc/proto"
"github.com/turbot/steampipe/pkg/steampipeconfig/modconfig"
)
type ConnectionConfigMap map[string]*sdkproto.ConnectionConfig
// NewConnectionConfigMap creates a map of sdkproto.ConnectionConfig keyed by connection name
// NOTE: connections in error are EXCLUDED
func NewConnectionConfigMap(connectionMap map[string]*modconfig.Connection) ConnectionConfigMap {
configMap := make(ConnectionConfigMap)
for k, v := range connectionMap {
if v.Error != nil {
continue
}
configMap[k] = &sdkproto.ConnectionConfig{
Connection: v.Name,
Plugin: v.Plugin,
PluginShortName: v.PluginAlias,
Config: v.Config,
ChildConnections: v.GetResolveConnectionNames(),
PluginInstance: typehelpers.SafeString(v.PluginInstance),
}
}
return configMap
}
func (m ConnectionConfigMap) Diff(otherMap ConnectionConfigMap) (addedConnections, deletedConnections, changedConnections map[string][]*sdkproto.ConnectionConfig) {
// results are maps of connections keyed by plugin instance
addedConnections = make(map[string][]*sdkproto.ConnectionConfig)
deletedConnections = make(map[string][]*sdkproto.ConnectionConfig)
changedConnections = make(map[string][]*sdkproto.ConnectionConfig)
for name, connection := range m {
if otherConnection, ok := otherMap[name]; !ok {
deletedConnections[connection.PluginInstance] = append(deletedConnections[connection.PluginInstance], connection)
} else {
// check for changes
// special case - if the plugin has changed, treat this as a deletion and a re-add
if connection.PluginInstance != otherConnection.PluginInstance {
addedConnections[otherConnection.PluginInstance] = append(addedConnections[otherConnection.PluginInstance], otherConnection)
deletedConnections[connection.PluginInstance] = append(deletedConnections[connection.PluginInstance], connection)
} else {
if !connection.Equals(otherConnection) {
changedConnections[connection.PluginInstance] = append(changedConnections[connection.PluginInstance], otherConnection)
}
}
}
}
for otherName, otherConnection := range otherMap {
if _, ok := m[otherName]; !ok {
addedConnections[otherConnection.PluginInstance] = append(addedConnections[otherConnection.PluginInstance], otherConnection)
}
}
return
}