-
Notifications
You must be signed in to change notification settings - Fork 24
/
plugin.go
310 lines (278 loc) · 8.62 KB
/
plugin.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
package source
import (
"context"
"fmt"
"sync"
"time"
"github.com/cloudquery/plugin-sdk/backend"
"github.com/cloudquery/plugin-sdk/caser"
"github.com/cloudquery/plugin-sdk/internal/backends/local"
"github.com/cloudquery/plugin-sdk/internal/backends/nop"
"github.com/cloudquery/plugin-sdk/schema"
"github.com/cloudquery/plugin-sdk/specs"
"github.com/rs/zerolog"
"golang.org/x/sync/semaphore"
)
type Options struct {
Backend backend.Backend
}
type NewExecutionClientFunc func(context.Context, zerolog.Logger, specs.Source, Options) (schema.ClientMeta, error)
// Plugin is the base structure required to pass to sdk.serve
// We take a declarative approach to API here similar to Cobra
type Plugin struct {
// Name of plugin i.e aws,gcp, azure etc'
name string
// Version of the plugin
version string
// Called upon configure call to validate and init configuration
newExecutionClient NewExecutionClientFunc
// dynamic table function if specified
getDynamicTables GetTables
// Tables is all tables supported by this source plugin
tables schema.Tables
// status sync metrics
metrics *Metrics
// Logger to call, this logger is passed to the serve.Serve Client, if not defined Serve will create one instead.
logger zerolog.Logger
// resourceSem is a semaphore that limits the number of concurrent resources being fetched
resourceSem *semaphore.Weighted
// tableSem is a semaphore that limits the number of concurrent tables being fetched
tableSems []*semaphore.Weighted
// maxDepth is the max depth of tables
maxDepth uint64
// caser
caser *caser.Caser
// mu is a mutex that limits the number of concurrent init/syncs (can only be one at a time)
mu sync.Mutex
// client is the initialized session client
client schema.ClientMeta
// sessionTables are the
sessionTables schema.Tables
// backend is the backend used to store the cursor state
backend backend.Backend
// spec is the spec the client was initialized with
spec specs.Source
}
const (
maxAllowedDepth = 4
)
// Add internal columns
func addInternalColumns(tables []*schema.Table) error {
for _, table := range tables {
if c := table.Column("_cq_id"); c != nil {
return fmt.Errorf("table %s already has column _cq_id", table.Name)
}
cqID := schema.CqIDColumn
if len(table.PrimaryKeys()) == 0 {
cqID.CreationOptions.PrimaryKey = true
}
table.Columns = append([]schema.Column{cqID, schema.CqParentIDColumn}, table.Columns...)
if err := addInternalColumns(table.Relations); err != nil {
return err
}
}
return nil
}
// Set parent links on relational tables
func setParents(tables schema.Tables, parent *schema.Table) {
for _, table := range tables {
table.Parent = parent
setParents(table.Relations, table)
}
}
// Apply transformations to tables
func transformTables(tables schema.Tables) error {
for _, table := range tables {
if table.Transform != nil {
if err := table.Transform(table); err != nil {
return fmt.Errorf("failed to transform table %s: %w", table.Name, err)
}
}
if err := transformTables(table.Relations); err != nil {
return err
}
}
return nil
}
func maxDepth(tables schema.Tables) uint64 {
var depth uint64
if len(tables) == 0 {
return 0
}
for _, table := range tables {
newDepth := 1 + maxDepth(table.Relations)
if newDepth > depth {
depth = newDepth
}
}
return depth
}
// NewPlugin returns a new plugin with a given name, version, tables, newExecutionClient
// and additional options.
func NewPlugin(name string, version string, tables []*schema.Table, newExecutionClient NewExecutionClientFunc, options ...Option) *Plugin {
p := Plugin{
name: name,
version: version,
tables: tables,
newExecutionClient: newExecutionClient,
metrics: &Metrics{TableClient: make(map[string]map[string]*TableClientMetrics)},
caser: caser.New(),
}
for _, opt := range options {
opt(&p)
}
setParents(p.tables, nil)
if err := transformTables(p.tables); err != nil {
panic(err)
}
if err := addInternalColumns(p.tables); err != nil {
panic(err)
}
if err := p.validate(); err != nil {
panic(err)
}
p.maxDepth = maxDepth(p.tables)
if p.maxDepth > maxAllowedDepth {
panic(fmt.Errorf("max depth of tables is %d, max allowed is %d", p.maxDepth, maxAllowedDepth))
}
return &p
}
func (p *Plugin) SetLogger(logger zerolog.Logger) {
p.logger = logger.With().Str("module", p.name+"-src").Logger()
}
// Tables returns all tables supported by this source plugin
func (p *Plugin) Tables() schema.Tables {
return p.tables
}
func (p *Plugin) HasDynamicTables() bool {
return p.getDynamicTables != nil
}
func (p *Plugin) GetDynamicTables() schema.Tables {
return p.sessionTables
}
// TablesForSpec returns all tables supported by this source plugin that match the given spec.
// It validates the tables part of the spec and will return an error if it is found to be invalid.
// This is deprecated method
func (p *Plugin) TablesForSpec(spec specs.Source) (schema.Tables, error) {
spec.SetDefaults()
if err := spec.Validate(); err != nil {
return nil, fmt.Errorf("invalid spec: %w", err)
}
tables, err := p.tables.FilterDfs(spec.Tables, spec.SkipTables)
if err != nil {
return nil, fmt.Errorf("failed to filter tables: %w", err)
}
return tables, nil
}
// Name return the name of this plugin
func (p *Plugin) Name() string {
return p.name
}
// Version returns the version of this plugin
func (p *Plugin) Version() string {
return p.version
}
func (p *Plugin) Metrics() *Metrics {
return p.metrics
}
func (p *Plugin) Init(ctx context.Context, spec specs.Source) error {
if !p.mu.TryLock() {
return fmt.Errorf("plugin already in use")
}
defer p.mu.Unlock()
var err error
spec.SetDefaults()
if err := spec.Validate(); err != nil {
return fmt.Errorf("invalid spec: %w", err)
}
p.spec = spec
switch spec.Backend {
case specs.BackendNone:
p.backend = nop.New()
case specs.BackendLocal:
p.backend, err = local.New(spec)
if err != nil {
return fmt.Errorf("failed to initialize local backend: %w", err)
}
default:
return fmt.Errorf("unknown backend: %s", spec.Backend)
}
tables := p.tables
if p.getDynamicTables != nil {
p.client, err = p.newExecutionClient(ctx, p.logger, spec, Options{Backend: p.backend})
if err != nil {
return fmt.Errorf("failed to create execution client for source plugin %s: %w", p.name, err)
}
tables, err = p.getDynamicTables(ctx, p.client)
if err != nil {
return fmt.Errorf("failed to get dynamic tables: %w", err)
}
tables, err = tables.FilterDfs(spec.Tables, spec.SkipTables)
if err != nil {
return fmt.Errorf("failed to filter tables: %w", err)
}
if len(tables) == 0 {
return fmt.Errorf("no tables to sync - please check your spec 'tables' and 'skip_tables' settings")
}
setParents(tables, nil)
if err := transformTables(tables); err != nil {
return err
}
if err := addInternalColumns(tables); err != nil {
return err
}
if err := p.validate(); err != nil {
return err
}
p.maxDepth = maxDepth(tables)
if p.maxDepth > maxAllowedDepth {
return fmt.Errorf("max depth of tables is %d, max allowed is %d", p.maxDepth, maxAllowedDepth)
}
} else {
tables, err = tables.FilterDfs(spec.Tables, spec.SkipTables)
if err != nil {
return fmt.Errorf("failed to filter tables: %w", err)
}
}
p.sessionTables = tables
return nil
}
// Sync is syncing data from the requested tables in spec to the given channel
func (p *Plugin) Sync(ctx context.Context, res chan<- *schema.Resource) error {
if !p.mu.TryLock() {
return fmt.Errorf("plugin already in use")
}
defer p.mu.Unlock()
if p.client == nil {
var err error
p.client, err = p.newExecutionClient(ctx, p.logger, p.spec, Options{Backend: p.backend})
if err != nil {
return fmt.Errorf("failed to create execution client for source plugin %s: %w", p.name, err)
}
}
startTime := time.Now()
switch p.spec.Scheduler {
case specs.SchedulerDFS:
p.syncDfs(ctx, p.spec, p.client, p.sessionTables, res)
case specs.SchedulerRoundRobin:
p.syncRoundRobin(ctx, p.spec, p.client, p.sessionTables, res)
default:
return fmt.Errorf("unknown scheduler %s. Options are: %v", p.spec.Scheduler, specs.AllSchedulers.String())
}
p.logger.Info().Uint64("resources", p.metrics.TotalResources()).Uint64("errors", p.metrics.TotalErrors()).Uint64("panics", p.metrics.TotalPanics()).TimeDiff("duration", time.Now(), startTime).Msg("sync finished")
return nil
}
func (p *Plugin) Close(ctx context.Context) error {
if !p.mu.TryLock() {
return fmt.Errorf("plugin already in use")
}
defer p.mu.Unlock()
if p.backend != nil {
err := p.backend.Close(ctx)
if err != nil {
return fmt.Errorf("failed to close backend: %w", err)
}
p.backend = nil
}
return nil
}