/
connector.go
300 lines (255 loc) · 9.18 KB
/
connector.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
package base
import (
"encoding/json"
"fmt"
"github.com/gofrs/uuid"
"go.uber.org/zap"
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/known/structpb"
pipelinePB "github.com/instill-ai/protogen-go/vdp/pipeline/v1beta"
)
// IConnector is the interface that all connectors need to implement
type IConnector interface {
IComponent
LoadConnectorDefinition(definitionJSON []byte, tasksJSON []byte, additionalJSONBytes map[string][]byte) error
// Note: Some content in the definition JSON schema needs to be generated by sysVars or component setting.
GetConnectorDefinition(sysVars map[string]any, component *pipelinePB.ConnectorComponent) (*pipelinePB.ConnectorDefinition, error)
CreateExecution(sysVars map[string]any, connection *structpb.Struct, task string) (*ExecutionWrapper, error)
Test(sysVars map[string]any, connection *structpb.Struct) error
IsCredentialField(target string) bool
}
// Connector is the base struct for all connectors
type BaseConnector struct {
Logger *zap.Logger
UsageHandler UsageHandler
taskInputSchemas map[string]string
taskOutputSchemas map[string]string
definition *pipelinePB.ConnectorDefinition
credentialFields []string
}
type IConnectorExecution interface {
IExecution
GetConnector() IConnector
GetConnection() *structpb.Struct
}
type BaseConnectorExecution struct {
Connector IConnector
SystemVariables map[string]any
Connection *structpb.Struct
Task string
}
func (c *BaseConnector) GetID() string {
return c.definition.Id
}
func (c *BaseConnector) GetUID() uuid.UUID {
return uuid.FromStringOrNil(c.definition.Uid)
}
func (c *BaseConnector) GetLogger() *zap.Logger {
return c.Logger
}
func (c *BaseConnector) GetUsageHandler() UsageHandler {
return c.UsageHandler
}
func (c *BaseConnector) GetConnectorDefinition(sysVars map[string]any, component *pipelinePB.ConnectorComponent) (*pipelinePB.ConnectorDefinition, error) {
return c.definition, nil
}
func (c *BaseConnector) GetTaskInputSchemas() map[string]string {
return c.taskInputSchemas
}
func (c *BaseConnector) GetTaskOutputSchemas() map[string]string {
return c.taskOutputSchemas
}
// LoadConnectorDefinition loads the connector definitions from json files
func (c *BaseConnector) LoadConnectorDefinition(definitionJSONBytes []byte, tasksJSONBytes []byte, additionalJSONBytes map[string][]byte) error {
var err error
var definitionJSON any
c.credentialFields = []string{}
err = json.Unmarshal(definitionJSONBytes, &definitionJSON)
if err != nil {
return err
}
renderedTasksJSON, err := RenderJSON(tasksJSONBytes, additionalJSONBytes)
if err != nil {
return nil
}
availableTasks := []string{}
for _, availableTask := range definitionJSON.(map[string]interface{})["available_tasks"].([]interface{}) {
availableTasks = append(availableTasks, availableTask.(string))
}
tasks, taskStructs, err := loadTasks(availableTasks, renderedTasksJSON)
if err != nil {
return err
}
c.taskInputSchemas = map[string]string{}
c.taskOutputSchemas = map[string]string{}
for k := range taskStructs {
var s []byte
s, err = protojson.Marshal(taskStructs[k].Fields["input"].GetStructValue())
if err != nil {
return err
}
c.taskInputSchemas[k] = string(s)
s, err = protojson.Marshal(taskStructs[k].Fields["output"].GetStructValue())
if err != nil {
return err
}
c.taskOutputSchemas[k] = string(s)
}
c.definition = &pipelinePB.ConnectorDefinition{}
err = protojson.UnmarshalOptions{DiscardUnknown: true}.Unmarshal(definitionJSONBytes, c.definition)
if err != nil {
return err
}
c.definition.Name = fmt.Sprintf("connector-definitions/%s", c.definition.Id)
c.definition.Tasks = tasks
if c.definition.Spec == nil {
c.definition.Spec = &pipelinePB.ConnectorSpec{}
}
c.definition.Spec.ComponentSpecification, err = generateComponentSpec(c.definition.Title, tasks, taskStructs)
if err != nil {
return err
}
raw := &structpb.Struct{}
err = protojson.Unmarshal(definitionJSONBytes, raw)
if err != nil {
return err
}
// TODO: Avoid using structpb traversal here.
if _, ok := raw.Fields["spec"]; ok {
if v, ok := raw.Fields["spec"].GetStructValue().Fields["connection_specification"]; ok {
connection, err := c.refineResourceSpec(v.GetStructValue())
if err != nil {
return err
}
connectionPropStruct := &structpb.Struct{Fields: map[string]*structpb.Value{}}
connectionPropStruct.Fields["connection"] = structpb.NewStructValue(connection)
c.definition.Spec.ComponentSpecification.Fields["properties"] = structpb.NewStructValue(connectionPropStruct)
}
}
c.definition.Spec.DataSpecifications, err = generateDataSpecs(taskStructs)
if err != nil {
return err
}
c.initCredentialField(c.definition)
return nil
}
func (c *BaseConnector) refineResourceSpec(resourceSpec *structpb.Struct) (*structpb.Struct, error) {
spec := proto.Clone(resourceSpec).(*structpb.Struct)
if _, ok := spec.Fields["instillShortDescription"]; !ok {
spec.Fields["instillShortDescription"] = structpb.NewStringValue(spec.Fields["description"].GetStringValue())
}
if _, ok := spec.Fields["properties"]; ok {
for k, v := range spec.Fields["properties"].GetStructValue().AsMap() {
s, err := structpb.NewStruct(v.(map[string]interface{}))
if err != nil {
return nil, err
}
converted, err := c.refineResourceSpec(s)
if err != nil {
return nil, err
}
spec.Fields["properties"].GetStructValue().Fields[k] = structpb.NewStructValue(converted)
}
}
if _, ok := spec.Fields["patternProperties"]; ok {
for k, v := range spec.Fields["patternProperties"].GetStructValue().AsMap() {
s, err := structpb.NewStruct(v.(map[string]interface{}))
if err != nil {
return nil, err
}
converted, err := c.refineResourceSpec(s)
if err != nil {
return nil, err
}
spec.Fields["patternProperties"].GetStructValue().Fields[k] = structpb.NewStructValue(converted)
}
}
for _, target := range []string{"allOf", "anyOf", "oneOf"} {
if _, ok := spec.Fields[target]; ok {
for idx, item := range spec.Fields[target].GetListValue().AsSlice() {
s, err := structpb.NewStruct(item.(map[string]interface{}))
if err != nil {
return nil, err
}
converted, err := c.refineResourceSpec(s)
if err != nil {
return nil, err
}
spec.Fields[target].GetListValue().AsSlice()[idx] = structpb.NewStructValue(converted)
}
}
}
return spec, nil
}
// IsCredentialField checks if the target field is credential field
func (c *BaseConnector) IsCredentialField(target string) bool {
for _, field := range c.credentialFields {
if target == field {
return true
}
}
return false
}
// ListCredentialField lists the credential fields by definition id
func (c *BaseConnector) ListCredentialField() ([]string, error) {
return c.credentialFields, nil
}
func (c *BaseConnector) initCredentialField(def *pipelinePB.ConnectorDefinition) {
if c.credentialFields == nil {
c.credentialFields = []string{}
}
credentialFields := []string{}
connection := def.Spec.GetComponentSpecification().GetFields()["properties"].GetStructValue().GetFields()["connection"].GetStructValue()
credentialFields = c.traverseCredentialField(connection.GetFields()["properties"], "", credentialFields)
if l, ok := connection.GetFields()["oneOf"]; ok {
for _, v := range l.GetListValue().Values {
credentialFields = c.traverseCredentialField(v.GetStructValue().GetFields()["properties"], "", credentialFields)
}
}
c.credentialFields = credentialFields
}
func (c *BaseConnector) traverseCredentialField(input *structpb.Value, prefix string, credentialFields []string) []string {
for key, v := range input.GetStructValue().GetFields() {
if isCredential, ok := v.GetStructValue().GetFields()["instillCredentialField"]; ok {
if isCredential.GetBoolValue() || isCredential.GetStringValue() == "true" {
credentialFields = append(credentialFields, fmt.Sprintf("%s%s", prefix, key))
}
}
if tp, ok := v.GetStructValue().GetFields()["type"]; ok {
if tp.GetStringValue() == "object" {
if l, ok := v.GetStructValue().GetFields()["oneOf"]; ok {
for _, v := range l.GetListValue().Values {
credentialFields = c.traverseCredentialField(v.GetStructValue().GetFields()["properties"], fmt.Sprintf("%s%s.", prefix, key), credentialFields)
}
}
credentialFields = c.traverseCredentialField(v.GetStructValue().GetFields()["properties"], fmt.Sprintf("%s%s.", prefix, key), credentialFields)
}
}
}
return credentialFields
}
func (e *BaseConnectorExecution) GetTask() string {
return e.Task
}
func (e *BaseConnectorExecution) GetConnector() IConnector {
return e.Connector
}
func (e *BaseConnectorExecution) GetConnection() *structpb.Struct {
return e.Connection
}
func (e *BaseConnectorExecution) GetSystemVariables() map[string]any {
return e.SystemVariables
}
func (e *BaseConnectorExecution) GetLogger() *zap.Logger {
return e.Connector.GetLogger()
}
func (e *BaseConnectorExecution) GetUsageHandler() UsageHandler {
return e.Connector.GetUsageHandler()
}
func (e *BaseConnectorExecution) GetTaskInputSchema() string {
return e.Connector.GetTaskInputSchemas()[e.Task]
}
func (e *BaseConnectorExecution) GetTaskOutputSchema() string {
return e.Connector.GetTaskOutputSchemas()[e.Task]
}