-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathclient.go
More file actions
129 lines (109 loc) · 3.43 KB
/
client.go
File metadata and controls
129 lines (109 loc) · 3.43 KB
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
package plugin
import (
"context"
"encoding/json"
"fmt"
"github.com/apache/arrow-go/v18/arrow"
"github.com/cloudquery/plugin-sdk/examples/simple_plugin/client"
"github.com/cloudquery/plugin-sdk/examples/simple_plugin/services"
"github.com/cloudquery/plugin-sdk/v4/message"
"github.com/cloudquery/plugin-sdk/v4/plugin"
"github.com/cloudquery/plugin-sdk/v4/scheduler"
"github.com/cloudquery/plugin-sdk/v4/schema"
"github.com/cloudquery/plugin-sdk/v4/transformers"
"github.com/rs/zerolog"
)
type Client struct {
logger zerolog.Logger
config client.Spec
tables schema.Tables
scheduler *scheduler.Scheduler
}
func (c *Client) Logger() *zerolog.Logger {
return &c.logger
}
func (c *Client) Sync(ctx context.Context, options plugin.SyncOptions, res chan<- message.SyncMessage) error {
tt, err := c.tables.FilterDfs(options.Tables, options.SkipTables, options.SkipDependentTables)
if err != nil {
return err
}
schedulerClient := &client.TestClient{
Logger: c.logger,
Spec: c.config,
}
return c.scheduler.Sync(ctx, schedulerClient, tt, res, scheduler.WithSyncDeterministicCQID(options.DeterministicCQID))
}
func (c *Client) Tables(_ context.Context, options plugin.TableOptions) (schema.Tables, error) {
tt, err := c.tables.FilterDfs(options.Tables, options.SkipTables, options.SkipDependentTables)
if err != nil {
return nil, err
}
return tt, nil
}
func (*Client) Close(_ context.Context) error {
return nil
}
func (*Client) Write(context.Context, <-chan message.WriteMessage) error {
// Not implemented, just used for testing destination packaging
return nil
}
func (*Client) Read(context.Context, *schema.Table, chan<- arrow.RecordBatch) error {
// Not implemented, just used for testing destination packaging
return nil
}
func (*Client) Transform(_ context.Context, _ <-chan arrow.RecordBatch, _ chan<- arrow.RecordBatch) error {
// Not implemented, just used for testing destination packaging
return nil
}
func (*Client) TransformSchema(_ context.Context, _ *arrow.Schema) (*arrow.Schema, error) {
// Not implemented, just used for testing destination packaging
return nil, nil
}
func Configure(_ context.Context, logger zerolog.Logger, spec []byte, opts plugin.NewClientOptions) (plugin.Client, error) {
if opts.NoConnection {
return &Client{
logger: logger,
tables: getTables(),
}, nil
}
config := &client.Spec{}
if err := json.Unmarshal(spec, config); err != nil {
return nil, fmt.Errorf("failed to unmarshal spec: %w", err)
}
config.SetDefaults()
if err := config.Validate(); err != nil {
return nil, fmt.Errorf("failed to validate spec: %w", err)
}
return &Client{
logger: logger,
config: *config,
scheduler: scheduler.NewScheduler(
scheduler.WithLogger(logger),
),
tables: getTables(),
}, nil
}
func TestConnection(_ context.Context, _ zerolog.Logger, spec []byte) error {
config := &client.Spec{}
if err := json.Unmarshal(spec, config); err != nil {
return plugin.NewTestConnError("INVALID_SPEC", fmt.Errorf("failed to unmarshal spec: %w", err))
}
config.SetDefaults()
if err := config.Validate(); err != nil {
return plugin.NewTestConnError("INVALID_SPEC", fmt.Errorf("failed to validate spec: %w", err))
}
return nil
}
func getTables() schema.Tables {
tables := schema.Tables{
services.TestSomeTable(),
}
if err := transformers.TransformTables(tables); err != nil {
panic(err)
}
for _, t := range tables {
schema.AddCqIDs(t)
schema.AddCqClientID(t)
}
return tables
}